31 #ifndef MAIN_CONFIGFILE_HPP_ 32 #define MAIN_CONFIGFILE_HPP_ 36 #include <boost/core/typeinfo.hpp> 37 #include <boost/lexical_cast.hpp> 70 bool getValue(
const std::string& name, std::string& to)
const;
91 template<
typename T>
bool getValue(
const std::string& name, T& to)
const {
94 if(this->
getValue(name, result) && !result.empty()) {
96 to = boost::lexical_cast<T>(result);
100 catch(
const boost::bad_lexical_cast& e) {
103 " Could not convert config file entry \"" + name +
"\"" 104 " (=\"" + result +
"\")" 105 " to " + boost::core::demangled_name(
typeid(T))
120 std::vector<std::pair<std::string, std::string>>
entries;
124 std::string fileName;
141 std::ifstream fileStream(name);
144 if(fileStream.is_open()) {
145 while(std::getline(fileStream, line)) {
146 const auto nameEnd{line.find(
'=')};
148 if(nameEnd < line.length()) {
149 std::string nameInLine(line, 0, nameEnd);
156 return std::tolower(c);
162 line.substr(nameEnd + 1)
176 throw Exception(
"Could not open \"" + name +
"\" for reading");
195 std::string nameCopy(name);
202 return std::tolower(c);
210 [&nameCopy](
const auto& entry) {
211 return entry.first == nameCopy;
216 if(valueIt != this->
entries.cend()) {
217 to = valueIt->second;
ConfigFile(const std::string &name)
Constructor reading the file.
Definition: ConfigFile.hpp:140
std::vector< std::pair< std::string, std::string > > entries
Definition: ConfigFile.hpp:120
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
Configuration file.
Definition: ConfigFile.hpp:58
bool getValue(const std::string &name, std::string &to) const
Gets the string value of a configuration entry.
Definition: ConfigFile.hpp:194
Class for configuration file exceptions.
Definition: ConfigFile.hpp:116
Namespace for the main classes of the program.
Definition: App.cpp:34
bool getValue(const std::string &name, T &to) const
Gets the converted value of a configuration entry.
Definition: ConfigFile.hpp:91