00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <bogotel/Portability.h>
00011 #include <bogotel/Properties.h>
00012
00013 #include <sstream>
00014
00015 namespace bogotel {
00016
00018
00020 CPropertiesFailure::CPropertiesFailure(const std::string& reason) :
00021 std::runtime_error(reason) {
00022 }
00023
00025
00027
00028 const char * const CProperties::szKeyTerminators = "=:";
00029 const std::string CProperties::_strBlank = "";
00030
00031 CProperties::CProperties()
00032 {
00033 }
00034
00035 CProperties::~CProperties()
00036 {
00037 }
00038
00039 void CProperties::load(const std::string filename) throw (CPropertiesFailure)
00040 {
00041 if (filename == "") {
00042 throw CPropertiesFailure(std::string("No filename specified"));
00043 }
00044
00045 std::ifstream initFile(filename.c_str());
00046
00047 if (!initFile) {
00048 throw CPropertiesFailure(std::string("Config File ") + filename + " does not exist or is unreadable");
00049 }
00050
00051 clear();
00052
00053 std::string strValue;
00054 std::string strKey;
00055 std::string strReader;
00056
00057 while (std::getline(initFile, strReader)) {
00058 std::istringstream reader(strReader);
00059
00060 if (!(reader >> strReader)) {
00061 continue;
00062 }
00063
00064
00065 if (strReader[0] == '#' || strReader[0] == '!') {
00066 continue;
00067 }
00068
00069 std::string::size_type iTermPos;
00070
00071 if ((iTermPos = strReader.find_first_of(szKeyTerminators)) != std::string::npos) {
00072 strKey = strReader.substr(0, iTermPos);
00073
00074 reader >> std::ws;
00075
00076 if ((strReader.length() - 1) > iTermPos) {
00077 strValue = strReader.substr(iTermPos + 1);
00078
00079 if (std::getline(reader, strReader)) {
00080
00081 strValue += strReader;
00082 }
00083 } else {
00084
00085 if (std::getline(reader, strReader)) {
00086
00087 if ((iTermPos = strReader.find_first_of(szKeyTerminators)) == std::string::npos) {
00088
00089 strValue = strReader;
00090 } else {
00091 strValue = strReader.substr(iTermPos);
00092 }
00093 }
00094 }
00095 } else {
00096 strKey = strReader;
00097 if (!(reader >> strReader)) {
00098
00099 strValue = "";
00100 } else {
00101
00102 if ((iTermPos = strReader.find_first_of(szKeyTerminators)) == std::string::npos) {
00103
00104 strValue = strReader;
00105 } else {
00106 if (strReader.length() > 1) {
00107
00108 strValue = strReader.substr(1);
00109 } else {
00110 strValue = "";
00111 }
00112
00113 if (reader >> strReader) {
00114 strValue += strReader;
00115 }
00116 }
00117 }
00118 }
00119 operator[](strKey) = strValue;
00120 }
00121 }
00122
00123 const std::string CProperties::getProperty(const std::string& strKey)
00124 {
00125 return getProperty(strKey, _strBlank);
00126 }
00127
00128 const std::string CProperties::getProperty(const std::string& strKey, const std::string& strDefault)
00129 {
00130 StringMap::const_iterator i = find(strKey);
00131 if (i != end()) {
00132 return i->second;
00133 } else {
00134 return strDefault;
00135 }
00136 }
00137
00138 const int CProperties::getProperty(const std::string& strKey, const int iDefault)
00139 {
00140 std::ostringstream o;
00141 o << iDefault;
00142 return atoi(getProperty(strKey, std::string(o.str())).c_str());
00143 }
00144
00145 const long CProperties::getProperty(const std::string& strKey, const long lDefault)
00146 {
00147 std::ostringstream o;
00148 o << lDefault;
00149 return atol(getProperty(strKey, std::string(o.str())).c_str());
00150 }
00151
00152 const unsigned long CProperties::getProperty(const std::string& strKey, const unsigned long ulDefault)
00153 {
00154 std::ostringstream o;
00155 o << ulDefault;
00156 return strtoul(getProperty(strKey, std::string(o.str())).c_str(), NULL, 10);
00157 }
00158
00159 const std::string CProperties::setProperty(const std::string& strKey, const std::string& strValue)
00160 {
00161 std::string strPrev = getProperty(strKey);
00162 operator[](strKey) = strValue;
00163 return strPrev;
00164 }
00165
00166 const int CProperties::setProperty(const std::string& strKey, const int iValue)
00167 {
00168 std::ostringstream o;
00169 o << iValue;
00170 return atoi(setProperty(strKey, o.str()).c_str());
00171 }
00172
00173 void CProperties::list(std::ostream& out)
00174 {
00175 StringMap::const_iterator iter = begin();
00176 while (iter != end()) {
00177 out << '\"'
00178 << iter->first
00179 << '\"'
00180 << '='
00181 << '\"'
00182 << iter->second
00183 << '\"'
00184 << std::endl;
00185
00186 iter++;
00187 }
00188 }
00189
00190 void CProperties::putAll(CProperties& src)
00191 {
00192 for (StringMap::iterator iter = src.begin();
00193 iter != src.end();
00194 iter++) {
00195 operator[](iter->first) = iter->second;
00196 }
00197
00198 }
00199 }