-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiniparser.cpp
More file actions
174 lines (139 loc) · 3.45 KB
/
iniparser.cpp
File metadata and controls
174 lines (139 loc) · 3.45 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
#include "stdafx.h"
#include "iniparser.h"
#include "util.h"
namespace FileSystem
{
void IniParser::trim(std::string& str)
{
unsigned startPos = str.size();
unsigned endPos = 0;
for (unsigned i = 0; i < str.size(); ++i)
{
if (!isblank(str[i]))
{
startPos = i;
break;
}
}
for (unsigned i = str.size() - 1; i > startPos; --i)
{
if (!isblank(str[i]))
{
endPos = i + 1;
break;
}
}
if (startPos == str.size() || endPos == 0)
{
str.clear();
return;
}
else
str = str.substr(startPos, endPos - startPos);
}
bool IniParser::parseFile(const char path[])
{
std::fstream file(path, std::ios::in);
if (!file)
{
Warning(std::string("Error opening ") + path);
return false;
}
std::string activeSystem = "DEFAULT";
std::string temp;
unsigned lineNo = 0;
static unsigned ustrnpos = (unsigned)std::string::npos;
while (getline(file, temp))
{
++lineNo;
//Discard invalid lines.
if (temp.size() == 0) continue;
unsigned eqPos = temp.find('=');
if (eqPos == ustrnpos)
{
//Check whether it's a comment.
trim(temp);
if (!temp.size()) continue; //Just empty line
//If a command to change the System:
if (temp[0] == '[')
{
unsigned endPos = temp.rfind(']');
if (endPos == ustrnpos) //If no ending symbol (invalid. Could guess, but best ignored)
{
Warning(std::string("Invalid line ") + toString(lineNo) + " in " + path +
" System declaration missing ']'.");
continue;
}
activeSystem = temp.substr(1, endPos - 1);
toUpperCase(activeSystem);
trim(activeSystem);
continue;
}
if (temp[0] != '#') //Not a comment - error
Warning(std::string("Invalid line ") + toString(lineNo) + " in " + path +
" : Non-comment line does not contain '='.");
continue; //No need to proccess further if comment
}
toLowerCase(temp);
//split (into arr[0] and arr[1]) (left hand size, right hand size)
std::string arr[2];
arr[0] = temp.substr(0, eqPos);
arr[1] = temp.substr(eqPos + 1);
trim(arr[0]);
trim(arr[1]);
//If either side is empty:
if (arr[0].size() == 0 || arr[1].size() == 0)
{
Warning(std::string("Invalid line ") + toString(lineNo) + " in " + path +
" : One or more sides of an association are empty.");
continue;
}
//If still a comment:
if ((arr[0])[0] == '#')
continue;
//If commands contain spaces:
if (!(none_of(arr[0].begin(), arr[0].end(), isblank) &&
none_of(arr[1].begin(), arr[1].end(), isblank)))
{
Warning(std::string("Invalid line ") + toString(lineNo) + " in " + path + " : Name of command contains spaces.");
continue;
}
//If system does not exist:
SystemListType::iterator sPos = systems.find(activeSystem);
if (sPos == systems.end())
{
std::map<std::string, std::string> entry;
entry[arr[0]] = arr[1];
systems[activeSystem] = entry;
}
//If system exists, update the internal map:
else
{
sPos->second[arr[0]] = arr[1];
}
}
init = true;
return true;
}
void IniParser::clear()
{
systems.clear();
init = false;
}
bool IniParser::initialised()
{
return init;
}
SystemListType::const_iterator IniParser::find(const std::string& system)
{
return systems.find(system);
}
SystemListType::const_iterator IniParser::begin()
{
return systems.begin();
}
SystemListType::const_iterator IniParser::end()
{
return systems.end();
}
}//End of namespace