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 #ifndef IBRCOMMON_CONFIGFILE_H
00044 #define IBRCOMMON_CONFIGFILE_H
00045
00046 #include <string>
00047 #include <map>
00048 #include <iostream>
00049 #include <fstream>
00050 #include <sstream>
00051
00052 using std::string;
00053
00054 namespace ibrcommon
00055 {
00056 class ConfigFile {
00057
00058 protected:
00059 string myDelimiter;
00060 string myComment;
00061 string mySentry;
00062 std::map<string,string> myContents;
00063
00064 typedef std::map<string,string>::iterator mapi;
00065 typedef std::map<string,string>::const_iterator mapci;
00066
00067
00068 public:
00069 ConfigFile( string filename,
00070 string delimiter = "=",
00071 string comment = "#",
00072 string sentry = "EndConfigFile" );
00073 ConfigFile();
00074
00075
00076 template<class T> T read( const string& key ) const;
00077 template<class T> T read( const string& key, const T& value ) const;
00078 template<class T> bool readInto( T& var, const string& key ) const;
00079 template<class T>
00080 bool readInto( T& var, const string& key, const T& value ) const;
00081
00082
00083 template<class T> void add( string key, const T& value );
00084 void remove( const string& key );
00085
00086
00087 bool keyExists( const string& key ) const;
00088
00089
00090 string getDelimiter() const { return myDelimiter; }
00091 string getComment() const { return myComment; }
00092 string getSentry() const { return mySentry; }
00093 string setDelimiter( const string& s )
00094 { string old = myDelimiter; myDelimiter = s; return old; }
00095 string setComment( const string& s )
00096 { string old = myComment; myComment = s; return old; }
00097
00098
00099 friend std::ostream& operator<<( std::ostream& os, const ConfigFile& cf );
00100 friend std::istream& operator>>( std::istream& is, ConfigFile& cf );
00101
00102 protected:
00103 template<class T> static string T_as_string( const T& t );
00104 template<class T> static T string_as_T( const string& s );
00105 static void trim( string& s );
00106
00107
00108
00109 public:
00110 struct file_not_found {
00111 string filename;
00112 file_not_found( const string& filename_ = string() )
00113 : filename(filename_) {} };
00114 struct key_not_found {
00115 string key;
00116 key_not_found( const string& key_ = string() )
00117 : key(key_) {} };
00118 };
00119
00120
00121
00122 template<class T>
00123 string ConfigFile::T_as_string( const T& t )
00124 {
00125
00126
00127 std::ostringstream ost;
00128 ost << t;
00129 return ost.str();
00130 }
00131
00132
00133
00134 template<class T>
00135 T ConfigFile::string_as_T( const string& s )
00136 {
00137
00138
00139 T t;
00140 std::istringstream ist(s);
00141 ist >> t;
00142 return t;
00143 }
00144
00145
00146
00147 template<>
00148 inline string ConfigFile::string_as_T<string>( const string& s )
00149 {
00150
00151
00152 return s;
00153 }
00154
00155
00156
00157 template<>
00158 inline bool ConfigFile::string_as_T<bool>( const string& s )
00159 {
00160
00161
00162
00163 bool b = true;
00164 string sup = s;
00165 for( string::iterator p = sup.begin(); p != sup.end(); ++p )
00166 *p = toupper(*p);
00167 if( sup==string("FALSE") || sup==string("F") ||
00168 sup==string("NO") || sup==string("N") ||
00169 sup==string("0") || sup==string("NONE") )
00170 b = false;
00171 return b;
00172 }
00173
00174
00175 template<class T>
00176 T ConfigFile::read( const string& key ) const
00177 {
00178
00179 mapci p = myContents.find(key);
00180 if( p == myContents.end() ) throw key_not_found(key);
00181 return string_as_T<T>( p->second );
00182 }
00183
00184
00185 template<class T>
00186 T ConfigFile::read( const string& key, const T& value ) const
00187 {
00188
00189
00190 mapci p = myContents.find(key);
00191 if( p == myContents.end() ) return value;
00192 return string_as_T<T>( p->second );
00193 }
00194
00195
00196 template<class T>
00197 bool ConfigFile::readInto( T& var, const string& key ) const
00198 {
00199
00200
00201
00202 mapci p = myContents.find(key);
00203 bool found = ( p != myContents.end() );
00204 if( found ) var = string_as_T<T>( p->second );
00205 return found;
00206 }
00207
00208
00209 template<class T>
00210 bool ConfigFile::readInto( T& var, const string& key, const T& value ) const
00211 {
00212
00213
00214
00215 mapci p = myContents.find(key);
00216 bool found = ( p != myContents.end() );
00217 if( found )
00218 var = string_as_T<T>( p->second );
00219 else
00220 var = value;
00221 return found;
00222 }
00223
00224
00225 template<class T>
00226 void ConfigFile::add( string key, const T& value )
00227 {
00228
00229 string v = T_as_string( value );
00230 trim(key);
00231 trim(v);
00232 myContents[key] = v;
00233 return;
00234 }
00235 }
00236
00237 #endif // CONFIGFILE_H
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256