source: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.h @ 2575

Revision 2575, 11.6 KB checked in by bittner, 17 years ago (diff)

big merge: preparation for havran ray caster, check if everything works

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