diff --git a/CMakeLists.txt b/CMakeLists.txt index cdf58d6..3970913 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -231,6 +231,7 @@ set(mapper_SRCS TileGenerator.cpp ZlibDecompressor.cpp Color.cpp + Settings.cpp BlockPos.cpp mapper.cpp ) diff --git a/Settings.cpp b/Settings.cpp new file mode 100644 index 0000000..abf8ad2 --- /dev/null +++ b/Settings.cpp @@ -0,0 +1,82 @@ + +#include "Settings.h" + +static std::string trim(const std::string s) +{ + size_t start = s.find_first_not_of(" \t\r\n"); + size_t end = s.find_last_not_of(" \t\r\n"); + return s.substr(start, (end-start+1)); +} + +Settings::Settings(std::string filename, std::string msgName) + : m_filename(filename), + m_messageName(msgName) +{ + if (m_messageName == "") + m_messageName = m_filename; + m_file.open(m_filename.c_str(), std::ifstream::in); + if (!m_file.is_open()) + throw std::runtime_error(std::string("Failed to open file '") + m_messageName + "'"); +} + +Settings::~Settings(void) +{ + m_file.close(); +} + +std::string Settings::get(std::string key) +{ + std::string value; + if (getGeneric(key, &value)) + return value; + else + throw std::runtime_error(std::string("Failed to read key '") + key + "' from file '" + m_messageName + "'"); +} + +std::string Settings::get(std::string key, std::string defaultValue) +{ + std::string value; + if (getGeneric(key, &value)) + return value; + else + return defaultValue; +} + +bool Settings::getGeneric(std::string key, std::string *pvalue) +{ + std::string line; + int linenr = 0; + bool found = false; + m_file.clear(); + m_file.seekg(0); + for (std::getline(m_file,line); m_file.good(); std::getline(m_file,line)) { + linenr++; + std::istringstream iline; + iline.str(line); + iline >> std::skipws; + std::string variable; + std::string eq; + iline >> variable; + if (variable != key) + continue; + found = true; + iline >> eq; + if (m_file.fail() || eq != "=") { + std::ostringstream oss; + oss << "Error parsing '" << key << "' in file " << m_messageName << " at line " << linenr << " (missing '=')"; + throw std::runtime_error(oss.str()); + } + if (pvalue) { + std::string value; + iline >> value; + if (m_file.fail()) { + std::ostringstream oss; + oss << "Error parsing value for '" << key << "' in file " << m_messageName << " at line " << linenr; + throw std::runtime_error(oss.str()); + } + *pvalue = trim(value); + } + } + return found; +} + diff --git a/Settings.h b/Settings.h new file mode 100644 index 0000000..e8beade --- /dev/null +++ b/Settings.h @@ -0,0 +1,25 @@ + +#ifndef _SETTINGS_H_ +#define _SETTINGS_H_ + +#include +#include +#include + +class Settings { +public: + Settings(std::string filename, std::string msgName = ""); + ~Settings(void); + std::string get(std::string key); + std::string get(std::string key, std::string defaultValue); + bool check(std::string key) { return getGeneric(key); } + bool check(std::string key, std::string &value) { return getGeneric(key, &value); } +private: + std::string m_filename; + std::string m_messageName; + std::ifstream m_file; + + bool getGeneric(std::string key, std::string *pvalue = NULL); +}; + +#endif // _SETTINGS_H_