00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include<fstream>
00021
00022 #include "PropertiesManager.h"
00023 #include "Error.h"
00024
00025 using namespace std;
00026
00027 namespace mdw
00028 {
00029 PropertiesManager * PropertiesManager::_instance;
00030
00031 PropertiesManager::PropertiesManager()
00032 {
00033 }
00034
00035
00036 PropertiesManager::~PropertiesManager()
00037 {
00038 }
00039
00040 void PropertiesManager::initialise (const std::string &configFile)
00041 {
00042 _instance = new PropertiesManager();
00043 _instance->init (configFile);
00044 }
00045 PropertiesManager &PropertiesManager::getInstance()
00046 {
00047 return *_instance;
00048 }
00049
00050 bool PropertiesManager::getBooleanProperty (const std::string &name)
00051 {
00052 map<string, string>::iterator aIter = _stringValues.find (name);
00053 if (aIter == _stringValues.end())
00054 {
00055 ERROR ("Property " << name << " does not have a value");
00056 }
00057 string res = parse (aIter->second);
00058 return res == "true";
00059 }
00060 bool PropertiesManager::getBooleanProperty (const std::string &name, bool defaultValue)
00061 {
00062 map<string, string>::iterator aIter = _stringValues.find (name);
00063 if (aIter == _stringValues.end())
00064 {
00065 return defaultValue;
00066 }
00067 string res = parse (aIter->second);
00068 return res == "true";
00069 }
00070
00071 int PropertiesManager::getIntProperty (const std::string &name)
00072 {
00073 map<string, string>::iterator aIter = _stringValues.find (name);
00074 if (aIter == _stringValues.end())
00075 {
00076 ERROR ("Property " << name << " does not have a value");
00077 }
00078 string res = parse (aIter->second);
00079 return atoi (res.c_str());
00080 }
00081 int PropertiesManager::getIntProperty (const std::string &name, int defaultValue)
00082 {
00083 map<string, string>::iterator aIter = _stringValues.find (name);
00084 if (aIter == _stringValues.end())
00085 {
00086 return defaultValue;
00087 }
00088 string res = parse (aIter->second);
00089 return atoi (res.c_str());
00090 }
00091
00092 std::string PropertiesManager::getStringProperty (const std::string &name)
00093 {
00094 map<string, string>::iterator aIter = _stringValues.find (name);
00095 if (aIter == _stringValues.end())
00096 {
00097 ERROR ("Property " << name << " does not have a value");
00098 }
00099 return parse (aIter->second);
00100 }
00101 std::string PropertiesManager::getStringProperty (const std::string &name, const std::string &defaultValue)
00102 {
00103 map<string, string>::iterator aIter = _stringValues.find (name);
00104 if (aIter == _stringValues.end())
00105 {
00106 return defaultValue;
00107 }
00108 return parse (aIter->second);
00109 }
00110
00111 std::string PropertiesManager::parse (const std::string &value)
00112 {
00113 size_t pos = value.find ("$(");
00114 if (pos == string::npos)
00115 {
00116 return value;
00117 }
00118 pos += 2;
00119 size_t current = pos;
00120 int depth = 1;
00121 while (current < value.size())
00122 {
00123 if (value[current] == '(')
00124 {
00125 ++depth;
00126 }
00127 else if (value[current] == ')')
00128 {
00129 --depth;
00130 }
00131 if (depth == 0)
00132 {
00133 break;
00134 }
00135 ++current;
00136 }
00137 if (current == value.size())
00138 {
00139
00140 return value;
00141 }
00142 string var = parse (value.substr (pos, current - pos));
00143 map<string, string>::iterator aIter = _stringValues.find (var);
00144 if (aIter == _stringValues.end())
00145 {
00146 ERROR ("Property " << var << " does not have a value");
00147 }
00148 return value.substr (0, pos - 2) + parse (aIter->second) + parse (value.substr (current + 1));
00149 }
00150
00151 void PropertiesManager::init (const std::string &configFile)
00152 {
00153 readFile (configFile);
00154 }
00155
00156 void PropertiesManager::readFile (const std::string &configFile)
00157 {
00158 ifstream aFile (configFile.c_str());
00159 string line;
00160 while (!aFile.eof())
00161 {
00162 getline (aFile, line);
00163 if (line.empty())
00164 {
00165 continue;
00166 }
00167 if (line[0] == '#')
00168 {
00169 continue;
00170 }
00171 if (line.size() > 9 && line.substr (0, 9) == ".include ")
00172 {
00173 readFile (parse (line.substr (9)));
00174 }
00175 size_t pos = line.find (" = ");
00176 if (pos != string::npos)
00177 {
00178 _stringValues[line.substr (0, pos) ] = line.substr (pos + 3);
00179 }
00180 else
00181 {
00182 pos = line.find ("=");
00183 _stringValues[line.substr (0, pos) ] = line.substr (pos + 1);
00184 }
00185 }
00186 }
00187
00188 }