-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinifile.hpp
399 lines (354 loc) · 12 KB
/
inifile.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
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
/**
* @author: 0.382
* @description: ini文件读取和修改
* @url: https://github.com/0382/util/tree/main/cpp/inifile
*/
#pragma once
#ifndef INIFILE_HPP
#define INIFILE_HPP
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace util
{
// ini 文件注释符号
static constexpr const char *ini_comment_delimiter[] = {
"#",
";",
};
class inifile
{
// 一个键值对
struct IniItem
{
std::string key;
std::string value;
};
// 一个ini段落(以`[]`指示的称为段落)
struct IniSection
{
private:
std::string secname;
std::vector<IniItem> items;
static std::string show_name(const std::string &secname)
{
if (secname.empty())
return "*DEFAULT*";
return secname;
}
public:
friend class inifile;
using item_iterator = std::vector<IniItem>::iterator;
using item_const_iterator = std::vector<IniItem>::const_iterator;
IniSection(std::string name) : secname(std::move(name)) {}
// 迭代器
item_iterator begin() { return items.begin(); }
item_iterator end() { return items.end(); }
item_const_iterator begin() const { return items.cbegin(); }
item_const_iterator end() const { return items.cend(); }
item_const_iterator cbegin() const { return items.cbegin(); }
item_const_iterator cend() const { return items.cend(); }
// 段落名
std::string name() const { return show_name(secname); }
bool has_key(const std::string &key) const { return find_key(key) != items.cend(); }
std::string get_string(const std::string &key) const
{
auto pos = check_key(key);
return pos->value;
}
int64_t get_int(const std::string &key) const
{
auto pos = check_key(key);
return std::stoll(pos->value);
}
double get_double(const std::string &key) const
{
auto pos = check_key(key);
return std::stod(pos->value);
}
bool get_bool(const std::string &key) const
{
auto pos = check_key(key);
std::string value = to_lower(pos->value);
if (value == "true" || value == "1")
return true;
if (value == "false" || value == "0")
return false;
std::cerr << "Section " << this->name() << ", key " << key << ", invalid bool value: " << pos->value
<< std::endl;
std::exit(-1);
}
void set_string(const std::string &key, const std::string &value)
{
auto pos = find_key(key);
if (pos == items.end())
{
items.push_back(IniItem{key, value});
}
else
{
pos->value = value;
}
}
void set_int(const std::string &key, int64_t value) { set_value<int64_t>(key, value); }
void set_double(const std::string &key, double value) { set_value<double>(key, value); }
void set_bool(const std::string &key, bool value)
{
auto pos = find_key(key);
if (pos == items.end())
{
items.push_back(IniItem{key, value ? "true" : "false"});
}
else
{
pos->value = value ? "true" : "false";
}
}
private:
item_iterator find_key(const std::string &key)
{
return std::find_if(items.begin(), items.end(), [&key](const IniItem &it) { return it.key == key; });
}
item_const_iterator find_key(const std::string &key) const
{
return std::find_if(items.cbegin(), items.cend(), [&key](const IniItem &it) { return it.key == key; });
}
item_iterator check_key(const std::string &key)
{
auto pos = find_key(key);
if (pos == items.end())
{
std::cerr << "Section " << this->name() << ", key not found: " << key << std::endl;
std::exit(-1);
}
return pos;
}
item_const_iterator check_key(const std::string &key) const
{
auto pos = find_key(key);
if (pos == items.cend())
{
std::cerr << "Section " << this->name() << ", key not found: " << key << std::endl;
std::exit(-1);
}
return pos;
}
template <typename T>
void set_value(const std::string &key, const T &value)
{
auto pos = find_key(key);
if (pos == items.end())
{
items.push_back(IniItem{key, std::to_string(value)});
}
else
{
pos->value = std::to_string(value);
}
}
};
// 数据
private:
std::vector<IniSection> sections;
bool _good;
std::string msg;
// 公开函数定义
public:
inifile(const std::string &fname) : _good(true)
{
std::ifstream fp(fname);
if (!fp.is_open())
{
this->_good = false;
this->msg = "open file failed: " + fname;
return;
}
std::string line;
IniSection sec = {""};
IniItem item;
while (!fp.eof())
{
std::getline(fp, line);
line = remove_comment(line);
line = strip(line);
line = strip(line, '\r');
if (line == "")
{
continue;
}
if (line.front() == '[' && line.back() == ']')
{
this->sections.push_back(sec);
sec.secname = line.substr(1, line.size() - 2);
if (has_section(sec.secname))
{
this->_good = false;
this->msg = "duplicate section: " + sec.secname;
return;
}
sec.items.clear();
}
else if (build_item(line, item))
{
if (sec.has_key(item.key))
{
this->_good = false;
this->msg = "duplicate key: " + item.key + " in section " + sec.name();
return;
}
sec.items.push_back(item);
}
else
{
this->_good = false;
this->msg = "invalid ini line: " + line;
return;
}
}
this->sections.push_back(sec);
}
// 迭代器
std::vector<IniSection>::iterator begin() { return sections.begin(); }
std::vector<IniSection>::iterator end() { return sections.end(); }
std::vector<IniSection>::const_iterator begin() const { return sections.cbegin(); }
std::vector<IniSection>::const_iterator end() const { return sections.cend(); }
std::vector<IniSection>::const_iterator cbegin() const { return sections.cbegin(); }
std::vector<IniSection>::const_iterator cend() const { return sections.cend(); }
// ini文件打开是否成功
bool good() const { return this->_good; }
// 出错时的错误信息
std::string error_msg() const { return this->msg; }
// 输出到控制台
void show() const { this->print(std::cout); }
// 保存到文件
void save_as(const std::string &fname) const
{
std::ofstream save_file(fname);
this->print(save_file);
save_file.close();
}
bool has_section(const std::string &name) const { return find_section(name) != sections.cend(); }
// 获取其中一个 section
IniSection §ion(const std::string &name) { return *check_section(name); }
const IniSection §ion(const std::string &name) const { return *check_section(name); }
inifile &add_section(const std::string &name)
{
if (has_section(name))
{
std::cerr << "duplicate section: " << IniSection::show_name(name) << std::endl;
std::exit(-1);
}
this->sections.push_back(IniSection{name});
return *this;
}
// 提供从默认的section中获取value
std::string get_string(const std::string &key) const { return section("").get_string(key); }
int64_t get_int(const std::string &key) const { return section("").get_int(key); }
double get_double(const std::string &key) const { return section("").get_double(key); }
bool get_bool(const std::string &key) const { return section("").get_bool(key); }
// 提供设置默认section的value的方法
void set_string(const std::string &key, const std::string &value) { section("").set_string(key, value); }
void set_int(const std::string &key, int64_t value) { section("").set_int(key, value); }
void set_double(const std::string &key, double value) { section("").set_double(key, value); }
void set_bool(const std::string &key, bool value) { section("").set_bool(key, value); }
// 一些有用的函数
private:
// 去除`string`前后的空格(或者其他字符)
static std::string strip(const std::string &line, char c = ' ')
{
auto p1 = line.find_first_not_of(c);
if (p1 == std::string::npos)
return "";
auto p2 = line.find_last_not_of(c);
return line.substr(p1, p2 - p1 + 1);
}
// 转换为小写
static std::string to_lower(std::string line)
{
for (auto &c : line)
{
if (c >= 'A' && c <= 'Z')
{
c += 'a' - 'A';
}
}
return line;
}
// 去除一行结尾的注释部分
static std::string remove_comment(const std::string &line)
{
for (auto com : ini_comment_delimiter)
{
auto pos = line.find(com);
if (pos != std::string::npos)
{
return line.substr(0, pos);
}
}
return line;
}
// 从一行构建一个 IniItem
static bool build_item(const std::string &line, IniItem &item)
{
auto pos = line.find('=');
if (pos == std::string::npos)
return false;
item.key = strip(line.substr(0, pos));
item.value = strip(line.substr(pos + 1));
// 暂时只处理引号,认为没有转义(装死)
item.value = strip(item.value, '\'');
item.value = strip(item.value, '"');
return true;
}
// 打印
void print(std::ostream &os) const
{
for (const auto &sec : this->sections)
{
if (!sec.secname.empty())
os << '[' << sec.secname << "]\n";
for (const auto &it : sec.items)
{
os << it.key << " = " << it.value << '\n';
}
}
}
std::vector<IniSection>::iterator find_section(const std::string &name)
{
return std::find_if(sections.begin(), sections.end(),
[&name](const IniSection &sec) { return sec.secname == name; });
}
std::vector<IniSection>::const_iterator find_section(const std::string &name) const
{
return std::find_if(sections.cbegin(), sections.cend(),
[&name](const IniSection &sec) { return sec.secname == name; });
}
// 查找 section
std::vector<IniSection>::iterator check_section(const std::string &name)
{
auto pos = find_section(name);
if (pos == sections.end())
{
std::cerr << "section not found: " << IniSection::show_name(name) << std::endl;
std::exit(-1);
}
return pos;
}
std::vector<IniSection>::const_iterator check_section(const std::string &name) const
{
auto pos = find_section(name);
if (pos == sections.cend())
{
std::cerr << "section not found: " << IniSection::show_name(name) << std::endl;
std::exit(-1);
}
return pos;
}
};
} // end namespace util
#endif // INIFILE_HPP