-
Notifications
You must be signed in to change notification settings - Fork 4
/
ini.cpp
513 lines (440 loc) · 13.5 KB
/
ini.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*=============================================================================
#
# Author: carbon - ecras_y@163.com
#
# QQ : 1330186768
#
# Last modified: 2016-05-09 11:08
#
# Filename: ini.cpp
#
# Description: CIniParser implementation
# CStdString a class emulate CString & std::string
# Utf8_16_Read from notepadd++ dealing with text encoding(utf8, unicode le & be)
#
=============================================================================*/
#include <iostream>
#include "ini.h"
CIniParser::CIniParser(LPCTSTR lpIniFile)
: m_strIniFile(lpIniFile)
, m_bParse(false)
, m_bUtf8Bom(false)
, m_bModified(false)
, m_bDosFormat(false)
{
}
CIniParser::~CIniParser()
{
Save();
}
bool CIniParser::Save()
{
if (m_bModified)
{
m_bModified = false;
IniData tmpData = m_iniData;
CStdString tmpFile = m_strIniFile + _T(".tmp");
std::ofstream fout;
fout.open(tmpFile.c_str(),
std::ios_base::trunc | std::ios_base::out | std::ios_base::binary);
std::ifstream fin;
fin.open(m_strIniFile.c_str(),
std::ios_base::in | std::ios_base::binary);
if (fin.good() && fout.good())
{
if (m_bUtf8Bom)
{
unsigned char bom[3] = { 0xef, 0xbb, 0xbf };
fout.write((char*)bom, 3);
fin.seekg(3, fin.beg);
}
CStdString strSection;
CStdString strOldSection;
std::string s;
while (!fin.eof())
{
if (std::getline(fin, s).good())
{
WriteSectionOldLine(fout, strSection, strOldSection, s);
}
}
WriteNewSection(fout);
fout.close();
#ifdef _WIN32
MoveFileEx(tmpFile, m_strIniFile, MOVEFILE_REPLACE_EXISTING);
#else
rename(tmpFile.c_str(), m_strIniFile.c_str());
#endif
}
else
{
return false;
}
m_iniData = tmpData;
for (auto& kvp : m_iniData) {
for (auto& ivp : kvp.second) {
ivp.second.bModify = false;
}
}
return true;
}
return true;
}
bool CIniParser::IsLineModified(const CStdString& strLine, const CStdString& strSection, CStdString& strKey)
{
if (IsComment(strLine)) return false;
IniVal iniVal;
if (GetIniValue(strLine, strKey, iniVal))
{
auto it = m_iniData.find(strSection);
if (it != m_iniData.end())
{
auto itsub = it->second.find(strKey);
if (itsub != it->second.end())
{
return itsub->second.bModify;
}
}
}
return false;
}
bool CIniParser::IsEmptySection(const CStdString& strSection)
{
auto it = m_iniData.find(strSection);
if (it != m_iniData.end())
{
return it->second.empty();
}
return true;
}
bool CIniParser::WriteSectionOldLine(std::ofstream& fout, CStdString& strSection, CStdString& strOldSection, const std::string& s)
{
CStdString strLine = s.c_str();
static bool bEmptySection = false;
strLine.Trim();
strOldSection = strSection;
if (IsSectionLine(strLine, strSection))
{
//write new key value pair to old section
WriteSectionNewLine(fout, strOldSection);
if (IsEmptySection(strSection))
{
//ignore section
bEmptySection = true;
}
else
{
bEmptySection = false;
//write section
fout.write(s.c_str(), s.size());
fout.write(("\n"), 1);
}
printf("section line [%s] in empty section: %d\n", strLine.c_str(), bEmptySection);
}
else
{
printf("normal line [%s] in empty section: %d\n", strLine.c_str(), bEmptySection);
if (bEmptySection == false)
{
CStdString strKey;
if (IsLineModified(strLine, strSection, strKey))
{
WriteModifiedLine(fout, strSection, strKey);
}
else
{
fout.write(s.c_str(), s.size());
fout.write(("\n"), 1);
}
m_iniData[strSection].erase(strKey);
}
}
return true;
}
bool CIniParser::WriteModifiedLine(std::ofstream& fout, const CStdString& strSection, const CStdString& strKey)
{
CStdString strLine;
IniSection& is = m_iniData[strSection];
CStdString& strVal = is[strKey].strVal;
if (strVal.IsEmpty()) {
return true;
}
if (m_bDosFormat) strLine.Format(("%s=%s\r\n"), strKey.c_str(), strVal.c_str());
else strLine.Format(("%s=%s\n"), strKey.c_str(), strVal.c_str());
fout.write(strLine.c_str(), strLine.GetLength());
return true;
}
bool CIniParser::WriteSectionNewLine(std::ofstream& fout, const CStdString& strSection)
{
if (strSection.IsEmpty() == false)
{
auto it = m_iniData.find(strSection);
if (it != m_iniData.end())
{
for (const auto& ivp : it->second)
{
CStdString strLine;
if (m_bDosFormat) strLine.Format(("%s=%s\r\n"), ivp.first.c_str(), ivp.second.strVal.c_str());
else strLine.Format(("%s=%s\n"), ivp.first.c_str(), ivp.second.strVal.c_str());
fout.write(strLine.c_str(), strLine.GetLength());
}
m_iniData.erase(it);
}
}
m_iniData.erase(strSection);
return true;
}
bool CIniParser::WriteNewSection(std::ofstream& fout)
{
for (const auto& kvp : m_iniData)
{
if (kvp.second.empty()) continue;
if (m_bDosFormat) fout << "[" << kvp.first.c_str() << "]\r\n";
else fout << "[" << kvp.first.c_str() << "]\n";
for (const auto& ivp : kvp.second)
{
if (m_bDosFormat) fout << ivp.first.c_str() << "=" << ivp.second.strVal.c_str() << "\r\n";
else fout << ivp.first.c_str() << "=" << ivp.second.strVal.c_str() << "\n";
}
}
return true;
}
bool CIniParser::Parse(LPCTSTR lpIniFile /* = _T("") */)
{
//if file name same as old name
if (m_strIniFile == lpIniFile) {
if (m_bParse) return m_iniData.empty() == false;
}
//else go through
CStdString strTmpName = lpIniFile;
if (strTmpName.IsEmpty()) //if file name is empty
{
if (m_bParse) return m_iniData.empty() == false;
}
else m_strIniFile = strTmpName;
m_iniData.clear();
m_bParse = false;
m_bUtf8Bom = false;
if (m_strIniFile.IsEmpty()) return m_bParse;
std::ifstream infile;
infile.open(m_strIniFile.c_str(), std::ios_base::binary | std::ios_base::in);
if (infile.good())
{
infile.seekg(0, infile.end);
std::streamoff len = infile.tellg();
infile.seekg(0, infile.beg);
if (len > 0)
{
std::vector<BYTE> buf;
buf.resize(static_cast<size_t>(len));
infile.read((char *)&buf[0], len);
infile.close();
UniMode encoding = Utf8_16_Read::determineEncoding(&buf[0], buf.size());
if (encoding == uniUTF8) m_bUtf8Bom = true;
else if (encoding == uni16BE || encoding == uni16LE)
{
//Convert buffer to utf8
Utf8_16_Read convertor;
size_t nret = convertor.convert((char*)&buf[0], buf.size());
buf.resize(nret);
memcpy(&buf[0], convertor.getNewBuf(), nret);
std::ofstream fout;
fout.open(m_strIniFile.c_str(),
std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
if (fout.good())
{
unsigned char bom[3] = { 0xef, 0xbb, 0xbf };
fout.write((char*)bom, 3);
fout.write((char*)(&buf[0]), buf.size());
fout.close();
}
}
Parse(buf);
}
}
m_bParse = true;
return m_iniData.empty() == false;
}
CStdString CIniParser::GetLineString(const std::vector<BYTE>& buf, int& posBegin)
{
int buflen = static_cast<int>(buf.size());
if (buf.empty() || posBegin >= buflen)
{
posBegin = 0;
return ("");
}
else
{
if (posBegin < 0) posBegin = 0;
CStdString strResult;
auto it = std::find(buf.begin() + posBegin, buf.end(), '\n');
if (it != buf.end())
{
strResult.append(buf.begin() + posBegin, it + 1);
if (m_bDosFormat == false)
{
if (strResult.Find(("\r\n")) != -1)
m_bDosFormat = true;
}
posBegin = it - buf.begin() + 1;
strResult.Trim();
return strResult;
}
else
{
strResult.append(buf.begin() + posBegin, buf.end());
strResult.Trim();
posBegin = buf.size();
return strResult;
}
}
}
bool CIniParser::IsSectionLine(const CStdString& strLine, CStdString& strSection)
{
if (strLine.Left(1) != ("[")) return false;
char cleft = '[';
char cright = ']';
int n1 = strLine.Find(cleft);
if (n1 != -1)
{
int n2 = strLine.Find(cright, n1 + 1);
if (n2 != -1)
{
CStdString tmpValue;
tmpValue = strLine.Mid(n1 + 1, n2 - n1 - 1);
tmpValue.Trim();
if (tmpValue.IsEmpty() == false)
{
strSection = tmpValue;
return true;
}
}
}
return false;
}
bool CIniParser::GetIniValue(const CStdString& strLine, CStdString& strKey, IniVal& iniVal)
{
int nfind = strLine.Find('=');
if (nfind != -1)
{
strKey = strLine.Mid(0, nfind);
strKey.Trim();
if (strKey.IsEmpty() == false)
{
iniVal.strVal = strLine.Mid(nfind + 1);
iniVal.strVal.Trim();
return true;
}
}
return false;
}
bool CIniParser::IsComment(const CStdString& strLine)
{
if (strLine.Left(1) == (";")) return true;
else return false;
}
bool CIniParser::Parse(const std::vector<BYTE>& buf)
{
CStdString strLine;
int pos = 0;
int buflen = static_cast<int>(buf.size());
if (m_bUtf8Bom) pos += 3;
CStdString strSection;
CStdString strNewSection;
IniSection sectionData;
while (pos < buflen)
{
strLine = GetLineString(buf, pos);
if (strLine.IsEmpty() == false)
{
if (IsComment(strLine.c_str()) == false)
{
if (IsSectionLine(strLine, strNewSection)) //new section coming
{
if (!strSection.IsEmpty()) //save old section data
{
m_iniData.insert(std::make_pair(strSection, sectionData));
}
//deal with coming section
sectionData.clear();
strSection = strNewSection;
}
else
{
if (strSection.IsEmpty()) continue;
//deal with coming section
strNewSection.clear();
CStdString strKey;
IniVal iniValue;
if (GetIniValue(strLine, strKey, iniValue))
{
sectionData.insert(std::make_pair(strKey, iniValue));
}
}
} //if (IsComment(strLine.c_str()) == false)
} //if (strLine.IsEmpty() == false)
} //while (pos < buflen)
//save last section data
m_iniData.insert(std::make_pair(strSection, sectionData));
return true;
}
bool CIniParser::SetValue(const CStdString& strSection, const CStdString& strKey, const CStdString& strVal)
{
auto it = m_iniData.find(strSection);
if (it != m_iniData.end()) //find section
{
auto itsub = it->second.find(strKey);
if (itsub != it->second.end()) //find key
{
if ((strVal.IsEmpty() == false) && (itsub->second.strVal == strVal)) return true;
else it->second.erase(itsub);
}
{
// if key exist and value is not equal, remove exist key value pair
// add new key value pair to exist section
IniVal iniVal;
iniVal.strVal = strVal;
iniVal.bModify = true;
it->second.insert(std::make_pair(strKey, iniVal));
}
}
else //add new key to new setion
{
IniSection newSection;
IniVal iniVal;
iniVal.strVal = strVal;
iniVal.bModify = true;
newSection.insert(std::make_pair(strKey, iniVal));
m_iniData.insert(std::make_pair(strSection, newSection));
}
m_bModified = true;
return true;
}
bool CIniParser::RemoveKey(const CStdString& strSection, const CStdString& strKey)
{
return SetValue(strSection, strKey, (""));
}
bool CIniParser::RemoveSection(const CStdString& strSection)
{
auto it = m_iniData.find(strSection);
if (it != m_iniData.end()) //find section
{
it->second.clear();
m_iniData.erase(it);
}
m_bModified = true;
return true;
}
void CIniParser::DumpIni(void)
{
std::cout << "dos format: " << (m_bDosFormat ? "true" : "false") << "\n";
for (const auto& kvp : m_iniData)
{
std::cout << "[" << kvp.first.c_str() << "]\n";
for (const auto& ivp : kvp.second)
{
std::cout << ivp.first.c_str() << "=" << ivp.second.strVal.c_str() << "\n";
}
std::cout << "\n";
}
}