/*----------------------------------------------------- Implementation of Opts class for command line options Generated by aargh 1.1.3 Sun Feb 12 10:08:59 2006 -----------------------------------------------------*/ #include #include #include #include #include #include "opts.h" namespace std {} using namespace std; void Opts::initialize() { new_argc_ = 0; new_argv_ = NULL; infile_found_ = false; infile_.erase(); counter_found_ = false; counter_ = 0; verbose_ = false; } int Opts::get( int argc, char * argv[] ) { int rc = 0; /* return code */ unsigned long counter_value = 0; char * tail = NULL; // Initialize data members as needed if( populated ) initialize(); else populated = true; // Suppress error messages from getopt() opterr = 0; // Define valid option characters const char optstring[] = ":f:n:v"; // Examine command line options int opt; while( ( opt = getopt( argc, argv, optstring ) ) != -1 ) { switch( opt ) { case 'f' : // Get infile if( infile_found_ ) { cerr << "Only one occurrence of -f option allowed\n"; rc = 1; break; } infile_found_ = true; infile_ = optarg; break; case 'n' : // Get counter if( counter_found_ ) { cerr << "Only one occurrence of -n option allowed\n"; rc = 1; break; } counter_found_ = true; // Skip white space; check for negative while( isspace( (unsigned char) *optarg ) ) ++optarg; if( '-' == *optarg ) { cerr << "Negative argument not allowed for " << "-n option: \"" << optarg << "\"\n"; rc = 1; break; } // Convert to numeric value errno = 0; counter_value = strtoul( optarg, &tail, 10 ); if( *tail != '\0' ) { cerr << "Invalid or non-numeric argument to " << "-n option: \"" << optarg << "\"\n"; rc = 1; break; } else if( errno != 0 ) { cerr << "Too large argument to " << "-n option: \'" << optarg << "\"\n"; rc = 1; break; } counter_ = counter_value; break; case 'v' : // Get verbose verbose_ = true; break; case ':' : // Missing argument cerr << "Required argument missing on '-" << static_cast< char >( optopt ) << "' option\n"; rc = 1; break; case '?' : // Invalid option cerr << "Invalid option '-" << static_cast< char >( optopt ) << "' on command line\n"; rc = 1; break; default : // Programmer error cerr << "Internal error: unexpected value '-" << static_cast< char >( optopt ) << "' of optopt\n"; rc = 1; break; } // end switch } // end while if( optind > argc ) { // This should never happen! cerr << "Program error: found more arguments than expected\n"; rc = 1; } else { // Calculate new_argcv_ and new_argc_ to reflect // the number of arguments consumed new_argc_ = argc - optind + 1; new_argv_ = argv + optind - 1; } return rc; }