1 | // ===================================================================
|
---|
2 | // $Id: environ.h,v 1.1 2004/02/16 14:46:00 bittner Exp $
|
---|
3 | //
|
---|
4 | // environ.h
|
---|
5 | // Header file for environment operations, i.e.. reading
|
---|
6 | // environment file, reading command line parameters etc.
|
---|
7 | //
|
---|
8 | // Class: CEnvironment
|
---|
9 | //
|
---|
10 | /// Initial coding by
|
---|
11 | /// @author Tomas Kopal
|
---|
12 |
|
---|
13 | #ifndef __ENVIRONMENT_H__
|
---|
14 | #define __ENVIRONMENT_H__
|
---|
15 |
|
---|
16 | // forward declarations
|
---|
17 |
|
---|
18 | #include "common.h"
|
---|
19 |
|
---|
20 | namespace GtpVisibilityPreprocessor {
|
---|
21 |
|
---|
22 | class Vector3;
|
---|
23 |
|
---|
24 |
|
---|
25 | /// Enumeration type used to distinguish the type of the environment variable
|
---|
26 | enum EOptType {
|
---|
27 | /// the integer variable
|
---|
28 | optInt,
|
---|
29 | /// the Real variable
|
---|
30 | optFloat,
|
---|
31 | /// the bool variable
|
---|
32 | optBool,
|
---|
33 | /// the vector variable
|
---|
34 | optVector,
|
---|
35 | /// the string variable
|
---|
36 | optString
|
---|
37 | };
|
---|
38 |
|
---|
39 | class COption
|
---|
40 | {
|
---|
41 | public:
|
---|
42 | COption() {
|
---|
43 | name = NULL;
|
---|
44 | value = NULL;
|
---|
45 | abbrev = NULL;
|
---|
46 | defaultValue = NULL;
|
---|
47 | description = NULL;
|
---|
48 | pointer = NULL;
|
---|
49 | }
|
---|
50 |
|
---|
51 | virtual ~COption() {
|
---|
52 | if (name)
|
---|
53 | delete name;
|
---|
54 | if (value)
|
---|
55 | delete value;
|
---|
56 | if (abbrev)
|
---|
57 | delete abbrev;
|
---|
58 | if (defaultValue)
|
---|
59 | delete defaultValue;
|
---|
60 | if (description)
|
---|
61 | delete description;
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | EOptType type;
|
---|
66 | char *name;
|
---|
67 | char *value;
|
---|
68 | char *abbrev;
|
---|
69 | char *defaultValue;
|
---|
70 |
|
---|
71 | char *description;
|
---|
72 | void *pointer;
|
---|
73 | friend ostream &operator <<(ostream &s, const COption &o) {
|
---|
74 | s<<o.name<<"\t";
|
---|
75 | if (o.abbrev)
|
---|
76 | s<<o.abbrev;
|
---|
77 | else
|
---|
78 | s<<"no abbrev";
|
---|
79 | s<<"\t";
|
---|
80 | if (o.value)
|
---|
81 | s<<o.value;
|
---|
82 | else
|
---|
83 | s<<"not set";
|
---|
84 | s<<"\t";
|
---|
85 | if (o.defaultValue)
|
---|
86 | s<<"["<<o.defaultValue<<"]";
|
---|
87 | else
|
---|
88 | s<<"[not set]";
|
---|
89 | s<<"\t";
|
---|
90 | if (o.description)
|
---|
91 | s<<endl<<o.description;
|
---|
92 | return s;
|
---|
93 | }
|
---|
94 |
|
---|
95 | };
|
---|
96 |
|
---|
97 | /** CEnvironment class.
|
---|
98 | This class is used for reading command line, reading environment file and
|
---|
99 | managing options. Each recognizable option must be registered in the
|
---|
100 | constructor of this class.
|
---|
101 | */
|
---|
102 | class Environment {
|
---|
103 | /**@name Variables */
|
---|
104 | //@{
|
---|
105 | private:
|
---|
106 | /// Maximal number of options
|
---|
107 | int maxOptions;
|
---|
108 | /// Number of columns in the parameter table.
|
---|
109 | int numParams;
|
---|
110 | /// Number of rows in the parameter table.
|
---|
111 | int paramRows;
|
---|
112 | /// String with prefixes with non-optional parameters.
|
---|
113 | char *optionalParams;
|
---|
114 | /// Current row in the parameter table.
|
---|
115 | int curRow;
|
---|
116 |
|
---|
117 | /** Parameter table itself.
|
---|
118 | The organization of this table is two dimensional array of character
|
---|
119 | strings. First column are parameters, all the other columns are options
|
---|
120 | prefixed by char passed to function GetCmdlineParams and paired with
|
---|
121 | corresponding parameters.
|
---|
122 | */
|
---|
123 | char ***params;
|
---|
124 |
|
---|
125 | /// Number of registered options.
|
---|
126 | int numOptions;
|
---|
127 | /** Options table itself.
|
---|
128 | This table is two dimensional array of character strings with four
|
---|
129 | columns. First column contains name of the option, second it's value,
|
---|
130 | third eventual abbreviation, and finally the fourth contains the default
|
---|
131 | value specified when registering the option.
|
---|
132 | */
|
---|
133 | COption *options;
|
---|
134 | // char ***options;
|
---|
135 | /** Options types.
|
---|
136 | This is auxiliary table to the table "options". It contains the type of
|
---|
137 | each option as specified when registering.
|
---|
138 | */
|
---|
139 | // EOptType *optTypes;
|
---|
140 | //@}
|
---|
141 |
|
---|
142 | /**@name Methods */
|
---|
143 | //@{
|
---|
144 | /** Auxiliary method for parsing environment file.
|
---|
145 | Function gets one string from buffer, skipping leading whitespaces, and
|
---|
146 | appends it at the end of the string parameter. Returns pointer to the next
|
---|
147 | string in the buffer with trailing whitespaces skipped. If no string in
|
---|
148 | the buffer can't be found, returns NULL.
|
---|
149 | @param buffer Input line for parsing.
|
---|
150 | @param string Buffer to append gathered string to.
|
---|
151 | @return Pointer to next string or NULL if not succesful.
|
---|
152 | */
|
---|
153 | char* ParseString(char *buffer, char *string) const;
|
---|
154 | /** Method for checking variable type.
|
---|
155 | This method is used to check if the value of the variable can be taken
|
---|
156 | as the specified type.
|
---|
157 | @param value Tested value of the variable.
|
---|
158 | @param type The type of the variable.
|
---|
159 | @return true if the variable is of the specified type, false elsewhere.
|
---|
160 | */
|
---|
161 | bool CheckType(const char *value, const EOptType type) const;
|
---|
162 | //@}
|
---|
163 |
|
---|
164 | int
|
---|
165 | FindOption(const char *name,
|
---|
166 | const bool isFatal=false) const;
|
---|
167 |
|
---|
168 | bool
|
---|
169 | ParseBool(const char *name) const;
|
---|
170 | void
|
---|
171 | ParseVector(const char *name, Vector3 &v) const;
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 | public:
|
---|
176 | /**@name Methods */
|
---|
177 | //@{
|
---|
178 | /// Constructor of the CEnvironment class.
|
---|
179 | Environment();
|
---|
180 | /// Destructor of the CEnvironment class.
|
---|
181 | virtual ~Environment();
|
---|
182 |
|
---|
183 | /// This method is used as a help on available command line options.
|
---|
184 | virtual void PrintUsage(ostream &s) const;
|
---|
185 |
|
---|
186 | virtual void SetStaticOptions();
|
---|
187 |
|
---|
188 |
|
---|
189 | /** Method for registering new option.
|
---|
190 | Using this method is possible to register new option with it's name, type,
|
---|
191 | abbreviation and default value.
|
---|
192 | @param name Name of the option.
|
---|
193 | @param type The type of the variable.
|
---|
194 | @param abbrev If not NULL, alias usable as a command line option.
|
---|
195 | @param defValue Implicit value used if not specified explicitly.
|
---|
196 | */
|
---|
197 | void RegisterOption(const char *name, const EOptType type,
|
---|
198 | const char *abbrev, const char *defValue = NULL);
|
---|
199 |
|
---|
200 | /** Check if the specified switch is present in the command line.
|
---|
201 | Primary use of this function is to check for presence of some special
|
---|
202 | switch, e.g. -h for help. It can be used anytime.
|
---|
203 | @param argc Number of command line arguments.
|
---|
204 | @param argv Array of command line arguments.
|
---|
205 | @param swtch Switch we are checking for.
|
---|
206 | @return true if found, false elsewhere.
|
---|
207 | */
|
---|
208 | bool CheckForSwitch(const int argc,
|
---|
209 | char **argv,
|
---|
210 | const char swtch) const;
|
---|
211 | /** First pass of parsing command line.
|
---|
212 | This function parses command line and gets from there all non-optional
|
---|
213 | parameters (i.e. not prefixed by the dash) and builds a table of
|
---|
214 | parameters. According to param optParams, it also writes some optional
|
---|
215 | parameters to this table and pair them with corresponding non-optional
|
---|
216 | parameters.
|
---|
217 | @param argc Number of command line arguments.
|
---|
218 | @param argv Array of command line arguments.
|
---|
219 | @param optParams String consisting of prefixes to non-optional parameters.
|
---|
220 | All prefixes must be only one character wide !
|
---|
221 | */
|
---|
222 | void ReadCmdlineParams(const int argc,
|
---|
223 | char **argv,
|
---|
224 | const char *optParams);
|
---|
225 | /** Reading of the environment file.
|
---|
226 | This function reads the environment file.
|
---|
227 | @param filename The name of the environment file to read.
|
---|
228 | @return true if OK, false in case of error.
|
---|
229 | */
|
---|
230 | bool ReadEnvFile(const char *filename);
|
---|
231 | /** Second pass of the command line parsing.
|
---|
232 | Performs parsing of the command line ignoring all parameters and options
|
---|
233 | read in the first pass. Builds table of options.
|
---|
234 | @param argc Number of command line arguments.
|
---|
235 | @param argv Array of command line arguments.
|
---|
236 | @param index Index of non-optional parameter, of which options should
|
---|
237 | be read in.
|
---|
238 | */
|
---|
239 | void ParseCmdline(const int argc, char **argv, const int index);
|
---|
240 |
|
---|
241 | /** Clears the environment data structures.
|
---|
242 | This function is intended to clear all data structures corresponding to
|
---|
243 | particular non-optional parameter to allow options for the next
|
---|
244 | non-optional parameter to be read. After this clearing, function
|
---|
245 | ReadEnvFile() can be called again to obtain next load of options.
|
---|
246 | */
|
---|
247 | // void ClearEnvironment();
|
---|
248 |
|
---|
249 | /** Parameters number query function.
|
---|
250 | This function returns number of non-optional parameters specified on
|
---|
251 | the command line.
|
---|
252 | */
|
---|
253 | int GetParamNum() const { return paramRows; }
|
---|
254 | /** Returns the indexed parameter or corresponding optional parameter.
|
---|
255 | This function is used for queries on individual non-optional parameters.
|
---|
256 | The table must be constructed to allow this function to operate (i.e. the
|
---|
257 | first pass of command line parsing must be done).
|
---|
258 | @param name If space (' '), returns the non-optional parameter, else returns
|
---|
259 | the corresponding optional parameter.
|
---|
260 | @param index Index of the non-optional parameter to which all other options
|
---|
261 | corresponds.
|
---|
262 | @param value Return value of the queried parameter.
|
---|
263 | @return true if OK, false in case of error or if the parameter wasn't
|
---|
264 | specified.
|
---|
265 | */
|
---|
266 | bool GetParam(const char name, const int index, char *value) const;
|
---|
267 |
|
---|
268 | /**@name Options query functions.
|
---|
269 | These functions are used for queries on individual options. The table must
|
---|
270 | be constructed to allow these functions to operate (i.e. the environment
|
---|
271 | file must be read and the second pass of command line parsing be done).
|
---|
272 | @param name Name of the option to retrieve.
|
---|
273 | @param value Return value of the queried option.
|
---|
274 | @param isFatal Boolean value specifying, if the function should exit or
|
---|
275 | return false in case of error.
|
---|
276 | @return true if OK, false in case of error.
|
---|
277 | */
|
---|
278 | //@{
|
---|
279 | /// returns true, if the specified option has been specified
|
---|
280 | bool OptionPresent(const char *name) const;
|
---|
281 | /// returns named option as a integral value.
|
---|
282 | bool GetIntValue(const char *name,
|
---|
283 | int &value,
|
---|
284 | const bool isFatal = false) const;
|
---|
285 |
|
---|
286 | /// returns named option as a floating point value.
|
---|
287 | bool GetDoubleValue(const char *name,
|
---|
288 | double &value,
|
---|
289 | const bool isFatal = false) const;
|
---|
290 |
|
---|
291 | /// returns named option as a floating point value.
|
---|
292 | bool GetRealValue(const char *name,
|
---|
293 | Real &value,
|
---|
294 | const bool isFatal = false) const;
|
---|
295 |
|
---|
296 | bool GetFloatValue(const char *name,
|
---|
297 | float &value,
|
---|
298 | const bool isFatal = false) const;
|
---|
299 |
|
---|
300 | /// returns named option as a boolean value.
|
---|
301 | bool GetBoolValue(const char *name,
|
---|
302 | bool &value,
|
---|
303 | const bool isFatal = false) const;
|
---|
304 |
|
---|
305 | bool GetBool(const char *name, const bool isFatal = false) const;
|
---|
306 |
|
---|
307 | /// returns named option as a 3D vector.
|
---|
308 | bool GetVectorValue(const char *name,
|
---|
309 | Vector3 &v,
|
---|
310 | const bool isFatal = false) const;
|
---|
311 |
|
---|
312 | /// returns named option as a character string.
|
---|
313 | bool GetStringValue(const char *name,
|
---|
314 | char *value,
|
---|
315 | const bool isFatal = false) const;
|
---|
316 |
|
---|
317 | //@}
|
---|
318 | /**@name Options set functions.
|
---|
319 | These functions are used for setting individual options.
|
---|
320 | @param name Name of the option to set.
|
---|
321 | @param value Value to set the option to.
|
---|
322 | */
|
---|
323 | //@{
|
---|
324 | /// sets named option as a integral value.
|
---|
325 | void SetInt(const char *name, const int value);
|
---|
326 | /// sets named option as a floating point value.
|
---|
327 | void SetFloat(const char *name, const Real value);
|
---|
328 | /// sets named option as a boolean value.
|
---|
329 | void SetBool(const char *name, const bool value);
|
---|
330 | /// sets named option as a 3D vector.
|
---|
331 | void SetVector(const char *name,
|
---|
332 | const Vector3 &v);
|
---|
333 | /// sets named option as a character string.
|
---|
334 | void SetString(const char *name, const char *value);
|
---|
335 | //@}
|
---|
336 | //@}
|
---|
337 |
|
---|
338 | void Parse(const int argc, char **argv, bool useExePath);
|
---|
339 |
|
---|
340 | void
|
---|
341 | CodeString(char *buff, int max);
|
---|
342 |
|
---|
343 | void
|
---|
344 | DecodeString(char *buff, int max);
|
---|
345 |
|
---|
346 | void
|
---|
347 | SaveCodedFile(char *filenameText,
|
---|
348 | char *filenameCoded);
|
---|
349 |
|
---|
350 | };
|
---|
351 |
|
---|
352 | // global environment value
|
---|
353 | extern Environment *environment;
|
---|
354 |
|
---|
355 | }
|
---|
356 |
|
---|
357 | #endif // __ENVIRON_H__
|
---|
358 |
|
---|