00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "configurationdatabase.h"
00019
00020 #include <fstream>
00021 using std::ifstream;
00022 using std::endl;
00023
00024 const char ConfigurationDatabase::BEGIN_COMMENT='#';
00025 const char ConfigurationDatabase::BEGIN_HEADER='[';
00026 const char ConfigurationDatabase::END_HEADER=']';
00027
00028 ConfigurationDatabase::ConfigurationDatabase():m_configurations(), m_comments()
00029 {
00030 }
00031
00032 ConfigurationDatabase::ConfigurationDatabase(const string &filename):m_configurations(), m_comments()
00033 {
00034 ifstream inputFile;
00035
00036 inputFile.open(filename.c_str());
00037 if (!inputFile)
00038 throw "ERROR: Failed to find the configuration file " + filename + ".";
00039 inputFile >> *this;
00040 inputFile.close();
00041 }
00042
00043 int ConfigurationDatabase::intValue(const string &header, const string &key) const
00044 {
00045 int value=0;
00046
00047 if (m_configurations.find(header) != m_configurations.end())
00048 if (m_configurations.find(header)->second.find(key) != m_configurations.find(header)->second.end())
00049 value=atoi(m_configurations.find(header)->second.find(key)->second.c_str());
00050 else
00051 throw "ERROR: Failed to find the key " + key + ".";
00052 else
00053 throw "ERROR: Failed to find the header " + header + ".";
00054 return value;
00055 }
00056
00057 double ConfigurationDatabase::doubleValue(const string &header, const string &key) const
00058 {
00059 double value=0.0;
00060
00061 if (m_configurations.find(header) != m_configurations.end())
00062 if (m_configurations.find(header)->second.find(key) != m_configurations.find(header)->second.end())
00063 value=atof(m_configurations.find(header)->second.find(key)->second.c_str());
00064 else
00065 throw "ERROR: Failed to find the key " + key + ".";
00066 else
00067 throw "ERROR: Failed to find the header " + header + ".";
00068 return value;
00069 }
00070
00071 string ConfigurationDatabase::stringValue(const string &header, const string &key) const
00072 {
00073 string value="";
00074
00075 if (m_configurations.find(header) != m_configurations.end())
00076 if (m_configurations.find(header)->second.find(key) != m_configurations.find(header)->second.end())
00077 value=m_configurations.find(header)->second.find(key)->second.c_str();
00078 else
00079 throw "ERROR: Failed to find the key " + key + ".";
00080 else
00081 throw "ERROR: Failed to find the header " + header + ".";
00082 return value;
00083 }
00084
00085 ConfigurationDatabase::Headers ConfigurationDatabase::headers() const
00086 {
00087 ConfigurationDatabaseType::const_iterator iterHeader=m_configurations.begin();
00088 Headers headers;
00089
00090 while (iterHeader != m_configurations.end())
00091 {
00092 headers.push_back(iterHeader->first);
00093 iterHeader++;
00094 }
00095 return headers;
00096 }
00097
00098 ConfigurationDatabase::Keys ConfigurationDatabase::keys(const string &header) const
00099 {
00100 KeyValuePair::const_iterator iterKey=m_configurations.find(header)->second.begin();
00101 KeyValuePair::const_iterator iterEnd=m_configurations.find(header)->second.end();
00102 Keys keys;
00103
00104 while (iterKey != iterEnd)
00105 {
00106 keys.push_back(iterKey->first);
00107 iterKey++;
00108 }
00109 return keys;
00110 }
00111
00112 void ConfigurationDatabase::addHeaderKeyValuePair(const string &header, const string &key, const string &value)
00113 {
00114 m_configurations[header][key]=value;
00115 }
00116
00117 void ConfigurationDatabase::addKeyValuePair(const string &header, const string &key, const string &value)
00118 {
00119 if (m_configurations.find(header) != m_configurations.end())
00120 m_configurations[header][key]=value;
00121 else
00122 throw "ERROR: Failed to find the header " + header + ".";
00123 }
00124
00125 istream& operator>>(istream &in, ConfigurationDatabase &database)
00126 {
00127 string testPart="", key="", header="", value="", comment="";
00128 int lineNumber=-1;
00129
00130 while (!in.fail())
00131 {
00132 lineNumber++;
00133 testPart="";
00134 in >> testPart;
00135 if (testPart[0] == ConfigurationDatabase::BEGIN_COMMENT)
00136 {
00137 getline(in, comment);
00138 database.m_comments[lineNumber]=testPart.substr(1, testPart.length()) + comment;
00139 }
00140 else if (testPart[0] == ConfigurationDatabase::BEGIN_HEADER)
00141 {
00142 header=testPart;
00143 header.erase(0, 1);
00144 header.erase(header.length() - 1, 1);
00145 continue;
00146 }
00147 else if (!testPart.empty())
00148 {
00149 key=testPart;
00150 getline(in, value);
00151 if (!value.empty())
00152 {
00153 value.erase(0, 1);
00154 if (header == "")
00155 throw "ERROR: Key-value pair, " + key + "-" + value + ", found without a header!";
00156 database.m_configurations[header][key]=value;
00157 value="";
00158 key="";
00159 }
00160 else
00161 throw "ERROR: Attempted to add a key, " + key + ", without a value!";
00162 }
00163 }
00164 return in;
00165 }
00166
00167 ostream& operator<<(ostream &out, const ConfigurationDatabase &database)
00168 {
00169 ConfigurationDatabase::ConfigurationDatabaseType::const_iterator iterHeader=database.m_configurations.begin();
00170 ConfigurationDatabase::KeyValuePair::const_iterator iterKey;
00171 int lineNumber=0;
00172
00173 while (iterHeader != database.m_configurations.end())
00174 {
00175 if (database.m_comments.find(lineNumber) != database.m_comments.end())
00176 {
00177 out << ConfigurationDatabase::BEGIN_COMMENT << database.m_comments.find(lineNumber)->second << endl;
00178 lineNumber++;
00179 }
00180 else
00181 {
00182 iterKey=iterHeader->second.begin();
00183 out << ConfigurationDatabase::BEGIN_HEADER << iterHeader->first << ConfigurationDatabase::END_HEADER << endl;
00184 lineNumber++;
00185 while (iterKey != iterHeader->second.end())
00186 {
00187 out << iterKey->first << ' ' << iterKey->second << endl;
00188 lineNumber++;
00189 iterKey++;
00190 }
00191 iterHeader++;
00192 out << endl;
00193 }
00194 }
00195 return out;
00196 }