00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 ¶mStream)
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
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
00168
00169
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