summaryrefslogtreecommitdiff
path: root/src/config.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2015-06-04 00:22:57 +0200
committerJoel Klinghed <the_jk@yahoo.com>2015-06-04 00:22:57 +0200
commita6dfc269d93cdf557f6dac62b03b886d694faecd (patch)
tree8f9c04a4200a99fc4e986f1b0b23808040580011 /src/config.cc
parent052a162715449252bb6126c41dd1700b1440c394 (diff)
Add config and change all commands to one
Diffstat (limited to 'src/config.cc')
-rw-r--r--src/config.cc55
1 files changed, 55 insertions, 0 deletions
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 <fstream>
+#include <unordered_map>
+
+#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<std::string, std::string> 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<std::string, std::string> data_;
+};
+
+} // namespace
+
+std::unique_ptr<Config> Config::create() {
+ return std::unique_ptr<Config>(new ConfigImpl());
+}
+
+} // namespace stuff