00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef CFG_H
00021 #define CFG_H
00022 #include <stdexcept>
00023 #include <map>
00024 #include "configreader.h"
00025 #include "db.h"
00026
00032 class Cfg{
00033 typedef enum {
00034 RM_init = 0,
00035 RM_normal,
00036 RM_weak,
00037 RM_reverse,
00038 _ENUM_last
00039 }RunMode;
00040 public:
00041 class WLEntry {
00042 public:
00043 typedef enum {
00044 WL_off = 0,
00045 WL_db,
00046 WL_dbcached,
00047 _ENUM_last
00048 }WLMode;
00049 public:
00050 WLEntry(WLMode mode,const std::string&) : _mode(mode) { }
00051 WLMode getMode() const { return _mode; }
00052 private:
00053 WLMode _mode;
00054 };
00055 typedef std::map<std::string,WLEntry>WLMap;
00056
00057 Cfg(RunMode runMode=RM_init,unsigned long ulTimeout=3600,
00058 const std::string dbtype="mysql",bool verbose=false,
00059 unsigned significant_bytes_in_weak_mode=3)
00060 : _runMode(runMode),_verbose(verbose),_dbtype(dbtype),
00061 _ulTimeout(ulTimeout),_uWeakBytes(significant_bytes_in_weak_mode)
00062 { }
00063 void init(const ConfigReader& cfgreader) throw (std::exception);
00064 ~Cfg() { };
00065
00066 bool isVerbose() const { return _verbose; }
00067 void setVerbose(bool b) { _verbose = b; }
00068 bool isModeReverse() const { return _runMode == RM_reverse; }
00069 const std::string& getDbType() const { return _dbtype; }
00070 const DB::ParamVector& getDbParamVector() const { return _dbParams; }
00071 bool isModeInit() const { return _runMode == RM_init; }
00072 bool isModeWeak() const { return _runMode == RM_weak; }
00073 unsigned getWeakBytes() const { return _uWeakBytes; }
00074
00075 bool isWlDb(const std::string& key) const { return getWLEntry(key).getMode() == WLEntry::WL_db; }
00076 bool isWlDbCached(const std::string& key) const { return getWLEntry(key).getMode() == WLEntry::WL_dbcached; }
00077 WLEntry::WLMode getWLMode(const std::string& key) const { return getWLEntry(key).getMode(); }
00078 unsigned long getTimeout() const { return _ulTimeout; }
00079 const WLMap& getWLMap() const { return _wlMap; }
00080 const std::string& getWlModeName(const WLEntry::WLMode mode) const throw (std::exception);
00081 protected:
00082
00083 const WLEntry getWLEntry(const std::string&) const;
00085 unsigned long parseNumeric(const ConfigReader& reader,const std::string& prefix,const std::string& key,unsigned long def) const;
00086 private:
00088 RunMode _runMode;
00089 bool _verbose;
00091 std::string _dbtype;
00092
00096 DB::ParamVector _dbParams;
00097
00101 unsigned long _ulTimeout;
00102
00103 unsigned _uWeakBytes;
00104
00105 WLMap _wlMap;
00106
00107 static const std::string _s_strMode;
00108 static const std::string _s_strRunModes[_ENUM_last];
00109 static const std::string _s_strTimeout;
00110 static const std::string _s_strVerbose;
00111 static const std::string _s_strDbType;
00112 static const std::string _s_strNoPrefix;
00113 static const std::string _s_strDbPrefix;
00114 static const std::string _s_strWlPrefix;
00115 static const std::string _s_strWlModes[WLEntry::_ENUM_last];
00116 static const std::string _s_strWeakBytes;
00117
00118 template <typename T,unsigned size>
00119 T parseSelect(const std::string& key, const std::string array[T::_ENUM_last]) const
00120 {
00121
00122 for(unsigned i=0;i < size; i++) {
00123 if(key == array[i]) {
00124 return T(i);
00125 }
00126 }
00127 return T(0);
00128 }
00129
00130
00131 };
00133 Cfg& g_getCfgNonConst();
00134 const Cfg& g_getCfg();
00135
00136 #endif