configreader.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 
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) != '#') { //ignore comments
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 //                                      cout << "pre: " << prefix << " key: " << key << " val=" << val << endl;
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 int main()
00118 {
00119         ConfigReader reader(cin);
00120         try {
00121                 reader.get("bla");
00122         } catch(exception &e) {
00123                 cout <<"ok - exception caught: "<< e.what()<<endl;
00124         }
00125         const ConfigReader::StringMap& preMap = reader.get("prefix");
00126         for(ConfigReader::StringMap::const_iterator itr = preMap.begin(); itr != preMap.end();++itr) 
00127                 cout << itr->first << " = " << itr->second << endl;
00128 
00129         return 0;
00130 }
00131 */

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