-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiniparser.h
More file actions
274 lines (246 loc) · 7.07 KB
/
iniparser.h
File metadata and controls
274 lines (246 loc) · 7.07 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
#include <fstream>
#include <map>
#include <vector>
#include <iostream>
namespace ini {
typedef std::map<std::string, std::map<std::string, std::string>> inimap;
class IniParser {
using string = std::string;
typedef std::vector<string> vecstr;
public:
IniParser() {}
IniParser(string file_path) {
readFile(file_path);
}
IniParser(string file_path, inimap& data) {
if(readFile(file_path)) {
data = parse();
}
}
~IniParser() {
_lines.clear();
_values.clear();
}
inimap parseString(string& str) {
if(readString(str)) {
return parse();
} else {
auto val = _values;
val.clear();
return val;
}
}
inimap parseFile(string file) {
if(readFile(file)) {
return parse();
} else {
auto val = _values;
val.clear();
return val;
}
}
inimap parse() {
_values.clear();
string head = "";
string headBefore = "";
vecstr vec;
int i = 0;
for(string line : _lines) {
//ignoring ini comments at begin of line
if(line[0] != ' ' && line != "") {
if(line[0] == _sectionOpen) {
// remove [] chars
head = line.substr(1, line.length()-2);
} else {
if(line.find(_keyValueDelim) != string::npos && line != "") {
vec = _split(line, _keyValueDelim);
if(vec[0] != "" && vec[1] != "") {
_values[head][vec[0]] = vec[1];
}
} else if(_comments && line[0] == _commentSign) {
if(i != 0 && head != headBefore)
i = 0;
_values[head][_commentSign + std::to_string(i)] = line;
headBefore = head;
i++;
}
}
}
}
return _values;
}
bool writeFile(string file_path, inimap& data) {
std::ofstream file(file_path);
try {
for(auto const& m1 : data) {
if(m1.first != "") {
if(m1.first != data.begin()->first) {
file << _lineSeparator;
}
file << _sectionOpen << m1.first << _sectionClose << _lineSeparator;
}
for(auto const& m2 : m1.second) {
if(_comments && (m2.first[0] == _commentSign || m2.first.find(_commentSign)
!= string::npos)) {
file << m2.second << _lineSeparator;
} else {
file << m2.first << _keyValueDelim << m2.second
<< _lineSeparator;
}
}
}
file.close();
return true;
} catch(std::exception ex) {
file.close();
throw;
}
return false;
}
/*
bool writeLine(string file_path, string section, string key, string value) {
vecstr lines;
std::fstream file;
bool isSection = false;
try {
file.open(file_path);
if(file.good()) {
for(string line; getline(file, line);) {
lines.push_back(line);
}
file.close();
} else {
file.close();
return false;
}
for(string l : lines) {
if(l.find("[" + section + "]") != string::npos &&
!isSection) {
isSection = true;
continue;
}
if(l.find("[") != string::npos && isSection) {
return false;
}
}
} catch(std::exception) {
file.close();
return false;
}
return true;
}
*/
string writeString(inimap& data) {
try {
std::string str = "";
for(auto const& m1 : data) {
if(m1.first != "") {
if(m1.first != data.begin()->first) {
str += _lineSeparator;
}
str += _sectionOpen + m1.first + _sectionClose + _lineSeparator;
}
for(auto const& m2 : m1.second) {
str += m2.first + _keyValueDelim + m2.second +
_lineSeparator;
}
}
return str;
} catch(std::exception ex) {
throw;
}
}
bool readString(string& str) {
try {
_lines.clear();
string line;
_lines = _split(str, _lineSeparator);
} catch(std::exception ex) {
return false;
}
return true;
}
bool readFile(string file) {
_lines.clear();
string line;
std::ifstream ini_file;
try {
ini_file.open(file);
if(ini_file.good()) {
while(!ini_file.eof()) {
std::getline(ini_file, line);
_lines.push_back(line);
}
ini_file.close();
return true;
} else {
return false;
}
} catch(std::exception ex) {
ini_file.close();
throw;
}
return false;
}
void setKeyValueDelimiter(string str) {
_keyValueDelim = str;
}
string getKeyValueDelimiter() {
return _keyValueDelim;
}
void setLineSeparator(string str) {
_lineSeparator = str;
}
string getLineSeparator() {
return _lineSeparator;
}
void setCommentSign(char c) {
_commentSign = c;
}
char getCommentSign() {
return _commentSign;
}
void setAllowComments(bool b) {
_comments = b;
}
bool getAllowComments() {
return _comments;
}
void setSectionTags(char open, char close) {
_sectionOpen = open;
_sectionClose = close;
}
void setSectionOpen(char open) {
_sectionOpen = open;
}
void setSectionClose(char close) {
_sectionClose = close;
}
char getSectionOpen() {
return _sectionOpen;
}
char getSectionClose() {
return _sectionClose;
}
private:
vecstr _lines;
inimap _values;
string _keyValueDelim = "=";
string _lineSeparator = "\n";
char _commentSign = ';';
bool _comments = false;
char _sectionOpen = '[';
char _sectionClose = ']';
vecstr _split(string& str, const string delim) {
vecstr parts;
std::size_t pos = 0;
string token;
while ((pos = str.find(delim)) != string::npos) {
token = str.substr(0, pos);
parts.push_back(token);
str.erase(0, pos + delim.length());
}
parts.push_back(str);
return parts;
}
};}