summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-01-06 01:08:53 -0700
committerMike Buland <eichlan@xagasoft.com>2012-01-06 01:08:53 -0700
commit46ccdb146476f8ac140af75ab2decccbce800db2 (patch)
tree7349b40681db46b38998096d26b7ddb74fe14072 /src
parent3af6cc33ff246d31513b9a645403a9ece58983a0 (diff)
downloadstage-46ccdb146476f8ac140af75ab2decccbce800db2.tar.gz
stage-46ccdb146476f8ac140af75ab2decccbce800db2.tar.bz2
stage-46ccdb146476f8ac140af75ab2decccbce800db2.tar.xz
stage-46ccdb146476f8ac140af75ab2decccbce800db2.zip
smlToConsole is now non-recursive.
It's a lot better this way overall, it's one self-contained function, it doesn't require as much setup, and it works wonderfully overall.
Diffstat (limited to 'src')
-rw-r--r--src/options.cpp233
1 files changed, 140 insertions, 93 deletions
diff --git a/src/options.cpp b/src/options.cpp
index 0f84e1c..319532e 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -118,129 +118,176 @@ void smlToHtml( const SmlNode *pNode )
118 } 118 }
119} 119}
120 120
121void appendToken( Bu::String &sCurLine, Bu::String &sNextToken, int &iLineLen ) 121void appendToken( Bu::String &sCurLine, Bu::String &sNextToken, int &iLineLen,
122 int &iNextLen )
122{ 123{
123 if( iLineLen + sNextToken.getSize() + 1 >= 78 ) 124 if( iLineLen + iNextLen + 1 >= 78 )
124 { 125 {
125 sio << sCurLine << sio.nl; 126 sio << sCurLine << sio.nl;
126 iLineLen = 0; 127 iLineLen = 0;
127 sCurLine = sNextToken; 128 sCurLine = sNextToken;
128
129 } 129 }
130 else 130 else
131 { 131 {
132 sCurLine += sNextToken; 132 sCurLine += sNextToken;
133 } 133 }
134 iLineLen += sNextToken.getSize() + 1; 134 iLineLen += iNextLen + 1;
135 sCurLine += " "; 135 sCurLine += " ";
136 iNextLen = 0;
136 sNextToken.clear(); 137 sNextToken.clear();
137} 138}
138 139
139void smlToConsole( const SmlNode *pNode, Bu::String &sCurLine, 140void smlToConsole( const SmlNode *pNode )
140 Bu::String &sNextToken, int &iLineLen, int &iState )
141{ 141{
142// sio << "Begin: iState = " << iState << sio.nl; 142 Bu::String sCurLine;
143 switch( pNode->getType() ) 143 Bu::String sNextToken;
144 int iLineLen = 0;
145 int iNextLen = 0;
146 int iState = 0;
147 typedef Bu::List<SmlNode::SmlNodeList::const_iterator> NodeStack;
148 NodeStack sNode;
149
150 sNode.push( pNode->getChildren().begin() );
151
152 for(;;)
144 { 153 {
145 case SmlNode::typeRoot: 154 if( !sNode.peek() )
146 for( SmlNode::SmlNodeList::const_iterator i = 155 {
147 pNode->getChildren().begin(); i; i++ ) 156 sNode.pop();
157 if( sNode.isEmpty() )
158 break;
159 if( sNode.peek() )
148 { 160 {
149 smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState ); 161 // sio << "Pop'd: " << (*sNode.peek())->getText() << sio.nl;
162 Bu::String sTag = (*sNode.peek())->getText();
163 if( sTag == "green" || sTag == "red" )
164 {
165 sNextToken += "\x1B[0m";
166 }
167 sNode.peek()++;
168 continue;
150 } 169 }
170 }
171 if( sNode.isEmpty() )
172 {
151 break; 173 break;
152 174 }
153 case SmlNode::typeText: 175 const SmlNode *pNode = (*sNode.peek());
154 { 176 switch( pNode->getType() )
155 Bu::String::const_iterator iBgn = pNode->getText().begin(); 177 {
156 Bu::String::const_iterator iEnd = iBgn; 178 case SmlNode::typeRoot:
157 for(;iBgn;) 179 throw Bu::ExceptionBase("Invalid root.");
180
181 case SmlNode::typeText:
158 { 182 {
159// sio << iState << ": [" << (iBgn?*iBgn:'-') << ":" 183 // sio << "Process text node: " << pNode->getText() <<
160// << (iEnd?*iEnd:'-') << "]" 184 // sio.nl;
161// << sio.nl; 185 Bu::String::const_iterator iBgn = pNode->getText().begin();
186 Bu::String::const_iterator iEnd = iBgn;
162 int iTmpLen = 0; 187 int iTmpLen = 0;
163 switch( iState ) 188 for(;iBgn;)
164 { 189 {
165 case 0: // begining of paragraph 190 switch( iState )
166 if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' || 191 {
167 *iBgn == '\r' || *iBgn == '\t' ) ) 192 case 0: // begining of paragraph
168 { 193 if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' ||
169 iBgn++; 194 *iBgn == '\r' || *iBgn == '\t' ) )
170 } 195 {
171 else 196 iBgn++;
172 { 197 }
173 // Here is where you would indent paragraphs 198 else
174 iEnd = iBgn; 199 {
175 iState = 1; 200 // Here is where you would indent paragraphs
176 } 201 iEnd = iBgn;
177 break; 202 iState = 1;
178 203 }
179 case 1: // non-whitespace 204 break;
180 if( !iEnd ) 205
181 { 206 case 1: // non-whitespace
182 sNextToken.append( iBgn, iEnd ); 207 if( !iEnd )
183 iBgn = iEnd; 208 {
184 } 209 sNextToken.append( iBgn, iEnd );
185 else if( *iEnd == ' ' || *iEnd == '\n' || 210 iBgn = iEnd;
186 *iEnd == '\r' || *iEnd == '\t' ) 211 }
187 { 212 else if( *iEnd == ' ' || *iEnd == '\n' ||
188 sNextToken.append( iBgn, iEnd ); 213 *iEnd == '\r' || *iEnd == '\t' )
189 iState = 2; 214 {
190 iBgn = iEnd; 215 sNextToken.append( iBgn, iEnd );
191 } 216 iNextLen += iTmpLen;
192 else 217 iTmpLen = 0;
193 { 218 iState = 2;
194 iEnd++; 219 iBgn = iEnd;
195 iTmpLen++; 220 }
196 } 221 else
197 break; 222 {
198 223 iEnd++;
199 case 2: // Whitespace 224 iTmpLen++;
200 if( iBgn && (*iBgn == ' ' || *iBgn == '\n' || 225 }
201 *iBgn == '\r' || *iBgn == '\t') ) 226 break;
202 { 227
203 iBgn++; 228 case 2: // Whitespace
204 } 229 if( iBgn && (*iBgn == ' ' || *iBgn == '\n' ||
205 else 230 *iBgn == '\r' || *iBgn == '\t') )
206 { 231 {
207 iEnd = iBgn; 232 iBgn++;
208 iState = 1; 233 }
209 appendToken( sCurLine, sNextToken, iLineLen ); 234 else
210 } 235 {
211 break; 236 iEnd = iBgn;
237 iState = 1;
238 appendToken( sCurLine, sNextToken,
239 iLineLen, iNextLen );
240 }
241 break;
242 }
212 } 243 }
213 } 244 }
214 } 245 break;
215 break;
216 246
217 case SmlNode::typeTag: 247 case SmlNode::typeTag:
218 if( pNode->getChildren().isEmpty() ) 248 if( pNode->getChildren().isEmpty() )
219 {
220 if( pNode->getText() == "break" )
221 { 249 {
222 appendToken( sCurLine, sNextToken, iLineLen ); 250 if( pNode->getText() == "break" )
223 if( !sCurLine.isEmpty() ) 251 {
224 sio << sCurLine << sio.nl; 252 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
225 sCurLine.clear(); 253 if( !sCurLine.isEmpty() )
226 iLineLen = 0; 254 sio << sCurLine << sio.nl;
227 iState = 0; 255 sCurLine.clear();
256 iLineLen = 0;
257 iState = 0;
258 }
228 } 259 }
229 } 260 else
230 else
231 {
232 for( SmlNode::SmlNodeList::const_iterator i =
233 pNode->getChildren().begin(); i; i++ )
234 { 261 {
235 smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState ); 262// sio << "Push'd: " << pNode->getText() << sio.nl;
263 Bu::String sTag = pNode->getText();
264 if( sTag == "green" )
265 {
266 sNextToken += "\x1B[1;32m";
267 }
268 else if( sTag == "red" )
269 {
270 sNextToken += "\x1B[1;31m";
271 }
272 sNode.push( pNode->getChildren().begin() );
273 continue;
274/* for( SmlNode::SmlNodeList::const_iterator i =
275 pNode->getChildren().begin(); i; i++ )
276 {
277 s
278 smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState );
279 }
280*/
236 } 281 }
237 } 282 break;
238 break; 283 }
284 sNode.peek()++;
239 } 285 }
240 286 if( !sNextToken.isEmpty() )
241// sio << "Green? \x1b[1mhello\x1b[0m huh" << sio.nl; 287 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
288 sio << sCurLine << sio.nl;
242} 289}
243 290/*
244void smlToConsole( const SmlNode *pNode ) 291void smlToConsole( const SmlNode *pNode )
245{ 292{
246 Bu::String sBuf, sBuf2; 293 Bu::String sBuf, sBuf2;
@@ -248,7 +295,7 @@ void smlToConsole( const SmlNode *pNode )
248 smlToConsole( pNode, sBuf, sBuf2, i1, i2 ); 295 smlToConsole( pNode, sBuf, sBuf2, i1, i2 );
249 appendToken( sBuf, sBuf2, i1 ); 296 appendToken( sBuf, sBuf2, i1 );
250 sio << sBuf << sBuf2 << sio.nl; 297 sio << sBuf << sBuf2 << sio.nl;
251} 298}*/
252 299
253int Options::smlTest( Bu::Array<Bu::String> aArgs ) 300int Options::smlTest( Bu::Array<Bu::String> aArgs )
254{ 301{