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 #ifndef _ANYOPTION_H
00028 #define _ANYOPTION_H
00029
00030 #include <iostream>
00031 #include <fstream>
00032 #include <cstdlib>
00033 #include <string>
00034
00035 #define COMMON_OPT 1
00036 #define COMMAND_OPT 2
00037 #define FILE_OPT 3
00038 #define COMMON_FLAG 4
00039 #define COMMAND_FLAG 5
00040 #define FILE_FLAG 6
00041
00042 #define COMMAND_OPTION_TYPE 1
00043 #define COMMAND_FLAG_TYPE 2
00044 #define FILE_OPTION_TYPE 3
00045 #define FILE_FLAG_TYPE 4
00046 #define UNKNOWN_TYPE 5
00047
00048 #define DEFAULT_MAXOPTS 10
00049 #define MAX_LONG_PREFIX_LENGTH 2
00050
00051 #define DEFAULT_MAXUSAGE 3
00052 #define DEFAULT_MAXHELP 10
00053
00054 #define TRUE_FLAG "true"
00055
00056 namespace Mimotera {
00057
00059
00073 class AnyOption {
00074
00075 public:
00076 AnyOption();
00077 AnyOption(int maxoptions );
00078 AnyOption(int maxoptions , int maxcharoptions);
00079 ~AnyOption();
00080
00081
00082
00083
00084
00085
00086
00087 void setCommandPrefixChar( char _prefix );
00088 void setCommandLongPrefix( char *_prefix );
00089 void setFileCommentChar( char _comment );
00090 void setFileDelimiterChar( char _delimiter );
00091
00092
00093
00094
00095
00096
00097
00098 void useCommandArgs( int _argc, char **_argv );
00099 void useFiileName( const char *_filename );
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 void noPOSIX();
00111
00112
00113
00114
00115 void setVerbose();
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 void setOption( const char *opt_string );
00138 void setOption( char opt_char );
00139 void setOption( const char *opt_string , char opt_char );
00140 void setFlag( const char *opt_string );
00141 void setFlag( char opt_char );
00142 void setFlag( const char *opt_string , char opt_char );
00143
00144
00145 void setCommandOption( const char *opt_string );
00146 void setCommandOption( char opt_char );
00147 void setCommandOption( const char *opt_string , char opt_char );
00148 void setCommandFlag( const char *opt_string );
00149 void setCommandFlag( char opt_char );
00150 void setCommandFlag( const char *opt_string , char opt_char );
00151
00152
00153 void setFileOption( const char *opt_string );
00154 void setFileOption( char opt_char );
00155 void setFileOption( const char *opt_string , char opt_char );
00156 void setFileFlag( const char *opt_string );
00157 void setFileFlag( char opt_char );
00158 void setFileFlag( const char *opt_string , char opt_char );
00159
00160
00161
00162
00163
00164 void processOptions();
00165 void processCommandArgs();
00166 void processCommandArgs( int max_args );
00167 bool processFile();
00168
00169
00170
00171
00172 void processCommandArgs( int _argc, char **_argv );
00173 void processCommandArgs( int _argc, char **_argv, int max_args );
00174 bool processFile( const char *_filename );
00175
00176
00177
00178
00179
00180 char *getValue( const char *_option );
00181 bool getFlag( const char *_option );
00182 char *getValue( char _optchar );
00183 bool getFlag( char _optchar );
00184
00185
00186
00187
00188 void printUsage();
00189 void printAutoUsage();
00190 void addUsage( const char *line );
00191 void printHelp();
00192
00193 void autoUsagePrint(bool flag);
00194
00195
00196
00197
00198 int getArgc();
00199 char* getArgv( int index );
00200 bool hasOptions();
00201
00202 private:
00203 int argc;
00204 char **argv;
00205 const char* filename;
00206 char* appname;
00207
00208 int *new_argv;
00209 int new_argc;
00210 int max_legal_args;
00211
00212
00213
00214 int max_options;
00215 const char **options;
00216 int *optiontype;
00217 int *optionindex;
00218 int option_counter;
00219
00220
00221 int max_char_options;
00222 char *optionchars;
00223 int *optchartype;
00224 int *optcharindex;
00225 int optchar_counter;
00226
00227
00228 char **values;
00229 int g_value_counter;
00230
00231
00232 const char **usage;
00233 int max_usage_lines;
00234 int usage_lines;
00235
00236 bool command_set;
00237 bool file_set;
00238 bool mem_allocated;
00239 bool posix_style;
00240 bool verbose;
00241 bool print_usage;
00242 bool print_help;
00243
00244 char opt_prefix_char;
00245 char long_opt_prefix[MAX_LONG_PREFIX_LENGTH];
00246 char file_delimiter_char;
00247 char file_comment_char;
00248 char equalsign;
00249 char comment;
00250 char delimiter;
00251 char endofline;
00252 char whitespace;
00253 char nullterminate;
00254
00255 bool set;
00256 bool once;
00257
00258 bool hasoptions;
00259 bool autousage;
00260
00261 private:
00262 void init();
00263 void init(int maxopt, int maxcharopt );
00264 bool alloc();
00265 void cleanup();
00266 bool valueStoreOK();
00267
00268
00269 bool doubleOptStorage();
00270 bool doubleCharStorage();
00271 bool doubleUsageStorage();
00272
00273 bool setValue( const char *option , char *value );
00274 bool setFlagOn( const char *option );
00275 bool setValue( char optchar , char *value);
00276 bool setFlagOn( char optchar );
00277
00278 void addOption( const char* option , int type );
00279 void addOption( char optchar , int type );
00280 void addOptionError( const char *opt);
00281 void addOptionError( char opt);
00282 bool findFlag( char* value );
00283 void addUsageError( const char *line );
00284 bool CommandSet();
00285 bool FileSet();
00286 bool POSIX();
00287
00288 char parsePOSIX( char* arg );
00289 int parseGNU( char *arg );
00290 bool matchChar( char c );
00291 int matchOpt( char *opt );
00292
00293
00294 char *readFile();
00295 char *readFile( const char* fname );
00296 bool consumeFile( char *buffer );
00297 void processLine( char *theline, int length );
00298 char *chomp( char *str );
00299 void valuePairs( char *type, char *value );
00300 void justValue( char *value );
00301
00302 void printVerbose( const char *msg );
00303 void printVerbose( char *msg );
00304 void printVerbose( char ch );
00305 void printVerbose( );
00306
00307
00308 };
00309 }
00310
00311 #endif