cfg.cpp

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 #include <sstream>
00021 #include "cfg.h"
00022 
00023 //Global 
00024 static Cfg gso_CfgObject;
00025 
00026 Cfg& g_getCfgNonConst()
00027 {
00028         return gso_CfgObject; 
00029 }
00030 const Cfg& g_getCfg() 
00031 {
00032         return gso_CfgObject; 
00033 }
00034 
00035 //Cfg
00036 const std::string Cfg::_s_strMode = "mode";
00037 const std::string Cfg::_s_strRunModes[Cfg::_ENUM_last] =
00038     { "init","normal","weak","reverse" };
00039 const std::string Cfg::_s_strTimeout = "timeout";
00040 const std::string Cfg::_s_strVerbose = "verbose";
00041 const std::string Cfg::_s_strDbType = "dbtype";
00042 const std::string Cfg::_s_strNoPrefix = "";     
00043 const std::string Cfg::_s_strDbPrefix = "db";   
00044 const std::string Cfg::_s_strWlPrefix = "wl";   
00045 const std::string Cfg::_s_strWlModes[Cfg::WLEntry::_ENUM_last] = {"off","db","dbcached"};
00046 const std::string Cfg::_s_strWeakBytes = "weakbytes";
00047 
00048 void Cfg::init(const ConfigReader& reader) throw (std::exception)
00049 {
00050 //      const std::string& verbose = reader.getValOrDef(_s_strNoPrefix,_s_strVerbose,_s_strRunModes[0]);
00051         const std::string& mode = reader.getValOrDef(_s_strNoPrefix,_s_strMode,_s_strRunModes[0]);
00052         _runMode = parseSelect<RunMode,Cfg::_ENUM_last>(mode,_s_strRunModes); 
00053         _dbtype = reader.getValOrDef(_s_strNoPrefix,_s_strDbType,_dbtype);
00054         _ulTimeout = parseNumeric(reader,_s_strNoPrefix,_s_strTimeout,_ulTimeout);
00055         _uWeakBytes = parseNumeric(reader,_s_strNoPrefix,_s_strWeakBytes,_uWeakBytes);
00056         //read db_<params>
00057         //this throws (and exits)
00058         //TODO can be empty
00059         const ConfigReader::StringMap& preMap = reader.get(_s_strDbPrefix); //get everything prefixed with db_
00060         for(ConfigReader::StringMap::const_iterator itr = preMap.begin(); itr != preMap.end();++itr) {
00061                 _dbParams.push_back(itr->first);
00062                 _dbParams.push_back(itr->second);
00063         }
00064         //read wl_<params>
00065         const ConfigReader::StringMap& wlMap = reader.get(_s_strWlPrefix,true); //get everything prefixed with wl_,dont throw
00066         for(ConfigReader::StringMap::const_iterator itr = wlMap.begin(); itr != wlMap.end();++itr) {
00067                 WLEntry entry(parseSelect<WLEntry::WLMode,WLEntry::_ENUM_last>(itr->second,_s_strWlModes),"");
00068                 _wlMap.insert(WLMap::value_type(itr->first,entry));
00069         }
00070 } 
00071 unsigned long Cfg::parseNumeric(const ConfigReader& reader,const std::string& prefix,const std::string& key,unsigned long def) const
00072 {
00073         std::stringstream defStm;
00074         defStm << def;
00075         const std::string& str = reader.getValOrDef(prefix,key,defStm.str());
00076         if(str != defStm.str()) {
00077                 unsigned long ul = def;
00078                 std::stringstream stm;
00079                 stm << str;
00080                 stm >> ul;
00081                 def = ul;
00082         }
00083         return def;
00084 }
00085 const std::string& Cfg::getWlModeName(const WLEntry::WLMode mode) const throw (std::exception)
00086 {
00087         if(mode >= WLEntry::_ENUM_last)
00088                 throw std::out_of_range("Cfg::getWlModeName mode out of range");
00089         return _s_strWlModes[mode];
00090 }
00091 const Cfg::WLEntry Cfg::getWLEntry(const std::string& key) const
00092 {
00093         WLMap::const_iterator itr = _wlMap.find(key);
00094         if(itr != _wlMap.end())
00095                 return itr->second;
00096         else
00097                 return WLEntry(WLEntry::WL_off,"");
00098 }
00099 

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