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