summaryrefslogtreecommitdiff
path: root/src/options.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/options.cpp')
-rw-r--r--src/options.cpp264
1 files changed, 30 insertions, 234 deletions
diff --git a/src/options.cpp b/src/options.cpp
index c2e7313..ac43dfc 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -10,9 +10,12 @@
10#include <bu/optparser.h> 10#include <bu/optparser.h>
11#include <bu/sio.h> 11#include <bu/sio.h>
12 12
13#include "interfaceplugger.h"
14
13using namespace Bu; 15using namespace Bu;
14 16
15Options::Options() 17Options::Options() :
18 sInterface("console")
16{ 19{
17} 20}
18 21
@@ -29,12 +32,27 @@ void Options::parse( int argc, char *argv[] )
29 opt.addHelpBanner("usage: " + Bu::String(argv[0]) + 32 opt.addHelpBanner("usage: " + Bu::String(argv[0]) +
30 " [options] <filename>\n"); 33 " [options] <filename>\n");
31 34
35 Bu::String sIFaces;
36 Bu::StringList lIFaces = InterfacePlugger::getInstance().getPluginList();
37 bool bBegin = true;
38 for( Bu::List<Bu::String>::const_iterator i = lIFaces.begin(); i; i++ )
39 {
40 if( bBegin )
41 bBegin = false;
42 else
43 sIFaces += ", ";
44 sIFaces += *i;
45 }
46
32 opt.addOption( Bu::slot( this, &Options::smlTest ), "sml-test", 47 opt.addOption( Bu::slot( this, &Options::smlTest ), "sml-test",
33 "Test SML parser." ); 48 "Test SML parser." );
34 opt.addOption( Bu::slot( this, &Options::version ), "version", 49 opt.addOption( Bu::slot( this, &Options::version ), "version",
35 "Show full version info." ); 50 "Show full version info." );
36 opt.addOption( Bu::slot( this, &Options::builtins ), "builtins", 51 opt.addOption( Bu::slot( this, &Options::builtins ), "builtins",
37 "List available builtins." ); 52 "List available builtins." );
53 opt.addOption( sInterface, 'i', "interface",
54 "Select interface module. Default is " + sInterface +
55 ". Available modules: " + sIFaces );
38 56
39 opt.addHelpOption('h', "help"); 57 opt.addHelpOption('h', "help");
40 opt.setNonOption( Bu::slot( this, &Options::nonOption ) ); 58 opt.setNonOption( Bu::slot( this, &Options::nonOption ) );
@@ -67,238 +85,6 @@ int Options::builtins( Bu::StrArray aArgs )
67 return 0; 85 return 0;
68} 86}
69 87
70void smlToHtml( const SmlNode *pNode )
71{
72 switch( pNode->getType() )
73 {
74 case SmlNode::typeRoot:
75 sio << "<p>";
76 for( SmlNode::SmlNodeList::const_iterator i =
77 pNode->getChildren().begin(); i; i++ )
78 {
79 smlToHtml( *i );
80 }
81 sio << "</p>";
82 break;
83
84 case SmlNode::typeText:
85 sio << pNode->getText();
86 break;
87
88 case SmlNode::typeTag:
89 if( pNode->getChildren().isEmpty() )
90 {
91 if( pNode->getText() == "break" )
92 sio << "</p><p>";
93 }
94 else
95 {
96 if( pNode->getText() == "red" )
97 {
98 sio << "<span style=\"color: red;\">";
99 for( SmlNode::SmlNodeList::const_iterator i =
100 pNode->getChildren().begin(); i; i++ )
101 {
102 smlToHtml( *i );
103 }
104 sio << "</span>";
105 }
106 else if( pNode->getText() == "green" )
107 {
108 sio << "<span style=\"color: green;\">";
109 for( SmlNode::SmlNodeList::const_iterator i =
110 pNode->getChildren().begin(); i; i++ )
111 {
112 smlToHtml( *i );
113 }
114 sio << "</span>";
115 }
116 }
117 break;
118 }
119}
120
121void appendToken( Bu::String &sCurLine, Bu::String &sNextToken, int &iLineLen,
122 int &iNextLen )
123{
124 if( iLineLen + iNextLen + 1 >= 78 )
125 {
126 sio << sCurLine << sio.nl;
127 iLineLen = 0;
128 sCurLine = sNextToken;
129 }
130 else
131 {
132 sCurLine += sNextToken;
133 }
134 iLineLen += iNextLen + 1;
135 sCurLine += " ";
136 iNextLen = 0;
137 sNextToken.clear();
138}
139
140void smlToConsole( const SmlNode *pNode )
141{
142 Bu::String sCurLine;
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(;;)
153 {
154 if( !sNode.peek() )
155 {
156 sNode.pop();
157 if( sNode.isEmpty() )
158 break;
159 if( sNode.peek() )
160 {
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;
169 }
170 }
171 if( sNode.isEmpty() )
172 {
173 break;
174 }
175 const SmlNode *pNode = (*sNode.peek());
176 switch( pNode->getType() )
177 {
178 case SmlNode::typeRoot:
179 throw Bu::ExceptionBase("Invalid root.");
180
181 case SmlNode::typeText:
182 {
183 // sio << "Process text node: " << pNode->getText() <<
184 // sio.nl;
185 Bu::String::const_iterator iBgn = pNode->getText().begin();
186 Bu::String::const_iterator iEnd = iBgn;
187 int iTmpLen = 0;
188 for(;iBgn;)
189 {
190 switch( iState )
191 {
192 case 0: // begining of paragraph
193 if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' ||
194 *iBgn == '\r' || *iBgn == '\t' ) )
195 {
196 iBgn++;
197 }
198 else
199 {
200 // Here is where you would indent paragraphs
201 iEnd = iBgn;
202 iState = 1;
203 }
204 break;
205
206 case 1: // non-whitespace
207 if( !iEnd )
208 {
209 sNextToken.append( iBgn, iEnd );
210 iBgn = iEnd;
211 iNextLen += iTmpLen;
212 iTmpLen = 0;
213 }
214 else if( *iEnd == ' ' || *iEnd == '\n' ||
215 *iEnd == '\r' || *iEnd == '\t' )
216 {
217 sNextToken.append( iBgn, iEnd );
218 iNextLen += iTmpLen;
219 iTmpLen = 0;
220 iState = 2;
221 iBgn = iEnd;
222 }
223 else
224 {
225 iEnd++;
226 iTmpLen++;
227 }
228 break;
229
230 case 2: // Whitespace
231 if( iBgn && (*iBgn == ' ' || *iBgn == '\n' ||
232 *iBgn == '\r' || *iBgn == '\t') )
233 {
234 iBgn++;
235 }
236 else
237 {
238 iEnd = iBgn;
239 iState = 1;
240 appendToken( sCurLine, sNextToken,
241 iLineLen, iNextLen );
242 }
243 break;
244 }
245 }
246 }
247 break;
248
249 case SmlNode::typeTag:
250 if( pNode->getChildren().isEmpty() )
251 {
252 if( pNode->getText() == "break" )
253 {
254 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
255 if( !sCurLine.isEmpty() )
256 sio << sCurLine << sio.nl;
257 sCurLine.clear();
258 iLineLen = 0;
259 iState = 0;
260 }
261 }
262 else
263 {
264// sio << "Push'd: " << pNode->getText() << sio.nl;
265 Bu::String sTag = pNode->getText();
266 if( sTag == "green" )
267 {
268 sNextToken += "\x1B[1;32m";
269 }
270 else if( sTag == "red" )
271 {
272 sNextToken += "\x1B[1;31m";
273 }
274 sNode.push( pNode->getChildren().begin() );
275 continue;
276/* for( SmlNode::SmlNodeList::const_iterator i =
277 pNode->getChildren().begin(); i; i++ )
278 {
279 s
280 smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState );
281 }
282*/
283 }
284 break;
285 }
286 sNode.peek()++;
287 }
288 if( !sNextToken.isEmpty() )
289 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
290 sio << sCurLine << sio.nl;
291}
292/*
293void smlToConsole( const SmlNode *pNode )
294{
295 Bu::String sBuf, sBuf2;
296 int i1 = 0, i2 = 0;
297 smlToConsole( pNode, sBuf, sBuf2, i1, i2 );
298 appendToken( sBuf, sBuf2, i1 );
299 sio << sBuf << sBuf2 << sio.nl;
300}*/
301
302int Options::smlTest( Bu::Array<Bu::String> aArgs ) 88int Options::smlTest( Bu::Array<Bu::String> aArgs )
303{ 89{
304 Bu::String sContent; 90 Bu::String sContent;
@@ -313,7 +99,17 @@ int Options::smlTest( Bu::Array<Bu::String> aArgs )
313 SmlNode *pRoot = SmlNode::parse( sContent ); 99 SmlNode *pRoot = SmlNode::parse( sContent );
314 sio << *pRoot << sio.nl; 100 sio << *pRoot << sio.nl;
315 101
316 smlToConsole( pRoot ); 102 try
103 {
104 InterfacePlugger &ip = InterfacePlugger::getInstance();
105 Interface *pIface = ip.instantiate( sInterface );
106 pIface->display( pRoot );
107 ip.destroy( pIface );
108 }
109 catch( Bu::HashException &e )
110 {
111 sio << "No such interface found." << sio.nl;
112 }
317 113
318 delete pRoot; 114 delete pRoot;
319 115