-
Notifications
You must be signed in to change notification settings - Fork 0
/
inipp.h
224 lines (192 loc) · 6.37 KB
/
inipp.h
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
/*
MIT License
Copyright (c) 2017-2020 Matthias C. M. Troffaes
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <cstring>
#include <string>
#include <iostream>
#include <list>
#include <map>
#include <algorithm>
#include <functional>
#include <cctype>
#include <sstream>
namespace inipp {
namespace detail {
// trim functions based on http://stackoverflow.com/a/217605
template <class CharT>
inline void ltrim(std::basic_string<CharT> & s) {
s.erase(s.begin(),
std::find_if(s.begin(), s.end(),
[](int ch) { return !std::isspace(ch); }));
}
template <class CharT>
inline void rtrim(std::basic_string<CharT> & s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](int ch) { return !std::isspace(ch); }).base(),
s.end());
}
// string replacement function based on http://stackoverflow.com/a/3418285
template <class CharT>
inline bool replace(std::basic_string<CharT> & str, const std::basic_string<CharT> & from, const std::basic_string<CharT> & to) {
auto changed = false;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::basic_string<CharT>::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
changed = true;
}
return changed;
}
} // namespace detail
template <typename CharT, typename T>
inline bool extract(const std::basic_string<CharT> & value, T & dst) {
CharT c;
std::basic_istringstream<CharT> is{ value };
T result;
if ((is >> std::boolalpha >> result) && !(is >> c)) {
dst = result;
return true;
}
else {
return false;
}
}
template <typename CharT>
inline bool extract(const std::basic_string<CharT> & value, std::basic_string<CharT> & dst) {
dst = value;
return true;
}
template<class CharT>
class Ini
{
public:
typedef std::basic_string<CharT> String;
typedef std::map<String, String> Section;
typedef std::map<String, Section> Sections;
Sections sections;
std::list<String> errors;
static const CharT char_section_start = (CharT)'[';
static const CharT char_section_end = (CharT)']';
static const CharT char_assign = (CharT)'=';
static const CharT char_comment = (CharT)';';
static const CharT char_interpol = (CharT)'$';
static const CharT char_interpol_start = (CharT)'{';
static const CharT char_interpol_sep = (CharT)':';
static const CharT char_interpol_end = (CharT)'}';
static const int max_interpolation_depth = 10;
void generate(std::basic_ostream<CharT> & os) const {
for (auto const & sec : sections) {
os << char_section_start << sec.first << char_section_end << std::endl;
for (auto const & val : sec.second) {
os << val.first << char_assign << val.second << std::endl;
}
os << std::endl;
}
}
void parse(std::basic_istream<CharT> & is) {
String line;
String section;
while (std::getline(is, line)) {
detail::ltrim(line);
detail::rtrim(line);
const auto length = line.length();
if (length > 0) {
const auto pos = line.find_first_of(char_assign);
const auto & front = line.front();
if (front == char_comment) {
continue;
}
else if (front == char_section_start) {
if (line.back() == char_section_end)
section = line.substr(1, length - 2);
else
errors.push_back(line);
}
else if (pos != 0 && pos != String::npos) {
String variable(line.substr(0, pos));
String value(line.substr(pos + 1, length));
detail::rtrim(variable);
detail::ltrim(value);
auto & sec = sections[section];
if (sec.find(variable) == sec.end())
sec.insert(std::make_pair(variable, value));
else
errors.push_back(line);
}
else {
errors.push_back(line);
}
}
}
}
void interpolate() {
int global_iteration = 0;
auto changed = false;
// replace each "${variable}" by "${section:variable}"
for (auto & sec : sections)
replace_symbols(local_symbols(sec.first, sec.second), sec.second);
// replace each "${section:variable}" by its value
do {
changed = false;
const auto syms = global_symbols();
for (auto & sec : sections)
changed |= replace_symbols(syms, sec.second);
} while (changed && (max_interpolation_depth > global_iteration++));
}
void default_section(const Section & sec) {
for (auto & sec2 : sections)
for (const auto & val : sec)
sec2.second.insert(val);
}
void clear() {
sections.clear();
errors.clear();
}
private:
typedef std::list<std::pair<String, String> > Symbols;
auto local_symbol(const String & name) const {
return char_interpol + (char_interpol_start + name + char_interpol_end);
}
auto global_symbol(const String & sec_name, const String & name) const {
return local_symbol(sec_name + char_interpol_sep + name);
}
auto local_symbols(const String & sec_name, const Section & sec) const {
Symbols result;
for (const auto & val : sec)
result.push_back(std::make_pair(local_symbol(val.first), global_symbol(sec_name, val.first)));
return result;
}
auto global_symbols() const {
Symbols result;
for (const auto & sec : sections)
for (const auto & val : sec.second)
result.push_back(
std::make_pair(global_symbol(sec.first, val.first), val.second));
return result;
}
bool replace_symbols(const Symbols & syms, Section & sec) const {
auto changed = false;
for (auto & sym : syms)
for (auto & val : sec)
changed |= detail::replace(val.second, sym.first, sym.second);
return changed;
}
};
} // namespace inipp