-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigReader.cpp
More file actions
476 lines (416 loc) · 15.3 KB
/
ConfigReader.cpp
File metadata and controls
476 lines (416 loc) · 15.3 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include"ConfigReader.hpp"
#include<fstream>
#include<regex>
#include<filesystem>
template<typename T>
std::string to_string(const T& t) {
return std::to_string(t);
}
//TODO OVERLOAD?
template<>
inline std::string to_string<std::string>(const std::string& t) {
return t;
}
template<>
inline std::string to_string<const char*>(const char* const& t) {
return std::string(t);
}
std::string lowercase_str(const std::string& str) { //TODO make it portable with 16bitchar
std::string result = "";
for (auto& c : str)
{
result += std::tolower(c);
}
return result;
}
std::string uppercase_str(const std::string& str) { //TODO make it portable with 16bitchar
std::string result = "";
for (auto& c : str)
{
result += std::toupper(c);
}
return result;
}
template<typename T>
T from_string(char* const& str) {
return from_string<T>(std::string(str));
}
template<typename T>
T from_string(const std::string& str) {
return T{ str };
}
template<>
std::string from_string<std::string>(const std::string& str) {
return str;
}
template<>
char from_string<char>(const std::string& str) {
if (str.length() > 0) return str[0];
else return (char)0;
}
template<>
int from_string<int>(const std::string& str) {
int result = 0;
try {
result = std::stoi(str);
}
catch (std::exception e) {
}
return result;
}
template<>
std::size_t from_string<std::size_t>(const std::string& str) {
int result = 0;
try {
result = std::stoi(str);
}
catch (std::exception e) {
}
return result;
}
template<>
double from_string<double>(const std::string& str) {
double result = 0;
try {
result = std::stod(str);
}
catch (std::exception e) {
}
return result;
}
template<>
long double from_string<long double>(const std::string& str) {
double result = 0;
try {
result = std::stod(str);
}
catch (std::exception e) {
}
return result;
}
template<>
bool from_string<bool>(const std::string& str) {
bool result = false;
try {
std::string _temp = "";
for (const auto& i : str) _temp += (char)std::tolower(i);
result = (_temp == "true");
}
catch (std::exception e) {
}
return result;
}
//https://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
std::istream& safeGetline(std::istream& is, std::string& t)
{
t.clear();
// The characters in the stream are read one-by-one using a std::streambuf.
// That is faster than reading them one-by-one using the std::istream.
// Code that uses streambuf this way must be guarded by a sentry object.
// The sentry object performs various tasks,
// such as thread synchronization and updating the stream state.
std::istream::sentry se(is, true);
std::streambuf* sb = is.rdbuf();
for (;;) {
int c = sb->sbumpc();
switch (c) {
case '\n':
return is;
case '\r':
if (sb->sgetc() == '\n')
sb->sbumpc();
return is;
case std::streambuf::traits_type::eof():
// Also handle the case when the last line has no line ending
if (t.empty())
is.setstate(std::ios::eofbit);
return is;
default:
t += (char)c;
}
}
}
std::string correct_case(std::string const& s, ConfigReader::CaseType m_case) {
switch (m_case) {
case ConfigReader::CaseType::DontCare:
return lowercase_str(s);
break;
case ConfigReader::CaseType::Lower:
return lowercase_str(s);
break;
case ConfigReader::CaseType::Upper:
return uppercase_str(s);
break;
case ConfigReader::CaseType::Sensitive:
return s;
break;
}
}
ConfigReader::ConfigReader(const std::string& file_location) {
file_path = file_location;
is_open = open(file_location);
}
ConfigReader::ConfigReader(const std::string& file_location, CaseType const& _m_case)
:m_case(_m_case)
{
file_path = file_location;
is_open = open(file_location);
}
bool ConfigReader::open(const std::string& file_location) {
const std::size_t max_tries = 3;
std::size_t tries = 0;
do {
file_data.open(file_location.c_str(), std::ios::in);
is_open = file_data.is_open();
is_loaded = file_data.good() && !file_data.fail() && !file_data.bad();
++tries;
} while (tries < max_tries && (!is_open || !is_loaded));
if (!is_open || !is_loaded) {
{
std::ofstream file_data(file_location);
}
file_data.open(file_location.c_str(), std::ios::in);
is_open = file_data.is_open();
is_loaded = file_data.good() && !file_data.fail() && !file_data.bad();
}
std::string line = "";
while (!safeGetline(file_data, line).eof()) {
read_lines.push_back(correct_case(line, m_case));
}
elaborate();
return is_loaded;
}
void ConfigReader::elaborate() {
//for (decltype(line_vector.size()) i=0; i<line_vector.size(); ++i) {
std::regex r_variable_name("^\\s*([^\\s\\=]*)\\s*\\=");
std::regex r_variable_values("([^,]*),");
std::regex r_variable_values_end("([^,]+)");
std::string variable_name_corrected;
for (auto& line_string : read_lines) {
std::smatch matches;
std::regex_search(line_string, matches, r_variable_name);
if (!matches.empty()) {
std::string variable_name = matches[1];
variable_name_corrected = correct_case(variable_name, m_case);
std::size_t line_vector_position;
if (name_map.find(variable_name_corrected) == name_map.end()) {
//name_map[variable_name]=std::vector<std::string>();
line_vector.push_back(std::vector<std::string>(1, variable_name));
line_vector_position = line_vector.size() - 1;
}
else {
line_vector_position = name_map[variable_name_corrected];
}
name_map[variable_name] = line_vector_position;
//std::vector<std::string>& value_vector_ref=name_map[variable_name];
std::smatch value_matches;
decltype(matches[0].second) start_it = matches[0].second;
while (std::regex_search(start_it, line_string.cend(), value_matches, r_variable_values)) {
line_vector[line_vector_position].push_back(value_matches[1]);
start_it = value_matches[0].second;
}
if (std::regex_search(start_it, line_string.cend(), value_matches, r_variable_values_end))
{
line_vector[line_vector_position].push_back(value_matches[1]);
}
else {
//line_vector[line_vector_position].push_back("");
}
}
else {
line_vector.push_back(std::vector<std::string>(1, line_string));
}
}
}
template<typename T>
std::vector<T> ConfigReader::get(char* name) {
return get<T>(std::string(name));
}
template<typename T>
std::vector<T> ConfigReader::get(const std::string& name) {
std::vector<T> result;
if (name.length() <= 0) return result;
if (!is_loaded) return result;
std::string corrected_name = correct_case(name, m_case);
if (name_map.find(corrected_name) == name_map.end()) return result;
std::vector<std::string> const& value_vector_ref = line_vector[name_map[corrected_name]];
result.resize(value_vector_ref.size() - 1);
for (decltype(value_vector_ref.size()) i = 1; i < value_vector_ref.size(); ++i) {
std::string corrected_string = correct_case(value_vector_ref[i], m_case);
result[i - 1] = from_string<T>(corrected_string);
}
return result;
}
template<typename T>
void ConfigReader::append(char* name, T const& t) {
return append<T>(std::string(name), t);
}
template<>
void ConfigReader::append(std::string const& name, bool const& t) {
if (t)
append(name, std::string("true"));
else
append(name, std::string("false"));
}
template<typename T>
void ConfigReader::append(std::string const& name, T const& t) {
if (!is_loaded) return;
if (name.length() <= 0) return;
std::string corrected_name = correct_case(name, m_case);
if (name_map.find(corrected_name) == name_map.end()) {
line_vector.push_back(std::vector<std::string>(1, corrected_name));
name_map[corrected_name] = line_vector.size() - 1;
}
line_vector[name_map[corrected_name]].push_back(to_string(t));
}
template<typename T>
void ConfigReader::overwrite(char* name, std::vector<T> const& t) {
return overwrite<T>(std::string(name), t);
}
template<>
void ConfigReader::overwrite(std::string const& name, std::vector<bool> const& t) {
std::vector<std::string> string_vector;
for (const auto& i : t) {
if (i) string_vector.push_back(std::string("true"));
else string_vector.push_back(std::string("false"));
}
return overwrite<std::string>(std::string(name), string_vector);
}
template<typename T>
void ConfigReader::overwrite(std::string const& name, std::vector<T> const& t) {
if (!is_loaded) return;
if (name.length() <= 0) return;
std::string corrected_name = correct_case(name, m_case);
if (name_map.find(corrected_name) == name_map.end()) {
line_vector.push_back(std::vector<std::string>(1, corrected_name));
name_map[corrected_name] = line_vector.size() - 1;
}
std::vector<std::string> t_to_string;
t_to_string.push_back(corrected_name);
for (const auto& i : t)
t_to_string.push_back(to_string(i));
line_vector[name_map[corrected_name]] = t_to_string;
}
template<typename T>
void ConfigReader::overwrite(char* name, T const& t) {
return std::move(overwrite<T>(std::string(name), std::vector<T>(1, t)));
}
template<typename T>
void ConfigReader::overwrite(std::string const& name, T const& t) {
return std::move(overwrite<T>(name, std::vector<T>(1, t)));
}
void ConfigReader::print(std::ostream& os) {
for (const auto& line : line_vector) {
os << line[0];
if (line.size() >= 2) {
os << "=";
for (std::size_t i = 1; i < line.size() - 1; ++i) {
os << line[i] << ",";
}
os << line[line.size() - 1];
}
os << "\n";
}
}
bool ConfigReader::close() {
if (!is_open) return true;
file_data.close();
is_open = file_data.is_open();
return !is_open;
}
bool ConfigReader::save() {
try {
std::filesystem::path final_p = std::filesystem::current_path() / file_path;
std::filesystem::path tmp_p = std::filesystem::current_path() / (file_path + std::string(".tmp"));
std::ofstream file_data_output(tmp_p, std::ios::out);
for (const auto& line : line_vector) {
file_data_output << line[0];
if (line.size() >= 2) {
file_data_output << "=";
for (std::size_t i = 1; i < line.size() - 1; ++i) {
file_data_output << line[i] << ",";
}
file_data_output << line[line.size() - 1];
}
file_data_output << "\n";
}
file_data_output.close();
if (file_data.is_open()) file_data.close();
std::filesystem::path bak_p = std::filesystem::current_path() / (file_path + std::string(".bak"));
std::filesystem::rename(final_p, bak_p);
std::filesystem::rename(tmp_p, final_p);
std::filesystem::remove(bak_p);
}
catch (std::exception ex) {
return false;
}
return true;
}
template std::vector<double> ConfigReader::get(char* name);
template std::vector<double> ConfigReader::get(const std::string& name);
template std::vector<long double> ConfigReader::get(char* name);
template std::vector<long double> ConfigReader::get(const std::string& name);
//template std::vector<float> ConfigReader::get(char* name);
//template std::vector<float> ConfigReader::get(const std::string& name);
template std::vector<bool> ConfigReader::get(char* name);
template std::vector<bool> ConfigReader::get(const std::string& name);
//template std::vector<long long> ConfigReader::get(char* name);
//template std::vector<long long> ConfigReader::get(const std::string& name);
//template std::vector<unsigned long long> ConfigReader::get(char* name);
//template std::vector<unsigned long long> ConfigReader::get(const std::string& name);
template std::vector<int> ConfigReader::get(char* name);
template std::vector<int> ConfigReader::get(const std::string& name);
template std::vector<std::size_t> ConfigReader::get(char* name);
template std::vector<std::size_t> ConfigReader::get(const std::string& name);
//template std::vector<unsigned int> ConfigReader::get(char* name);
//template std::vector<unsigned int> ConfigReader::get(const std::string& name);
template std::vector<char> ConfigReader::get(char* name);
template std::vector<char> ConfigReader::get(const std::string& name);
template std::vector<std::string> ConfigReader::get(char* name);
template std::vector<std::string> ConfigReader::get(const std::string& name);
template void ConfigReader::append(char* name, double const&);
template void ConfigReader::append(const std::string& name, double const&);
template void ConfigReader::append(char* name, long double const&);
template void ConfigReader::append(const std::string& name, long double const&);
//template void ConfigReader::append(char* name, float const&);
//template void ConfigReader::append(const std::string& name, float const&);
template void ConfigReader::append(char* name, bool const&);
template void ConfigReader::append(const std::string& name, bool const&);
//template ConfigReader::append(char* name, unsigned long const&);
//template ConfigReader::append(const std::string& name, unsigned long);
//template std::vector<unsigned long long> ConfigReader::append(char* name);
//template std::vector<unsigned long long> ConfigReader::append(const std::string& name);
template void ConfigReader::append(char* name, int const&);
template void ConfigReader::append(const std::string& name, int const&);
template void ConfigReader::append(char* name, std::size_t const&);
template void ConfigReader::append(const std::string& name, std::size_t const&);
//template void ConfigReader::append(char* name, unsigned int const&);
//template void ConfigReader::append(const std::string& name, unsigned int const&);
template void ConfigReader::append(char* name, char const&);
template void ConfigReader::append(const std::string& name, char const&);
template void ConfigReader::append(char* name, std::string const&);
template void ConfigReader::append(const std::string& name, std::string const&);
template void ConfigReader::overwrite(char* name, std::vector<double> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<double> const&);
template void ConfigReader::overwrite(char* name, std::vector<long double> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<long double> const&);
//template void ConfigReader::overwrite(char* name, float const&);
//template void ConfigReader::overwrite(const std::string& name, float const&);
template void ConfigReader::overwrite(char* name, std::vector<bool> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<bool> const&);
//template ConfigReader::overwrite(char* name, unsigned long const&);
//template ConfigReader::overwrite(const std::string& name, unsigned long);
//template std::vector<unsigned long long> ConfigReader::overwrite(char* name);
//template std::vector<unsigned long long> ConfigReader::overwrite(const std::string& name);
template void ConfigReader::overwrite(char* name, std::vector<int> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<int> const&);
template void ConfigReader::overwrite(char* name, std::vector<std::size_t> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<std::size_t> const&);
template void ConfigReader::overwrite(char* name, std::vector<std::size_t> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<std::size_t> const&);
//template void ConfigReader::overwrite(char* name, std::vector<unsigned int> const&);
//template void ConfigReader::overwrite(const std::string& name, std::vector<unsigned int> const&);
template void ConfigReader::overwrite(char* name, std::vector<char> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<char> const&);
template void ConfigReader::overwrite(char* name, std::vector<std::string> const&);
template void ConfigReader::overwrite(const std::string& name, std::vector<std::string> const&);