diff options
Diffstat (limited to 'src/paramproc.cpp')
| -rw-r--r-- | src/paramproc.cpp | 523 | 
1 files changed, 0 insertions, 523 deletions
diff --git a/src/paramproc.cpp b/src/paramproc.cpp deleted file mode 100644 index f4fd36e..0000000 --- a/src/paramproc.cpp +++ /dev/null  | |||
| @@ -1,523 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2007-2010 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/paramproc.h" | ||
| 9 | #include <stdio.h> | ||
| 10 | #include <stdlib.h> | ||
| 11 | #include <string.h> | ||
| 12 | |||
| 13 | #define ptrtype( iitype, iiname ) \ | ||
| 14 | Bu::ParamProc::ParamPtr::ParamPtr( iitype *iiname ) : \ | ||
| 15 | type( vt ##iiname ) { val.iiname = iiname; } | ||
| 16 | |||
| 17 | Bu::ParamProc::ParamPtr::ParamPtr() | ||
| 18 | { | ||
| 19 | val.str = NULL; | ||
| 20 | type = vtunset; | ||
| 21 | } | ||
| 22 | |||
| 23 | ptrtype( Bu::FString, str ); | ||
| 24 | ptrtype( uint64_t, uint64 ); | ||
| 25 | ptrtype( uint32_t, uint32 ); | ||
| 26 | ptrtype( uint16_t, uint16 ); | ||
| 27 | ptrtype( uint8_t, uint8 ); | ||
| 28 | ptrtype( int64_t, int64 ); | ||
| 29 | ptrtype( int32_t, int32 ); | ||
| 30 | ptrtype( int16_t, int16 ); | ||
| 31 | ptrtype( int8_t, int8 ); | ||
| 32 | ptrtype( float, float32 ); | ||
| 33 | ptrtype( double, float64 ); | ||
| 34 | ptrtype( long double, float96 ); | ||
| 35 | ptrtype( bool, bln ); | ||
| 36 | |||
| 37 | Bu::ParamProc::ParamPtr &Bu::ParamProc::ParamPtr::operator=( ParamProc::ParamPtr &ptr ) | ||
| 38 | { | ||
| 39 | val = ptr.val; | ||
| 40 | type = ptr.type; | ||
| 41 | |||
| 42 | return *this; | ||
| 43 | } | ||
| 44 | |||
| 45 | bool Bu::ParamProc::ParamPtr::isSet() | ||
| 46 | { | ||
| 47 | return type != vtunset; | ||
| 48 | } | ||
| 49 | |||
| 50 | Bu::ParamProc::ParamPtr &Bu::ParamProc::ParamPtr::operator=( const char *str ) | ||
| 51 | { | ||
| 52 | if( !isSet() ) return *this; | ||
| 53 | switch( type ) | ||
| 54 | { | ||
| 55 | case vtstr: | ||
| 56 | (*val.str) = str; | ||
| 57 | break; | ||
| 58 | |||
| 59 | case vtuint64: | ||
| 60 | (*val.uint64) = strtoull( str, NULL, 10 ); | ||
| 61 | break; | ||
| 62 | |||
| 63 | case vtuint32: | ||
| 64 | (*val.uint32) = strtoul( str, NULL, 10 ); | ||
| 65 | break; | ||
| 66 | |||
| 67 | case vtuint16: | ||
| 68 | (*val.uint16) = (uint16_t)strtoul( str, NULL, 10 ); | ||
| 69 | break; | ||
| 70 | |||
| 71 | case vtuint8: | ||
| 72 | (*val.uint8) = (uint8_t)strtoul( str, NULL, 10 ); | ||
| 73 | break; | ||
| 74 | |||
| 75 | case vtint64: | ||
| 76 | (*val.int64) = strtoll( str, NULL, 10 ); | ||
| 77 | break; | ||
| 78 | |||
| 79 | case vtint32: | ||
| 80 | (*val.int32) = strtol( str, NULL, 10 ); | ||
| 81 | break; | ||
| 82 | |||
| 83 | case vtint16: | ||
| 84 | (*val.int16) = (int16_t)strtol( str, NULL, 10 ); | ||
| 85 | break; | ||
| 86 | |||
| 87 | case vtint8: | ||
| 88 | (*val.int8) = (int8_t)strtol( str, NULL, 10 ); | ||
| 89 | break; | ||
| 90 | |||
| 91 | case vtfloat32: | ||
| 92 | (*val.float32) = strtof( str, NULL ); | ||
| 93 | break; | ||
| 94 | |||
| 95 | case vtfloat64: | ||
| 96 | (*val.float64) = strtod( str, NULL ); | ||
| 97 | break; | ||
| 98 | |||
| 99 | case vtfloat96: | ||
| 100 | (*val.float96) = strtold( str, NULL ); | ||
| 101 | break; | ||
| 102 | |||
| 103 | case vtbln: | ||
| 104 | if( strcasecmp("yes", str ) == 0 || | ||
| 105 | strcasecmp("true", str ) == 0 ) | ||
| 106 | { | ||
| 107 | (*val.bln) = true; | ||
| 108 | } | ||
| 109 | else | ||
| 110 | { | ||
| 111 | (*val.bln) = false; | ||
| 112 | } | ||
| 113 | break; | ||
| 114 | } | ||
| 115 | |||
| 116 | return *this; | ||
| 117 | } | ||
| 118 | |||
| 119 | Bu::ParamProc::ParamProc() | ||
| 120 | { | ||
| 121 | } | ||
| 122 | |||
| 123 | Bu::ParamProc::~ParamProc() | ||
| 124 | { | ||
| 125 | for( Bu::List<ArgSpec *>::iterator i = lArg.begin(); | ||
| 126 | i != lArg.end(); i++ ) | ||
| 127 | { | ||
| 128 | delete *i; | ||
| 129 | } | ||
| 130 | |||
| 131 | for( Bu::List<Banner *>::iterator i = lBan.begin(); | ||
| 132 | i != lBan.end(); i++ ) | ||
| 133 | { | ||
| 134 | delete *i; | ||
| 135 | } | ||
| 136 | |||
| 137 | } | ||
| 138 | /* | ||
| 139 | void Bu::ParamProc::addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val ) | ||
| 140 | { | ||
| 141 | printf("Calling callback...\n"); | ||
| 142 | val = "Hello there, this is set in the ParamProc"; | ||
| 143 | (this->*proc)(); | ||
| 144 | }*/ | ||
| 145 | |||
| 146 | void Bu::ParamProc::addParam( const char *lpWord, char cChar, Proc proc, | ||
| 147 | ParamPtr val, const char *lpDesc, const char *lpExtra, | ||
| 148 | const char *lpValue ) | ||
| 149 | { | ||
| 150 | ArgSpec *as = new ArgSpec; | ||
| 151 | if( lpWord ) | ||
| 152 | as->sWord = lpWord; | ||
| 153 | |||
| 154 | as->cChar = cChar; | ||
| 155 | as->proc = proc; | ||
| 156 | as->val = val; | ||
| 157 | if( lpDesc ) | ||
| 158 | as->sDesc = lpDesc; | ||
| 159 | if( lpExtra ) | ||
| 160 | as->sExtra = lpExtra; | ||
| 161 | if( lpValue ) | ||
| 162 | as->sValue = lpValue; | ||
| 163 | |||
| 164 | lArg.append( as ); | ||
| 165 | |||
| 166 | if( !lBan.isEmpty() ) | ||
| 167 | { | ||
| 168 | if( lBan.last()->pBefore == NULL ) | ||
| 169 | lBan.last()->pBefore = as; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | void Bu::ParamProc::addParam( const char *lpWord, char cChar, Proc proc, | ||
| 174 | const char *lpDesc, const char *lpExtra, | ||
| 175 | const char *lpValue ) | ||
| 176 | { | ||
| 177 | addParam( lpWord, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
| 178 | } | ||
| 179 | |||
| 180 | void Bu::ParamProc::addParam( const char *lpWord, char cChar, ParamPtr val, | ||
| 181 | const char *lpDesc, const char *lpExtra, | ||
| 182 | const char *lpValue ) | ||
| 183 | { | ||
| 184 | addParam( lpWord, cChar, NULL, val, lpDesc, lpExtra, lpValue ); | ||
| 185 | } | ||
| 186 | |||
| 187 | void Bu::ParamProc::addParam( const char *lpWord, Proc proc, ParamPtr val, | ||
| 188 | const char *lpDesc, const char *lpExtra, | ||
| 189 | const char *lpValue ) | ||
| 190 | { | ||
| 191 | addParam( lpWord, '\0', proc, val, lpDesc, lpExtra, lpValue ); | ||
| 192 | } | ||
| 193 | |||
| 194 | void Bu::ParamProc::addParam( const char *lpWord, Proc proc, | ||
| 195 | const char *lpDesc, const char *lpExtra, | ||
| 196 | const char *lpValue ) | ||
| 197 | { | ||
| 198 | addParam( lpWord, '\0', proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
| 199 | } | ||
| 200 | |||
| 201 | void Bu::ParamProc::addParam( const char *lpWord, ParamPtr val, | ||
| 202 | const char *lpDesc, const char *lpExtra, | ||
| 203 | const char *lpValue ) | ||
| 204 | { | ||
| 205 | addParam( lpWord, '\0', NULL, val, lpDesc, lpExtra, lpValue ); | ||
| 206 | } | ||
| 207 | |||
| 208 | void Bu::ParamProc::addParam( char cChar, Proc proc, ParamPtr val, | ||
| 209 | const char *lpDesc, const char *lpExtra, | ||
| 210 | const char *lpValue ) | ||
| 211 | { | ||
| 212 | addParam( NULL, cChar, proc, val, lpDesc, lpExtra, lpValue ); | ||
| 213 | } | ||
| 214 | |||
| 215 | void Bu::ParamProc::addParam( char cChar, Proc proc, | ||
| 216 | const char *lpDesc, const char *lpExtra, | ||
| 217 | const char *lpValue ) | ||
| 218 | { | ||
| 219 | addParam( NULL, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); | ||
| 220 | } | ||
| 221 | |||
| 222 | void Bu::ParamProc::addParam( char cChar, ParamPtr val, | ||
| 223 | const char *lpDesc, const char *lpExtra, | ||
| 224 | const char *lpValue ) | ||
| 225 | { | ||
| 226 | addParam( NULL, cChar, NULL, val, lpDesc, lpExtra, lpValue ); | ||
| 227 | } | ||
| 228 | |||
| 229 | void Bu::ParamProc::process( int argc, char *argv[] ) | ||
| 230 | { | ||
| 231 | for( int arg = 1; arg < argc; arg++ ) | ||
| 232 | { | ||
| 233 | //printf(":::%d:::%s\n", arg, argv[arg] ); | ||
| 234 | if( argv[arg][0] == '-' ) | ||
| 235 | { | ||
| 236 | if( argv[arg][1] == '-' ) | ||
| 237 | { | ||
| 238 | ArgSpec *s = checkWord( argv[arg]+2 ); | ||
| 239 | if( s ) | ||
| 240 | { | ||
| 241 | if( argv[arg][s->sWord.getSize()+2] == '=' ) | ||
| 242 | { | ||
| 243 | if( s->val.isSet() ) | ||
| 244 | { | ||
| 245 | if( s->sValue == "" ) | ||
| 246 | { | ||
| 247 | s->val = argv[arg]+s->sWord.getSize()+3; | ||
| 248 | } | ||
| 249 | else | ||
| 250 | { | ||
| 251 | s->val = s->sValue.getStr(); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | if( s->proc ) | ||
| 255 | { | ||
| 256 | char **tmp = new char*[argc-arg]; | ||
| 257 | tmp[0] = argv[arg]+s->sWord.getSize()+3; | ||
| 258 | for( int k = 1; k < argc-arg; k++ ) | ||
| 259 | tmp[k] = argv[arg+k]; | ||
| 260 | int ret = (this->*s->proc)( argc-arg, tmp ); | ||
| 261 | if( ret > 0 ) | ||
| 262 | { | ||
| 263 | arg += ret-1; | ||
| 264 | } | ||
| 265 | delete tmp; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | else | ||
| 269 | { | ||
| 270 | int add = 0; | ||
| 271 | if( s->val.isSet() ) | ||
| 272 | { | ||
| 273 | if( s->sValue == "" ) | ||
| 274 | { | ||
| 275 | if( arg+1 >= argc ) | ||
| 276 | { | ||
| 277 | return; | ||
| 278 | } | ||
| 279 | s->val = argv[arg+1]; | ||
| 280 | add++; | ||
| 281 | } | ||
| 282 | else | ||
| 283 | { | ||
| 284 | s->val = s->sValue.getStr(); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | if( s->proc ) | ||
| 288 | { | ||
| 289 | int ret = (this->*s->proc)( | ||
| 290 | argc-arg-1, argv+arg+1 ); | ||
| 291 | |||
| 292 | if( ret > add ) | ||
| 293 | add = 0; | ||
| 294 | else | ||
| 295 | add -= ret; | ||
| 296 | arg += ret; | ||
| 297 | } | ||
| 298 | arg += add; | ||
| 299 | } | ||
| 300 | continue; | ||
| 301 | } | ||
| 302 | else | ||
| 303 | { | ||
| 304 | unknownParam( argc-arg, argv+arg ); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | else | ||
| 308 | { | ||
| 309 | for( int chr = 1; argv[arg][chr]; chr++ ) | ||
| 310 | { | ||
| 311 | ArgSpec *s = checkLetr( argv[arg][chr] ); | ||
| 312 | if( s ) | ||
| 313 | { | ||
| 314 | if( argv[arg][chr+1] != '\0' ) | ||
| 315 | { | ||
| 316 | bool bUsed = false; | ||
| 317 | if( s->val.isSet() ) | ||
| 318 | { | ||
| 319 | if( s->sValue == "" ) | ||
| 320 | { | ||
| 321 | s->val = argv[arg]+chr+1; | ||
| 322 | bUsed = true; | ||
| 323 | } | ||
| 324 | else | ||
| 325 | { | ||
| 326 | s->val = s->sValue.getStr(); | ||
| 327 | } | ||
| 328 | } | ||
| 329 | if( s->proc ) | ||
| 330 | { | ||
| 331 | char **tmp = new char*[argc-arg]; | ||
| 332 | tmp[0] = argv[arg]+chr+1; | ||
| 333 | for( int k = 1; k < argc-arg; k++ ) | ||
| 334 | tmp[k] = argv[arg+k]; | ||
| 335 | int ret = (this->*s->proc)( argc-arg, tmp ); | ||
| 336 | if( ret > 0 ) | ||
| 337 | { | ||
| 338 | arg += ret - 1; | ||
| 339 | delete tmp; | ||
| 340 | break; | ||
| 341 | } | ||
| 342 | delete tmp; | ||
| 343 | } | ||
| 344 | if( bUsed ) | ||
| 345 | { | ||
| 346 | break; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | else | ||
| 350 | { | ||
| 351 | bool bUsed = false; | ||
| 352 | if( s->val.isSet() ) | ||
| 353 | { | ||
| 354 | if( s->sValue == "" ) | ||
| 355 | { | ||
| 356 | s->val = argv[arg+1]; | ||
| 357 | bUsed = true; | ||
| 358 | } | ||
| 359 | else | ||
| 360 | { | ||
| 361 | s->val = s->sValue.getStr(); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | if( s->proc ) | ||
| 365 | { | ||
| 366 | int ret = (this->*s->proc)( | ||
| 367 | argc-arg-1, argv+arg+1 | ||
| 368 | ); | ||
| 369 | if( ret > 0 ) | ||
| 370 | { | ||
| 371 | arg += ret; | ||
| 372 | break; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | if( bUsed ) | ||
| 376 | { | ||
| 377 | arg++; | ||
| 378 | break; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | } | ||
| 382 | else | ||
| 383 | { | ||
| 384 | unknownParam( argc-arg, argv+arg ); | ||
| 385 | } | ||
| 386 | } | ||
| 387 | } | ||
| 388 | } | ||
| 389 | else | ||
| 390 | { | ||
| 391 | cmdParam( argc-arg, argv+arg ); | ||
| 392 | } | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | Bu::ParamProc::ArgSpec *Bu::ParamProc::checkWord( const char *arg ) | ||
| 397 | { | ||
| 398 | //printf("Checking \"%s\"...\n", arg ); | ||
| 399 | Bu::List<ArgSpec *>::const_iterator i = lArg.begin(); | ||
| 400 | for( ; i != lArg.end(); i++ ) | ||
| 401 | { | ||
| 402 | if( (*i)->sWord == "" ) | ||
| 403 | continue; | ||
| 404 | |||
| 405 | if( !strcmp( (*i)->sWord.getStr(), arg ) ) | ||
| 406 | return *i; | ||
| 407 | |||
| 408 | if( (*i)->val.isSet() ) | ||
| 409 | { | ||
| 410 | if( !strncmp( (*i)->sWord.getStr(), arg, (*i)->sWord.getSize() ) && | ||
| 411 | arg[(*i)->sWord.getSize()] == '=' ) | ||
| 412 | { | ||
| 413 | return *i; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | return NULL; | ||
| 419 | } | ||
| 420 | |||
| 421 | Bu::ParamProc::ArgSpec *Bu::ParamProc::checkLetr( const char arg ) | ||
| 422 | { | ||
| 423 | //printf("Checking \'%c\'...\n", arg ); | ||
| 424 | Bu::List<ArgSpec *>::const_iterator i = lArg.begin(); | ||
| 425 | for( ; i != lArg.end(); i++ ) | ||
| 426 | { | ||
| 427 | if( (*i)->cChar == '\0' ) | ||
| 428 | continue; | ||
| 429 | |||
| 430 | if( (*i)->cChar == arg ) | ||
| 431 | { | ||
| 432 | return *i; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | return NULL; | ||
| 437 | } | ||
| 438 | |||
| 439 | int Bu::ParamProc::cmdParam( int /*argc*/, char *argv[] ) | ||
| 440 | { | ||
| 441 | printf("Unhandled command parameter \"%s\" found!\n", argv[0] ); | ||
| 442 | return 0; | ||
| 443 | } | ||
| 444 | |||
| 445 | int Bu::ParamProc::unknownParam( int /*argc*/, char *argv[] ) | ||
| 446 | { | ||
| 447 | printf("Unknown parameter \"%s\" found!\n", argv[0] ); | ||
| 448 | return 0; | ||
| 449 | } | ||
| 450 | |||
| 451 | int Bu::ParamProc::help( int /*argc*/, char * /*argv*/ [] ) | ||
| 452 | { | ||
| 453 | Bu::List<Banner *>::const_iterator b = lBan.begin(); | ||
| 454 | Bu::List<ArgSpec *>::const_iterator i = lArg.begin(); | ||
| 455 | int len=0; | ||
| 456 | for( ; i != lArg.end(); i++ ) | ||
| 457 | { | ||
| 458 | if( len < (*i)->sWord.getSize() + (*i)->sExtra.getSize() ) | ||
| 459 | len = (*i)->sWord.getSize() + (*i)->sExtra.getSize(); | ||
| 460 | } | ||
| 461 | char fmt[10]; | ||
| 462 | sprintf( fmt, "%%-%ds ", len ); | ||
| 463 | |||
| 464 | for( i = lArg.begin(); i != lArg.end(); i++ ) | ||
| 465 | { | ||
| 466 | if( b != lBan.end() ) | ||
| 467 | { | ||
| 468 | if( (*b)->pBefore == (*i) ) | ||
| 469 | { | ||
| 470 | printf( (*b)->sBanner.getStr() ); | ||
| 471 | b++; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | printf(" "); | ||
| 475 | if( (*i)->cChar ) | ||
| 476 | { | ||
| 477 | if( (*i)->sWord.getStr() ) | ||
| 478 | { | ||
| 479 | printf("-%c, ", (*i)->cChar ); | ||
| 480 | } | ||
| 481 | else | ||
| 482 | { | ||
| 483 | printf("-%c ", (*i)->cChar ); | ||
| 484 | } | ||
| 485 | } | ||
| 486 | else | ||
| 487 | { | ||
| 488 | printf(" "); | ||
| 489 | } | ||
| 490 | if( (*i)->sWord.getStr() ) | ||
| 491 | { | ||
| 492 | printf("--"); | ||
| 493 | Bu::FString sTmp = (*i)->sWord.getStr(); | ||
| 494 | if( (*i)->sExtra.getStr() ) | ||
| 495 | sTmp += (*i)->sExtra.getStr(); | ||
| 496 | printf( fmt, sTmp.getStr() ); | ||
| 497 | } | ||
| 498 | else | ||
| 499 | { | ||
| 500 | printf(" "); | ||
| 501 | printf(fmt, "" ); | ||
| 502 | } | ||
| 503 | printf("%s\n", (*i)->sDesc.getStr() ); | ||
| 504 | } | ||
| 505 | if( b != lBan.end() ) | ||
| 506 | { | ||
| 507 | if( (*b)->pBefore == NULL ) | ||
| 508 | { | ||
| 509 | printf( (*b)->sBanner.getStr() ); | ||
| 510 | } | ||
| 511 | } | ||
| 512 | |||
| 513 | exit( 0 ); | ||
| 514 | } | ||
| 515 | |||
| 516 | void Bu::ParamProc::addHelpBanner( const char *sHelpBanner ) | ||
| 517 | { | ||
| 518 | Banner *pBan = new Banner; | ||
| 519 | pBan->sBanner = sHelpBanner; | ||
| 520 | pBan->pBefore = NULL; | ||
| 521 | lBan.append( pBan ); | ||
| 522 | } | ||
| 523 | |||
