-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibProp.cpp
320 lines (274 loc) · 5.78 KB
/
libProp.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
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
#include "libProp.h"
using namespace std;
using namespace libProp;
void libProp::Value::CheckType(ValType type)
{
if (this->type != type)
throw runtime_error("type error.");
}
Value::Value(string data) :Value()
{
this->type = ValueType;
this->mData = data;
}
Value::Value(vector<Value> array) :Value()
{
this->type = ArrayType;//设置为数组的类型
this->mArray = move(array);
}
Value::Value() :isDataChanged(false)
{
}
libProp::Value::operator int()
{
try {
return stoi(this->mData);
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
Value::operator double()
{
try {
return stod(this->mData);
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
Value::operator float() {
try {
return stof(this->mData);
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
Value::operator long long()
{
try {
return stoll(this->mData);
}
catch (const exception& e) {
throw runtime_error(errMsgPrefix + e.what());
}
}
Value::operator std::string()
{
try {
return this->mData;
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
Value libProp::Value::operator[](int index)
{
try {
CheckType(ArrayType);//判断是否为数组的类型
Value temp = this->mArray.at(index);//获取数组下标的内容
return temp;//返回获取的内容
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
int libProp::Value::size()
{
try {
CheckType(ArrayType);//判断是否为数组的类型
return this->mArray.size();//返回数组的大小
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
Value::operator long()
{
try {
return stol(this->mData);
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
Value::operator bool()
{
try
{
if (this->mData == "true")
{
return true;
}
else if (this->mData == "false")
{
return false;
}
else//数据不合法
{
throw runtime_error("invalid bool data.");
}
}
catch (const exception& e)
{
throw runtime_error(errMsgPrefix + e.what());
}
}
Value::~Value()
{
}
std::string Value::toFileStr()
{
if (this->type == ValueType)
{
return this->mData;
}
else if (this->type == ArrayType)
{
stringstream tempData;
tempData << "[";
for (auto it = this->mArray.begin(); it != this->mArray.end(); it++)
{
if (it != this->mArray.end())
{
tempData << (*it).toFileStr() << ",";
}
}
tempData << "]";
}
}
void Config::ParseLineStr(std::string& data)
{
if(data.at(0) == '#')//判断是否为注释
{
this->mCommentsList.push_back(data);//将注释放入到注释列表中
return;
}
if (data == "" || data.find("=") == string::npos)//判断是否为有效的行
return;//不是有效行返回
string& keyData = data.substr(0, data.find("="));//获取等号前面的数据
string& valData = data.substr(data.find("=") + 1);//获取等号后面的数据
EraseFBSpace(keyData);//去除前面的空格
EraseFBSpace(valData);//去除后面的空格
if (*valData.begin() == '[')//判断是否以数组的格式开头
{
valData.erase(0, 1);
valData.pop_back();//删除末尾元素
vector<Value> temp;
while (valData != "")//没有提取完毕则一直循环提取
{
auto pos = valData.find(",");//获取分隔符的位置
string tempVal;
if (pos == string::npos)//判断是否有分隔符
{
tempVal = valData;
valData.clear();//没有分隔符则为最后一个元素
}
else {
tempVal = valData.substr(0, pos);
valData.erase(0, pos + 1);//删除包括分隔符的的已解析元素
}
temp.push_back(tempVal);//插入元素
}
Value tempVal(move(temp));
this->mConfMap.insert(make_pair(keyData, move(tempVal)));
}
else {
Value tempVal(move(valData));
this->mConfMap.insert(make_pair(keyData, move(tempVal)));
}
}
bool Config::isDataChanged()
{
for (auto& it : this->mConfMap)//遍历数据,判断数据是否有更改
{
if(it.second.isDataChanged)
{
return true;
}
}
return false;
}
Config Config::Parse(std::string&& filePath)
{
try {
fstream fs(filePath);
if (!fs.is_open())
{
throw runtime_error("Could not open file.");
}
string temp;
Config conf;//配置文件对象
while (getline(fs, temp))//一行行读取配置文件信息
{
conf.ParseLineStr(temp);//解析单行数据
}
fs.close();//关闭文件
conf.mFilePath = filePath;
return conf;
}
catch (const exception& e)
{
throw runtime_error(string("libProp:An error occured when parsing the file,error message: ") + e.what());
}
}
Config libProp::Config::operator=(const Config& conf)
{
this->mConfMap = conf.mConfMap;
this->mFilePath = conf.mFilePath;
return *this;
}
void Config::save()
{
for (auto& it : this->mConfMap)
{
it.second.isDataChanged = false;//更新保存状态
}
stringstream data;//写入到硬盘的数据
for(auto &it : this->mCommentsList)
{
data << it<<endl;//写入注释
}
for (auto &it : this->mConfMap)//遍历哈希表
{
data << it.first << " = " << it.second.toFileStr() << endl;
}
fstream fs(this->mFilePath, ios::out);
if (!fs.is_open())
{
throw runtime_error("Could not open the config file for writing.");
}
fs << data.str();
fs.close();//关闭文件
}
Config::Config()
{
}
Config::~Config()
{
if (isDataChanged())//判断是否需要保存
{
this->save();
}
}
Value& Config::operator[](const std::string& key)
{
if (this->mConfMap.find(key) == this->mConfMap.end())//无法找到键值
{
throw runtime_error("Could not find the key.");
}
return this->mConfMap[key];
}
void libProp::EraseFBSpace(std::string& data)
{
data.erase(0, data.find_first_not_of(" "));//删除前面的空格
data = data.substr(0, data.find_last_not_of(" ") + 1);//删除后面的空格
}