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