FFSM++
1.1.0
French Forest Sector Model ++
Main Page
Related Pages
Classes
Files
File List
File Members
anyoption.h
Go to the documentation of this file.
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
30
using namespace
std
;
31
32
class
AnyOption
33
{
34
35
public
:
/* 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
162
private
:
/* 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
+ 1];
/* '--' 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
221
private
:
/* 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 */
AnyOption::hasoptions
bool hasoptions
Definition:
anyoption.h:218
AnyOption::optcharindex
int * optcharindex
Definition:
anyoption.h:184
MAX_LONG_PREFIX_LENGTH
#define MAX_LONG_PREFIX_LENGTH
Definition:
anyoption.h:23
AnyOption::optiontype
int * optiontype
Definition:
anyoption.h:176
AnyOption::opt_prefix_char
char opt_prefix_char
Definition:
anyoption.h:204
AnyOption::new_argc
int new_argc
Definition:
anyoption.h:169
AnyOption::delimiter
char delimiter
Definition:
anyoption.h:210
AnyOption::argv
char ** argv
Definition:
anyoption.h:164
AnyOption::usage
const char ** usage
Definition:
anyoption.h:192
AnyOption::whitespace
char whitespace
Definition:
anyoption.h:212
std
STL namespace.
AnyOption::file_delimiter_char
char file_delimiter_char
Definition:
anyoption.h:206
AnyOption::optchar_counter
int optchar_counter
Definition:
anyoption.h:185
AnyOption::command_set
bool command_set
Definition:
anyoption.h:196
AnyOption::option_counter
int option_counter
Definition:
anyoption.h:178
AnyOption::equalsign
char equalsign
Definition:
anyoption.h:208
AnyOption::nullterminate
char nullterminate
Definition:
anyoption.h:213
AnyOption::once
bool once
Definition:
anyoption.h:216
AnyOption::file_comment_char
char file_comment_char
Definition:
anyoption.h:207
AnyOption::max_char_options
int max_char_options
Definition:
anyoption.h:181
AnyOption::autousage
bool autousage
Definition:
anyoption.h:219
AnyOption::g_value_counter
int g_value_counter
Definition:
anyoption.h:189
AnyOption::max_legal_args
int max_legal_args
Definition:
anyoption.h:170
AnyOption::filename
const char * filename
Definition:
anyoption.h:165
AnyOption::endofline
char endofline
Definition:
anyoption.h:211
AnyOption::new_argv
int * new_argv
Definition:
anyoption.h:168
AnyOption::verbose
bool verbose
Definition:
anyoption.h:200
AnyOption::posix_style
bool posix_style
Definition:
anyoption.h:199
AnyOption::file_set
bool file_set
Definition:
anyoption.h:197
AnyOption
Definition:
anyoption.h:32
AnyOption::print_usage
bool print_usage
Definition:
anyoption.h:201
AnyOption::argc
int argc
Definition:
anyoption.h:163
AnyOption::options
const char ** options
Definition:
anyoption.h:175
AnyOption::mem_allocated
bool mem_allocated
Definition:
anyoption.h:198
AnyOption::usage_lines
int usage_lines
Definition:
anyoption.h:194
AnyOption::values
char ** values
Definition:
anyoption.h:188
AnyOption::optchartype
int * optchartype
Definition:
anyoption.h:183
AnyOption::max_options
int max_options
Definition:
anyoption.h:174
AnyOption::print_help
bool print_help
Definition:
anyoption.h:202
AnyOption::optionchars
char * optionchars
Definition:
anyoption.h:182
AnyOption::comment
char comment
Definition:
anyoption.h:209
AnyOption::max_usage_lines
int max_usage_lines
Definition:
anyoption.h:193
AnyOption::appname
char * appname
Definition:
anyoption.h:166
AnyOption::optionindex
int * optionindex
Definition:
anyoption.h:177