123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #ifndef _ANYOPTION_H
- #define _ANYOPTION_H
- #define _CRT_SECURE_NO_WARNINGS
- #include <cstring>
- #include <fstream>
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- #define COMMON_OPT 1
- #define COMMAND_OPT 2
- #define FILE_OPT 3
- #define COMMON_FLAG 4
- #define COMMAND_FLAG 5
- #define FILE_FLAG 6
- #define COMMAND_OPTION_TYPE 1
- #define COMMAND_FLAG_TYPE 2
- #define FILE_OPTION_TYPE 3
- #define FILE_FLAG_TYPE 4
- #define UNKNOWN_TYPE 5
- #define DEFAULT_MAXOPTS 10
- #define MAX_LONG_PREFIX_LENGTH 2
- #define DEFAULT_MAXUSAGE 3
- #define DEFAULT_MAXHELP 10
- #define TRUE_FLAG "true"
- using namespace std;
- class AnyOption {
- public:
- AnyOption();
- explicit AnyOption(int maxoptions);
- explicit AnyOption(int maxoptions, int maxcharoptions);
- ~AnyOption();
-
- void setCommandPrefixChar(char _prefix);
- void setCommandLongPrefix(const char *_prefix);
- void setFileCommentChar(char _comment);
- void setFileDelimiterChar(char _delimiter);
-
- void useCommandArgs(int _argc, char **_argv);
- void useFiileName(const char *_filename);
-
- void noPOSIX();
-
- void setVerbose();
-
-
- void setOption(const char *opt_string);
- void setOption(char opt_char);
- void setOption(const char *opt_string, char opt_char);
- void setFlag(const char *opt_string);
- void setFlag(char opt_char);
- void setFlag(const char *opt_string, char opt_char);
-
- void setCommandOption(const char *opt_string);
- void setCommandOption(char opt_char);
- void setCommandOption(const char *opt_string, char opt_char);
- void setCommandFlag(const char *opt_string);
- void setCommandFlag(char opt_char);
- void setCommandFlag(const char *opt_string, char opt_char);
-
- void setFileOption(const char *opt_string);
- void setFileOption(char opt_char);
- void setFileOption(const char *opt_string, char opt_char);
- void setFileFlag(const char *opt_string);
- void setFileFlag(char opt_char);
- void setFileFlag(const char *opt_string, char opt_char);
-
- void processOptions();
- void processCommandArgs();
- void processCommandArgs(int max_args);
- bool processFile();
-
- void processCommandArgs(int _argc, char **_argv);
- void processCommandArgs(int _argc, char **_argv, int max_args);
- bool processFile(const char *_filename);
-
- char *getValue(const char *_option);
- bool getFlag(const char *_option);
- char *getValue(char _optchar);
- bool getFlag(char _optchar);
-
- void printUsage();
- void printAutoUsage();
- void addUsage(const char *line);
- void printHelp();
-
- void autoUsagePrint(bool flag);
-
- int getArgc() const;
- char *getArgv(int index) const;
- bool hasOptions() const;
- private:
- int argc;
- char **argv;
- const char *filename;
- char *appname;
- int *new_argv;
- int new_argc;
- int max_legal_args;
-
- int max_options;
- const char **options;
- int *optiontype;
- int *optionindex;
- int option_counter;
-
- int max_char_options;
- char *optionchars;
- int *optchartype;
- int *optcharindex;
- int optchar_counter;
-
- char **values;
- int g_value_counter;
-
- const char **usage;
- int max_usage_lines;
- int usage_lines;
- bool command_set;
- bool file_set;
- bool mem_allocated;
- bool posix_style;
- bool verbose;
- bool print_usage;
- bool print_help;
- char opt_prefix_char;
- char long_opt_prefix[MAX_LONG_PREFIX_LENGTH + 1];
- char file_delimiter_char;
- char file_comment_char;
- char equalsign;
- char comment;
- char delimiter;
- char endofline;
- char whitespace;
- char nullterminate;
- bool set;
- bool once;
- bool hasoptions;
- bool autousage;
- private:
- void init();
- void init(int maxopt, int maxcharopt);
- bool alloc();
- void allocValues(int index, size_t length);
- void cleanup();
- bool valueStoreOK();
-
- bool doubleOptStorage();
- bool doubleCharStorage();
- bool doubleUsageStorage();
- bool setValue(const char *option, char *value);
- bool setFlagOn(const char *option);
- bool setValue(char optchar, char *value);
- bool setFlagOn(char optchar);
- void addOption(const char *option, int type);
- void addOption(char optchar, int type);
- void addOptionError(const char *opt) const;
- void addOptionError(char opt) const;
- bool findFlag(char *value);
- void addUsageError(const char *line);
- bool CommandSet() const;
- bool FileSet() const;
- bool POSIX() const;
- char parsePOSIX(char *arg);
- int parseGNU(char *arg);
- bool matchChar(char c);
- int matchOpt(char *opt);
-
- char *readFile();
- char *readFile(const char *fname);
- bool consumeFile(char *buffer);
- void processLine(char *theline, int length);
- char *chomp(char *str);
- void valuePairs(char *type, char *value);
- void justValue(char *value);
- void printVerbose(const char *msg) const;
- void printVerbose(char *msg) const;
- void printVerbose(char ch) const;
- void printVerbose() const;
- };
- #endif
|