Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

configurationdatabase.cpp

Go to the documentation of this file.
00001 /***************************************************************************************************
00002 *****           Copyright (C) 2005 John Schneiderman <JohnMS@member.fsf.org>                   *****
00003 *****                                                                                          *****
00004 *****           This program is free software; you can redistribute it and/or modify           *****
00005 *****           it under the terms of the GNU General Public License as published by           *****
00006 *****           the Free Software Foundation; either version 2 of the License, or              *****
00007 *****           (at your option) any later version.                                            *****
00008 *****                                                                                          *****
00009 *****           This program is distributed in the hope that it will be useful,                *****
00010 *****           but WITHOUT ANY WARRANTY; without even the implied warranty of                 *****
00011 *****           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  *****
00012 *****           GNU General Public License for more details.                                   *****
00013 *****                                                                                          *****
00014 *****           You should have received a copy of the GNU General Public License              *****
00015 *****           along with this program; if not, write to the Free Software                    *****
00016 *****           Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA     *****
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); //Get the comment
00138             database.m_comments[lineNumber]=testPart.substr(1, testPart.length()) + comment; //Add it to our comment list.
00139         }
00140         else if (testPart[0] == ConfigurationDatabase::BEGIN_HEADER)
00141         {
00142             header=testPart; // testKey is the header
00143             header.erase(0, 1); //Erase the [
00144             header.erase(header.length() - 1, 1); //Erase the ]
00145             continue;
00146         }
00147         else if (!testPart.empty())
00148         {
00149             key=testPart;
00150             getline(in, value); //It must be a key 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()) //Write out comment
00176         {
00177             out << ConfigurationDatabase::BEGIN_COMMENT << database.m_comments.find(lineNumber)->second << endl;
00178             lineNumber++;
00179         }
00180         else //Write out header's key-values
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()) //Write out key-value pair
00186             {
00187                 out << iterKey->first << ' ' << iterKey->second << endl;
00188                 lineNumber++;
00189                 iterKey++;
00190             }
00191             iterHeader++;
00192             out << endl;
00193         }
00194     }
00195     return out;
00196 }

Generated on Tue Mar 28 23:28:03 2006 for ClusterSim by  doxygen 1.4.4