cfg.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Michael Moritz                                  *
00003  *   mimo@restoel.net                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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         // user
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 //      WLMode isWLNetwork() const { return _wlNetwork; }
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         /* return WL_off if not found */
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                 //unsigned bla = ;
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

Generated on Tue Jul 24 16:36:53 2007 for gps by  doxygen 1.5.1