Bugz Door Project
anyoption.h
1 #ifndef _ANYOPTION_H
2 #define _ANYOPTION_H
3 
4 #define _CRT_SECURE_NO_WARNINGS /* Microsoft C/C++ Compiler: Disable C4996 \
5  warnings for security-enhanced CRT \
6  functions */
7 
8 #include <cstring>
9 #include <fstream>
10 #include <iostream>
11 #include <stdlib.h>
12 #include <string>
13 
14 #define COMMON_OPT 1
15 #define COMMAND_OPT 2
16 #define FILE_OPT 3
17 #define COMMON_FLAG 4
18 #define COMMAND_FLAG 5
19 #define FILE_FLAG 6
20 
21 #define COMMAND_OPTION_TYPE 1
22 #define COMMAND_FLAG_TYPE 2
23 #define FILE_OPTION_TYPE 3
24 #define FILE_FLAG_TYPE 4
25 #define UNKNOWN_TYPE 5
26 
27 #define DEFAULT_MAXOPTS 10
28 #define MAX_LONG_PREFIX_LENGTH 2
29 
30 #define DEFAULT_MAXUSAGE 3
31 #define DEFAULT_MAXHELP 10
32 
33 #define TRUE_FLAG "true"
34 
35 using namespace std;
36 
37 class AnyOption {
38 
39 public: /* the public interface */
40  AnyOption();
41 
42  explicit AnyOption(int maxoptions);
43  explicit AnyOption(int maxoptions, int maxcharoptions);
44  ~AnyOption();
45 
46  /*
47  * following set methods specifies the
48  * special characters and delimiters
49  * if not set traditional defaults will be used
50  */
51 
52  void setCommandPrefixChar(char _prefix); /* '-' in "-w" */
53  void setCommandLongPrefix(const char *_prefix); /* '--' in "--width" */
54  void setFileCommentChar(char _comment); /* '#' in shell scripts */
55  void setFileDelimiterChar(char _delimiter); /* ':' in "width : 100" */
56 
57  /*
58  * provide the input for the options
59  * like argv[] for commnd line and the
60  * option file name to use;
61  */
62 
63  void useCommandArgs(int _argc, char **_argv);
64  void useFiileName(const char *_filename);
65 
66  /*
67  * turn off the POSIX style options
68  * this means anything starting with a '-' or "--"
69  * will be considered a valid option
70  * which also means you cannot add a bunch of
71  * POIX options chars together like "-lr" for "-l -r"
72  *
73  */
74 
75  void noPOSIX();
76 
77  /*
78  * prints warning verbose if you set anything wrong
79  */
80  void setVerbose();
81 
82  /*
83  * there are two types of options
84  *
85  * Option - has an associated value ( -w 100 )
86  * Flag - no value, just a boolean flag ( -nogui )
87  *
88  * the options can be either a string ( GNU style )
89  * or a character ( traditional POSIX style )
90  * or both ( --width, -w )
91  *
92  * the options can be common to the command line and
93  * the option file, or can belong only to either of
94  * command line and option file
95  *
96  * following set methods, handle all the above
97  * cases of options.
98  */
99 
100  /* options command to command line and option file */
101  void setOption(const char *opt_string);
102  void setOption(char opt_char);
103  void setOption(const char *opt_string, char opt_char);
104  void setFlag(const char *opt_string);
105  void setFlag(char opt_char);
106  void setFlag(const char *opt_string, char opt_char);
107 
108  /* options read from command line only */
109  void setCommandOption(const char *opt_string);
110  void setCommandOption(char opt_char);
111  void setCommandOption(const char *opt_string, char opt_char);
112  void setCommandFlag(const char *opt_string);
113  void setCommandFlag(char opt_char);
114  void setCommandFlag(const char *opt_string, char opt_char);
115 
116  /* options read from an option file only */
117  void setFileOption(const char *opt_string);
118  void setFileOption(char opt_char);
119  void setFileOption(const char *opt_string, char opt_char);
120  void setFileFlag(const char *opt_string);
121  void setFileFlag(char opt_char);
122  void setFileFlag(const char *opt_string, char opt_char);
123 
124  /*
125  * process the options, registered using
126  * useCommandArgs() and useFileName();
127  */
128  void processOptions();
129  void processCommandArgs();
130  void processCommandArgs(int max_args);
131  bool processFile();
132 
133  /*
134  * process the specified options
135  */
136  void processCommandArgs(int _argc, char **_argv);
137  void processCommandArgs(int _argc, char **_argv, int max_args);
138  bool processFile(const char *_filename);
139 
140  /*
141  * get the value of the options
142  * will return NULL if no value is set
143  */
144  char *getValue(const char *_option);
145  bool getFlag(const char *_option);
146  char *getValue(char _optchar);
147  bool getFlag(char _optchar);
148 
149  /*
150  * Print Usage
151  */
152  void printUsage();
153  void printAutoUsage();
154  void addUsage(const char *line);
155  void printHelp();
156  /* print auto usage printing for unknown options or flag */
157  void autoUsagePrint(bool flag);
158 
159  /*
160  * get the argument count and arguments sans the options
161  */
162  int getArgc() const;
163  char *getArgv(int index) const;
164  bool hasOptions() const;
165 
166 private: /* the hidden data structure */
167  int argc; /* command line arg count */
168  char **argv; /* commnd line args */
169  const char *filename; /* the option file */
170  char *appname; /* the application name from argv[0] */
171 
172  int *new_argv; /* arguments sans options (index to argv) */
173  int new_argc; /* argument count sans the options */
174  int max_legal_args; /* ignore extra arguments */
175 
176  /* option strings storage + indexing */
177  int max_options; /* maximum number of options */
178  const char **options; /* storage */
179  int *optiontype; /* type - common, command, file */
180  int *optionindex; /* index into value storage */
181  int option_counter; /* counter for added options */
182 
183  /* option chars storage + indexing */
184  int max_char_options; /* maximum number options */
185  char *optionchars; /* storage */
186  int *optchartype; /* type - common, command, file */
187  int *optcharindex; /* index into value storage */
188  int optchar_counter; /* counter for added options */
189 
190  /* values */
191  char **values; /* common value storage */
192  int g_value_counter; /* globally updated value index LAME! */
193 
194  /* help and usage */
195  const char **usage; /* usage */
196  int max_usage_lines; /* max usage lines reserved */
197  int usage_lines; /* number of usage lines */
198 
199  bool command_set; /* if argc/argv were provided */
200  bool file_set; /* if a filename was provided */
201  bool mem_allocated; /* if memory allocated in init() */
202  bool posix_style; /* enables to turn off POSIX style options */
203  bool verbose; /* silent|verbose */
204  bool print_usage; /* usage verbose */
205  bool print_help; /* help verbose */
206 
207  char opt_prefix_char; /* '-' in "-w" */
208  char long_opt_prefix[MAX_LONG_PREFIX_LENGTH + 1]; /* '--' in "--width" */
209  char file_delimiter_char; /* ':' in width : 100 */
210  char file_comment_char; /* '#' in "#this is a comment" */
211  char equalsign;
212  char comment;
213  char delimiter;
214  char endofline;
215  char whitespace;
216  char nullterminate;
217 
218  bool set; // was static member
219  bool once; // was static member
220 
221  bool hasoptions;
222  bool autousage;
223 
224 private: /* the hidden utils */
225  void init();
226  void init(int maxopt, int maxcharopt);
227  bool alloc();
228  void allocValues(int index, size_t length);
229  void cleanup();
230  bool valueStoreOK();
231 
232  /* grow storage arrays as required */
233  bool doubleOptStorage();
234  bool doubleCharStorage();
235  bool doubleUsageStorage();
236 
237  bool setValue(const char *option, char *value);
238  bool setFlagOn(const char *option);
239  bool setValue(char optchar, char *value);
240  bool setFlagOn(char optchar);
241 
242  void addOption(const char *option, int type);
243  void addOption(char optchar, int type);
244  void addOptionError(const char *opt) const;
245  void addOptionError(char opt) const;
246  bool findFlag(char *value);
247  void addUsageError(const char *line);
248  bool CommandSet() const;
249  bool FileSet() const;
250  bool POSIX() const;
251 
252  char parsePOSIX(char *arg);
253  int parseGNU(char *arg);
254  bool matchChar(char c);
255  int matchOpt(char *opt);
256 
257  /* dot file methods */
258  char *readFile();
259  char *readFile(const char *fname);
260  bool consumeFile(char *buffer);
261  void processLine(char *theline, int length);
262  char *chomp(char *str);
263  void valuePairs(char *type, char *value);
264  void justValue(char *value);
265 
266  void printVerbose(const char *msg) const;
267  void printVerbose(char *msg) const;
268  void printVerbose(char ch) const;
269  void printVerbose() const;
270 };
271 
272 #endif /* ! _ANYOPTION_H */
AnyOption
Definition: anyoption.h:35
buffer
char buffer[10]
pushback buffer to store keys we're not ready for yet.
Definition: getkey.cpp:116