configParser.hpp
00001
00008 #ifndef CONFIG_PARSER
00009 #define CONFIG_PARSER
00010
00011 class Convert
00012 {
00013 public:
00014 template <typename T>
00015 static std::string T_to_string(T const &val)
00016 {
00017 std::ostringstream ostr;
00018 ostr << val;
00019
00020 return ostr.str();
00021 }
00022
00023 template <typename T>
00024 static T string_to_T(std::string const &val)
00025 {
00026 std::istringstream istr(val);
00027 T returnVal;
00028 if (!(istr >> returnVal))
00029 {
00030 exitWithError("CFG: Not a valid " + (std::string)typeid(T).name() + " received!\n");
00031
00032 }
00033
00034 return returnVal;
00035 }
00036
00037
00038
00039
00040
00041
00042 };
00043
00044 template <>
00045 std::string Convert::string_to_T<std::string>(std::string const &val);
00046
00047 class ConfigFile
00048 {
00049 private:
00050 std::map<std::string, std::string> contents;
00051 std::string fName;
00052
00053 void removeComment(std::string &line) const
00054 {
00055 if (line.find(';') != line.npos)
00056 line.erase(line.find(';'));
00057 }
00058
00059 bool onlyWhitespace(const std::string &line) const
00060 {
00061 return (line.find_first_not_of(' ') == line.npos);
00062 }
00063 bool validLine(const std::string &line) const
00064 {
00065 std::string temp = line;
00066 temp.erase(0, temp.find_first_not_of("\t "));
00067 if (temp[0] == '=')
00068 return false;
00069
00070 for (size_t i = temp.find('=') + 1; i < temp.length(); i++)
00071 if (temp[i] != ' ')
00072 return true;
00073
00074 return false;
00075 }
00076
00077 void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
00078 {
00079 key = line.substr(0, sepPos);
00080 if (key.find('\t') != line.npos || key.find(' ') != line.npos)
00081 key.erase(key.find_first_of("\t "));
00082 }
00083 void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
00084 {
00085 value = line.substr(sepPos + 1);
00086 value.erase(0, value.find_first_not_of("\t "));
00087 value.erase(value.find_last_not_of("\t ") + 1);
00088 }
00089
00090 void extractContents(const std::string &line)
00091 {
00092 std::string temp = line;
00093 temp.erase(0, temp.find_first_not_of("\t "));
00094 size_t sepPos = temp.find('=');
00095
00096 std::string key, value;
00097 extractKey(key, sepPos, temp);
00098 extractValue(value, sepPos, temp);
00099
00100 if (!keyExists(key))
00101 contents.insert(std::pair<std::string, std::string>(key, value));
00102 else
00103 exitWithError("CFG: Can only have unique key names!\n");
00104 }
00105
00106 void parseLine(const std::string &line, size_t const lineNo)
00107 {
00108 if (line.find('=') == line.npos)
00109 exitWithError("CFG: Couldn't find separator on line: " + Convert::T_to_string(lineNo) + "\n");
00110
00111 if (!validLine(line))
00112 exitWithError("CFG: Bad format for line: " + Convert::T_to_string(lineNo) + "\n");
00113
00114 extractContents(line);
00115 }
00116
00117 void ExtractKeys()
00118 {
00119 std::ifstream file;
00120 file.open(fName.c_str());
00121 if (!file)
00122 exitWithError("CFG: File " + fName + " couldn't be found!\n");
00123
00124 std::string line;
00125 size_t lineNo = 0;
00126 while (std::getline(file, line))
00127 {
00128 lineNo++;
00129 std::string temp = line;
00130
00131 if (temp.empty())
00132 continue;
00133
00134 removeComment(temp);
00135 if (onlyWhitespace(temp))
00136 continue;
00137
00138 parseLine(temp, lineNo);
00139 }
00140
00141 file.close();
00142 }
00143 public:
00144 ConfigFile(const std::string &fName)
00145 {
00146 this->fName = fName;
00147 ExtractKeys();
00148 }
00149
00150 bool keyExists(const std::string &key) const
00151 {
00152 return contents.find(key) != contents.end();
00153 }
00154
00155 template <typename ValueType>
00156 ValueType getValueOfKey(const std::string &key, ValueType const &defaultValue = ValueType()) const
00157 {
00158 if (!keyExists(key))
00159 return defaultValue;
00160
00161 return Convert::string_to_T<ValueType>(contents.find(key)->second);
00162 }
00163 };
00164
00165
00166
00167 #endif