forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyFileConfiguration.cpp
More file actions
301 lines (245 loc) · 5.6 KB
/
PropertyFileConfiguration.cpp
File metadata and controls
301 lines (245 loc) · 5.6 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
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
//
// PropertyFileConfiguration.cpp
//
// Library: Util
// Package: Configuration
// Module: PropertyFileConfiguration
//
// Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Util/PropertyFileConfiguration.h"
#include "Poco/Exception.h"
#include "Poco/String.h"
#include "Poco/Path.h"
#include "Poco/FileStream.h"
#include "Poco/LineEndingConverter.h"
#include "Poco/Ascii.h"
using Poco::trim;
using Poco::Path;
namespace Poco {
namespace Util {
PropertyFileConfiguration::PropertyFileConfiguration() :
_preserveComment(false)
{
}
PropertyFileConfiguration::PropertyFileConfiguration(std::istream& istr, bool preserveComment) :
_preserveComment(preserveComment)
{
load(istr, preserveComment);
}
PropertyFileConfiguration::PropertyFileConfiguration(const std::string& path, bool preserveComment) :
_preserveComment(preserveComment)
{
load(path, preserveComment);
}
PropertyFileConfiguration::~PropertyFileConfiguration()
{
}
void PropertyFileConfiguration::load(std::istream& istr, bool preserveComment)
{
_preserveComment = preserveComment;
clear();
_fileContent.clear();
_keyFileContentItMap.clear();
while (!istr.eof()) parseLine(istr);
}
void PropertyFileConfiguration::load(const std::string& path, bool preserveComment)
{
Poco::FileInputStream istr(path);
if (istr.good())
load(istr, preserveComment);
else
throw Poco::OpenFileException(path);
}
void PropertyFileConfiguration::save(std::ostream& ostr) const
{
if (_preserveComment)
{
for (FileContent::const_iterator it = _fileContent.begin(); it != _fileContent.end(); ++it)
ostr << *it;
}
else
{
for (MapConfiguration::iterator it = begin(); it != end(); ++it)
ostr << composeOneLine(it->first, it->second);
}
}
void PropertyFileConfiguration::save(const std::string& path) const
{
Poco::FileOutputStream ostr(path);
if (ostr.good())
{
Poco::OutputLineEndingConverter lec(ostr);
save(lec);
lec.flush();
ostr.flush();
if (!ostr.good()) throw Poco::WriteFileException(path);
}
else throw Poco::CreateFileException(path);
}
// If _preserveComment is true, not only save key-value into map
// but also save the entire file into _fileContent.
// Otherwise, only save key-value into map.
void PropertyFileConfiguration::parseLine(std::istream& istr)
{
skipSpace(istr);
if (!istr.eof())
{
if (isComment(istr.peek()))
{
if (_preserveComment) saveComment(istr);
else skipLine(istr);
}
else saveKeyValue(istr);
}
}
void PropertyFileConfiguration::skipSpace(std::istream& istr) const
{
while (!istr.eof() && Poco::Ascii::isSpace(istr.peek())) istr.get();
}
int PropertyFileConfiguration::readChar(std::istream& istr)
{
for (;;)
{
int c = istr.get();
if (c == '\\')
{
c = istr.get();
switch (c)
{
case 't':
return '\t';
case 'r':
return '\r';
case 'n':
return '\n';
case 'f':
return '\f';
case '\r':
if (istr.peek() == '\n')
istr.get();
continue;
case '\n':
continue;
default:
return c;
}
}
else if (c == '\n' || c == '\r')
return 0;
else
return c;
}
}
void PropertyFileConfiguration::setRaw(const std::string& key, const std::string& value)
{
MapConfiguration::setRaw(key, value);
if (_preserveComment)
{
// Insert the key-value to the end of _fileContent and update _keyFileContentItMap.
if (_keyFileContentItMap.count(key) == 0)
{
FileContent::iterator fit = _fileContent.insert(_fileContent.end(), composeOneLine(key, value));
_keyFileContentItMap[key] = fit;
}
// Update the key-value in _fileContent.
else
{
FileContent::iterator fit = _keyFileContentItMap[key];
*fit = composeOneLine(key, value);
}
}
}
void PropertyFileConfiguration::removeRaw(const std::string& key)
{
MapConfiguration::removeRaw(key);
// remove the key from _fileContent and _keyFileContentItMap.
if (_preserveComment)
{
_fileContent.erase(_keyFileContentItMap[key]);
_keyFileContentItMap.erase(key);
}
}
bool PropertyFileConfiguration::isComment(int c) const
{
return c == '#' || c == '!';
}
void PropertyFileConfiguration::saveComment(std::istream& istr)
{
std::string comment;
int c = istr.get();
while (!isNewLine(c))
{
comment += (char) c;
c = istr.get();
}
comment += (char) c;
_fileContent.push_back(comment);
}
void PropertyFileConfiguration::skipLine(std::istream& istr) const
{
int c = istr.get();
while (!isNewLine(c)) c = istr.get();
}
void PropertyFileConfiguration::saveKeyValue(std::istream& istr)
{
int c = istr.get();
std::string key;
while (!isNewLine(c) && !isKeyValueSeparator(c))
{
key += (char) c;
c = istr.get();
}
std::string value;
if (isKeyValueSeparator(c))
{
c = readChar(istr);
while (!istr.eof() && c)
{
value += (char) c;
c = readChar(istr);
}
}
setRaw(trim(key), trim(value));
}
bool PropertyFileConfiguration::isNewLine(int c) const
{
return c == std::char_traits<char>::eof() || c == '\n' || c == '\r';
}
std::string PropertyFileConfiguration::composeOneLine(const std::string& key, const std::string& value) const
{
std::string result = key + ": ";
for (std::string::const_iterator its = value.begin(); its != value.end(); ++its)
{
switch (*its)
{
case '\t':
result += "\\t";
break;
case '\r':
result += "\\r";
break;
case '\n':
result += "\\n";
break;
case '\f':
result += "\\f";
break;
case '\\':
result += "\\\\";
break;
default:
result += *its;
break;
}
}
return result += "\n";
}
bool PropertyFileConfiguration::isKeyValueSeparator(int c) const
{
return c == '=' || c == ':';
}
} } // namespace Poco::Util