forked from tindy2013/subconverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini_reader.h
733 lines (640 loc) · 20.9 KB
/
ini_reader.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#ifndef INI_READER_H_INCLUDED
#define INI_READER_H_INCLUDED
#include <map>
#include <vector>
#include <algorithm>
typedef std::map<std::string, std::multimap<std::string, std::string>> ini_data_struct;
typedef std::multimap<std::string, std::string> string_multimap;
class INIReader
{
/**
* @brief A simple INI reader which utilize map and vector
* to store sections and items, allowing access in logarithmic time.
*/
private:
/**
* @brief Internal parsed flag.
*/
bool parsed = false;
std::string current_section;
ini_data_struct ini_content;
string_array exclude_sections, include_sections, read_sections;
std::string cached_section;
string_multimap cached_section_content;
std::string isolated_items_section;
bool chkIgnore(std::string section)
{
bool excluded = false, included = false;
if(count(exclude_sections.begin(), exclude_sections.end(), section) > 0)
excluded = true;
if(include_sections.size() != 0)
{
if(count(include_sections.begin(), include_sections.end(), section) > 0)
included = true;
}
else
included = true;
return excluded || !included;
}
public:
/**
* @brief Set this flag to true to do a UTF8-To-GBK conversion before parsing data. Only useful in Windows.
*/
bool do_utf8_to_gbk = false;
/**
* @brief Set this flag to true so any line within the section will be stored even it doesn't follow the "name=value" format.
* These lines will store as the name "{NONAME}".
*/
bool store_any_line = false;
/**
* @brief Save isolated items before any section definitions.
*/
bool store_isolated_line = false;
/**
* @brief Allow a section title to appear multiple times.
*/
bool allow_dup_section_titles = false;
/**
* @brief Initialize the reader.
*/
INIReader()
{
parsed = false;
}
/**
* @brief Parse a file during initialization.
*/
INIReader(std::string filePath)
{
parsed = false;
ParseFile(filePath);
}
~INIReader()
{
//nothing to do
}
/**
* @brief Exclude a section with the given name.
*/
void ExcludeSection(std::string section)
{
exclude_sections.emplace_back(section);
}
/**
* @brief Include a section with the given name.
*/
void IncludeSection(std::string section)
{
include_sections.emplace_back(section);
}
/**
* @brief Set isolated items to given section.
*/
void SetIsolatedItemsSection(std::string section)
{
isolated_items_section = section;
}
/**
* @brief Parse INI content into mapped data structure.
* If exclude sections are set, these sections will not be stored.
* If include sections are set, only these sections will be stored.
*/
int Parse(std::string content) //parse content into mapped data
{
if(!content.size()) //empty content
return -1;
//remove UTF-8 BOM
removeUTF8BOM(content);
bool inExcludedSection = false;
std::string strLine, thisSection, curSection, itemName, itemVal;
string_multimap itemGroup, existItemGroup;
std::stringstream strStrm;
unsigned int lineSize = 0;
char delimiter = count(content.begin(), content.end(), '\n') < 1 ? '\r' : '\n';
EraseAll(); //first erase all data
if(do_utf8_to_gbk && is_str_utf8(content))
content = UTF8ToGBK(content); //do conversion if flag is set
if(store_isolated_line)
curSection = isolated_items_section; //items before any section define will be store in this section
strStrm<<content;
while(getline(strStrm, strLine, delimiter))
//while(getline(strStrm, strLine)) //get one line of content
{
lineSize = strLine.size();
if(!lineSize || strLine[0] == ';' || strLine[0] == '#') //empty lines and comments are ignored
continue;
if(strLine[lineSize - 1] == '\r') //remove line break
{
strLine = strLine.substr(0, lineSize - 1);
lineSize--;
}
if(strLine.find("=") != strLine.npos) //is an item
{
if(inExcludedSection) //this section is excluded
continue;
if(!curSection.size()) //not in any section
return -1;
itemName = trim(strLine.substr(0, strLine.find("=")));
itemVal = trim(strLine.substr(strLine.find("=") + 1));
itemGroup.emplace(itemName, itemVal); //insert to current section
}
else if(strLine[0] == '[' && strLine[lineSize - 1] == ']') //is a section title
{
thisSection = strLine.substr(1, lineSize - 2); //save section title
inExcludedSection = chkIgnore(thisSection); //check if this section is excluded
if(curSection.size() && itemGroup.size()) //just finished reading a section
{
if(ini_content.count(curSection)) //a section with the same name has been inserted
{
if(allow_dup_section_titles)
{
eraseElements(existItemGroup);
existItemGroup = ini_content.at(curSection); //get the old items
for(auto &x : existItemGroup)
itemGroup.emplace(x); //insert them all into new section
ini_content.erase(curSection); //remove the old section
}
else
return -1; //not allowed, stop
}
ini_content.emplace(curSection, itemGroup); //insert previous section to content map
read_sections.emplace_back(curSection); //add to read sections list
}
eraseElements(itemGroup); //reset section storage
curSection = thisSection; //start a new section
}
else if(store_any_line && !inExcludedSection && curSection.size()) //store a line without name
{
itemGroup.emplace("{NONAME}", strLine);
}
if(include_sections.size() && include_sections == read_sections) //all included sections has been read
break; //exit now
}
if(curSection.size() && itemGroup.size()) //final section
{
if(ini_content.count(curSection)) //a section with the same name has been inserted
{
if(allow_dup_section_titles)
{
eraseElements(existItemGroup);
existItemGroup = ini_content.at(curSection); //get the old items
for(auto &x : existItemGroup)
itemGroup.emplace(x); //insert them all into new section
ini_content.erase(curSection); //remove the old section
}
else
return -1; //not allowed, stop
}
ini_content.emplace(curSection, itemGroup); //insert this section to content map
read_sections.emplace_back(curSection); //add to read sections list
}
parsed = true;
return 0; //all done
}
/**
* @brief Parse an INI file into mapped data structure.
*/
int ParseFile(std::string filePath)
{
if(!fileExist(filePath))
return -1;
return Parse(fileGet(filePath, false));
}
/**
* @brief Check whether a section exist.
*/
bool SectionExist(std::string section)
{
return ini_content.find(section) != ini_content.end();
}
/**
* @brief Count of sections in the whole INI.
*/
unsigned int SectionCount()
{
return ini_content.size();
}
/**
* @brief Return all section names inside INI.
*/
string_array GetSections()
{
string_array retData;
for(auto &x : ini_content)
{
retData.emplace_back(x.first);
}
return retData;
}
/**
* @brief Enter a section with the given name. Section name and data will be cached to speed up the following reading process.
*/
int EnterSection(std::string section)
{
if(!SectionExist(section))
return -1;
current_section = cached_section = section;
cached_section_content = ini_content.at(section);
return 0;
}
/**
* @brief Set current section.
*/
void SetCurrentSection(std::string section)
{
current_section = section;
}
/**
* @brief Check whether an item exist in the given section. Return false if the section does not exist.
*/
bool ItemExist(std::string section, std::string itemName)
{
if(!SectionExist(section))
return false;
if(section != cached_section)
{
cached_section = section;
cached_section_content= ini_content.at(section);
}
return cached_section_content.find(itemName) != cached_section_content.end();
}
/**
* @brief Check whether an item exist in current section. Return false if the section does not exist.
*/
bool ItemExist(std::string itemName)
{
return current_section.size() ? ItemExist(current_section, itemName) : false;
}
/**
* @brief Check whether an item with the given name prefix exist in the given section. Return false if the section does not exist.
*/
bool ItemPrefixExist(std::string section, std::string itemName)
{
if(!SectionExist(section))
return false;
if(section != cached_section)
{
cached_section = section;
cached_section_content= ini_content.at(section);
}
for(auto &x : cached_section_content)
{
if(x.first.find(itemName) == 0)
return true;
}
return false;
}
/**
* @brief Check whether an item with the given name prefix exist in current section. Return false if the section does not exist.
*/
bool ItemPrefixExist(std::string itemName)
{
return current_section.size() ? ItemPrefixExist(current_section, itemName) : false;
}
/**
* @brief Count of items in the given section. Return 0 if the section does not exist.
*/
unsigned int ItemCount(std::string section)
{
if(!parsed || !SectionExist(section))
return 0;
return ini_content.at(section).size();
}
/**
* @brief Erase all data from the data structure and reset parser status.
*/
void EraseAll()
{
eraseElements(ini_content);
parsed = false;
}
/**
* @brief Retrieve all items in the given section.
*/
int GetItems(std::string section, string_multimap &data)
{
if(!parsed || !SectionExist(section))
return -1;
if(cached_section != section)
{
cached_section = section;
cached_section_content = ini_content.at(section);
}
data = cached_section_content;
return 0;
}
/**
* @brief Retrieve all items in current section.
*/
int GetItems(string_multimap &data)
{
return current_section.size() ? GetItems(current_section, data) : -1;
}
/**
* @brief Retrieve item(s) with the same name prefix in the given section.
*/
int GetAll(std::string section, std::string itemName, string_array &results) //retrieve item(s) with the same itemName prefix
{
if(!parsed)
return -1;
string_multimap mapTemp;
if(GetItems(section, mapTemp) != 0)
return -1;
for(auto &x : mapTemp)
{
if(x.first.find(itemName) == 0)
results.emplace_back(x.second);
}
return 0;
}
/**
* @brief Retrieve item(s) with the same name prefix in current section.
*/
int GetAll(std::string itemName, string_array &results)
{
return current_section.size() ? GetAll(current_section, itemName, results) : -1;
}
/**
* @brief Retrieve one item with the exact same name in the given section.
*/
std::string Get(std::string section, std::string itemName) //retrieve one item with the exact same itemName
{
if(!parsed)
return std::string();
string_multimap mapTemp;
if(GetItems(section, mapTemp) != 0)
return std::string();
for(auto &x : mapTemp)
{
if(x.first == itemName)
return x.second;
}
return std::string();
}
/**
* @brief Retrieve one item with the exact same name in current section.
*/
std::string Get(std::string itemName)
{
return current_section.size() ? Get(current_section, itemName) : std::string();
}
/**
* @brief Retrieve one boolean item value with the exact same name in the given section.
*/
bool GetBool(std::string section, std::string itemName)
{
return Get(section, itemName) == "true";
}
/**
* @brief Retrieve one boolean item value with the exact same name in current section.
*/
bool GetBool(std::string itemName)
{
return current_section.size() ? Get(current_section, itemName) == "true" : false;
}
/**
* @brief Retrieve one integer item value with the exact same name in the given section.
*/
int GetInt(std::string section, std::string itemName)
{
return stoi(Get(section, itemName));
}
/**
* @brief Retrieve one integer item value with the exact same name in current section.
*/
int GetInt(std::string itemName)
{
return GetInt(current_section, itemName);
}
/**
* @brief Retrieve the first item found in the given section.
*/
std::string GetFirst(std::string section, std::string itemName) //return the first item value found in section
{
if(!parsed)
return std::string();
string_array result;
if(GetAll(section, itemName, result) != -1)
return result[0];
else
return std::string();
}
/**
* @brief Retrieve the first item found in current section.
*/
std::string GetFirst(std::string itemName)
{
return current_section.size() ? GetFirst(current_section, itemName) : std::string();
}
/**
* @brief Retrieve a string style array with specific separator and write into integer array.
*/
template <typename T> void GetIntArray(std::string section, std::string itemName, std::string separator, T &Array)
{
string_array vArray;
unsigned int index, UBound = sizeof(Array) / sizeof(Array[0]);
vArray = split(Get(section, itemName), separator);
for(index = 0; index < vArray.size() && index < UBound; index++)
Array[index] = stoi(vArray[index]);
for(; index < UBound; index++)
Array[index] = 0;
}
/**
* @brief Retrieve a string style array with specific separator and write into integer array.
*/
template <typename T> void GetIntArray(std::string itemName, std::string separator, T &Array)
{
if(current_section.size())
GetIntArray(current_section, itemName, separator, Array);
}
/**
* @brief Add a std::string value with given values.
*/
int Set(std::string section, std::string itemName, std::string itemVal)
{
std::string value;
if(!section.size())
return -1;
if(!parsed)
parsed = true;
if(SectionExist(section))
{
string_multimap &mapTemp = ini_content.at(section);
mapTemp.insert(std::pair<std::string, std::string>(itemName, itemVal));
}
else
{
string_multimap mapTemp;
mapTemp.insert(std::pair<std::string, std::string>(itemName, itemVal));
ini_content.insert(std::pair<std::string, std::multimap<std::string, std::string>>(section, mapTemp));
}
return 0;
}
/**
* @brief Add a string value with given values.
*/
int Set(std::string itemName, std::string itemVal)
{
if(!current_section.size())
return -1;
return Set(current_section, itemName, itemVal);
}
/**
* @brief Add a boolean value with given values.
*/
int SetBool(std::string section, std::string itemName, bool itemVal)
{
return Set(section, itemName, itemVal ? "true" : "false");
}
/**
* @brief Add a boolean value with given values.
*/
int SetBool(std::string itemName, bool itemVal)
{
return SetBool(current_section, itemName, itemVal);
}
/**
* @brief Add a double value with given values.
*/
int SetDouble(std::string section, std::string itemName, double itemVal)
{
return Set(section, itemName, std::__cxx11::to_string(itemVal));
}
/**
* @brief Add a double value with given values.
*/
int SetDouble(std::string itemName, double itemVal)
{
return SetDouble(current_section, itemName, itemVal);
}
/**
* @brief Add a long value with given values.
*/
int SetLong(std::string section, std::string itemName, long itemVal)
{
return Set(section, itemName, std::__cxx11::to_string(itemVal));
}
/**
* @brief Add a long value with given values.
*/
int SetLong(std::string itemName, long itemVal)
{
return SetLong(current_section, itemName, itemVal);
}
/**
* @brief Add an array with the given separator.
*/
template <typename T> int SetArray(std::string section, std::string itemName, std::string separator, T &Array)
{
std::string data;
for(auto &x : Array)
data += std::__cxx11::to_string(x) + separator;
data = data.substr(0, data.size() - separator.size());
return Set(section, itemName, data);
}
/**
* @brief Add an array with the given separator.
*/
template <typename T> int SetArray(std::string itemName, std::string separator, T &Array)
{
return current_section.size() ? SetArray(current_section, itemName, separator, Array) : -1;
}
/**
* @brief Erase all items with the given name.
*/
int Erase(std::string section, std::string itemName)
{
int retVal;
if(!SectionExist(section))
return -1;
retVal = ini_content.at(section).erase(itemName);
if(retVal && cached_section == section)
{
cached_section_content = ini_content.at(section);
}
return retVal;
}
/**
* @brief Erase all items with the given name.
*/
int Erase(std::string itemName)
{
return current_section.size() ? Erase(current_section, itemName) : -1;
}
/**
* @brief Erase the first item with the given name.
*/
int EraseFirst(std::string section, std::string itemName)
{
string_multimap &mapTemp = ini_content.at(section);
string_multimap::iterator iter = mapTemp.find(itemName);
if(iter != mapTemp.end())
{
mapTemp.erase(iter);
if(cached_section == section)
{
cached_section_content = mapTemp;
}
return 0;
}
else
{
return -1;
}
}
/**
* @brief Erase the first item with the given name.
*/
int EraseFirst(std::string itemName)
{
return current_section.size() ? EraseFirst(current_section, itemName) : -1;
}
/**
* @brief Erase all items in the given section.
*/
void EraseSection(std::string section)
{
if(ini_content.find(section) == ini_content.end())
return;
eraseElements(ini_content.at(section));
if(cached_section == section)
eraseElements(cached_section_content);
}
/**
* @brief Erase all items in current section.
*/
void EraseSection()
{
if(current_section.size())
EraseSection(current_section);
}
/**
* @brief Export the whole INI data structure into a string.
*/
std::string ToString()
{
std::string content;
if(!parsed)
return std::string();
for(auto &x : ini_content)
{
content += "[" + x.first + "]\n";
for(auto &y : x.second)
{
if(y.first != "{NONAME}")
content += y.first + " = ";
content += y.second + "\n";
}
content += "\n";
}
return content.substr(0, content.size() - 2);
}
/**
* @brief Export the whole INI data structure into a file.
*/
int ToFile(std::string filePath)
{
return fileWrite(filePath, ToString(), true);
}
};
#endif // INI_READER_H_INCLUDED