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