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