-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiIni.h
362 lines (312 loc) · 10.2 KB
/
MiIni.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
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
/*
Made by Mauricius
Part of my MUtilize repo: https://github.com/LegendaryMauricius/MUtilize
*/
#pragma once
#ifndef _MIINI_H
#define _MIINI_H
#include <string.h>
#include <map>
#include <istream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <functional>
//#include <experimental/filesystem>
#include <codecvt>
#include <locale>
namespace MiIniUtils
{
template <class _StringT>
class Utf8Path
{
public:
Utf8Path(_StringT path)
{
std::wstring wpath;
wpath.resize(path.size(), 'a');
for (size_t i = 0; i< path.size(); i++)
{
wpath[i] = path[i];
}
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
utf8path = myconv.to_bytes(wpath);
}
std::string utf8path;
operator std::string() const
{
return utf8path;
}
};
template <class _IStreamT, class _IFStreamT, class _StringT, class _PathT>
class readFunc
{
public:
void operator()(const _StringT& filename, std::function<void(_IStreamT&)> streamReader) const
{
_IFStreamT file;
file.open(_PathT(filename), std::ios::in);
if (file.good()) {
streamReader(file);
file.close();
}
}
};
template <class _OStreamT, class _OFStreamT, class _StringT, class _PathT>
class writeFunc
{
public:
void operator()(const _StringT& filename, std::function<void(_OStreamT&)> streamWriter) const
{
_OFStreamT file;
file.open(_PathT(filename), std::ios::out);
if (file.good()) {
streamWriter(file);
file.close();
}
else {
std::string reasons = strerror(errno);
std::string text = ((std::stringstream&)(
std::stringstream() <<
"Can't open ini file \"" << std::string(filename.begin(), filename.end()) <<
"\" because " << reasons << "!"
)).str();
throw std::runtime_error(text);
}
}
};
}
/*
A simple class for ini files.
The template allows you to specify the type used as a string, as well as the string, input, output and file stream types.
Default is std::string.
*/
template<
class _StringT = std::string,
class _SStreamT = std::basic_stringstream<typename _StringT::value_type>,
class _IStreamT = std::basic_istream<typename _StringT::value_type>,
class _OStreamT = std::basic_ostream<typename _StringT::value_type>,
class _PathT = MiIniUtils::Utf8Path<_StringT>,//std::experimental::filesystem::path,
class _ReadFunctorT = MiIniUtils::readFunc<_IStreamT, std::basic_ifstream<typename _StringT::value_type>, _StringT, _PathT>,
class _WriteFunctorT = MiIniUtils::writeFunc<_OStreamT, std::basic_ofstream<typename _StringT::value_type>, _StringT, _PathT>
>
class MiIni
{
public:
using String = _StringT;
using StringStream = _SStreamT;
using InputStream = _IStreamT;
using OutputStream = _OStreamT;
using Path = _PathT;
private:
String mFilename;
bool mAutoSync;
_ReadFunctorT mReadFunctor;
_WriteFunctorT mWriteFunctor;
public:
struct FileError : public std::runtime_error {
FileError() {}
FileError(const std::string& what) : std::runtime_error(what) {}
};
struct FormatException : public std::runtime_error {
FormatException() {}
FormatException(const std::string& what, size_t line) :
std::runtime_error(what),
line(line)
{}
size_t line;
};
std::map<String, std::map<String, String> > dataMap;// dataMap[section][key] = value
MiIni(): mAutoSync(false) {}
/*
@param filename The name of the file to open and read
@param autosync If enabled, the file will automatically be synced to this MinIni's content before being closed
*/
MiIni(String filename, bool autosync) {
open(filename, autosync);
}
// Default copy-constructors and assignments are okay
~MiIni() {
close();
}
/*
The name of the file that's linked to this MinIni.
Note that the filename will be linked only if it's specified in the call to open() or in the constructor.
Using read() to read a file will not link the file.
*/
String filename() const {
return mFilename;
}
// If enabled, the linked file will automatically be synced to this MinIni's content before being closed or destroyed
bool autoSyncEnabled() const {
return mAutoSync;
}
void setFilename(String filename) {
mFilename = filename;
}
void enableAutoSync(bool enable = true) {
mAutoSync = enable;
}
// Returns the String value if it exists. If not, inserts the default value (def) and returns it
String getStr(String sect, String key, String def = String()) {
auto& keyvalmap = dataMap[sect];
auto it = keyvalmap.find(key);
if (it == keyvalmap.end()) {
keyvalmap.insert(std::make_pair(key, def));
return def;
}
else {
return it->second;
}
}
// Sets the value to the String
void setStr(String sect, String key, String val) {
dataMap[sect][key] = val;
}
// Returns the value if it exists. If not, inserts the default value (def) and returns it. val must be streamable to and from a StringStream.
template<class _T>
_T get(String sect, String key, _T def = _T()) {
StringStream ss;
auto& keyvalmap = dataMap[sect];
auto it = keyvalmap.find(key);
if (it == keyvalmap.end()) {
ss << def;
keyvalmap.insert(std::make_pair(key, ss.str()));
return def;
}
else {
ss << it->second;
_T ret;
ss >> ret;
return ret;
}
}
std::string get(String sect, String key, const char* def = "") {
return get(sect, key, std::string(def));
}
std::string get(String sect, String key, const wchar_t* def = L"") {
return get(sect, key, std::wstring(def));
}
// Sets the value to val. val must be streamable to a StringStream.
template<class _T>
void set(String sect, String key, _T val) {
StringStream ss;
ss << val;
setStr(sect, key, ss.str());
}
// Returns whether a section exists
bool exists(String sect) const {
return (dataMap.find(sect) != dataMap.end());
}
// Returns whether a value exists with the key in the section sect
bool exists(String sect, String key) const {
auto it = dataMap.find(sect);
return (it != dataMap.end() && it->second.find(key) != it->second.end());
}
/*
Reads the content from the stream, adding it to the already existing content.
The stream needs to be formatted as an ini file.If not, a FormatException will be thrown, unless ignoreErrors argument is enabled.
In that case the improper line will be skipped, and the rest of the file will be read normally.
Note that specifying a FileStream as the input stream won't link the file, i.e. the filename won't be changed.
*/
void readMore(InputStream& is, bool ignoreErrors = false) {
String sect;
String ln;
size_t line = 0;
while (std::getline(is, ln)) {
line++;
size_t commentPos;
StringStream spacesSS;
spacesSS << " \t\r\n";
String spaces = spacesSS.str();
ln.erase(0, ln.find_first_not_of(spaces));
ln.erase(ln.find_last_not_of(spaces) + 1);
if ((commentPos = ln.find_first_of('#')) != String::npos) {
ln.erase(commentPos);
}
if (ln.length()) {
if (ln[0] == '[') {
sect = ln.substr(1, ln.find_first_of(']') - 1);
sect.erase(0, sect.find_first_not_of(spaces));
sect.erase(sect.find_last_not_of(spaces) + 1);
dataMap[sect];
}
else {
size_t eqpos = ln.find_first_of('=');
if (eqpos == String::npos) {
if (!ignoreErrors)
throw FormatException(((std::stringstream&)(std::stringstream() <<
"Wrong ini file format at line " << line << "!"
)).str(), line);
continue;
}
String key = ln.substr(0, eqpos);
key.erase(key.find_last_not_of(spaces) + 1);
String val = ln.substr(eqpos + 1, String::npos);
val.erase(0, val.find_first_not_of(spaces));
dataMap[sect][key] = val;
}
}
}
}
// Clears the content and reads it from the stream using readMore().
void read(InputStream& is, bool ignoreErrors = false) {
dataMap.clear();
readMore(is, ignoreErrors);
}
// Writes the content to the output stream, formatted as an ini file
void write(OutputStream& os) const {
for (auto& sect : dataMap) {
if (sect.first != String()) {
os << "[" << sect.first << "]\n";
}
for (auto& keyval : sect.second) {
os << keyval.first << " = " << keyval.second << std::endl;
}
os << std::endl;
}
}
/*
Reads the file and links it to this MinIni
@param filename The name of the file to open
@param autosync If enabled, the file will automatically be synced to this MinIni's content before being closed
*/
void open(String filename, bool autosync, bool ignoreErrors = false) {
mFilename = filename;
mAutoSync = autosync;
mReadFunctor(
mFilename, [this, ignoreErrors](InputStream &ss) {
this->read(ss, ignoreErrors);
}
);
}
// Writes the content to the linked file. Throws FileError if the file can't be opened or isn't linked.
void sync() const {
if (mFilename.length())
{
mWriteFunctor(
mFilename, [this](OutputStream &ss) {
this->write(ss);
}
);
}
else
{
throw std::runtime_error("No linked file specified to be synced to this MinIni!");
}
}
// Resets the MinIni and syncs the linked file before closing if autoSync is enabled.
void close() {
if (mFilename.length() && mAutoSync) {
sync();
}
dataMap.clear();
mFilename = String();
mAutoSync = false;
}
};
// ascii version of the default MinIni template
using AMiIni = MiIni<>;
// widechar version of the default MinIni template
using WMiIni = MiIni<std::wstring>;
#endif