-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniParser.cpp
251 lines (201 loc) · 5.57 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
#include <iostream>
#include <fstream>
#include <map>
#include <string>
class IniException final : public std::exception
{
public:
explicit IniException(const std::string &aMessage) noexcept : mMessage(aMessage)
{
}
const char *what() const noexcept override
{
return mMessage.c_str();
}
private:
std::string mMessage;
};
typedef struct IniContext_s
{
char sectionStart = '[';
char sectionEnd = ']';
char pairSeparator = '=';
std::string commentsStart = ";#";
std::string spaces = " \f\t\v"; // no line feed or carriage return
} IniContext;
class IniHelper final
{
public:
IniHelper(const IniContext &aContext) noexcept : mContext(aContext)
{
}
std::string Trim(const std::string &aString) const noexcept
{
const auto first = aString.find_first_not_of(mContext.spaces);
if (first == std::string::npos)
{
return {};
}
const auto last = aString.find_last_not_of(mContext.spaces);
return aString.substr(first, last - first + 1);
}
private:
IniContext mContext;
};
class IniLexer final
{
public:
enum class Token : uint8_t
{
NONE,
COMMENT,
SECTION,
PAIR
};
explicit IniLexer(const IniContext &aContext = {}) noexcept : mContext(aContext), mHelper(aContext)
{
}
std::string ParseSection(std::string aLine) const
{
if (!IsSection(aLine))
{
throw IniException("Invalid section!");
}
aLine = mHelper.Trim(aLine);
const auto section(aLine.substr(1, aLine.size() - 2));
return mHelper.Trim(section);
}
std::pair<std::string, std::string> ParsePair(std::string aLine) const
{
if (!IsPair(aLine))
{
throw IniException("Invalid pair!");
}
aLine = mHelper.Trim(aLine);
const auto pos = aLine.find(mContext.pairSeparator);
const auto key = aLine.substr(0, pos);
const auto value = aLine.substr(pos + 1);
return {mHelper.Trim(key), mHelper.Trim(value)};
}
std::string FormatSection(const std::string &aSection) const noexcept
{
return mContext.sectionStart + aSection + mContext.sectionEnd;
}
std::string FormatPair(const std::string &aKey, const std::string &aValue) const noexcept
{
return aKey + mContext.pairSeparator + aValue;
}
Token FindToken(const std::string &aLine) const noexcept
{
if (IsComment(aLine))
{
return Token::COMMENT;
}
if (IsSection(aLine))
{
return Token::SECTION;
}
if (IsPair(aLine))
{
return Token::PAIR;
}
return Token::NONE;
}
private:
IniContext mContext;
IniHelper mHelper;
bool IsSection(std::string aLine) const noexcept
{
aLine = mHelper.Trim(aLine);
return aLine.front() == mContext.sectionStart && aLine.back() == mContext.sectionEnd;
}
bool IsPair(std::string aLine) const noexcept
{
aLine = mHelper.Trim(aLine);
return aLine.find(mContext.pairSeparator) != std::string::npos;
}
bool IsComment(const std::string &aLine) const noexcept
{
for (const auto commentStart : mContext.commentsStart)
{
if (aLine.front() == commentStart)
{
return true;
}
}
return false;
}
};
class IniParser final
{
public:
using Section = std::map<std::string, std::string>;
explicit IniParser(const IniContext &aContext = IniContext()) noexcept
: mContext(aContext), mHelper(mContext), mLexer(mContext)
{
}
void Deserialize(std::ifstream &aStream)
{
std::string sectionLast;
std::string line;
while (std::getline(aStream, line))
{
line = mHelper.Trim(line);
if (line.empty())
{
continue;
}
switch (mLexer.FindToken(line))
{
case IniLexer::Token::SECTION: {
sectionLast = mLexer.ParseSection(line);
}
break;
case IniLexer::Token::PAIR: {
const auto keyAndValue(mLexer.ParsePair(line));
mSections[sectionLast][keyAndValue.first] = keyAndValue.second;
}
break;
case IniLexer::Token::COMMENT:
case IniLexer::Token::NONE:
default:
break;
}
}
}
void Serialize(std::ofstream &aStream) const
{
const auto itLast = std::prev(mSections.cend(), 1);
for (auto it = mSections.begin(); it != mSections.end(); it++)
{
aStream << mLexer.FormatSection(it->first) << '\n';
for (const auto &keyAndValue : it->second)
{
aStream << mLexer.FormatPair(keyAndValue.first, keyAndValue.second) << '\n';
}
if (it != itLast)
{
aStream << '\n';
}
}
}
const std::string &Get(const std::string &aSection, const std::string &aKey) const
{
return mSections.at(aSection).at(aKey);
}
private:
std::map<std::string, Section> mSections;
IniContext mContext;
IniHelper mHelper;
IniLexer mLexer;
};
int main()
{
IniParser parser;
std::ifstream ifs("input.ini");
parser.Deserialize(ifs);
std::cout << parser.Get("Install", "AppFolder") << std::endl;
std::ofstream ofs("output.ini");
parser.Serialize(ofs);
return 0;
}