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