00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 #include "mimotera++/MimoteraAnyOption.hh"
00088
00089 using namespace std;
00090 using namespace Mimotera;
00091
00092 AnyOption::AnyOption() {
00093 init();
00094 }
00095
00096 AnyOption::AnyOption(int maxopt) {
00097 init( maxopt , maxopt );
00098 }
00099
00100 AnyOption::AnyOption(int maxopt, int maxcharopt) {
00101 init( maxopt , maxcharopt );
00102 }
00103
00104 AnyOption::~AnyOption() {
00105 if( mem_allocated )
00106 cleanup();
00107 }
00108
00109 void AnyOption::init() {
00110 init( DEFAULT_MAXOPTS , DEFAULT_MAXOPTS );
00111 }
00112
00113 void AnyOption::init(int maxopt, int maxcharopt ) {
00114
00115 max_options = maxopt;
00116 max_char_options = maxcharopt;
00117 max_usage_lines = DEFAULT_MAXUSAGE;
00118 usage_lines = 0 ;
00119 argc = 0;
00120 argv = NULL;
00121 posix_style = true;
00122 verbose = false;
00123 filename = NULL;
00124 appname = NULL;
00125 option_counter = 0;
00126 optchar_counter = 0;
00127 new_argv = NULL;
00128 new_argc = 0 ;
00129 max_legal_args = 0 ;
00130 command_set = false;
00131 file_set = false;
00132 values = NULL;
00133 g_value_counter = 0;
00134 mem_allocated = false;
00135 command_set = false;
00136 file_set = false;
00137 opt_prefix_char = '-';
00138 file_delimiter_char = ':';
00139 file_comment_char = '#';
00140 equalsign = '=';
00141 comment = '#' ;
00142 delimiter = ':' ;
00143 endofline = '\n';
00144 whitespace = ' ' ;
00145 nullterminate = '\0';
00146 set = false;
00147 once = true;
00148 hasoptions = false;
00149 autousage = false;
00150
00151 strcpy( long_opt_prefix , "--" );
00152
00153 if( alloc() == false ){
00154 cout << endl << "OPTIONS ERROR : Failed allocating memory" ;
00155 cout << endl ;
00156 cout << "Exiting." << endl ;
00157 exit (0);
00158 }
00159 }
00160
00161 bool AnyOption::alloc() {
00162 int i = 0 ;
00163 int size = 0 ;
00164
00165 if( mem_allocated )
00166 return true;
00167
00168 size = (max_options+1) * sizeof(const char*);
00169 options = (const char**)malloc( size );
00170 optiontype = (int*) malloc( (max_options+1)*sizeof(int) );
00171 optionindex = (int*) malloc( (max_options+1)*sizeof(int) );
00172 if( options == NULL || optiontype == NULL || optionindex == NULL )
00173 return false;
00174 else
00175 mem_allocated = true;
00176 for( i = 0 ; i < max_options ; i++ ){
00177 options[i] = NULL;
00178 optiontype[i] = 0 ;
00179 optionindex[i] = -1 ;
00180 }
00181 optionchars = (char*) malloc( (max_char_options+1)*sizeof(char) );
00182 optchartype = (int*) malloc( (max_char_options+1)*sizeof(int) );
00183 optcharindex = (int*) malloc( (max_char_options+1)*sizeof(int) );
00184 if( optionchars == NULL ||
00185 optchartype == NULL ||
00186 optcharindex == NULL )
00187 {
00188 mem_allocated = false;
00189 return false;
00190 }
00191 for( i = 0 ; i < max_char_options ; i++ ){
00192 optionchars[i] = '0';
00193 optchartype[i] = 0 ;
00194 optcharindex[i] = -1 ;
00195 }
00196
00197 size = (max_usage_lines+1) * sizeof(const char*) ;
00198 usage = (const char**) malloc( size );
00199
00200 if( usage == NULL ){
00201 mem_allocated = false;
00202 return false;
00203 }
00204 for( i = 0 ; i < max_usage_lines ; i++ )
00205 usage[i] = NULL;
00206
00207 return true;
00208 }
00209
00210 bool AnyOption::doubleOptStorage() {
00211 options = (const char**)realloc( options,
00212 ((2*max_options)+1) * sizeof( const char*) );
00213 optiontype = (int*) realloc( optiontype ,
00214 ((2 * max_options)+1)* sizeof(int) );
00215 optionindex = (int*) realloc( optionindex,
00216 ((2 * max_options)+1) * sizeof(int) );
00217 if( options == NULL || optiontype == NULL || optionindex == NULL )
00218 return false;
00219
00220 for( int i = max_options ; i < 2*max_options ; i++ ){
00221 options[i] = NULL;
00222 optiontype[i] = 0 ;
00223 optionindex[i] = -1 ;
00224 }
00225 max_options = 2 * max_options ;
00226 return true;
00227 }
00228
00229 bool AnyOption::doubleCharStorage() {
00230 optionchars = (char*) realloc( optionchars,
00231 ((2*max_char_options)+1)*sizeof(char) );
00232 optchartype = (int*) realloc( optchartype,
00233 ((2*max_char_options)+1)*sizeof(int) );
00234 optcharindex = (int*) realloc( optcharindex,
00235 ((2*max_char_options)+1)*sizeof(int) );
00236 if( optionchars == NULL ||
00237 optchartype == NULL ||
00238 optcharindex == NULL )
00239 return false;
00240
00241 for( int i = max_char_options ; i < 2*max_char_options ; i++ ){
00242 optionchars[i] = '0';
00243 optchartype[i] = 0 ;
00244 optcharindex[i] = -1 ;
00245 }
00246 max_char_options = 2 * max_char_options;
00247 return true;
00248 }
00249
00250 bool AnyOption::doubleUsageStorage() {
00251 usage = (const char**)realloc( usage,
00252 ((2*max_usage_lines)+1) * sizeof( const char*) );
00253 if ( usage == NULL )
00254 return false;
00255 for( int i = max_usage_lines ; i < 2*max_usage_lines ; i++ )
00256 usage[i] = NULL;
00257 max_usage_lines = 2 * max_usage_lines ;
00258 return true;
00259
00260 }
00261
00262
00263 void AnyOption::cleanup() {
00264 free (options);
00265 free (optiontype);
00266 free (optionindex);
00267 free (optionchars);
00268 free (optchartype);
00269 free (optcharindex);
00270 free (usage);
00271 if( values != NULL )
00272 free (values);
00273 if( new_argv != NULL )
00274 free (new_argv);
00275 }
00276
00277 void AnyOption::setCommandPrefixChar( char _prefix ) {
00278 opt_prefix_char = _prefix;
00279 }
00280
00281 void AnyOption::setCommandLongPrefix( char *_prefix ) {
00282 if( strlen( _prefix ) > MAX_LONG_PREFIX_LENGTH ){
00283 *( _prefix + MAX_LONG_PREFIX_LENGTH ) = '\0';
00284 }
00285
00286 strcpy (long_opt_prefix, _prefix);
00287 }
00288
00289 void AnyOption::setFileCommentChar( char _comment ) {
00290 file_delimiter_char = _comment;
00291 }
00292
00293
00294 void AnyOption::setFileDelimiterChar( char _delimiter ) {
00295 file_comment_char = _delimiter ;
00296 }
00297
00298 bool AnyOption::CommandSet() {
00299 return( command_set );
00300 }
00301
00302 bool AnyOption::FileSet() {
00303 return( file_set );
00304 }
00305
00306 void AnyOption::noPOSIX() {
00307 posix_style = false;
00308 }
00309
00310 bool AnyOption::POSIX() {
00311 return posix_style;
00312 }
00313
00314
00315 void AnyOption::setVerbose() {
00316 verbose = true ;
00317 }
00318
00319 void AnyOption::printVerbose() {
00320 if( verbose )
00321 cout << endl ;
00322 }
00323
00324 void AnyOption::printVerbose( const char *msg ) {
00325 if( verbose )
00326 cout << msg ;
00327 }
00328
00329 void AnyOption::printVerbose( char *msg ) {
00330 if( verbose )
00331 cout << msg ;
00332 }
00333
00334 void AnyOption::printVerbose( char ch ) {
00335 if( verbose )
00336 cout << ch ;
00337 }
00338
00339 bool AnyOption::hasOptions() {
00340 return hasoptions;
00341 }
00342
00343 void AnyOption::autoUsagePrint(bool _autousage) {
00344 autousage = _autousage;
00345 }
00346
00347 void AnyOption::useCommandArgs( int _argc, char **_argv ) {
00348 argc = _argc;
00349 argv = _argv;
00350 command_set = true;
00351 appname = argv[0];
00352 if(argc > 1) hasoptions = true;
00353 }
00354
00355 void AnyOption::useFiileName( const char *_filename ) {
00356 filename = _filename;
00357 file_set = true;
00358 }
00359
00360
00361
00362
00363
00364 void AnyOption::setCommandOption( const char *opt ) {
00365 addOption( opt , COMMAND_OPT );
00366 g_value_counter++;
00367 }
00368
00369 void AnyOption::setCommandOption( char opt ) {
00370 addOption( opt , COMMAND_OPT );
00371 g_value_counter++;
00372 }
00373
00374 void AnyOption::setCommandOption( const char *opt , char optchar ) {
00375 addOption( opt , COMMAND_OPT );
00376 addOption( optchar , COMMAND_OPT );
00377 g_value_counter++;
00378 }
00379
00380 void AnyOption::setCommandFlag( const char *opt ) {
00381 addOption( opt , COMMAND_FLAG );
00382 g_value_counter++;
00383 }
00384
00385 void AnyOption::setCommandFlag( char opt ) {
00386 addOption( opt , COMMAND_FLAG );
00387 g_value_counter++;
00388 }
00389
00390 void AnyOption::setCommandFlag( const char *opt , char optchar ) {
00391 addOption( opt , COMMAND_FLAG );
00392 addOption( optchar , COMMAND_FLAG );
00393 g_value_counter++;
00394 }
00395
00396 void AnyOption::setFileOption( const char *opt ) {
00397 addOption( opt , FILE_OPT );
00398 g_value_counter++;
00399 }
00400
00401 void AnyOption::setFileOption( char opt ) {
00402 addOption( opt , FILE_OPT );
00403 g_value_counter++;
00404 }
00405
00406 void AnyOption::setFileOption( const char *opt , char optchar ) {
00407 addOption( opt , FILE_OPT );
00408 addOption( optchar, FILE_OPT );
00409 g_value_counter++;
00410 }
00411
00412 void AnyOption::setFileFlag( const char *opt ) {
00413 addOption( opt , FILE_FLAG );
00414 g_value_counter++;
00415 }
00416
00417 void AnyOption::setFileFlag( char opt ) {
00418 addOption( opt , FILE_FLAG );
00419 g_value_counter++;
00420 }
00421
00422 void AnyOption::setFileFlag( const char *opt , char optchar ) {
00423 addOption( opt , FILE_FLAG );
00424 addOption( optchar , FILE_FLAG );
00425 g_value_counter++;
00426 }
00427
00428 void AnyOption::setOption( const char *opt ) {
00429 addOption( opt , COMMON_OPT );
00430 g_value_counter++;
00431 }
00432
00433 void
00434 AnyOption::setOption( char opt )
00435 {
00436 addOption( opt , COMMON_OPT );
00437 g_value_counter++;
00438 }
00439
00440 void
00441 AnyOption::setOption( const char *opt , char optchar )
00442 {
00443 addOption( opt , COMMON_OPT );
00444 addOption( optchar , COMMON_OPT );
00445 g_value_counter++;
00446 }
00447
00448 void
00449 AnyOption::setFlag( const char *opt )
00450 {
00451 addOption( opt , COMMON_FLAG );
00452 g_value_counter++;
00453 }
00454
00455 void
00456 AnyOption::setFlag( const char opt )
00457 {
00458 addOption( opt , COMMON_FLAG );
00459 g_value_counter++;
00460 }
00461
00462 void
00463 AnyOption::setFlag( const char *opt , char optchar )
00464 {
00465 addOption( opt , COMMON_FLAG );
00466 addOption( optchar , COMMON_FLAG );
00467 g_value_counter++;
00468 }
00469
00470 void
00471 AnyOption::addOption( const char *opt, int type )
00472 {
00473 if( option_counter >= max_options ){
00474 if( doubleOptStorage() == false ){
00475 addOptionError( opt );
00476 return;
00477 }
00478 }
00479 options[ option_counter ] = opt ;
00480 optiontype[ option_counter ] = type ;
00481 optionindex[ option_counter ] = g_value_counter;
00482 option_counter++;
00483 }
00484
00485 void
00486 AnyOption::addOption( char opt, int type )
00487 {
00488 if( !POSIX() ){
00489 printVerbose("Ignoring the option character \"");
00490 printVerbose( opt );
00491 printVerbose( "\" ( POSIX options are turned off )" );
00492 printVerbose();
00493 return;
00494 }
00495
00496
00497 if( optchar_counter >= max_char_options ){
00498 if( doubleCharStorage() == false ){
00499 addOptionError( opt );
00500 return;
00501 }
00502 }
00503 optionchars[ optchar_counter ] = opt ;
00504 optchartype[ optchar_counter ] = type ;
00505 optcharindex[ optchar_counter ] = g_value_counter;
00506 optchar_counter++;
00507 }
00508
00509 void
00510 AnyOption::addOptionError( const char *opt )
00511 {
00512 cout << endl ;
00513 cout << "OPTIONS ERROR : Failed allocating extra memory " << endl ;
00514 cout << "While adding the option : \""<< opt << "\"" << endl;
00515 cout << "Exiting." << endl ;
00516 cout << endl ;
00517 exit(0);
00518 }
00519
00520 void
00521 AnyOption::addOptionError( char opt )
00522 {
00523 cout << endl ;
00524 cout << "OPTIONS ERROR : Failed allocating extra memory " << endl ;
00525 cout << "While adding the option: \""<< opt << "\"" << endl;
00526 cout << "Exiting." << endl ;
00527 cout << endl ;
00528 exit(0);
00529 }
00530
00531 void
00532 AnyOption::processOptions()
00533 {
00534 if( ! valueStoreOK() )
00535 return;
00536 }
00537
00538 void
00539 AnyOption::processCommandArgs(int max_args)
00540 {
00541 max_legal_args = max_args;
00542 processCommandArgs();
00543 }
00544
00545 void
00546 AnyOption::processCommandArgs( int _argc, char **_argv, int max_args )
00547 {
00548 max_legal_args = max_args;
00549 processCommandArgs( _argc, _argv );
00550 }
00551
00552 void
00553 AnyOption::processCommandArgs( int _argc, char **_argv )
00554 {
00555 useCommandArgs( _argc, _argv );
00556 processCommandArgs();
00557 }
00558
00559 void
00560 AnyOption::processCommandArgs()
00561 {
00562 if( ! ( valueStoreOK() && CommandSet() ) )
00563 return;
00564
00565 if( max_legal_args == 0 )
00566 max_legal_args = argc;
00567 new_argv = (int*) malloc( (max_legal_args+1) * sizeof(int) );
00568 for( int i = 1 ; i < argc ; i++ ){
00569 if( argv[i][0] == long_opt_prefix[0] &&
00570 argv[i][1] == long_opt_prefix[1] ) {
00571 int match_at = parseGNU( argv[i]+2 );
00572 if( match_at >= 0 && i < argc-1 )
00573 setValue( options[match_at] , argv[++i] );
00574 }else if( argv[i][0] == opt_prefix_char ) {
00575 if( POSIX() ){
00576 char ch = parsePOSIX( argv[i]+1 );
00577 if( ch != '0' && i < argc-1 )
00578 setValue( ch , argv[++i] );
00579 } else {
00580 int match_at = parseGNU( argv[i]+1 );
00581 if( match_at >= 0 && i < argc-1 )
00582 setValue( options[match_at] , argv[++i] );
00583 }
00584 }else {
00585 if( new_argc < max_legal_args ){
00586 new_argv[ new_argc ] = i ;
00587 new_argc++;
00588 }else{
00589 printVerbose( "Ignoring extra argument: " );
00590 printVerbose( argv[i] );
00591 printVerbose( );
00592 printAutoUsage();
00593 }
00594 printVerbose( "Unknown command argument option : " );
00595 printVerbose( argv[i] );
00596 printVerbose( );
00597 printAutoUsage();
00598 }
00599 }
00600 }
00601
00602 char
00603 AnyOption::parsePOSIX( char* arg )
00604 {
00605
00606 for( unsigned int i = 0 ; i < strlen(arg) ; i++ ){
00607 char ch = arg[i] ;
00608 if( matchChar(ch) ) {
00609
00610 if( i == strlen(arg)-1 ){
00611 return ch;
00612 }else{
00613 i++;
00614 while( arg[i] == whitespace
00615 || arg[i] == equalsign )
00616 i++;
00617 setValue( ch , arg+i );
00618 return '0';
00619 }
00620 }
00621 }
00622 printVerbose( "Unknown command argument option : " );
00623 printVerbose( arg );
00624 printVerbose( );
00625 printAutoUsage();
00626 return '0';
00627 }
00628
00629 int
00630 AnyOption::parseGNU( char *arg )
00631 {
00632 int split_at = 0;
00633
00634 for( unsigned int i = 0 ; i < strlen(arg) ; i++ ){
00635 if(arg[i] == equalsign ){
00636 split_at = i ;
00637 i = strlen(arg);
00638 }
00639 }
00640 if( split_at > 0 ){
00641 char* tmp = (char*) malloc( (split_at+1)*sizeof(char) );
00642 for( int i = 0 ; i < split_at ; i++ )
00643 tmp[i] = arg[i];
00644 tmp[split_at] = '\0';
00645
00646 if ( matchOpt( tmp ) >= 0 ){
00647 setValue( options[matchOpt(tmp)] , arg+split_at+1 );
00648 free (tmp);
00649 }else{
00650 printVerbose( "Unknown command argument option : " );
00651 printVerbose( arg );
00652 printVerbose( );
00653 printAutoUsage();
00654 free (tmp);
00655 return -1;
00656 }
00657 }else{
00658 return matchOpt(arg);
00659 }
00660 return -1;
00661 }
00662
00663
00664 int
00665 AnyOption::matchOpt( char *opt )
00666 {
00667 for( int i = 0 ; i < option_counter ; i++ ){
00668 if( strcmp( options[i], opt ) == 0 ){
00669 if( optiontype[i] == COMMON_OPT ||
00670 optiontype[i] == COMMAND_OPT )
00671 {
00672 return i;
00673 }else if( optiontype[i] == COMMON_FLAG ||
00674 optiontype[i] == COMMAND_FLAG )
00675 {
00676 setFlagOn( opt );
00677 return -1;
00678 }
00679 }
00680 }
00681 printVerbose( "Unknown command argument option : " );
00682 printVerbose( opt ) ;
00683 printVerbose( );
00684 printAutoUsage();
00685 return -1;
00686 }
00687 bool
00688 AnyOption::matchChar( char c )
00689 {
00690 for( int i = 0 ; i < optchar_counter ; i++ ){
00691 if( optionchars[i] == c ) {
00692 if(optchartype[i] == COMMON_OPT ||
00693 optchartype[i] == COMMAND_OPT )
00694 {
00695 return true;
00696 }else if( optchartype[i] == COMMON_FLAG ||
00697 optchartype[i] == COMMAND_FLAG ) {
00698 setFlagOn( c );
00699 return false;
00700 }
00701 }
00702 }
00703 printVerbose( "Unknown command argument option : " );
00704 printVerbose( c ) ;
00705 printVerbose( );
00706 printAutoUsage();
00707 return false;
00708 }
00709
00710 bool
00711 AnyOption::valueStoreOK( )
00712 {
00713 int size= 0;
00714 if( !set ){
00715 if( g_value_counter > 0 ){
00716 size = g_value_counter * sizeof(char*);
00717 values = (char**)malloc( size );
00718 for( int i = 0 ; i < g_value_counter ; i++)
00719 values[i] = NULL;
00720 set = true;
00721 }
00722 }
00723 return set;
00724 }
00725
00726
00727
00728
00729 char*
00730 AnyOption::getValue( const char *option )
00731 {
00732 if( !valueStoreOK() )
00733 return NULL;
00734
00735 for( int i = 0 ; i < option_counter ; i++ ){
00736 if( strcmp( options[i], option ) == 0 )
00737 return values[ optionindex[i] ];
00738 }
00739 return NULL;
00740 }
00741
00742 bool
00743 AnyOption::getFlag( const char *option )
00744 {
00745 if( !valueStoreOK() )
00746 return false;
00747 for( int i = 0 ; i < option_counter ; i++ ){
00748 if( strcmp( options[i], option ) == 0 )
00749 return findFlag( values[ optionindex[i] ] );
00750 }
00751 return false;
00752 }
00753
00754 char*
00755 AnyOption::getValue( char option )
00756 {
00757 if( !valueStoreOK() )
00758 return NULL;
00759 for( int i = 0 ; i < optchar_counter ; i++ ){
00760 if( optionchars[i] == option )
00761 return values[ optcharindex[i] ];
00762 }
00763 return NULL;
00764 }
00765
00766 bool
00767 AnyOption::getFlag( char option )
00768 {
00769 if( !valueStoreOK() )
00770 return false;
00771 for( int i = 0 ; i < optchar_counter ; i++ ){
00772 if( optionchars[i] == option )
00773 return findFlag( values[ optcharindex[i] ] ) ;
00774 }
00775 return false;
00776 }
00777
00778 bool
00779 AnyOption::findFlag( char* val )
00780 {
00781 if( val == NULL )
00782 return false;
00783
00784 if( strcmp( TRUE_FLAG , val ) == 0 )
00785 return true;
00786
00787 return false;
00788 }
00789
00790
00791
00792
00793 bool
00794 AnyOption::setValue( const char *option , char *value )
00795 {
00796 if( !valueStoreOK() )
00797 return false;
00798 for( int i = 0 ; i < option_counter ; i++ ){
00799 if( strcmp( options[i], option ) == 0 ){
00800 values[ optionindex[i] ] = (char*) malloc((strlen(value)+1)*sizeof(char));
00801 strcpy( values[ optionindex[i] ], value );
00802 return true;
00803 }
00804 }
00805 return false;
00806 }
00807
00808 bool
00809 AnyOption::setFlagOn( const char *option )
00810 {
00811 if( !valueStoreOK() )
00812 return false;
00813 for( int i = 0 ; i < option_counter ; i++ ){
00814 if( strcmp( options[i], option ) == 0 ){
00815 values[ optionindex[i] ] = (char*) malloc((strlen(TRUE_FLAG)+1)*sizeof(char));
00816 strcpy( values[ optionindex[i] ] , TRUE_FLAG );
00817 return true;
00818 }
00819 }
00820 return false;
00821 }
00822
00823 bool
00824 AnyOption::setValue( char option , char *value )
00825 {
00826 if( !valueStoreOK() )
00827 return false;
00828 for( int i = 0 ; i < optchar_counter ; i++ ){
00829 if( optionchars[i] == option ){
00830 values[ optcharindex[i] ] = (char*) malloc((strlen(value)+1)*sizeof(char));
00831 strcpy( values[ optcharindex[i] ], value );
00832 return true;
00833 }
00834 }
00835 return false;
00836 }
00837
00838 bool
00839 AnyOption::setFlagOn( char option )
00840 {
00841 if( !valueStoreOK() )
00842 return false;
00843 for( int i = 0 ; i < optchar_counter ; i++ ){
00844 if( optionchars[i] == option ){
00845 values[ optcharindex[i] ] = (char*) malloc((strlen(TRUE_FLAG)+1)*sizeof(char));
00846 strcpy( values[ optcharindex[i] ] , TRUE_FLAG );
00847 return true;
00848 }
00849 }
00850 return false;
00851 }
00852
00853
00854 int
00855 AnyOption::getArgc( )
00856 {
00857 return new_argc;
00858 }
00859
00860 char*
00861 AnyOption::getArgv( int index )
00862 {
00863 if( index < new_argc ){
00864 return ( argv[ new_argv[ index ] ] );
00865 }
00866 return NULL;
00867 }
00868
00869
00870
00871 bool
00872 AnyOption::processFile()
00873 {
00874 if( ! (valueStoreOK() && FileSet()) )
00875 return false;
00876 return ( consumeFile(readFile()) );
00877 }
00878
00879 bool
00880 AnyOption::processFile( const char *filename )
00881 {
00882 useFiileName(filename );
00883 return ( processFile() );
00884 }
00885
00886 char*
00887 AnyOption::readFile()
00888 {
00889 return ( readFile(filename) );
00890 }
00891
00892
00893
00894
00895
00896 char*
00897 AnyOption::readFile( const char* fname )
00898 {
00899 int length;
00900 char *buffer;
00901 ifstream is;
00902 is.open ( fname , ifstream::in );
00903 if( ! is.good() ){
00904 is.close();
00905 return NULL;
00906 }
00907 is.seekg (0, ios::end);
00908 length = is.tellg();
00909 is.seekg (0, ios::beg);
00910 buffer = (char*) malloc(length*sizeof(char));
00911 is.read (buffer,length);
00912 is.close();
00913 return buffer;
00914 }
00915
00916
00917
00918
00919
00920 bool
00921 AnyOption::consumeFile( char *buffer )
00922 {
00923
00924 if( buffer == NULL )
00925 return false;
00926
00927 char *cursor = buffer;
00928 char *pline = NULL ;
00929 int linelength = 0;
00930 bool newline = true;
00931 for( unsigned int i = 0 ; i < strlen( buffer ) ; i++ ){
00932 if( *cursor == endofline ) {
00933 if( pline != NULL )
00934 processLine( pline, linelength );
00935 pline = NULL;
00936 newline = true;
00937 }else if( newline ){
00938 newline = false;
00939 if( (*cursor != comment ) ){
00940 pline = cursor ;
00941 linelength = 0 ;
00942 }
00943 }
00944 cursor++;
00945 linelength++;
00946 }
00947 free (buffer);
00948 return true;
00949 }
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972 void
00973 AnyOption::processLine( char *theline, int length )
00974 {
00975 bool found = false;
00976 char *pline = (char*) malloc( (length+1)*sizeof(char) );
00977 for( int i = 0 ; i < length ; i ++ )
00978 pline[i]= *(theline++);
00979 pline[length] = nullterminate;
00980 char *cursor = pline ;
00981 if( *cursor == delimiter || *(cursor+length-1) == delimiter ){
00982 justValue( pline );
00983 }else{
00984 for( int i = 1 ; i < length-1 && !found ; i++){
00985 if( *cursor == delimiter ){
00986 *(cursor-1) = nullterminate;
00987 found = true;
00988 valuePairs( pline , cursor+1 );
00989 }
00990 cursor++;
00991 }
00992 cursor++;
00993 if( !found )
00994 justValue( pline );
00995 }
00996 free (pline);
00997 }
00998
00999
01000
01001
01002 char*
01003 AnyOption::chomp( char *str )
01004 {
01005 while( *str == whitespace )
01006 str++;
01007 char *end = str+strlen(str)-1;
01008 while( *end == whitespace )
01009 end--;
01010 *(end+1) = nullterminate;
01011 return str;
01012 }
01013
01014 void
01015 AnyOption::valuePairs( char *type, char *value )
01016 {
01017 if ( strlen(chomp(type)) == 1 ){
01018 for( int i = 0 ; i < optchar_counter ; i++ ){
01019 if( optionchars[i] == type[0] ){
01020 if( optchartype[i] == COMMON_OPT ||
01021 optchartype[i] == FILE_OPT )
01022 {
01023 setValue( type[0] , chomp(value) );
01024 return;
01025 }
01026 }
01027 }
01028 }
01029
01030 for( int i = 0 ; i < option_counter ; i++ ){
01031 if( strcmp( options[i], type ) == 0 ){
01032 if( optiontype[i] == COMMON_OPT ||
01033 optiontype[i] == FILE_OPT )
01034 {
01035 setValue( type , chomp(value) );
01036 return;
01037 }
01038 }
01039 }
01040 printVerbose( "Unknown option in resourcefile : " );
01041 printVerbose( type );
01042 printVerbose( );
01043 }
01044
01045 void
01046 AnyOption::justValue( char *type )
01047 {
01048
01049 if ( strlen(chomp(type)) == 1 ){
01050 for( int i = 0 ; i < optchar_counter ; i++ ){
01051 if( optionchars[i] == type[0] ){
01052 if( optchartype[i] == COMMON_FLAG ||
01053 optchartype[i] == FILE_FLAG )
01054 {
01055 setFlagOn( type[0] );
01056 return;
01057 }
01058 }
01059 }
01060 }
01061
01062 for( int i = 0 ; i < option_counter ; i++ ){
01063 if( strcmp( options[i], type ) == 0 ){
01064 if( optiontype[i] == COMMON_FLAG ||
01065 optiontype[i] == FILE_FLAG )
01066 {
01067 setFlagOn( type );
01068 return;
01069 }
01070 }
01071 }
01072 printVerbose( "Unknown option in resourcefile : " );
01073 printVerbose( type );
01074 printVerbose( );
01075 }
01076
01077
01078
01079
01080
01081
01082 void
01083 AnyOption::printAutoUsage()
01084 {
01085 if( autousage ) printUsage();
01086 }
01087
01088 void
01089 AnyOption::printUsage()
01090 {
01091
01092 if( once ) {
01093 once = false ;
01094 cout << endl ;
01095 for( int i = 0 ; i < usage_lines ; i++ )
01096 cout << usage[i] << endl ;
01097 cout << endl ;
01098 }
01099 }
01100
01101
01102 void
01103 AnyOption::addUsage( const char *line )
01104 {
01105 if( usage_lines >= max_usage_lines ){
01106 if( doubleUsageStorage() == false ){
01107 addUsageError( line );
01108 exit(1);
01109 }
01110 }
01111 usage[ usage_lines ] = line ;
01112 usage_lines++;
01113 }
01114
01115 void
01116 AnyOption::addUsageError( const char *line )
01117 {
01118 cout << endl ;
01119 cout << "OPTIONS ERROR : Failed allocating extra memory " << endl ;
01120 cout << "While adding the usage/help : \""<< line << "\"" << endl;
01121 cout << "Exiting." << endl ;
01122 cout << endl ;
01123 exit(0);
01124
01125 }