aboutsummaryrefslogtreecommitdiff
path: root/src/minimacro.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-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