/*----------------------------------------------------- Implementation of Sample for command line options This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Copyright Fred Smith 2006 All rights reserved Generated by aargh 1.2 Sat Mar 18 20:30:38 2006 -----------------------------------------------------*/ #include #include #include #include #include #include #include "sample_opts.h" namespace std {} using namespace std; void Sample::initialize() { new_argc_ = 0; new_argv_ = NULL; sample_bool_ = false; sample_dates_found_ = false; sample_dates_.clear(); host_found_ = false; host_.erase(); sample_int_found_ = false; sample_int_ = 0; multiple_ints_found_ = false; multiple_ints_.clear(); universe_found_ = false; universe_ = 0; } int Sample::get_sample_opts( int argc, char * argv[] ) { int rc = 0; /* return code */ unsigned long sample_int_value = 0; unsigned long multiple_ints_value = 0; unsigned long universe_value = 0; char * tail = NULL; // Initialize data members as needed if( populated ) initialize(); else populated = true; // Apply any default values host_ = "localhost"; universe_ = 42; // Suppress error messages from getopt() opterr = 0; // Define valid option characters const char optstring[] = ":cd:h:i:n:u:"; // Examine command line options int opt; while( ( opt = getopt( argc, argv, optstring ) ) != -1 ) { switch( opt ) { case 'c' : // Get sample_bool sample_bool_ = true; break; case 'd' : // Get sample_dates sample_dates_found_ = true; // Check for too long or too short if( strlen( optarg ) < 3UL ) { cerr << "Argument to " << "-d option \"" << optarg << "\" is less than the minimum length\n"; rc = 1; break; } if( strlen( optarg ) > 9UL ) { cerr << "Argument to " << "-d option \"" << optarg << "\" is more than the maximum length\n"; rc = 1; break; } // Call validator function if( val_date( optarg ) ) { cerr << "Argument to " << "-d option \"" << optarg << "\" is invalid\n"; rc = 1; } sample_dates_.push_back( string( optarg ) ); break; case 'h' : // Get host if( host_found_ ) { cerr << "Only one occurrence of -h option allowed\n"; rc = 1; break; } host_found_ = true; host_ = optarg; break; case 'i' : // Get sample_int if( sample_int_found_ ) { cerr << "Only one occurrence of -i option allowed\n"; rc = 1; break; } sample_int_found_ = true; // Skip white space; check for negative while( isspace( (unsigned char) *optarg ) ) ++optarg; if( '-' == *optarg ) { cerr << "Negative argument not allowed for " << "-i option: \"" << optarg << "\"\n"; rc = 1; break; } // Convert to numeric value errno = 0; sample_int_value = strtoul( optarg, &tail, 10 ); if( *tail != '\0' ) { cerr << "Invalid or non-numeric argument to " << "-i option: \"" << optarg << "\"\n"; rc = 1; break; } else if( errno != 0 ) { cerr << "Too large argument to " << "-i option: \'" << optarg << "\"\n"; rc = 1; break; } // Check for values out of bounds if( sample_int_value < 1UL ) { cerr << "Argument to " << "-i option \"" << optarg << "\" is less than minimum\n"; rc = 1; break; } if( sample_int_value > 6UL ) { cerr << "Argument to " << "-i option \"" << optarg << "\" exceeds maximum allowed value\n"; rc = 1; break; } sample_int_ = sample_int_value; break; case 'n' : // Get multiple_ints multiple_ints_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; multiple_ints_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; } multiple_ints_.push_back( multiple_ints_value ); break; case 'u' : // Get universe if( universe_found_ ) { cerr << "Only one occurrence of -u option allowed\n"; rc = 1; break; } universe_found_ = true; // Skip white space; check for negative while( isspace( (unsigned char) *optarg ) ) ++optarg; if( '-' == *optarg ) { cerr << "Negative argument not allowed for " << "-u option: \"" << optarg << "\"\n"; rc = 1; break; } // Convert to numeric value errno = 0; universe_value = strtoul( optarg, &tail, 10 ); if( *tail != '\0' ) { cerr << "Invalid or non-numeric argument to " << "-u option: \"" << optarg << "\"\n"; rc = 1; break; } else if( errno != 0 ) { cerr << "Too large argument to " << "-u option: \'" << optarg << "\"\n"; rc = 1; break; } universe_ = universe_value; 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 // See if required options were supplied if( ! sample_dates_found_ ) { cerr << "Required option -d not provided on command line\n"; rc = 1; } if( ! sample_int_found_ ) { cerr << "Required option -i not provided on command line\n"; rc = 1; } 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; if( static_cast< unsigned long >( new_argc_ ) < 2UL ) { cerr << "Not enough arguments beyond options; " "must be at least 1\n"; rc = 1; } if( static_cast< unsigned long >( new_argc_ ) > 3UL ) { cerr << "Too many arguments beyond options; " "must be no more than 2\n"; rc = 1; } } return rc; }