summaryrefslogtreecommitdiff
path: root/src/unstable/minimacro.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/minimacro.cpp')
-rw-r--r--src/unstable/minimacro.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/unstable/minimacro.cpp b/src/unstable/minimacro.cpp
new file mode 100644
index 0000000..b6fd6a8
--- /dev/null
+++ b/src/unstable/minimacro.cpp
@@ -0,0 +1,187 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/minimacro.h"
9
10Bu::MiniMacro::MiniMacro()
11{
12 hFuncs.insert("toupper", new FuncToUpper() );
13 hFuncs.insert("tolower", new FuncToLower() );
14}
15
16Bu::MiniMacro::MiniMacro( const StrHash &sVarSrc )
17{
18 for( StrHash::const_iterator i = sVarSrc.begin(); i != sVarSrc.end(); i++ )
19 {
20 addVar( i.getKey(), i.getValue() );
21 }
22}
23
24Bu::MiniMacro::~MiniMacro()
25{
26}
27
28Bu::String Bu::MiniMacro::parse( const Bu::String &sIn )
29{
30 bContinue = true;
31 Bu::String sOut;
32 for( sCur = sIn.getStr(); *sCur && bContinue; sCur++ )
33 {
34 if( *sCur == '{' )
35 {
36 switch( sCur[1] )
37 {
38 case '=':
39 sCur += 2;
40 sOut += parseRepl();
41 break;
42
43 case '?':
44 sCur += 2;
45 sOut += parseCond();
46 break;
47
48 case ':':
49 sCur += 2;
50 sOut += parseCmd();
51 break;
52
53 default:
54 sOut += *sCur;
55 continue;
56 }
57 }
58 else
59 {
60 sOut += *sCur;
61 }
62 }
63
64 iLastPos = (ptrdiff_t)sCur - (ptrdiff_t)sIn.getStr();
65
66 return sOut;
67}
68
69Bu::String Bu::MiniMacro::parseRepl()
70{
71 Bu::String sOut;
72 bool bIsFirst = true;
73 for( const char *sNext = sCur;;)
74 {
75 for(; *sNext != ':' && *sNext != '}' && *sNext != '\0'; sNext++ ) { }
76 if( *sNext == '\0' )
77 break;
78 Bu::String sName( sCur, (ptrdiff_t)sNext-(ptrdiff_t)sCur );
79 if( bIsFirst )
80 {
81 sOut = hVars[sName];
82 bIsFirst = false;
83 //printf("Variable: \"%s\"\n", sName.getStr() );
84 }
85 else
86 {
87 sOut = callFunc( sOut, sName );
88 //printf("Filter: \"%s\"\n", sName.getStr() );
89 }
90 if( *sNext == '}' )
91 {
92 sCur = sNext;
93 break;
94 }
95 else if( *sNext == ':' )
96 {
97 }
98 sNext++;
99 sCur = sNext;
100 }
101 return sOut;
102}
103
104Bu::String Bu::MiniMacro::parseCond()
105{
106 Bu::String sOut;
107 //printf("%20s\n", sCur );
108 return sOut;
109}
110
111Bu::String Bu::MiniMacro::parseCmd()
112{
113 Bu::String sOut;
114 const char *sNext = sCur;
115 for(; *sNext != ':' && *sNext != '}' && *sNext != '\0'; sNext++ ) { }
116 if( *sNext != '\0' )
117 {
118 Bu::String sName( sCur, (ptrdiff_t)sNext-(ptrdiff_t)sCur );
119 if( sName == "end" )
120 {
121 sCur = sNext;
122 bContinue = false;
123 return sOut;
124 }
125 else
126 {
127 throw Bu::ExceptionBase("Unknown command '%s'.",
128 sName.getStr()
129 );
130 }
131 }
132 else
133 {
134 //printf("Uh...?\n");
135 }
136
137 //printf("%20s\n", sCur );
138 return sOut;
139}
140
141Bu::String Bu::MiniMacro::callFunc(
142 const Bu::String &sIn, const Bu::String &sFunc )
143{
144 int i = sFunc.findIdx('(');
145 if( i < 0 )
146 throw Bu::ExceptionBase("That doesn't look like a function call");
147 Bu::String sName( sFunc.getStr(), i );
148 StrList lsParams;
149 for( const char *s = sFunc.getStr()+i+1; *s && *s != ')'; s++ )
150 {
151 for(; *s == ' ' || *s == '\t' || *s == '\r' || *s == '\n'; s++ ) { }
152 const char *sNext;
153 for( sNext = s; *sNext && *sNext != ')' && *sNext != ','; sNext++ ) { }
154 Bu::String p( s, (ptrdiff_t)sNext-(ptrdiff_t)s );
155 lsParams.append( p );
156 sNext++;
157 s = sNext;
158 }
159 return hFuncs.get( sName )->call( sIn, lsParams );
160}
161
162void Bu::MiniMacro::addVar(
163 const Bu::String &sName, const Bu::String &sValue )
164{
165 hVars.insert( sName, sValue );
166}
167
168bool Bu::MiniMacro::hasVar( const Bu::String &sName )
169{
170 return hVars.has( sName );
171}
172
173const Bu::String &Bu::MiniMacro::getVar( const Bu::String &sName )
174{
175 return hVars.get( sName );
176}
177
178const Bu::StrHash &Bu::MiniMacro::getVars()
179{
180 return hVars;
181}
182
183int Bu::MiniMacro::getPosition()
184{
185 return iLastPos;
186}
187