blob: 516ccde4a49264b6a9a28a3e74ca207ef951fba1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
|