Modernize Settings Class
This commit is contained in:
parent
38ef37c48e
commit
abd91ab507
@ -1,11 +1,11 @@
|
||||
#include "Settings.h"
|
||||
|
||||
#include <iostream>
|
||||
#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");
|
||||
const size_t start = s.find_first_not_of(" \t\r\n");
|
||||
const size_t end = s.find_last_not_of(" \t\r\n");
|
||||
return s.substr(start, (end - start + 1));
|
||||
}
|
||||
|
||||
@ -13,18 +13,13 @@ Settings::Settings(const std::string &filename, const std::string &msgName)
|
||||
: m_filename(filename),
|
||||
m_messageName(msgName)
|
||||
{
|
||||
if (m_messageName == "")
|
||||
if (m_messageName.empty())
|
||||
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())
|
||||
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 value;
|
||||
@ -52,7 +47,7 @@ bool Settings::getGeneric(const std::string &key, std::string *pvalue)
|
||||
m_file.seekg(0);
|
||||
for (std::getline(m_file, line); m_file.good(); std::getline(m_file, line)) {
|
||||
linenr++;
|
||||
size_t keylen = line.find_first_of('=');
|
||||
const size_t keylen = line.find_first_of('=');
|
||||
if (keylen == std::string::npos) {
|
||||
std::cerr << "Error parsing config line at " << m_filename << ":" << linenr << ": expected: <name> = <value> ('=' not found)";
|
||||
continue;
|
||||
@ -65,4 +60,3 @@ bool Settings::getGeneric(const std::string &key, std::string *pvalue)
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef _SETTINGS_H_
|
||||
#define _SETTINGS_H_
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
class Settings {
|
||||
class Settings
|
||||
{
|
||||
public:
|
||||
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, const std::string &defaultValue);
|
||||
bool check(const std::string &key) { return getGeneric(key); }
|
||||
bool check(const std::string &key, std::string &value) { return getGeneric(key, &value); }
|
||||
|
||||
private:
|
||||
std::string m_filename;
|
||||
std::string m_messageName;
|
||||
@ -21,5 +21,3 @@ private:
|
||||
|
||||
bool getGeneric(const std::string &key, std::string *pvalue = nullptr);
|
||||
};
|
||||
|
||||
#endif // _SETTINGS_H_
|
||||
|
Loading…
x
Reference in New Issue
Block a user