Modernize Settings Class

This commit is contained in:
Unknown 2019-02-22 13:53:23 +01:00
parent 38ef37c48e
commit abd91ab507
2 changed files with 17 additions and 25 deletions

View File

@ -1,30 +1,25 @@
#include "Settings.h"
#include <iostream> #include <iostream>
#include "Settings.h"
static std::string trim(const std::string &s) static std::string trim(const std::string &s)
{ {
size_t start = s.find_first_not_of(" \t\r\n"); const size_t start = s.find_first_not_of(" \t\r\n");
size_t end = s.find_last_not_of(" \t\r\n"); const size_t end = s.find_last_not_of(" \t\r\n");
return s.substr(start, (end-start+1)); return s.substr(start, (end - start + 1));
} }
Settings::Settings(const std::string &filename, const std::string &msgName) Settings::Settings(const std::string &filename, const std::string &msgName)
: m_filename(filename), : m_filename(filename),
m_messageName(msgName) m_messageName(msgName)
{ {
if (m_messageName == "") if (m_messageName.empty())
m_messageName = m_filename; m_messageName = m_filename;
m_file.open(m_filename.c_str(), std::ifstream::in); m_file.open(m_filename, std::ifstream::in);
if (!m_file.is_open()) if (!m_file.is_open())
throw std::runtime_error(std::string("Failed to open file '") + m_messageName + "'"); throw std::runtime_error(std::string("Failed to open file '") + m_messageName + "'");
} }
Settings::~Settings(void)
{
m_file.close();
}
std::string Settings::get(const std::string &key) std::string Settings::get(const std::string &key)
{ {
std::string value; std::string value;
@ -50,9 +45,9 @@ bool Settings::getGeneric(const std::string &key, std::string *pvalue)
bool found = false; bool found = false;
m_file.clear(); m_file.clear();
m_file.seekg(0); m_file.seekg(0);
for (std::getline(m_file,line); m_file.good(); std::getline(m_file,line)) { for (std::getline(m_file, line); m_file.good(); std::getline(m_file, line)) {
linenr++; linenr++;
size_t keylen = line.find_first_of('='); const size_t keylen = line.find_first_of('=');
if (keylen == std::string::npos) { if (keylen == std::string::npos) {
std::cerr << "Error parsing config line at " << m_filename << ":" << linenr << ": expected: <name> = <value> ('=' not found)"; std::cerr << "Error parsing config line at " << m_filename << ":" << linenr << ": expected: <name> = <value> ('=' not found)";
continue; continue;
@ -65,4 +60,3 @@ bool Settings::getGeneric(const std::string &key, std::string *pvalue)
} }
return found; return found;
} }

View File

@ -1,19 +1,19 @@
#pragma once
#ifndef _SETTINGS_H_
#define _SETTINGS_H_
#include <stdexcept>
#include <sstream>
#include <fstream> #include <fstream>
#include <sstream>
#include <stdexcept>
class Settings { class Settings
{
public: public:
Settings(const std::string &filename, const std::string &msgName = ""); Settings(const std::string &filename, const std::string &msgName = "");
~Settings(void); ~Settings() = default;
std::string get(const std::string &key); std::string get(const std::string &key);
std::string get(const std::string &key, const std::string &defaultValue); std::string get(const std::string &key, const std::string &defaultValue);
bool check(const std::string &key) { return getGeneric(key); } bool check(const std::string &key) { return getGeneric(key); }
bool check(const std::string &key, std::string &value) { return getGeneric(key, &value); } bool check(const std::string &key, std::string &value) { return getGeneric(key, &value); }
private: private:
std::string m_filename; std::string m_filename;
std::string m_messageName; std::string m_messageName;
@ -21,5 +21,3 @@ private:
bool getGeneric(const std::string &key, std::string *pvalue = nullptr); bool getGeneric(const std::string &key, std::string *pvalue = nullptr);
}; };
#endif // _SETTINGS_H_