8#ifndef CONFIGPARSER_HPP
9#define CONFIGPARSER_HPP
34 std::istringstream is(val);
36 if (!(is >> Val))
throw std::runtime_error(
"CFG: invalid " + (std::string)
typeid(T).name() +
"\n");
43 std::istringstream iss(val);
47 if (iss >> c && c ==
'[')
50 if (iss && iss >> c && c ==
']' && !(iss >> c))
61 std::map<std::string, std::string>
data;
69 if (line.find(
'#') != line.npos) line.erase(line.find(
'#'));
77 return (line.find_first_not_of(
' ') == line.npos);
83 void extractKey(std::string &key,
size_t const &sepPos,
const std::string &line)
const
85 key = line.substr(0, sepPos);
86 if (key.find(
'\t') != line.npos || key.find(
' ') != line.npos)
87 key.erase(key.find_first_of(
"\t "));
92 void extractValue(std::string &value,
size_t const &sepPos,
const std::string &line)
const
94 value = line.substr(sepPos + 1);
95 value.erase(0, value.find_first_not_of(
"\t "));
96 value.erase(value.find_last_not_of(
"\t ") + 1);
102 void parseLine(
const std::string &line,
size_t const lineNum)
104 std::string temp = line;
105 temp.erase(0, temp.find_first_not_of(
"\t "));
106 size_t sepPos = temp.find(
'=');
107 std::string key, value;
112 data.insert(std::pair<std::string, std::string>(key, value));
114 throw std::runtime_error(
"CFG: only unique key names are allowed!\n");
129 while (std::getline(file, line))
132 std::string temp = line;
133 if (temp.empty())
continue;
148 bool getKey(
const std::string &key)
const
150 return data.find(key) !=
data.end();
155 template <
typename scalarT>
156 scalarT
getValue(
const std::string &key, scalarT
const &defaultValue = scalarT())
const
158 if (!
getKey(key))
return defaultValue;
a wrapper class which contains functions for parsing the configuration file end class configParser
void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
a function that extracts the value from the pair of key = value
bool whitespace(const std::string &line) const
a function that returns false if a non-space character was found, true otherwise. The function is "co...
scalarT getValue(const std::string &key, scalarT const &defaultValue=scalarT()) const
a function that retrieves the value of a specific key
std::map< std::string, std::string > data
which will hold pairs of key-value
bool getKey(const std::string &key) const
a function for finding the key
ConfigParser(const std::string &fileName)
a class to set the name of the configuration file and extracts and parses the data
void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
a function that extracts the key from the pair of key = value
std::string fileName
As member variables, we will only have a std::string, which will hold the name of the configuration f...
void parseLine(const std::string &line, size_t const lineNum)
a function that parse the line by calling above mentioed functions
void removeComment(std::string &line) const
a function that removes everything from the semicolon (including it) to the end of the line.
a wrapper class which contain function for the conversion of std::string to primitive types (int,...
static std::vector< T > stringTOArray(std::string const &val)
static T stringTOScalarT(std::string const &val)
#define fileopen(myfile, filename)
open an open file
#define fileclose(myfile, filename)
close an open file
it parses the user-defined configuration file (.cfg)