summaryrefslogtreecommitdiff
path: root/src/minimacro.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-12-03 19:51:09 +0000
committerMike Buland <eichlan@xagasoft.com>2007-12-03 19:51:09 +0000
commit96ca22c7a1f9c5990e136a6ccddc8a33319b9043 (patch)
treeb6223207babe8f22c600e9a0fde3e5b25143d3b7 /src/minimacro.cpp
parent81e7d758bbb83fc1bdf9df2b9b17d4a7998aec5b (diff)
downloadlibbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.tar.gz
libbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.tar.bz2
libbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.tar.xz
libbu++-96ca22c7a1f9c5990e136a6ccddc8a33319b9043.zip
Hey, Bu::MiniMacro works, that's a funny name. There's still some more to add,
but it does everything I need it to at the moment.
Diffstat (limited to 'src/minimacro.cpp')
-rw-r--r--src/minimacro.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/minimacro.cpp b/src/minimacro.cpp
index 274c13b..374b7de 100644
--- a/src/minimacro.cpp
+++ b/src/minimacro.cpp
@@ -6,12 +6,129 @@
6 */ 6 */
7 7
8#include "bu/minimacro.h" 8#include "bu/minimacro.h"
9#include "bu/exceptions.h"
9 10
10Bu::MiniMacro::MiniMacro() 11Bu::MiniMacro::MiniMacro()
11{ 12{
13 hFuncs.insert("toupper", new FuncToUpper() );
14 hFuncs.insert("tolower", new FuncToLower() );
12} 15}
13 16
14Bu::MiniMacro::~MiniMacro() 17Bu::MiniMacro::~MiniMacro()
15{ 18{
16} 19}
17 20
21Bu::FString Bu::MiniMacro::parse( const Bu::FString &sIn )
22{
23 Bu::FString sOut;
24 for( sCur = sIn.getStr(); *sCur; sCur++ )
25 {
26 if( *sCur == '{' )
27 {
28 switch( sCur[1] )
29 {
30 case '=':
31 sCur += 2;
32 sOut += parseRepl();
33 break;
34
35 case '?':
36 sCur += 2;
37 sOut += parseCond();
38 break;
39
40 case ':':
41 sCur += 2;
42 sOut += parseCmd();
43 break;
44
45 default:
46 sOut += *sCur;
47 continue;
48 }
49 }
50 else
51 {
52 sOut += *sCur;
53 }
54 }
55
56 return sOut;
57}
58
59Bu::FString Bu::MiniMacro::parseRepl()
60{
61 Bu::FString sOut;
62 bool bIsFirst = true;
63 for( const char *sNext = sCur;;)
64 {
65 for(; *sNext != ':' && *sNext != '}' && *sNext != '\0'; sNext++ );
66 if( *sNext == '\0' )
67 break;
68 Bu::FString sName( sCur, (int)sNext-(int)sCur );
69 if( bIsFirst )
70 {
71 sOut = hVars[sName];
72 bIsFirst = false;
73 printf("Variable: \"%s\"\n", sName.getStr() );
74 }
75 else
76 {
77 sOut = callFunc( sOut, sName );
78 printf("Filter: \"%s\"\n", sName.getStr() );
79 }
80 if( *sNext == '}' )
81 {
82 sCur = sNext;
83 break;
84 }
85 else if( *sNext == ':' )
86 {
87 }
88 sNext++;
89 sCur = sNext;
90 }
91 return sOut;
92}
93
94Bu::FString Bu::MiniMacro::parseCond()
95{
96 Bu::FString sOut;
97 printf("%20s\n", sCur );
98 return sOut;
99}
100
101Bu::FString Bu::MiniMacro::parseCmd()
102{
103 Bu::FString sOut;
104 printf("%20s\n", sCur );
105 return sOut;
106}
107
108Bu::FString Bu::MiniMacro::callFunc(
109 const Bu::FString &sIn, const Bu::FString &sFunc )
110{
111 int i = sFunc.find('(');
112 if( i < 0 )
113 throw Bu::ExceptionBase("That doesn't look like a function call");
114 Bu::FString sName( sFunc.getStr(), i );
115 StrList lsParams;
116 for( const char *s = sFunc.getStr()+i+1; *s && *s != ')'; s++ )
117 {
118 for(; *s == ' ' || *s == '\t' || *s == '\r' || *s == '\n'; s++ );
119 const char *sNext;
120 for( sNext = s; *sNext && *sNext != ')' && *sNext != ','; sNext++ );
121 Bu::FString p( s, (int)sNext-(int)s );
122 lsParams.append( p );
123 sNext++;
124 s = sNext;
125 }
126 return hFuncs.get( sName )->call( sIn, lsParams );
127}
128
129void Bu::MiniMacro::addVar(
130 const Bu::FString &sName, const Bu::FString &sValue )
131{
132 hVars.insert( sName, sValue );
133}
134