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
00047
00048 #ifndef __D_CONFIG_FILE__
00049 #define __D_CONFIG_FILE__
00050
00051 #include "DException.h"
00052 #include "FileModes.h"
00053 #include "LineFile.h"
00054 #include "StringFunctions.h"
00055 #include <map>
00056 #include <set>
00057 #include <fstream>
00058
00059 namespace DUtils {
00060
00062 class ConfigFile
00063 {
00064 public:
00065 ConfigFile();
00066 ~ConfigFile();
00067
00074 ConfigFile(const char *filename, const FILE_MODES mode);
00075
00082 ConfigFile(const std::string &filename, const FILE_MODES mode);
00083
00089 void OpenForReading(const char *filename);
00090
00096 inline void OpenForReading(const std::string &filename)
00097 {
00098 OpenForReading(filename.c_str());
00099 }
00100
00106 void OpenForWriting(const char *filename);
00107
00113 inline void OpenForWriting(const std::string &filename)
00114 {
00115 OpenForWriting(filename.c_str());
00116 }
00117
00123 void OpenForAppending(const char *filename);
00124
00130 inline void OpenForAppending(const std::string &filename)
00131 {
00132 OpenForAppending(filename.c_str());
00133 }
00134
00139 void Close();
00140
00145 template<class T>
00146 T get(const std::string &name) const;
00147
00152 template<class T>
00153 T get(const char *name) const;
00154
00159 inline int sizeAnonymous() const;
00160
00166 template<class T>
00167 T getAnonymous(int n) const;
00168
00174 template<class T>
00175 void put(const std::string &name, const T &data);
00176
00182 template<class T>
00183 void put(const char *name, const T &data);
00184
00185 protected:
00186
00190 void Init(const char *filename, const FILE_MODES mode);
00191
00198 void readContent();
00199
00203 void writeContent();
00204
00208 void resolveVariables();
00209
00216 void resolveVar(std::string &value, const std::set<string> &used);
00217
00218 protected:
00219
00221 std::map<std::string, std::string> m_data;
00223 LineFile m_file;
00225 int m_unknowns;
00226
00227 };
00228
00229
00230
00231 template<class T>
00232 T ConfigFile::getAnonymous(int n) const
00233 {
00234 const char U = '?';
00235 stringstream ss;
00236 ss << U << n;
00237 return get<T>(ss.str().c_str());
00238 }
00239
00240 template<class T>
00241 T ConfigFile::get(const std::string &name) const
00242 {
00243 return get<T>(name.c_str());
00244 }
00245
00246 template<class T>
00247 T ConfigFile::get(const char *name) const
00248 {
00249 std::map<std::string, std::string>::const_iterator it =
00250 m_data.find(name);
00251
00252 if(it != m_data.end())
00253 return StringFunctions::fromString<T>(it->second);
00254 else
00255 return T();
00256 }
00257
00258
00259
00260 template<class T>
00261 void ConfigFile::put(const std::string &name, const T &data)
00262 {
00263 put<T>(name.c_str(), data);
00264 }
00265
00266 template<class T>
00267 void ConfigFile::put(const char *name, const T &data)
00268 {
00269 pair<std::map<std::string, std::string>::iterator, bool> status;
00270
00271 std::string value = StringFunctions::toString<T>(data);
00272
00273 status = m_data.insert(make_pair(name, value));
00274
00275 if(!status.second)
00276 {
00277 status.first->second = value;
00278 }
00279 }
00280
00281
00282
00283 int ConfigFile::sizeAnonymous() const
00284 {
00285 return m_unknowns;
00286 }
00287
00288
00289
00290 }
00291
00292 #endif