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