forked from filgy/watcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iniParser.cpp
91 lines (68 loc) · 2.43 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
#include "iniParser.h"
/* - - - - - - - - - - - - - - - - - - - - - -
Class: iniFile
------------------------------------------- */
iniFile::iniFile(){
this->data.clear();
}
iniFile::~iniFile(){
this->data.clear();
}
void iniFile::addSection(string sectionName){
if(!this->isSection((sectionName)))
this->data.insert(pair<string, map<string, string> >(sectionName, map<string, string>()));
}
bool iniFile::isSection(string sectionName){
map<string, map<string, string> >::iterator p = this->data.find(sectionName);
return (p == this->data.end())? false : true;
}
void iniFile::setValue(string sectionName, string directive, string value){
if(!this->isSection(sectionName))
return;
if(this->data[sectionName].find(directive) == this->data[sectionName].end())
this->data[sectionName].insert(pair<string, string>(directive, value));
else
this->data[sectionName][directive] = value;
}
string iniFile::getValue(string sectionName, string directive){
if(!this->isSection(sectionName))
return "";
return (this->data[sectionName].find(directive) == this->data[sectionName].end())? "" : this->data[sectionName][directive];
}
list<string> iniFile::getSections(){
list<string> sections;
for(map<string, map<string, string> >::iterator p = this->data.begin(); p != this->data.end(); p++)
sections.push_back((*p).first);
return sections;
}
/* - - - - - - - - - - - - - - - - - - - - - -
Class: iniParser
------------------------------------------- */
iniFile iniParser::load(string fileName){
fstream fileHandler(fileName.c_str(), ios::in);
if(!fileHandler.is_open())
throw PARSER_CANNOT_OPEN_FILE;
iniFile file;
string line;
string section = "General";
string section_regul = "^\\[([A-z0-9\\-]+)\\]$";
string value_regul = "^([^\\=]+)\\=(.+)$";
regexApiMatch match;
while(getline(fileHandler, line)){
line = utility::trim(line);
if(regexApi::preg_match(section_regul, line, match)){
section = match[1];
file.addSection(section);
}
else if(regexApi::preg_match(value_regul, line, match)){
if(!file.isSection(section))
file.addSection(section);
file.setValue(section, match[1], match[2]);
}
else{
//piece of shit
}
}
fileHandler.close();
return file;
}