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