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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// -*- mode: c++; c-basic-offset: 2; -*-
#include "common.hh"
#include "config.hh"
#include "paths.hh"
#include "strings.hh"
#include "xdg.hh"
#include <cstring>
#include <fstream>
#include <sstream>
#include <unordered_map>
namespace {
class ConfigImpl : public Config {
public:
ConfigImpl()
: good_(true) {
}
bool load_name(std::string const& name) override {
name_ = name;
auto dirs = XDG::config_dirs();
bool good = true;
std::string error;
std::unordered_map<std::string,std::string> data;
for (auto& dir : dirs) {
std::string tmp_error;
std::unordered_map<std::string,std::string> tmp_data;
if (load_file(Paths::join(dir, name_), &tmp_data, &tmp_error, false)) {
data.insert(tmp_data.begin(), tmp_data.end());
} else {
if (good) {
// We want the most import error
error = tmp_error;
good = false;
}
}
}
return update(good, error, data);
}
bool load_file(std::string const& filename) override {
std::string error;
std::unordered_map<std::string,std::string> data;
bool good = load_file(filename, &data, &error, true);
return update(good, error, data);
}
bool good() const override {
return good_;
}
std::string const& last_error() const override {
return last_error_;
}
std::string const& get(std::string const& key,
std::string const& fallback) override {
auto i = override_.find(key);
if (i != override_.end()) return i->second;
i = data_.find(key);
if (i != data_.end()) return i->second;
return fallback;
}
char const* get(std::string const& key, char const* fallback) override {
auto i = override_.find(key);
if (i != override_.end()) return i->second.c_str();
i = data_.find(key);
if (i != data_.end()) return i->second.c_str();
return fallback;
}
bool is_set(std::string const& key) override {
return override_.count(key) > 0 || data_.count(key) > 0;
}
bool get(std::string const& key, bool fallback) override {
auto ret = get(key, nullptr);
if (!ret) return fallback;
return strcmp(ret, "true") == 0;
}
void set(std::string const& key, std::string const& value) override {
auto ret = override_.insert(std::make_pair(key, value));
if (!ret.second) {
ret.first->second = value;
}
}
void remove(std::string const& key) override {
override_.erase(key);
}
private:
bool update(bool good, std::string const& last_error,
std::unordered_map<std::string, std::string>& data) {
good_ = good;
if (!good_) {
last_error_ = last_error;
} else {
data_.clear();
data_.swap(data);
}
return good_;
}
static bool load_file(std::string const& filename,
std::unordered_map<std::string, std::string>* data,
std::string* error, bool should_exist) {
bool good = true;
data->clear();
error->clear();
std::ifstream in(filename);
if (!in) {
if (should_exist) {
std::stringstream ss;
ss << "Unable to read: " << filename;
*error = ss.str();
good = false;
}
return good;
}
std::string line;
uint32_t count = 0;
std::string key, value;
while (std::getline(in, line)) {
count++;
if (line.empty() || line[0] == '#') continue;
auto idx = line.find('=');
if (idx == 0 || idx == std::string::npos) {
std::stringstream ss;
if (idx == 0) {
ss << filename << ':' << count << ": Invalid line, starts with '='";
} else {
ss << filename << ':' << count << ": Invalid line, no '=' found";
}
*error = ss.str();
good = false;
break;
}
size_t start = 0, end = idx;
key.assign(Strings::trim(line, start, end));
if (data->count(key) > 0) {
std::stringstream ss;
ss << filename << ':' << count << ": '" << key << "' is already set";
*error = ss.str();
good = false;
break;
}
start = idx + 1;
end = line.size();
Strings::trim(line, &start, &end);
if (line[start] == '"' && line[end - 1] == '"') {
value.assign(Strings::unquote(line, start, end));
} else {
value.assign(line.substr(start, end - start));
}
(*data)[key] = value;
}
if (good && in.bad()) {
std::stringstream ss;
ss << filename << ": I/O error: " << strerror(errno);
*error = ss.str();
good = false;
}
if (!good) data->clear();
return good;
}
bool good_;
std::string name_;
std::string last_error_;
std::unordered_map<std::string, std::string> data_;
std::unordered_map<std::string, std::string> override_;
};
} // namespace
// static
Config* Config::create() {
return new ConfigImpl();
}
|