-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIniParser.cpp
354 lines (305 loc) · 9.38 KB
/
IniParser.cpp
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
#include "resource.h"
#include "CommonFuncs.h"
#include "StringConversions.h"
#include "IniParser.h"
#define INITRUNKLENGTH 256
IniParser::IniParser()
{
}
IniParser::~IniParser()
{
}
void IniParser::FromFile(const tstring& iniFile)
{
_groupsVector.clear();
//Read file content
FILE *file = _tfopen(iniFile.c_str(), _T("r"));
if (!file)
return;
while(!feof(file))
{
//Get line
TCHAR tline[4096] = _T("");
_fgetts(tline, sizeof(tline), file);
//Parse
ParseLine(tline);
}
fclose(file);
}
void IniParser::FromString(const tstring& buffer)
{
StringVector lines = explode(buffer, _T("\n"));
for (int i = 0; i<lines.size(); i++)
ParseLine(lines.at(i));
}
void IniParser::ToFile(const tstring& iniFile)
{
//Erase previous content
FILE *file = _tfopen(iniFile.c_str(), _T("w"));
if (!file)
return;
int groupsCount = _groupsVector.size();
for (int g = 0; g<groupsCount; g++)
{
_ftprintf(file, _T("[%s]\n"), _groupsVector.at(g).name.c_str());
const IniKeyValueVector *keyValueVec = &_groupsVector.at(g).keyValueVec;
int keyValueCount = keyValueVec->size();
for (int kv = 0; kv<keyValueCount; kv++)
{
if (keyValueVec->at(kv).value.size() <= INITRUNKLENGTH)
{
_ftprintf(file, _T("%s=%s\n"), keyValueVec->at(kv).key.c_str(),
keyValueVec->at(kv).value.c_str());
}
else
{
const tstring& value = keyValueVec->at(kv).value;
//Split the value into many INITRUNKLENGTH long strings
tstring trunk = _T("");
for (int i = 0, start = 0;
start < value.size();
i++, start += INITRUNKLENGTH)
{
trunk = value.substr(start, INITRUNKLENGTH);
_ftprintf(file, _T("%s%d=%s\n"), keyValueVec->at(kv).key.c_str(), i, trunk.c_str());
}
}
}
}
fclose(file);
}
void IniParser::SetInt(LPCTSTR group, LPCTSTR key, int value)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1)
{
groupIndex = AddGroup(group);
AddKeyValue(groupIndex, key, i2a(value).c_str());
return;
}
int keyIndex = FindKey(groupIndex, key);
//Edit it
if (keyIndex != -1)
_groupsVector[ groupIndex ].keyValueVec[ keyIndex ].value = i2a(value);
//Add it
else
AddKeyValue(groupIndex, key, i2a(value).c_str());
}
void IniParser::SetLong(LPCTSTR group, LPCTSTR key, long value)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1)
{
groupIndex = AddGroup(group);
AddKeyValue(groupIndex, key, l2a(value).c_str());
return;
}
int keyIndex = FindKey(groupIndex, key);
//Edit it
if (keyIndex != -1)
_groupsVector[ groupIndex ].keyValueVec[ keyIndex ].value = l2a(value);
//Add it
else
AddKeyValue(groupIndex, key, l2a(value).c_str());
}
void IniParser::SetString(LPCTSTR group, LPCTSTR key, const tstring& value)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1)
{
groupIndex = AddGroup(group);
AddKeyValue(groupIndex, key, value.c_str());
return;
}
int keyIndex = FindKey(groupIndex, key);
//Edit it
if (keyIndex != -1)
_groupsVector[ groupIndex ].keyValueVec[ keyIndex ].value = value;
//Add it
else
AddKeyValue(groupIndex, key, value.c_str());
}
int IniParser::GetInt(LPCTSTR group, LPCTSTR key, int defaultValue)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1) return defaultValue;
int keyIndex = FindKey(groupIndex, key);
if (keyIndex == -1) return defaultValue;
return a2i( _groupsVector[ groupIndex ].keyValueVec[ keyIndex ].value );
}
long IniParser::GetLong(LPCTSTR group, LPCTSTR key, long defaultValue)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1) return defaultValue;
int keyIndex = FindKey(groupIndex, key);
if (keyIndex == -1) return defaultValue;
return a2l( _groupsVector[ groupIndex ].keyValueVec[ keyIndex ].value );
}
tstring IniParser::GetString(LPCTSTR group, LPCTSTR key, const tstring& defaultValue)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1) return defaultValue;
int keyIndex = FindKey(groupIndex, key);
if (keyIndex != -1)
return _groupsVector[ groupIndex ].keyValueVec[ keyIndex ].value;
else
{
tstring completeString = _T("");
for (int i = 0;
(keyIndex = FindKey(groupIndex, strprintf(_T("%s%d"), key, i).c_str())) != -1;
i++)
{
completeString += _groupsVector[ groupIndex ].keyValueVec[ keyIndex ].value;
}
if (completeString.empty())
return defaultValue;
return completeString;
}
}
int IniParser::CountMatchingGroups(LPCTSTR group)
{
int matchingGroupsCount = 0;
int groupCount = _groupsVector.size();
for (int i = 0; i<groupCount; i++)
{
if (_tcsstr(_groupsVector.at(i).name.c_str(), group))
matchingGroupsCount++;
}
return matchingGroupsCount;
}
void IniParser::EraseMatchingGroups(LPCTSTR group)
{
int groupCount = _groupsVector.size();
for (int i = groupCount-1; i >= 0; i--)
{
if (_tcsstr(_groupsVector.at(i).name.c_str(), group))
_groupsVector.erase( _groupsVector.begin() + i );
}
}
void IniParser::EraseGroup(LPCTSTR group)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1) return;
_groupsVector.erase( _groupsVector.begin() +groupIndex );
}
void IniParser::MoveGroup(LPCTSTR name, int index)
{
int groupIndex = FindGroup(name);
if (groupIndex == -1) return;
if (index >= _groupsVector.size()) return;
//Make a copy of this group
const INIGROUP group = _groupsVector[ groupIndex ];
//Erase the group
_groupsVector.erase( _groupsVector.begin() + groupIndex );
//Insert the group at the specified position
_groupsVector.insert( _groupsVector.begin() + index, group );
}
BOOL IniParser::IsGroup(LPCTSTR name)
{
return FindGroup(name) != -1;
}
std::vector<tstring> IniParser::GetGroupKeys(LPCTSTR group)
{
std::vector<tstring> list;
int groupIndex = FindGroup(group);
if (groupIndex == -1) return list;
int length = _groupsVector[groupIndex].keyValueVec.size();
for (int i = 0; i < length; i++)
list.push_back( _groupsVector[groupIndex].keyValueVec[i].key );
return list;
}
void IniParser::EraseKey(LPCTSTR group, LPCTSTR key)
{
int groupIndex = FindGroup(group);
if (groupIndex == -1) return;
int keyIndex = FindKey(groupIndex, key);
if (keyIndex == -1) return;
_groupsVector[groupIndex].keyValueVec.erase(
_groupsVector[groupIndex].keyValueVec.begin() +keyIndex );
}
void IniParser::ParseLine(tstring line)
{
trim(line);
if (line.empty())
return;
if (line.at(0) == _T('['))
{
int pos = line.find_last_of(_T(']'));
if (pos == tstring::npos)
return;
//New group
AddGroup( line.substr(1, pos-1).c_str() );
}
else
{
//No group, no key/value.
if (_groupsVector.empty())
return;
//No =, no key
int pos = line.find(_T('='));
if (pos == tstring::npos)
return;
AddKeyValue( _groupsVector.size()-1, line.substr(0, pos).c_str(),
line.substr(pos+1, line.size() -pos -2).c_str() );
}
}
int IniParser::AddGroup(LPCTSTR name)
{
//Add a new group
INIGROUP group;
group.name = name;
trim( group.name );
if (!group.name.empty())
{
_groupsVector.push_back(group);
return _groupsVector.size()-1;
}
return -1;
}
void IniParser::AddKeyValue(int groupIndex, LPCTSTR key, LPCTSTR value)
{
INIKEYVALUE keyValue;
keyValue.key = key;
keyValue.value = value;
trim( keyValue.key );
trim( keyValue.value );
if (!keyValue.key.empty())
{
if (groupIndex >= 0 && groupIndex < _groupsVector.size())
_groupsVector[groupIndex].keyValueVec.push_back(keyValue);
}
}
int IniParser::FindGroup(LPCTSTR group)
{
int groupCount = _groupsVector.size();
for (int i = 0; i<groupCount; i++)
{
if (_tcsicmp(group, _groupsVector.at(i).name.c_str()) == 0)
return i;
}
return -1;
}
int IniParser::FindKey(int groupIndex, LPCTSTR key)
{
const INIGROUP *group = &_groupsVector.at(groupIndex);
int keyValueCount = group->keyValueVec.size();
for (int i = 0; i<keyValueCount; i++)
{
if (_tcsicmp(key, group->keyValueVec.at(i).key.c_str()) == 0)
return i;
}
return -1;
}
std::vector<tstring> IniParser::explode(tstring stringToExplode, const tstring& separator)
{
std::vector<tstring> explodedStrings;
int sepPos = 0;
while( (sepPos = stringToExplode.find(separator, 0)) != tstring::npos )
{
explodedStrings.push_back(stringToExplode.substr(0, sepPos));
stringToExplode.erase(0, sepPos + separator.size());
}
if(stringToExplode != _T("") )
explodedStrings.push_back(stringToExplode);
return explodedStrings;
}