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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
#include "pom.hpp"
#include <pom.h>
#include <cstring>
#include <cstdlib>
#include <string>
#include <istream>
#include <iostream>
namespace pom {
Configuration::~Configuration() {
pom_conf_free(static_cast<pom_conf *>(C));
}
Configuration::Configuration(): C(nullptr) {}
Error::~Error() { if (m_is_original) free(C); }
Error::Error(void *C_error): C(C_error), m_is_original(true) {
const pom_error *next = pom_error_next(static_cast<const pom_error *>(C));
if (next)
m_next = std::unique_ptr<Error>(new Error(static_cast<const void *>(next)));
}
// const_cast here to avoid creating a separate ConstError type
Error::Error(const void *C_error): C(const_cast<void *>(C_error)), m_is_original(false) {
const pom_error *next = pom_error_next(static_cast<const pom_error *>(C));
if (next)
m_next = std::unique_ptr<Error>(new Error(static_cast<const void *>(next)));
}
std::string_view Error::file() const noexcept {
return pom_error_file(static_cast<const pom_error *>(C));
}
uint64_t Error::line() const noexcept {
return pom_error_line(static_cast<const pom_error *>(C));
}
std::string_view Error::message() const noexcept {
return pom_error_message(static_cast<const pom_error *>(C));
}
const char *Error::what() const noexcept {
return m_is_original ? pom_error_to_string(static_cast<pom_error *>(C))
: pom_error_message(static_cast<const pom_error *>(C));
}
std::string_view Error::to_string() noexcept {
return what();
}
std::ostream &operator<<(std::ostream &o, Error &e) {
return o << e.to_string();
}
Configuration Configuration::section(std::string_view name) const {
if (!C) return {};
std::string name_str(name);
const pom_conf *C_section = pom_conf_section(
static_cast<const pom_conf *>(C), name_str.c_str());
pom_conf *C_section_copy = pom_conf_copy(C_section);
return Configuration(static_cast<void *>(C_section_copy));
}
void Settings::set_error_language(std::string_view lang) {
size_t len = std::min(sizeof m_error_lang - 1, lang.size());
memcpy(m_error_lang, lang.data(), len);
m_error_lang[len] = 0;
}
void Settings::to_C(void *C) const {
pom_settings &C_settings = *static_cast<pom_settings *>(C);
strcpy(C_settings.error_lang, m_error_lang);
}
static size_t readable_read(void *udata, char *buf, size_t count) {
return static_cast<Reader *>(udata)->read(buf, count);
}
void Configuration::load(std::string_view filename, Reader &source, const Settings *settings) {
pom_settings C_settings = {};
if (settings) {
settings->to_C(static_cast<void *>(&C_settings));
}
pom_error *error;
std::string filename_str(filename);
C = pom_load(&C_settings, filename_str.c_str(), readable_read, &source, &error);
if (error) {
throw Error(static_cast<void *>(error));
}
}
Configuration::Configuration(std::string_view filename, Reader &source, const Settings *settings) {
load(filename, source, settings);
}
Configuration::Configuration(std::string_view path, const Settings *settings) {
pom_settings C_settings = {};
if (settings) {
settings->to_C(static_cast<void *>(&C_settings));
}
pom_error *error;
std::string path_str(path);
C = pom_load_path(&C_settings, path_str.c_str(), &error);
if (error) {
throw Error(static_cast<void *>(error));
}
}
class StringViewReader: public Reader {
public:
explicit StringViewReader(std::string_view v): string(v) {}
virtual size_t read(char *buf, size_t count) {
count = std::min(count, string.size());
memcpy(buf, string.data(), count);
return count;
}
private:
std::string_view string;
};
Configuration::Configuration(std::string_view filename, std::string_view string, const Settings *settings) {
StringViewReader reader(string);
load(filename, reader, settings);
}
class IStreamReader: public Reader {
public:
explicit IStreamReader(std::istream &stream): ifstream(stream) {}
virtual size_t read(char *buf, size_t count) {
ifstream.read(buf, count);
return ifstream.gcount();
}
private:
std::istream &ifstream;
};
Configuration::Configuration(std::string_view filename, std::istream &stream, const Settings *settings) {
IStreamReader reader(stream);
load(filename, reader, settings);
}
void Configuration::merge(const Configuration &other) {
if (!other.C) return;
if (!C) {
C = pom_conf_copy(static_cast<const pom_conf *>(other.C));
return;
}
pom_conf_merge(static_cast<pom_conf *>(C), static_cast<const pom_conf *>(other.C));
}
std::optional<std::string> Configuration::get(std::string_view key) const {
if (!C) return {};
std::string key_str(key);
const char *value = pom_conf_get(static_cast<const pom_conf *>(C), key_str.c_str());
if (!value)
return {};
else
return value;
}
std::string Configuration::get_or_default(std::string_view key, std::string_view dflt) const {
auto value = get(key);
if (value.has_value())
return value.value();
else
return std::string(dflt);
}
std::vector<std::string> Configuration::unread_keys() const {
if (!C) return {};
std::vector<std::string> unread;
pom_unread_key_iter *iter = nullptr;
const char *key;
while ((key = pom_conf_next_unread_key(static_cast<const pom_conf *>(C), &iter))) {
unread.push_back(key);
}
return unread;
}
std::vector<std::string> Configuration::keys() const {
if (!C) return {};
std::vector<std::string> keys;
pom_key_iter *iter = nullptr;
const char *key;
while ((key = pom_conf_next_key(static_cast<const pom_conf *>(C), &iter))) {
keys.push_back(key);
}
return keys;
}
std::vector<std::unique_ptr<Item>> Configuration::items() const {
if (!C) return {};
std::vector<std::unique_ptr<Item>> items;
pom_item_iter *iter = nullptr;
const pom_item *item;
while ((item = pom_conf_next_item(static_cast<const pom_conf *>(C), &iter))) {
items.push_back(std::unique_ptr<Item>(new Item(item->key, item->value, item->file, item->line)));
}
return items;
}
std::optional<int64_t> Configuration::get_int(std::string_view key) const {
if (!C) return {};
std::string key_str(key);
if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) {
return {};
}
int64_t value;
pom_error *error = pom_conf_get_int(static_cast<const pom_conf *>(C), key_str.c_str(), &value);
if (error)
throw Error(error);
return value;
}
int64_t Configuration::get_int_or_default(std::string_view key, int64_t dflt) const {
return get_int(key).value_or(dflt);
}
std::optional<uint64_t> Configuration::get_uint(std::string_view key) const {
if (!C) return {};
std::string key_str(key);
if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) {
return {};
}
uint64_t value;
pom_error *error = pom_conf_get_uint(static_cast<const pom_conf *>(C), key_str.c_str(), &value);
if (error)
throw Error(error);
return value;
}
uint64_t Configuration::get_uint_or_default(std::string_view key, uint64_t dflt) const {
return get_uint(key).value_or(dflt);
}
std::optional<double> Configuration::get_float(std::string_view key) const {
if (!C) return {};
std::string key_str(key);
if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) {
return {};
}
double value;
pom_error *error = pom_conf_get_float(static_cast<const pom_conf *>(C), key_str.c_str(), &value);
if (error)
throw Error(error);
return value;
}
double Configuration::get_float_or_default(std::string_view key, double dflt) const {
return get_float(key).value_or(dflt);
}
std::optional<bool> Configuration::get_bool(std::string_view key) const {
if (!C) return {};
std::string key_str(key);
if (!pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str())) {
return {};
}
bool value;
pom_error *error = pom_conf_get_bool(static_cast<const pom_conf *>(C), key_str.c_str(), &value);
if (error)
throw Error(error);
return value;
}
bool Configuration::get_bool_or_default(std::string_view key, bool dflt) const {
return get_bool(key).value_or(dflt);
}
std::optional<std::vector<std::string>> Configuration::get_list(std::string_view key) const {
if (!C) return {};
std::string key_str(key);
char **list = pom_conf_get_list(static_cast<const pom_conf *>(C), key_str.c_str());
std::vector<std::string> vec;
for (size_t i = 0; list[i]; i++)
vec.emplace_back(list[i]);
free(list);
return vec;
}
std::vector<std::string> Configuration::get_list_or_default(std::string_view key, const std::vector<std::string> &dflt) const {
auto list = get_list(key);
if (list.has_value())
return list.value();
else
return dflt;
}
Configuration &Configuration::operator=(const Configuration &other) {
pom_conf_free(static_cast<pom_conf *>(C));
C = static_cast<void *>(pom_conf_copy(static_cast<const pom_conf *>(other.C)));
return *this;
}
std::ostream &operator<<(std::ostream &o, const Configuration &conf) {
for (const auto &item: conf.items())
o << item->key() << ": " << item->value() << "\n";
return o;
}
std::optional<Location> Configuration::location(std::string_view key) const {
if (!C) return {};
const char *file;
uint64_t line;
std::string key_str(key);
if (pom_conf_location(static_cast<const pom_conf *>(C), key_str.c_str(), &file, &line))
return Location(file, line);
else
return {};
}
bool Configuration::has(std::string_view key) const {
if (!C) return false;
std::string key_str(key);
return pom_conf_has(static_cast<const pom_conf *>(C), key_str.c_str());
}
std::ostream &operator<<(std::ostream &o, const Location &location) {
return o << location.file() << ":" << location.line();
}
} // namespace pom
|