-
Notifications
You must be signed in to change notification settings - Fork 0
/
inicxx.hh
360 lines (279 loc) · 7.59 KB
/
inicxx.hh
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
/*
* inicxx - A C++ INI file parser and writer
* Version 1.2.4
* Github: https://github.com/LordOfTrident/inicxx
*
* Examples under examples/ folder, read README.md on
* how to compile them
*/
#ifndef __INICXX_HH_HEADER_GUARD__
#define __INICXX_HH_HEADER_GUARD__
#include <string> // std::string, std::basic_string
#include <unordered_map> // std::unordered_map
#include <cstdint> // std::int8_t, std::int16_t, std::int32_t, std::int64_t,
// std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t
#include <cstddef> // std::size_t
#define INI_VERSION_MAJOR 1
#define INI_VERSION_MINOR 2
#define INI_VERSION_PATCH 4
namespace INI {
// Type aliases for shorter and readable code
using s8 = std::int8_t;
using s16 = std::int16_t;
using s32 = std::int32_t;
using s64 = std::int64_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = size_t;
using Section = std::unordered_map<std::string, std::string>;
using Sections = std::unordered_map<std::string, Section>;
// Constants for defaults
constexpr char DefaultSection[1] = "";
class Exception {
public:
Exception(const std::string &p_message):
m_message(p_message)
{};
~Exception() {};
const std::string& What() const {
return m_message;
};
protected:
std::string m_message;
}; // class Exception
class ParserException: public Exception {
public:
ParserException(
const std::string &p_message,
usize p_line
):
Exception(p_message),
m_line(p_line)
{};
~ParserException() {};
usize Line() const {
return m_line;
};
protected:
usize m_line;
}; // class ParserException
template <class CharT>
class Structure {
private:
using string = std::basic_string<CharT>;
public:
Structure() {};
Structure(const Sections &p_sections) {
m_sections = p_sections;
};
~Structure() {};
Structure& operator=(const Structure &p_structure) {
m_sections = p_structure.m_sections;
return *this;
};
// Index functions
Section& operator[](const string &p_idx) {
return m_sections[p_idx];
};
Section& At(const string &p_idx) {
return m_sections.at(p_idx);
};
string& At(const string &p_idxA, const string &p_idxB) {
return m_sections.at(p_idxA).at(p_idxB);
};
bool Contains(const string &p_idx) const {
return m_sections.count(p_idx);
};
bool Contains(const string &p_idxA, const string &p_idxB) const {
if (m_sections.count(p_idxA))
if (m_sections.at(p_idxA).count(p_idxB))
return true;
return false;
};
void Clear() {
m_sections.clear();
};
void Clear(const string &p_idx) {
m_sections[p_idx].clear();
};
// Conversion functions
double AsNumber(const string &p_idxA, const string &p_idxB) {
return std::stod(m_sections[p_idxA][p_idxB]);
};
long AsInteger(const string &p_idxA, const string &p_idxB) {
return std::stol(m_sections[p_idxA][p_idxB]);
};
string AsString(const string &p_idxA, const string &p_idxB) {
return m_sections[p_idxA][p_idxB];
};
bool AsBoolean(const string &p_idxA, const string &p_idxB) {
const string &val = m_sections[p_idxA][p_idxB];
if (val == "true")
return true;
else if (val == "false")
return false;
throw Exception("Attempt to use AsBoolean() on a non-boolean value");
};
string Stringify() const {
string stringified = "";
for (const std::pair<
const string&,
const Section&
> § : m_sections
) {
if (sect.first[0] == ' ' and sect.first.back() == ' ')
stringified += "[\"" + INIEscape(sect.first) + "\"]\n";
else
stringified += "[" + INIEscape(sect.first) + "]\n";
for (
const std::pair<
const string&, const string&
> &val :
sect.second
) {
if (val.first[0] == ' ' and val.first.back() == ' ')
stringified += "\"" + INIEscape(val.first) + "\"";
else
stringified += INIEscape(val.first);
stringified += "=";
if (val.second[0] == ' ' and val.second.back() == ' ')
stringified += "\"" + INIEscape(val.second) + "\"\n";
else
stringified += INIEscape(val.second) + "\n";
};
};
return stringified;
};
void Parse(const string &p_text) {
Clear();
string line = "";
string currentSection = DefaultSection;
usize currentLine = 0;
for (const char &ch : p_text + "\n") {
switch (ch) {
case '\n': {
++ currentLine;
Trim(line);
switch (line[0]) {
// Comment
case ';': case '#': break;
// Sections
case '[': {
usize pos = line.find_first_of("]");
if (pos == string::npos)
throw ParserException(
"Sector name closing expected",
currentLine
);
currentSection = line.substr(1, pos - 1);
Trim(currentSection);
RemoveApostrophes(currentSection);
};
break;
// Assignment
default: {
if (line == "")
break;
usize pos = 0;
do {
pos = line.find_first_of("=", pos + 1);
if (pos == string::npos)
throw ParserException(
"Expected assignment",
currentLine
);
} while (line[pos - 1] == '\\');
string key = line.substr(0, pos);
string value = "";
line = line.substr(pos + 1);
if (INIUnescape(string(key), key))
throw ParserException(
"Expected assignment",
currentLine
);
INIUnescape(line, value);
Trim(key); Trim(value);
RemoveApostrophes(key);
RemoveApostrophes(value);
if (key == "")
throw ParserException(
"Key name expected",
currentLine
);
m_sections[currentSection][key] = value;
};
break;
};
line = "";
};
break;
default: line += ch; break;
};
};
};
private:
void Trim(string &p_str) {
usize start = p_str.find_first_not_of(" \t");
if (start == string::npos) {
p_str = "";
return;
};
usize end = p_str.find_last_not_of(" \t");
p_str = p_str.substr(start, end + 1 - start);
};
void RemoveApostrophes(string &p_str) {
if (p_str[0] == '"' and p_str.back() == '"')
p_str = p_str.substr(1, p_str.length() - 2);
};
string INIEscape(const string &p_str) const {
string escaped = "";
for (usize i = 0; i < p_str.length(); ++ i)
switch (p_str[i]) {
case '\n': escaped += "\\n"; break;
case '\r': escaped += "\\r"; break;
case '\t': escaped += "\\t"; break;
case '\f': escaped += "\\f"; break;
case '\b': escaped += "\\b"; break;
case '\a': escaped += "\\a"; break;
case '\0': escaped += "\\0"; break;
case '"': escaped += "\\\""; break;
default: escaped += p_str[i]; break;
};
return escaped;
};
bool INIUnescape(
const string &p_line,
string &p_str
) {
bool escape = false;
p_str = "";
for (const char &ch : p_line) {
if (escape) {
escape = false;
switch (ch) {
case 'n': p_str += '\n'; break;
case 'r': p_str += '\r'; break;
case 't': p_str += '\t'; break;
case 'f': p_str += '\f'; break;
case 'b': p_str += '\b'; break;
case 'a': p_str += '\a'; break;
case '0': p_str += '\0'; break;
case '"': p_str += '\"'; break;
case '\\': p_str += '\\'; break;
case ';': p_str += ';'; break;
};
};
switch (ch) {
case ';': case '#': return true;
case '\\': escape = true; break;
default: p_str += ch; break;
};
};
return false;
};
Sections m_sections;
}; // class Structure
}; // namespace INI
#endif // __INI_HH_HEADER_GUARD__