aboutsummaryrefslogtreecommitdiff
path: root/src/paramproc.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-06-29 02:50:56 +0000
committerMike Buland <eichlan@xagasoft.com>2006-06-29 02:50:56 +0000
commit5e386890b41fe043e2639b25b613831ef8362e7b (patch)
tree60da93a6ade1db5ed2620fbe47b472291459a1a1 /src/paramproc.cpp
parentac98da8eacbf1147d69e378db62432db05e8c73e (diff)
downloadlibbu++-5e386890b41fe043e2639b25b613831ef8362e7b.tar.gz
libbu++-5e386890b41fe043e2639b25b613831ef8362e7b.tar.bz2
libbu++-5e386890b41fe043e2639b25b613831ef8362e7b.tar.xz
libbu++-5e386890b41fe043e2639b25b613831ef8362e7b.zip
Completely removed the old, crappy pproc and replaced it with the new, shiny
ParamProc class...it's soooo much better it makes me wanna' throw things...
Diffstat (limited to 'src/paramproc.cpp')
-rw-r--r--src/paramproc.cpp398
1 files changed, 398 insertions, 0 deletions
diff --git a/src/paramproc.cpp b/src/paramproc.cpp
new file mode 100644
index 0000000..10284c4
--- /dev/null
+++ b/src/paramproc.cpp
@@ -0,0 +1,398 @@
1#include "paramproc.h"
2#include <stdio.h>
3
4#define ptrtype( iitype, iiname ) \
5 ParamProc::ParamPtr::ParamPtr( iitype *iiname ) : \
6 type( vt ##iiname ) { val.iiname = iiname; }
7
8ParamProc::ParamPtr::ParamPtr()
9{
10 val.str = NULL;
11 type = vtunset;
12}
13
14ptrtype( std::string, str );
15ptrtype( uint64_t, uint64 );
16ptrtype( uint32_t, uint32 );
17ptrtype( uint16_t, uint16 );
18ptrtype( uint8_t, uint8 );
19ptrtype( int64_t, int64 );
20ptrtype( int32_t, int32 );
21ptrtype( int16_t, int16 );
22ptrtype( int8_t, int8 );
23ptrtype( float, float32 );
24ptrtype( double, float64 );
25ptrtype( long double, float96 );
26ptrtype( bool, bln );
27
28ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( ParamProc::ParamPtr &ptr )
29{
30 val = ptr.val;
31 type = ptr.type;
32
33 return *this;
34}
35
36bool ParamProc::ParamPtr::isSet()
37{
38 return type != vtunset;
39}
40
41ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( const char *str )
42{
43 if( !isSet() ) return *this;
44 switch( type )
45 {
46 case vtstr:
47 (*val.str) = str;
48 break;
49
50 case vtuint64:
51 (*val.uint64) = strtoull( str, NULL, 10 );
52 break;
53
54 case vtuint32:
55 (*val.uint32) = strtoul( str, NULL, 10 );
56 break;
57
58 case vtuint16:
59 (*val.uint16) = (uint16_t)strtoul( str, NULL, 10 );
60 break;
61
62 case vtuint8:
63 (*val.uint8) = (uint8_t)strtoul( str, NULL, 10 );
64 break;
65
66 case vtint64:
67 (*val.int64) = strtoll( str, NULL, 10 );
68 break;
69
70 case vtint32:
71 (*val.int32) = strtol( str, NULL, 10 );
72 break;
73
74 case vtint16:
75 (*val.int16) = (int16_t)strtol( str, NULL, 10 );
76 break;
77
78 case vtint8:
79 (*val.int8) = (int8_t)strtol( str, NULL, 10 );
80 break;
81
82 case vtfloat32:
83 (*val.float32) = strtof( str, NULL );
84 break;
85
86 case vtfloat64:
87 (*val.float64) = strtod( str, NULL );
88 break;
89
90 case vtfloat96:
91 (*val.float96) = strtold( str, NULL );
92 break;
93
94 case vtbln:
95 if( strcasecmp("yes", str ) == 0 ||
96 strcasecmp("true", str ) == 0 )
97 {
98 (*val.bln) = true;
99 }
100 else
101 {
102 (*val.bln) = false;
103 }
104 break;
105 }
106
107 return *this;
108}
109
110ParamProc::ParamProc()
111{
112}
113
114ParamProc::~ParamProc()
115{
116}
117/*
118void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val )
119{
120 printf("Calling callback...\n");
121 val = "Hello there, this is set in the ParamProc";
122 (this->*proc)();
123}*/
124
125void ParamProc::addParam( const char *lpWord, char cChar, Proc proc,
126 ParamPtr val, const char *lpDesc, const char *lpExtra,
127 const char *lpValue )
128{
129 ArgSpec *as = new ArgSpec;
130 if( lpWord )
131 as->sWord = lpWord;
132
133 as->cChar = cChar;
134 as->proc = proc;
135 as->val = val;
136 if( lpDesc )
137 as->sDesc = lpDesc;
138 if( lpExtra )
139 as->sExtra = lpExtra;
140 if( lpValue )
141 as->sValue = lpValue;
142
143 lArg.push_back( as );
144}
145
146void ParamProc::addParam( const char *lpWord, char cChar, Proc proc,
147 const char *lpDesc, const char *lpExtra,
148 const char *lpValue )
149{
150 addParam( lpWord, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue );
151}
152
153void ParamProc::addParam( const char *lpWord, char cChar, ParamPtr val,
154 const char *lpDesc, const char *lpExtra,
155 const char *lpValue )
156{
157 addParam( lpWord, cChar, NULL, val, lpDesc, lpExtra, lpValue );
158}
159
160void ParamProc::addParam( const char *lpWord, Proc proc, ParamPtr val,
161 const char *lpDesc, const char *lpExtra,
162 const char *lpValue )
163{
164 addParam( lpWord, '\0', proc, val, lpDesc, lpExtra, lpValue );
165}
166
167void ParamProc::addParam( const char *lpWord, Proc proc,
168 const char *lpDesc, const char *lpExtra,
169 const char *lpValue )
170{
171 addParam( lpWord, '\0', proc, ParamPtr(), lpDesc, lpExtra, lpValue );
172}
173
174void ParamProc::addParam( const char *lpWord, ParamPtr val,
175 const char *lpDesc, const char *lpExtra,
176 const char *lpValue )
177{
178 addParam( lpWord, '\0', NULL, val, lpDesc, lpExtra, lpValue );
179}
180
181void ParamProc::addParam( char cChar, Proc proc, ParamPtr val,
182 const char *lpDesc, const char *lpExtra,
183 const char *lpValue )
184{
185 addParam( NULL, cChar, proc, val, lpDesc, lpExtra, lpValue );
186}
187
188void ParamProc::addParam( char cChar, Proc proc,
189 const char *lpDesc, const char *lpExtra,
190 const char *lpValue )
191{
192 addParam( NULL, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue );
193}
194
195void ParamProc::addParam( char cChar, ParamPtr val,
196 const char *lpDesc, const char *lpExtra,
197 const char *lpValue )
198{
199 addParam( NULL, cChar, NULL, val, lpDesc, lpExtra, lpValue );
200}
201
202void ParamProc::process( int argc, char *argv[] )
203{
204 for( int arg = 1; arg < argc; arg++ )
205 {
206 printf(":::%d:::%s\n", arg, argv[arg] );
207 if( argv[arg][0] == '-' )
208 {
209 if( argv[arg][1] == '-' )
210 {
211 ArgSpec *s = checkWord( argv[arg]+2 );
212 if( s )
213 {
214 if( argv[arg][s->sWord.getLength()+2] == '=' )
215 {
216 if( s->val.isSet() )
217 {
218 if( s->sValue.getString() == NULL )
219 {
220 s->val = argv[arg]+s->sWord.getLength()+3;
221 }
222 else
223 {
224 s->val = s->sValue.getString();
225 }
226 }
227 if( s->proc )
228 {
229 char **tmp = new char*[argc-arg];
230 tmp[0] = argv[arg]+s->sWord.getLength()+3;
231 for( int k = 1; k < argc-arg; k++ )
232 tmp[k] = argv[arg+k];
233 int ret = (this->*s->proc)( argc-arg, tmp );
234 if( ret > 0 )
235 {
236 arg += ret-1;
237 }
238 delete tmp;
239 }
240 }
241 else
242 {
243 int add = 0;
244 if( s->val.isSet() )
245 {
246 if( s->sValue.getString() == NULL )
247 {
248 if( arg+1 >= argc )
249 {
250 return;
251 }
252 s->val = argv[arg+1];
253 add++;
254 }
255 else
256 {
257 s->val = s->sValue.getString();
258 }
259 }
260 if( s->proc )
261 {
262 int ret = (this->*s->proc)(
263 argc-arg-1, argv+arg+1 );
264
265 if( ret > add )
266 arg += ret;
267 else
268 arg += add;
269 }
270 }
271 continue;
272 }
273 }
274 else
275 {
276 for( int chr = 1; argv[arg][chr]; chr++ )
277 {
278 ArgSpec *s = checkLetr( argv[arg][chr] );
279 if( s )
280 {
281 if( argv[arg][chr+1] != '\0' )
282 {
283 bool bUsed = false;
284 if( s->val.isSet() )
285 {
286 if( s->sValue.getString() == NULL )
287 {
288 s->val = argv[arg]+chr+1;
289 bUsed = true;
290 }
291 else
292 {
293 s->val = s->sValue.getString();
294 }
295 }
296 if( s->proc )
297 {
298 char **tmp = new char*[argc-arg];
299 tmp[0] = argv[arg]+chr+1;
300 for( int k = 1; k < argc-arg; k++ )
301 tmp[k] = argv[arg+k];
302 int ret = (this->*s->proc)( argc-arg, tmp );
303 if( ret > 0 )
304 {
305 arg += ret - 1;
306 delete tmp;
307 break;
308 }
309 if( bUsed )
310 {
311 delete tmp;
312 break;
313 }
314 delete tmp;
315 }
316 }
317 else
318 {
319 bool bUsed = false;
320 if( s->val.isSet() )
321 {
322 if( s->sValue.getString() == NULL )
323 {
324 s->val = argv[arg+1];
325 bUsed = true;
326 }
327 else
328 {
329 s->val = s->sValue.getString();
330 }
331 }
332 if( s->proc )
333 {
334 int ret = (this->*s->proc)(
335 argc-arg-1, argv+arg+1
336 );
337 if( ret > 0 )
338 {
339 arg += ret;
340 break;
341 }
342 if( bUsed )
343 {
344 arg++;
345 break;
346 }
347 }
348 }
349 }
350 }
351 }
352 }
353 }
354}
355
356ParamProc::ArgSpec *ParamProc::checkWord( const char *arg )
357{
358 printf("Checking \"%s\"...\n", arg );
359 std::list<ArgSpec *>::const_iterator i;
360 for( i = lArg.begin(); i != lArg.end(); i++ )
361 {
362 if( (*i)->sWord.getString() == NULL )
363 continue;
364
365 if( !strcmp( (*i)->sWord, arg ) )
366 return *i;
367
368 if( (*i)->val.isSet() )
369 {
370 if( !strncmp( (*i)->sWord, arg, (*i)->sWord.getLength() ) &&
371 arg[(*i)->sWord.getLength()] == '=' )
372 {
373 return *i;
374 }
375 }
376 }
377
378 return NULL;
379}
380
381ParamProc::ArgSpec *ParamProc::checkLetr( const char arg )
382{
383 printf("Checking \'%c\'...\n", arg );
384 std::list<ArgSpec *>::const_iterator i;
385 for( i = lArg.begin(); i != lArg.end(); i++ )
386 {
387 if( (*i)->cChar == '\0' )
388 continue;
389
390 if( (*i)->cChar == arg )
391 {
392 return *i;
393 }
394 }
395
396 return NULL;
397}
398