00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024
00025 #include <iostream>
00026 #include <syslog.h>
00027 #include "strtrim.h"
00028 #include "configreader.h"
00029 #include "cfg.h"
00030
00031 using namespace std;
00032
00033 ConfigReader::StringMap ConfigReader::s_emptyMap;
00034
00035 ConfigReader::ConfigReader(std::istream &is) throw (std::exception)
00036 {
00037 bool verbose = g_getCfg().isVerbose();
00038 StrTrim trimmer(" \t");
00039 while(!is.eof()) {
00040 string line;
00041 getline(is,line);
00042 if(line.length() > 0) {
00043 if(line.at(0) != '#') {
00044 string prefix = "";
00045 string val = "";
00046 string key;
00047 string::size_type eqpos = line.find('=');
00048 string::size_type uscorepos = line.find('_');
00049 if((uscorepos != string::npos) && (uscorepos < eqpos)) {
00050 prefix = line.substr(0,uscorepos);
00051 key = line.substr(uscorepos+1,eqpos-uscorepos-1);
00052 } else {
00053 key = line.substr(0,eqpos);
00054 }
00055 if(eqpos != string::npos) {
00056 val = line.substr(eqpos+1,string::npos);
00057 trimmer.trim(prefix);
00058 trimmer.trim(key);
00059 trimmer.trim(val);
00060 if(verbose) {
00061 if(key == "password")
00062 syslog(LOG_DEBUG,"config: prefix: %s key: %s value=(hidden)",prefix.c_str(),key.c_str());
00063 else
00064 syslog(LOG_DEBUG,"config: prefix: %s key: %s value=%s",prefix.c_str(),key.c_str(),val.c_str());
00065 }
00066
00067 ConfigMap::iterator itr = _map.find(prefix);
00068 if( itr == _map.end() ) {
00069 StringMap tmpMap;
00070 tmpMap.insert(StringMap::value_type(key,val));
00071 _map.insert(ConfigMap::value_type(prefix,tmpMap));
00072 } else {
00073 itr->second.insert(StringMap::value_type(key,val));
00074 }
00075 } else {
00076 if(verbose)
00077 syslog(LOG_DEBUG,"config: ingoring: %s",line.c_str());
00078 }
00079 }
00080 }
00081 }
00082 }
00083 const ConfigReader::StringMap& ConfigReader::get(const std::string& prefix,bool dontThrow) const throw (std::exception)
00084 {
00085 ConfigMap::const_iterator itr = _map.find(prefix);
00086 if(itr != _map.end())
00087 return itr->second;
00088 else {
00089 if(!dontThrow)
00090 throw runtime_error("prefix: "+prefix+" not found");
00091 else
00092 return s_emptyMap;
00093 }
00094 }
00095 const std::string& ConfigReader::getVal(const std::string& prefix, const std::string& key) const throw (std::exception)
00096 {
00097 const StringMap& preMap = get(prefix);
00098 StringMap::const_iterator itr = preMap.find(key);
00099 if(itr != preMap.end())
00100 return itr->second;
00101 else
00102 throw runtime_error("key: "+key+" not found");
00103 }
00104 const std::string ConfigReader::getValOrDef(const std::string& prefix,const std::string& key,const std::string& def) const throw (std::exception)
00105 {
00106 ConfigMap::const_iterator pitr = _map.find(prefix);
00107 if(pitr != _map.end()) {
00108 StringMap::const_iterator itr = pitr->second.find(key);
00109 if(itr != pitr->second.end())
00110 return itr->second;
00111 }
00112 return def;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131