ConfigMap.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/common/ConfigMap.cpp#14 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/common/ConfigMap.h"
00026 #include <iostream>
00027 
00028 #include <boost/lexical_cast.hpp>
00029 
00030 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/common/ConfigMap.cpp#14 $");
00031 
00032 ConfigMap::ConfigMap()
00033 {
00034 }
00035 
00036 ConfigMap::~ConfigMap()
00037 {
00038 }
00039 
00040 void ConfigMap::readParams(std::istream &paramStream)
00041 {
00042     for (;;) {
00043         std::string name,value;
00044         paramStream >> name;
00045         if (name == "") {
00046             break;
00047         }
00048         paramStream >> value;
00049         paramVals[name] = value;
00050     }
00051 }
00052 
00053 void ConfigMap::dumpParams(std::ostream &dumpStream) const
00054 {
00055     for (StringMapConstIter pPair = paramVals.begin();
00056          pPair != paramVals.end(); ++pPair)
00057     {
00058         dumpStream << pPair->first;
00059         dumpStream << " ";
00060         dumpStream << pPair->second;
00061         dumpStream << std::endl;
00062     }
00063 }
00064 
00065 void ConfigMap::mergeFrom(const ConfigMap& that)
00066 {
00067     for (StringMapConstIter pPair = that.paramVals.begin();
00068          pPair != that.paramVals.end();
00069          ++pPair)
00070     {
00071         this->paramVals[pPair->first] = pPair->second;
00072     }
00073 }
00074 
00075 
00076 std::string ConfigMap::getStringParam(
00077     std::string paramName,
00078     std::string defaultVal) const
00079 {
00080     StringMapConstIter pPair = paramVals.find(paramName);
00081     if (pPair == paramVals.end()) {
00082         FENNEL_TRACE(
00083             TRACE_CONFIG,
00084             "parameter " << paramName
00085             << " using default value of '"
00086             << defaultVal << "'");
00087         return defaultVal;
00088     } else {
00089         FENNEL_TRACE(
00090             TRACE_CONFIG,
00091             "parameter " << paramName
00092             << " using specified value of '"
00093             << pPair->second << "'");
00094         return pPair->second;
00095     }
00096 }
00097 
00098 int ConfigMap::getIntParam(
00099     std::string paramName,
00100     int defaultVal) const
00101 {
00102     StringMapConstIter pPair = paramVals.find(paramName);
00103     if (pPair == paramVals.end()) {
00104         FENNEL_TRACE(
00105             TRACE_CONFIG,
00106             "parameter " << paramName
00107             << " using default value of '"
00108             << defaultVal << "'");
00109         return defaultVal;
00110     } else {
00111         FENNEL_TRACE(
00112             TRACE_CONFIG,
00113             "parameter " << paramName
00114             << " using specified value of '"
00115             << pPair->second << "'");
00116         return boost::lexical_cast<int>(pPair->second);
00117     }
00118 }
00119 
00120 bool ConfigMap::getBoolParam(
00121     std::string paramName,
00122     bool defaultVal) const
00123 {
00124     StringMapConstIter pPair = paramVals.find(paramName);
00125     if (pPair == paramVals.end()) {
00126         FENNEL_TRACE(
00127             TRACE_CONFIG,
00128             "parameter " << paramName
00129             << " using default value of '"
00130             << defaultVal << "'");
00131         return defaultVal;
00132     } else {
00133         FENNEL_TRACE(
00134             TRACE_CONFIG,
00135             "parameter " << paramName
00136             << " using specified value of '"
00137             << pPair->second << "'");
00138         // boost only likes 1/0, so preprocess true/false
00139         if (strcasecmp(pPair->second.c_str(), "true") == 0) {
00140             return true;
00141         } else if (strcasecmp(pPair->second.c_str(), "false") == 0) {
00142             return false;
00143         } else {
00144             return boost::lexical_cast<bool>(pPair->second);
00145         }
00146     }
00147 }
00148 
00149 long ConfigMap::getLongParam(
00150     std::string paramName,
00151     long defaultVal) const
00152 {
00153     StringMapConstIter pPair = paramVals.find(paramName);
00154     if (pPair == paramVals.end()) {
00155         FENNEL_TRACE(
00156             TRACE_CONFIG,
00157             "parameter " << paramName
00158             << " using default value of '"
00159             << defaultVal << "'");
00160         return defaultVal;
00161     } else {
00162         FENNEL_TRACE(
00163             TRACE_CONFIG,
00164             "parameter " << paramName
00165             << " using specified value of '"
00166             << pPair->second << "'");
00167         // REVIEW jvs 25-Nov-2008:  There used to be a note here,
00168         // but it didn't actually explain why this doesn't use
00169         // boost::lexical_cast; probably an old Boost bug.
00170         return atol(pPair->second.c_str());
00171     }
00172 }
00173 
00174 bool ConfigMap::isParamSet(std::string paramName) const
00175 {
00176     return paramVals.find(paramName) != paramVals.end();
00177 }
00178 
00179 void ConfigMap::setStringParam(
00180     std::string paramName,
00181     std::string paramVal)
00182 {
00183     paramVals[paramName] = paramVal;
00184 }
00185 
00186 void ConfigMap::clear()
00187 {
00188     paramVals.clear();
00189 }
00190 
00191 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/common/ConfigMap.cpp#14 $");
00192 
00193 // End ConfigMap.cpp

Generated on Mon Jun 22 04:00:18 2009 for Fennel by  doxygen 1.5.1