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