From a6dfc269d93cdf557f6dac62b03b886d694faecd Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 4 Jun 2015 00:22:57 +0200 Subject: Add config and change all commands to one --- src/config.cc | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/config.cc (limited to 'src/config.cc') diff --git a/src/config.cc b/src/config.cc new file mode 100644 index 0000000..516ccde --- /dev/null +++ b/src/config.cc @@ -0,0 +1,55 @@ +#include "common.hh" + +#include +#include + +#include "config.hh" +#include "strutils.hh" + +namespace stuff { + +namespace { + +class ConfigImpl : public Config { +public: + ~ConfigImpl() override { + } + + std::string get(const std::string& name, + const std::string& fallback) const override { + auto it = data_.find(name); + if (it == data_.end()) return fallback; + return it->second; + } + + bool load(const std::string& path) override { + std::ifstream in(path); + std::unordered_map data; + while (in.good()) { + std::string line; + std::getline(in, line); + if (line.empty() || line.front() == '#') continue; + auto pos = line.find('='); + if (pos == std::string::npos) return false; + data.insert(std::make_pair(trim(line.substr(0, pos)), + trim(line.substr(pos + 1)))); + } + if (!in.eof()) return false; + data_.swap(data); + return true; + } + + ConfigImpl() { + } + +private: + std::unordered_map data_; +}; + +} // namespace + +std::unique_ptr Config::create() { + return std::unique_ptr(new ConfigImpl()); +} + +} // namespace stuff -- cgit v1.2.3-70-g09d2