Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef FORIA_CONFIGURATIONFILE_HH
00046 #define FORIA_CONFIGURATIONFILE_HH
00047
00048 #include <string>
00049 #include <map>
00050 #include <iostream>
00051 #include <fstream>
00052 #include <sstream>
00053
00054 using std::string;
00055
00056 namespace ForIA{
00057
00065 class ConfigurationFile {
00066
00067 protected:
00068 string myDelimiter;
00069 string myComment;
00070 string mySentry;
00071 std::map<string,string> myContents;
00072
00073 typedef std::map<string,string>::iterator mapi;
00074 typedef std::map<string,string>::const_iterator mapci;
00075
00076
00077 public:
00078
00079
00080
00081
00082
00083
00084 ConfigurationFile();
00085
00086 void init(const string &filename, const string &delimiter = "=",
00087 const string &comment = "#", const string &sentry= "EndConfigurationFile");
00088
00089
00090 template<class T> T read( const string& key ) const;
00091 template<class T> T read( const string& key, const T& value ) const;
00092 template<class T> bool readInto( T& var, const string& key ) const;
00093 template<class T>
00094 bool readInto( T& var, const string& key, const T& value ) const;
00095
00096
00097 template<class T> void add( string key, const T& value );
00098 void remove( const string& key );
00099
00100
00101 bool keyExists( const string& key ) const;
00102
00103
00104 string getDelimiter() const { return myDelimiter; }
00105 string getComment() const { return myComment; }
00106 string getSentry() const { return mySentry; }
00107 string setDelimiter( const string& s )
00108 { string old = myDelimiter; myDelimiter = s; return old; }
00109 string setComment( const string& s )
00110 { string old = myComment; myComment = s; return old; }
00111
00112
00113 friend std::ostream& operator<<( std::ostream& os, const ConfigurationFile& cf );
00114 friend std::istream& operator>>( std::istream& is, ConfigurationFile& cf );
00115
00116 protected:
00117 template<class T> static string T_as_string( const T& t );
00118 template<class T> static T string_as_T( const string& s );
00119 static void trim( string& s );
00120
00121
00122
00123 public:
00124 struct file_not_found {
00125 string filename;
00126 file_not_found( const string& filename_ = string() )
00127 : filename(filename_) {} };
00128 struct key_not_found {
00129 string key;
00130 key_not_found( const string& key_ = string() )
00131 : key(key_) {} };
00132 };
00133
00134
00135
00136 template<class T>
00137 string ConfigurationFile::T_as_string( const T& t )
00138 {
00139
00140
00141 std::ostringstream ost;
00142 ost << t;
00143 return ost.str();
00144 }
00145
00146
00147
00148 template<class T>
00149 T ConfigurationFile::string_as_T( const string& s )
00150 {
00151
00152
00153 T t;
00154 std::istringstream ist(s);
00155 ist >> t;
00156 return t;
00157 }
00158
00159
00160
00161 template<>
00162 inline string ConfigurationFile::string_as_T<string>( const string& s )
00163 {
00164
00165
00166 return s;
00167 }
00168
00169
00170
00171 template<>
00172 inline bool ConfigurationFile::string_as_T<bool>( const string& s )
00173 {
00174
00175
00176
00177 bool b = true;
00178 string sup = s;
00179 for( string::iterator p = sup.begin(); p != sup.end(); ++p )
00180 *p = toupper(*p);
00181 if( sup==string("FALSE") || sup==string("F") ||
00182 sup==string("NO") || sup==string("N") ||
00183 sup==string("0") || sup==string("NONE") )
00184 b = false;
00185 return b;
00186 }
00187
00188
00189 template<class T>
00190 T ConfigurationFile::read( const string& key ) const
00191 {
00192
00193 mapci p = myContents.find(key);
00194 if( p == myContents.end() ) throw key_not_found(key);
00195 return string_as_T<T>( p->second );
00196 }
00197
00198
00199 template<class T>
00200 T ConfigurationFile::read( const string& key, const T& value ) const
00201 {
00202
00203
00204 mapci p = myContents.find(key);
00205 if( p == myContents.end() ) return value;
00206 return string_as_T<T>( p->second );
00207 }
00208
00209
00210 template<class T>
00211 bool ConfigurationFile::readInto( T& var, const string& key ) const
00212 {
00213
00214
00215
00216 mapci p = myContents.find(key);
00217 bool found = ( p != myContents.end() );
00218 if( found ) var = string_as_T<T>( p->second );
00219 return found;
00220 }
00221
00222
00223 template<class T>
00224 bool ConfigurationFile::readInto( T& var, const string& key, const T& value ) const
00225 {
00226
00227
00228
00229 mapci p = myContents.find(key);
00230 bool found = ( p != myContents.end() );
00231 if( found )
00232 var = string_as_T<T>( p->second );
00233 else
00234 var = value;
00235 return found;
00236 }
00237
00238
00239 template<class T>
00240 void ConfigurationFile::add( string key, const T& value )
00241 {
00242
00243 string v = T_as_string( value );
00244 trim(key);
00245 trim(v);
00246 myContents[key] = v;
00247 return;
00248 }
00249 }
00250 #endif //FORIA_CONFIGURATIONFILE_HH
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269