ALMaSS Rabbit ODdox  1.00
The rabbit model description following ODdox protocol
configurator.h
Go to the documentation of this file.
1 //
2 // configurator.h
3 //
4 /*
5 *
6 *******************************************************************************************************
7 Copyright (c) 2011, Christopher John Topping, University of Aarhus
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without modification, are permitted provided
11 that the following conditions are met:
12 
13 Redistributions of source code must retain the above copyright notice, this list of conditions and the
14 following disclaimer.
15 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
16 the following disclaimer in the documentation and/or other materials provided with the distribution.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ********************************************************************************************************
27 */
28 
29 #ifndef CONFIGURATOR_H
30 #define CONFIGURATOR_H
31 
32 // If defined will compile many methods as inlines for speed.
33 //#define CONF_INLINES
34 
35 #include <stdio.h>
36 #ifdef __UNIX
37 #undef max
38 #endif
39 #include <string>
40 #include <map>
41 #include <vector>
42 //#include <algorithm.h>
43 
44 // This one *has* to be a define!
45 // A global configurator variable will not do.
46 #define CFG_MAX_LINE_LENGTH 512
47 
48 using namespace std;
49 
50 
51 typedef enum {
57 } CfgType;
58 
59 typedef enum {
64 
65 
69 class CfgBase {
70  string m_key;
72 
73  public:
74  CfgBase( const char* a_key, CfgSecureLevel a_level );
75  virtual ~CfgBase( void );
76 
77  const string getkey ( void ) { return m_key; }
78  virtual CfgType gettype ( void ) { return CFG_NONE; }
79  CfgSecureLevel getlevel( void ) { return m_level; }
80 };
81 
85 class CfgInt : public CfgBase
86 {
87  int m_int;
88 
89  public:
90  CfgInt( const char* a_key, CfgSecureLevel a_level, int a_defval );
91 
92  int value( void ) { return m_int; }
93  void set( int a_newval ) { m_int = a_newval; }
94  virtual CfgType gettype( void ) { return CFG_INT; }
95 };
96 
100 class CfgFloat : public CfgBase
101 {
102  double m_float;
103 
104  public:
105  CfgFloat( const char* a_key, CfgSecureLevel a_level, double a_defval );
106 
107  double value( void ) { return m_float; }
108  void set( double a_newval ) { m_float = a_newval; }
109  virtual CfgType gettype( void ) { return CFG_FLOAT; }
110 };
111 
112 
116 class CfgBool : public CfgBase
117 {
118  bool m_bool;
119 
120  public:
121  CfgBool( const char* a_key, CfgSecureLevel a_level, bool a_defval );
122 
123  bool value( void ) { return m_bool; }
124  void set( bool a_newval ) { m_bool = a_newval; }
125  virtual CfgType gettype( void ) { return CFG_BOOL; }
126 };
127 
128 
132 class CfgStr : public CfgBase
133 {
134  string m_string;
135 
136  public:
137  CfgStr( const char* a_key, CfgSecureLevel a_level, const char* a_defval );
138 
139  const char* value( void ) { return m_string.c_str(); }
140  void set( char* a_newval ) { m_string = a_newval; }
141  virtual CfgType gettype( void ) { return CFG_STRING; }
142 };
143 
144 
149 {
150  map<string,unsigned int> CfgI;
151  vector<CfgBase*> CfgVals;
152 
153  // Private methods and fields for the configuration file parser.
154  unsigned int m_lineno;
155  void ParseCfgLine( char* a_line );
156  void SetCfgInt ( char* a_key, char* a_val );
157  void SetCfgFloat ( char* a_key, char* a_val );
158  void SetCfgBool ( char* a_key, char* a_val );
159  void SetCfgStr ( char* a_key, char* a_val );
160  bool LastDoubleQuote( char* a_rest_of_line );
161 
162  // Discreet security check. Returns true if we terminate line
163  // parsing early.
164  bool SetCfgGatekeeper( const char* a_method,
165  const char* a_key,
166  CfgSecureLevel a_level );
167 
168  // Helper methods.
169  void ShowIdType( unsigned int a_i );
170  char* ExtractString( char* a_line );
171  void DumpSymbols( const char *a_dumpfile,
172  CfgSecureLevel a_level );
173 
174  public:
175  // Write all configuration values with a security level at or below
176  // a_level to a_dumpfile in alphabetical order. a_level must
177  // be CFG_CUSTOM (user settable) or CFG_PUBLIC (user can see
178  // predefined value and the very existence of this parameter).
179  void DumpPublicSymbols( const char *a_dumpfile,
180  CfgSecureLevel a_level );
181 
182  // Dump *all* configuration values, including the private ones,
183  // to a_dumpfile and then calls exit(), noting the event in the error
184  // file!! For debugging purposes only. The call to exit() is there in
185  // order to try and prevent any use of this call from making it into
186  // production code.
187  void DumpAllSymbolsAndExit( const char *a_dumpfile );
188 
189  // Reads and parses a_cfgfile for configuration values. Unknown
190  // or CFG_PRIVATE keys are silently ignored thus preventing snooping
191  // on possibly interesting key values. Returns true if reading and
192  // parsing was error free.
193  bool ReadSymbols( const char *a_cfgfile );
194 
195  // You should never use these ones directly, they are
196  // called automagically by the CfgBase class con/destructor
197  // when needed.
198  Configurator( void );
199  ~Configurator( void );
200 
201  // Please don't use this unless you know what you are doing.
202  // Need to be public as it is used by the CfgBase class and friends.
203  bool Register( CfgBase* a_cfgval, const char* a_key );
204 };
205 
206 extern class Configurator *g_cfg;
207 
208 #endif // CONFIGURATOR_H
209 
210 
211 
class Configurator * g_cfg
CfgSecureLevel getlevel(void)
Definition: configurator.h:79
string m_string
Definition: configurator.h:134
Integer configurator entry class.
Definition: configurator.h:85
bool m_bool
Definition: configurator.h:118
double value(void)
Definition: configurator.h:107
A class to provide standard parameter entry facilities.
Definition: configurator.h:148
CfgType
Definition: configurator.h:51
const char * value(void)
Definition: configurator.h:139
int m_int
Definition: configurator.h:87
unsigned int m_lineno
Definition: configurator.h:154
bool value(void)
Definition: configurator.h:123
virtual CfgType gettype(void)
Definition: configurator.h:141
string m_key
Definition: configurator.h:70
CfgSecureLevel
Definition: configurator.h:59
virtual CfgType gettype(void)
Definition: configurator.h:94
vector< CfgBase * > CfgVals
Definition: configurator.h:151
String configurator entry class.
Definition: configurator.h:132
Bool configurator entry class.
Definition: configurator.h:116
virtual CfgType gettype(void)
Definition: configurator.h:78
double m_float
Definition: configurator.h:102
Base class for a configurator entry.
Definition: configurator.h:69
const string getkey(void)
Definition: configurator.h:77
int value(void)
Definition: configurator.h:92
virtual CfgType gettype(void)
Definition: configurator.h:109
Double configurator entry class.
Definition: configurator.h:100
virtual CfgType gettype(void)
Definition: configurator.h:125
map< string, unsigned int > CfgI
Definition: configurator.h:150
CfgSecureLevel m_level
Definition: configurator.h:71