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