-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_ini.hpp
321 lines (272 loc) · 12.4 KB
/
utils_ini.hpp
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
#ifndef UTILS_INI_HPP
#define UTILS_INI_HPP
#include "utils_compiler.hpp"
#include "utils_memory.hpp"
#include "utils_io.hpp"
#include "utils_string.hpp"
#include "utils_print.hpp"
#include "utils_misc.hpp"
#include "utils_traits.hpp"
#include <map>
#include <variant>
#include <sstream>
namespace utils::ini {
struct delimiters {
static inline constexpr utils::print::delimiters_values<char> values = { "[", " = ", "]" };
};
template<typename TDelimiters = utils::ini::delimiters>
class ConfigReader {
public:
using Value = std::variant<int, size_t, float, double, std::string, bool>;
using Contents = std::map<std::string, Value>;
private:
bool read_from_file;
const std::string filename;
std::stringstream inifile;
std::map<std::string, Contents> settings_map;
void parse() {
const size_t val_delim_size = std::strlen(TDelimiters::values.delimiter);
std::string current_section = "";
for (std::string line; std::getline(this->inifile, line); ) {
utils::string::ltrim(line);
// Continue if too small or comment
if (line.size() < 2 || line[0] == '#' || line[0] == ';') continue;
if (const auto left_delim = utils::string::contains(line, TDelimiters::values.prefix);
left_delim && *left_delim == 0)
{
if (const auto right_delim = utils::string::contains(line, TDelimiters::values.postfix, *left_delim + 1);
right_delim)
{
// Found both delimiters, add section
current_section = line.substr(*left_delim + 1, *right_delim - 1);
utils::string::trim(current_section);
this->settings_map.emplace(current_section, Contents());
}
} else if (current_section.size() > 0) {
// Entered mapped settings
if (const auto val_delim = utils::string::contains(line, TDelimiters::values.delimiter);
val_delim)
{
std::string val = line.substr(*val_delim + val_delim_size);
utils::string::rtrim(val);
this->settings_map[current_section]
.emplace(line.substr(0, *val_delim), val);
}
}
}
}
public:
ConfigReader()
: read_from_file(false)
, filename("")
{
// Empty
}
ConfigReader(const std::string_view filename)
: read_from_file(true)
, filename(filename)
{
try {
auto contents = utils::io::file_to_string(this->filename);
this->inifile << *contents;
this->parse();
} CATCH_AND_LOG_ERROR_TRACE(read_from_file = false)
}
ConfigReader(const std::istream& input)
: read_from_file(false)
, filename("")
{
this->inifile << input.rdbuf();
this->parse();
}
ConfigReader(ConfigReader&&) = delete;
void save(std::string name = "") {
if (name == "") {
if (this->read_from_file) {
name = this->filename;
} else {
throw utils::exceptions::FileWriteException(name);
}
}
// MAP TO STRING
std::stringstream().swap(this->inifile);
this->inifile << *this;
try {
utils::io::string_to_file(name, this->inifile.str());
} CATCH_AND_LOG_ERROR_TRACE()
}
template<class F>
void ForEachSection(F&& callback) {
static_assert(utils::traits::is_invocable_v<F, const std::string&>,
"ForEachSection: Callable function required.");
for (const auto& [section, mapped_values] : this->settings_map) {
UNUSED(mapped_values);
std::invoke(std::forward<F>(callback), section);
}
}
template<class F>
void ForEachSectionKey(const std::string& section, F&& callback) {
static_assert(utils::traits::is_invocable_v<F, const std::string&, const Value&>,
"ForEachSectionKey: Callable function required.");
if (const auto it = this->settings_map.find(section);
it != this->settings_map.end())
{
for (const auto& [key, value] : it->second) {
std::invoke(std::forward<F>(callback), key, value);
}
}
}
inline size_t SectionSize() const {
return this->settings_map.size();
}
inline size_t SectionKeySize(const std::string& section) const {
return this->hasSection(section)
? this->settings_map.at(section).size()
: 0;
}
inline bool hasSection(const std::string& section) const {
return this->settings_map.count(section) == 1;
}
inline bool operator[](const std::string& section) const {
return this->hasSection(section);
}
inline bool hasSectionKey(const std::string& section, const std::string& key) const {
return this->hasSection(section)
&& this->settings_map.at(section).count(key) == 1;
}
inline bool operator()(const std::string& section, const std::string& key) const {
return this->hasSectionKey(section, key);
}
void CreateSection(std::string section) {
utils::string::trim(section);
if (const auto it = this->settings_map.find(section);
it == this->settings_map.end())
{
// Does not exist yet
this->settings_map.emplace(section, Contents());
}
}
void RemoveSection(const std::string& section) {
if (const auto it = this->settings_map.find(section);
it != this->settings_map.end())
{
// Exists
this->settings_map.erase(it);
}
}
void CreateSectionKey(std::string section, std::string key) {
utils::string::trim(section);
utils::string::trim(key);
if (const auto it = this->settings_map.find(section);
it == this->settings_map.end())
{
// Does not exist yet
this->settings_map.emplace(section, Contents());
}
this->settings_map.at(section).emplace(key, Value());
}
void RemoveSectionKey(const std::string& section, const std::string& key, bool delete_section_if_empty = false) {
if (const auto it = this->settings_map.find(section);
it != this->settings_map.end())
{
// Exists
it->second.erase(key);
}
if (delete_section_if_empty && this->SectionKeySize(section) == 0) {
this->RemoveSection(section);
}
}
template<
typename T,
typename std::enable_if<utils::traits::is_variant_member_v<T, Value>, int>::type = 0
> void SetValue(std::string section, std::string key, T&& val) {
utils::string::trim(section);
utils::string::trim(key);
if (const auto it = this->settings_map.find(section);
it == this->settings_map.end())
{
// Does not exist yet
this->settings_map.emplace(section, Contents());
}
if (const auto it = this->settings_map.at(section).find(key);
it == this->settings_map.at(section).end())
{
// Does not exist yet
this->settings_map.at(section).emplace(key, Value(val));
} else {
this->settings_map.at(section).at(key) = Value(val);
}
}
template<
typename T,
typename std::enable_if<utils::traits::is_variant_member_v<T, Value>, int>::type = 0
> T GetValue(const std::string& section, const std::string& key, const T default_value = T()) const {
if (const auto it = this->settings_map.find(section);
it != this->settings_map.end())
{
if (const auto sett_it = it->second.find(key);
sett_it != it->second.end())
{
// map[section][key] exists
const auto& val = this->settings_map.at(section).at(key);
if (std::holds_alternative<T>(val)) {
// T is available
return std::get<T>(val);
} else {
std::stringstream ss;
ss << val;
// Print to string or cast to value
if constexpr (std::is_same_v<T, std::string>) {
return ss.str();
} else if constexpr (std::is_same_v<T, bool>) {
std::string str = utils::string::to_lowercase(ss.str());
if (str == "true" || str == "yes"
|| str == "on" || str == "1")
{
return true;
}
if (str == "false" || str == "no"
|| str == "off" || str == "0")
{
return false;
}
} else {
return utils::misc::try_lexical_cast<T>(ss.str().c_str()).value_or(default_value);
}
}
} else {
// Possibly return default_value instead?
throw utils::exceptions::KeyDoesNotExistException("ConfigReader[" + section + "]", key);
}
} else {
// Possibly return default_value instead?
throw utils::exceptions::KeyDoesNotExistException("ConfigReader", section);
}
return default_value;
}
template<typename TChar, typename TCharTraits, typename TDelimiters_ = TDelimiters>
friend std::basic_ostream<TChar, TCharTraits>&
operator<<(
std::basic_ostream<TChar, TCharTraits>& stream,
const utils::ini::ConfigReader<TDelimiters_>& cfgreader)
{
for (auto [section, mapped_values] : cfgreader.settings_map) {
if (mapped_values.size() > 0) {
stream << TDelimiters_::values.prefix
<< section
<< TDelimiters_::values.postfix
<< std::endl;
for (auto [key, value] : mapped_values) {
stream << key
<< TDelimiters_::values.delimiter
<< std::boolalpha << value
<< std::endl;
}
stream << std::endl;
}
}
return stream;
}
};
}
#endif // UTILS_INI_HPP