diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
commit | f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 (patch) | |
tree | 13cdf64f7cf134f397a7165b7a3fe0807e37026b /src/paramproc.cpp | |
parent | 74d4c8cd27334fc7204d5a8773deb3d424565778 (diff) | |
download | libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.gz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.bz2 libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.xz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.zip |
Ok, no code is left in src, it's all in src/old. We'll gradually move code back
into src as it's fixed and re-org'd. This includes tests, which, I may write a
unit test system into libbu++ just to make my life easier.
Diffstat (limited to 'src/paramproc.cpp')
-rw-r--r-- | src/paramproc.cpp | 514 |
1 files changed, 0 insertions, 514 deletions
diff --git a/src/paramproc.cpp b/src/paramproc.cpp deleted file mode 100644 index a352e66..0000000 --- a/src/paramproc.cpp +++ /dev/null | |||
@@ -1,514 +0,0 @@ | |||
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 | |||
8 | ParamProc::ParamPtr::ParamPtr() | ||
9 | { | ||
10 | val.str = NULL; | ||
11 | type = vtunset; | ||
12 | } | ||
13 | |||
14 | ptrtype( std::string, str ); | ||
15 | ptrtype( uint64_t, uint64 ); | ||
16 | ptrtype( uint32_t, uint32 ); | ||
17 | ptrtype( uint16_t, uint16 ); | ||
18 | ptrtype( uint8_t, uint8 ); | ||
19 | ptrtype( int64_t, int64 ); | ||
20 | ptrtype( int32_t, int32 ); | ||
21 | ptrtype( int16_t, int16 ); | ||
22 | ptrtype( int8_t, int8 ); | ||
23 | ptrtype( float, float32 ); | ||
24 | ptrtype( double, float64 ); | ||
25 | ptrtype( long double, float96 ); | ||
26 | ptrtype( bool, bln ); | ||
27 | |||
28 | ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( ParamProc::ParamPtr &ptr ) | ||
29 | { | ||
30 | val = ptr.val; | ||
31 | type = ptr.type; | ||
32 | |||
33 | return *this; | ||
34 | } | ||
35 | |||
36 | bool ParamProc::ParamPtr::isSet() | ||
37 | { | ||
38 | return type != vtunset; | ||
39 | } | ||
40 | |||
41 | ParamProc::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 | |||
110 | ParamProc::ParamProc() | ||
111 | { | ||
112 | } | ||
113 | |||
114 | ParamProc::~ParamProc() | ||
115 | { | ||
116 | for( std::list<ArgSpec *>::iterator i = lArg.begin(); | ||
117 | i != lArg.end(); i++ ) | ||
118 | { | ||
119 | delete *i; | ||
120 | } | ||
121 | |||
122 | for( std::list<Banner *>::iterator i = lBan.begin(); | ||
123 | i != lBan.end(); i++ ) | ||
124 | { | ||
125 | delete *i; | ||
126 | } | ||
127 | |||
128 | } | ||
129 | /* | ||
130 | void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val ) | ||
131 | { | ||
132 | printf("Calling callback...\n"); | ||
133 | val = "Hello there, this is set in the ParamProc"; | ||
134 | (this->*proc)(); | ||
135 | }*/ | ||
136 | |||
137 | void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, | ||
138 | ParamPtr val, const char *lpDesc, const char *lpExtra, | ||
139 | const char *lpValue ) | ||
140 | { | ||
141 | ArgSpec *as = new ArgSpec; | ||
142 | if( lpWord ) | ||
143 | as->sWord = lpWord; | ||
144 | |||
145 | as->cChar = cChar; | ||
146 | as->proc = proc; | ||
147 | as->val = val; | ||
148 | if( lpDesc ) | ||
149 | as->sDesc = lpDesc; | ||
150 | if( lpExtra ) | ||
151 | as->sExtra = lpExtra; | ||
152 | if( lpValue ) | ||
153 | as->sValue = lpValue; | ||
154 | |||
155 | lArg.push_back( as ); | ||
156 | |||
157 | if( !lBan.empty() ) | ||
158 | { | ||
159 | if( lBan.back()->pBefore == NULL ) | ||
160 | lBan.back()->pBefore = as; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, | ||
165 | const char *lpDesc, const char *lpExtra, | ||
166 | const char *lpValue ) | ||
167 | { | ||
168 | addParam( lpWord, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
169 | } | ||
170 | |||
171 | void ParamProc::addParam( const char *lpWord, char cChar, ParamPtr val, | ||
172 | const char *lpDesc, const char *lpExtra, | ||
173 | const char *lpValue ) | ||
174 | { | ||
175 | addParam( lpWord, cChar, NULL, val, lpDesc, lpExtra, lpValue ); | ||
176 | } | ||
177 | |||
178 | void ParamProc::addParam( const char *lpWord, Proc proc, ParamPtr val, | ||
179 | const char *lpDesc, const char *lpExtra, | ||
180 | const char *lpValue ) | ||
181 | { | ||
182 | addParam( lpWord, '\0', proc, val, lpDesc, lpExtra, lpValue ); | ||
183 | } | ||
184 | |||
185 | void ParamProc::addParam( const char *lpWord, Proc proc, | ||
186 | const char *lpDesc, const char *lpExtra, | ||
187 | const char *lpValue ) | ||
188 | { | ||
189 | addParam( lpWord, '\0', proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
190 | } | ||
191 | |||
192 | void ParamProc::addParam( const char *lpWord, ParamPtr val, | ||
193 | const char *lpDesc, const char *lpExtra, | ||
194 | const char *lpValue ) | ||
195 | { | ||
196 | addParam( lpWord, '\0', NULL, val, lpDesc, lpExtra, lpValue ); | ||
197 | } | ||
198 | |||
199 | void ParamProc::addParam( char cChar, Proc proc, ParamPtr val, | ||
200 | const char *lpDesc, const char *lpExtra, | ||
201 | const char *lpValue ) | ||
202 | { | ||
203 | addParam( NULL, cChar, proc, val, lpDesc, lpExtra, lpValue ); | ||
204 | } | ||
205 | |||
206 | void ParamProc::addParam( char cChar, Proc proc, | ||
207 | const char *lpDesc, const char *lpExtra, | ||
208 | const char *lpValue ) | ||
209 | { | ||
210 | addParam( NULL, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
211 | } | ||
212 | |||
213 | void ParamProc::addParam( char cChar, ParamPtr val, | ||
214 | const char *lpDesc, const char *lpExtra, | ||
215 | const char *lpValue ) | ||
216 | { | ||
217 | addParam( NULL, cChar, NULL, val, lpDesc, lpExtra, lpValue ); | ||
218 | } | ||
219 | |||
220 | void ParamProc::process( int argc, char *argv[] ) | ||
221 | { | ||
222 | for( int arg = 1; arg < argc; arg++ ) | ||
223 | { | ||
224 | //printf(":::%d:::%s\n", arg, argv[arg] ); | ||
225 | if( argv[arg][0] == '-' ) | ||
226 | { | ||
227 | if( argv[arg][1] == '-' ) | ||
228 | { | ||
229 | ArgSpec *s = checkWord( argv[arg]+2 ); | ||
230 | if( s ) | ||
231 | { | ||
232 | if( argv[arg][s->sWord.getLength()+2] == '=' ) | ||
233 | { | ||
234 | if( s->val.isSet() ) | ||
235 | { | ||
236 | if( s->sValue.getString() == NULL ) | ||
237 | { | ||
238 | s->val = argv[arg]+s->sWord.getLength()+3; | ||
239 | } | ||
240 | else | ||
241 | { | ||
242 | s->val = s->sValue.getString(); | ||
243 | } | ||
244 | } | ||
245 | if( s->proc ) | ||
246 | { | ||
247 | char **tmp = new char*[argc-arg]; | ||
248 | tmp[0] = argv[arg]+s->sWord.getLength()+3; | ||
249 | for( int k = 1; k < argc-arg; k++ ) | ||
250 | tmp[k] = argv[arg+k]; | ||
251 | int ret = (this->*s->proc)( argc-arg, tmp ); | ||
252 | if( ret > 0 ) | ||
253 | { | ||
254 | arg += ret-1; | ||
255 | } | ||
256 | delete tmp; | ||
257 | } | ||
258 | } | ||
259 | else | ||
260 | { | ||
261 | int add = 0; | ||
262 | if( s->val.isSet() ) | ||
263 | { | ||
264 | if( s->sValue.getString() == NULL ) | ||
265 | { | ||
266 | if( arg+1 >= argc ) | ||
267 | { | ||
268 | return; | ||
269 | } | ||
270 | s->val = argv[arg+1]; | ||
271 | add++; | ||
272 | } | ||
273 | else | ||
274 | { | ||
275 | s->val = s->sValue.getString(); | ||
276 | } | ||
277 | } | ||
278 | if( s->proc ) | ||
279 | { | ||
280 | int ret = (this->*s->proc)( | ||
281 | argc-arg-1, argv+arg+1 ); | ||
282 | |||
283 | if( ret > add ) | ||
284 | add = 0; | ||
285 | else | ||
286 | add -= ret; | ||
287 | arg += ret; | ||
288 | } | ||
289 | arg += add; | ||
290 | } | ||
291 | continue; | ||
292 | } | ||
293 | else | ||
294 | { | ||
295 | unknownParam( argc-arg, argv+arg ); | ||
296 | } | ||
297 | } | ||
298 | else | ||
299 | { | ||
300 | for( int chr = 1; argv[arg][chr]; chr++ ) | ||
301 | { | ||
302 | ArgSpec *s = checkLetr( argv[arg][chr] ); | ||
303 | if( s ) | ||
304 | { | ||
305 | if( argv[arg][chr+1] != '\0' ) | ||
306 | { | ||
307 | bool bUsed = false; | ||
308 | if( s->val.isSet() ) | ||
309 | { | ||
310 | if( s->sValue.getString() == NULL ) | ||
311 | { | ||
312 | s->val = argv[arg]+chr+1; | ||
313 | bUsed = true; | ||
314 | } | ||
315 | else | ||
316 | { | ||
317 | s->val = s->sValue.getString(); | ||
318 | } | ||
319 | } | ||
320 | if( s->proc ) | ||
321 | { | ||
322 | char **tmp = new char*[argc-arg]; | ||
323 | tmp[0] = argv[arg]+chr+1; | ||
324 | for( int k = 1; k < argc-arg; k++ ) | ||
325 | tmp[k] = argv[arg+k]; | ||
326 | int ret = (this->*s->proc)( argc-arg, tmp ); | ||
327 | if( ret > 0 ) | ||
328 | { | ||
329 | arg += ret - 1; | ||
330 | delete tmp; | ||
331 | break; | ||
332 | } | ||
333 | delete tmp; | ||
334 | } | ||
335 | if( bUsed ) | ||
336 | { | ||
337 | break; | ||
338 | } | ||
339 | } | ||
340 | else | ||
341 | { | ||
342 | bool bUsed = false; | ||
343 | if( s->val.isSet() ) | ||
344 | { | ||
345 | if( s->sValue.getString() == NULL ) | ||
346 | { | ||
347 | s->val = argv[arg+1]; | ||
348 | bUsed = true; | ||
349 | } | ||
350 | else | ||
351 | { | ||
352 | s->val = s->sValue.getString(); | ||
353 | } | ||
354 | } | ||
355 | if( s->proc ) | ||
356 | { | ||
357 | int ret = (this->*s->proc)( | ||
358 | argc-arg-1, argv+arg+1 | ||
359 | ); | ||
360 | if( ret > 0 ) | ||
361 | { | ||
362 | arg += ret; | ||
363 | break; | ||
364 | } | ||
365 | } | ||
366 | if( bUsed ) | ||
367 | { | ||
368 | arg++; | ||
369 | break; | ||
370 | } | ||
371 | } | ||
372 | } | ||
373 | else | ||
374 | { | ||
375 | unknownParam( argc-arg, argv+arg ); | ||
376 | } | ||
377 | } | ||
378 | } | ||
379 | } | ||
380 | else | ||
381 | { | ||
382 | cmdParam( argc-arg, argv+arg ); | ||
383 | } | ||
384 | } | ||
385 | } | ||
386 | |||
387 | ParamProc::ArgSpec *ParamProc::checkWord( const char *arg ) | ||
388 | { | ||
389 | //printf("Checking \"%s\"...\n", arg ); | ||
390 | std::list<ArgSpec *>::const_iterator i; | ||
391 | for( i = lArg.begin(); i != lArg.end(); i++ ) | ||
392 | { | ||
393 | if( (*i)->sWord.getString() == NULL ) | ||
394 | continue; | ||
395 | |||
396 | if( !strcmp( (*i)->sWord, arg ) ) | ||
397 | return *i; | ||
398 | |||
399 | if( (*i)->val.isSet() ) | ||
400 | { | ||
401 | if( !strncmp( (*i)->sWord, arg, (*i)->sWord.getLength() ) && | ||
402 | arg[(*i)->sWord.getLength()] == '=' ) | ||
403 | { | ||
404 | return *i; | ||
405 | } | ||
406 | } | ||
407 | } | ||
408 | |||
409 | return NULL; | ||
410 | } | ||
411 | |||
412 | ParamProc::ArgSpec *ParamProc::checkLetr( const char arg ) | ||
413 | { | ||
414 | //printf("Checking \'%c\'...\n", arg ); | ||
415 | std::list<ArgSpec *>::const_iterator i; | ||
416 | for( i = lArg.begin(); i != lArg.end(); i++ ) | ||
417 | { | ||
418 | if( (*i)->cChar == '\0' ) | ||
419 | continue; | ||
420 | |||
421 | if( (*i)->cChar == arg ) | ||
422 | { | ||
423 | return *i; | ||
424 | } | ||
425 | } | ||
426 | |||
427 | return NULL; | ||
428 | } | ||
429 | |||
430 | int ParamProc::cmdParam( int argc, char *argv[] ) | ||
431 | { | ||
432 | printf("Unhandled command parameter \"%s\" found!\n", argv[0] ); | ||
433 | return 0; | ||
434 | } | ||
435 | |||
436 | int ParamProc::unknownParam( int argc, char *argv[] ) | ||
437 | { | ||
438 | printf("Unknown parameter \"%s\" found!\n", argv[0] ); | ||
439 | return 0; | ||
440 | } | ||
441 | |||
442 | int ParamProc::help( int argc, char *argv[] ) | ||
443 | { | ||
444 | std::list<Banner *>::const_iterator b = lBan.begin(); | ||
445 | std::list<ArgSpec *>::const_iterator i; | ||
446 | int len=0; | ||
447 | for( i = lArg.begin(); i != lArg.end(); i++ ) | ||
448 | { | ||
449 | if( len < (*i)->sWord.getLength() + (*i)->sExtra.getLength() ) | ||
450 | len = (*i)->sWord.getLength() + (*i)->sExtra.getLength(); | ||
451 | } | ||
452 | char fmt[10]; | ||
453 | sprintf( fmt, "%%-%ds ", len ); | ||
454 | |||
455 | for( i = lArg.begin(); i != lArg.end(); i++ ) | ||
456 | { | ||
457 | if( b != lBan.end() ) | ||
458 | { | ||
459 | if( (*b)->pBefore == (*i) ) | ||
460 | { | ||
461 | printf( (*b)->sBanner.getString() ); | ||
462 | b++; | ||
463 | } | ||
464 | } | ||
465 | printf(" "); | ||
466 | if( (*i)->cChar ) | ||
467 | { | ||
468 | if( (*i)->sWord.getString() ) | ||
469 | { | ||
470 | printf("-%c, ", (*i)->cChar ); | ||
471 | } | ||
472 | else | ||
473 | { | ||
474 | printf("-%c ", (*i)->cChar ); | ||
475 | } | ||
476 | } | ||
477 | else | ||
478 | { | ||
479 | printf(" "); | ||
480 | } | ||
481 | if( (*i)->sWord.getString() ) | ||
482 | { | ||
483 | printf("--"); | ||
484 | std::string sTmp = (*i)->sWord.getString(); | ||
485 | if( (*i)->sExtra.getString() ) | ||
486 | sTmp += (*i)->sExtra.getString(); | ||
487 | printf( fmt, sTmp.c_str() ); | ||
488 | } | ||
489 | else | ||
490 | { | ||
491 | printf(" "); | ||
492 | printf(fmt, "" ); | ||
493 | } | ||
494 | printf("%s\n", (*i)->sDesc.getString() ); | ||
495 | } | ||
496 | if( b != lBan.end() ) | ||
497 | { | ||
498 | if( (*b)->pBefore == NULL ) | ||
499 | { | ||
500 | printf( (*b)->sBanner.getString() ); | ||
501 | } | ||
502 | } | ||
503 | |||
504 | exit( 0 ); | ||
505 | } | ||
506 | |||
507 | void ParamProc::addHelpBanner( const char *sHelpBanner ) | ||
508 | { | ||
509 | Banner *pBan = new Banner; | ||
510 | pBan->sBanner = sHelpBanner; | ||
511 | pBan->pBefore = NULL; | ||
512 | lBan.push_back( pBan ); | ||
513 | } | ||
514 | |||