-
Notifications
You must be signed in to change notification settings - Fork 58
/
waConfigFile.cpp
269 lines (232 loc) · 6.34 KB
/
waConfigFile.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
/// \file waConfigFile.cpp
/// INI格式配置文件解析类实现文件
#include <fstream>
#include <set>
#include <algorithm>
#include "waString.h"
#include "waTextFile.h"
#include "waConfigFile.h"
using namespace std;
/// Web Application Library namaspace
namespace webapp {
/// 读取解析配置文件
/// \param file 配置文件路径名
/// \retval true 解析成功
/// \retval false 解析失败
bool ConfigFile::load( const string &file ) {
if ( file == "" ) return false;
_file = file;
size_t pos;
bool line_continue = false;
String curr_block, curr_name, curr_value;
String line;
TextFile config( file );
while ( config.next_line(line) ) {
line.trim();
// multi-line continue
if ( line_continue ) {
if ( line.right(2) == " \\" ) {
// continue...
line_continue = true;
line = line.substr( 0, line.length()-2 );
line.trim();
curr_value = line;
} else {
// end
line_continue = false;
curr_value = line;
_config[ curr_block ][ curr_name ] = curr_value;
}
}
// comment line
else if ( line[0] == '#' ) {
continue;
}
// block define
else if ( line[0]=='[' && line[line.length()-1]==']' ) {
curr_block = line.substr( 1, line.length()-2 );
curr_block.trim();
if ( curr_block != "" ) {
value_def null_value;
_config[ curr_block ] = null_value;
}
}
// value define
else if ( (pos=line.find("=")) != line.npos ) {
String name = line.substr( 0, pos );
String value = line.substr( pos 1 );
name.trim();
value.trim();
if ( name == "" )
continue;
if ( value.right(2) == " \\" ) {
// multi-line
line_continue = true;
value = value.substr( 0, value.length()-2 );
value.trim();
curr_name = name;
curr_value = value;
} else {
// single line
line_continue = false;
_config[ curr_block ][ name ] = value;
}
}
// unknown
else {
continue; // ignore
}
}
config.close();
return true;
}
/// 保存配置文件
/// \param file 配置文件路径名,默认为空则使用读取文件参数
/// \retval true 保存成功
/// \retval false 保存失败
bool ConfigFile::save( const string &file ) {
string config_file = _file;
if ( file != "" )
config_file = file;
// for each block
String content;
content.reserve( 1024 );
_biter = _config.begin();
for ( ; _biter!=_config.end(); _biter ) {
if ( _biter->first != "" )
content = "[" (_biter->first) "]\n";
// for each value
value_def valuelist = _biter->second;
_viter = valuelist.begin();
for ( ; _viter!=valuelist.end(); _viter )
content = (_viter->first) " = " (_viter->second) "\n";
}
_unsaved = !( content.save_file(config_file) );
return _unsaved;
}
/// 检查配置项是否存在
/// \param block 配置块名,为空表示全局配置块
/// \param name 配置项名
/// \retval true 存在
/// \retval false 不存在
bool ConfigFile::value_exist( const string &block, const string &name ) {
if ( name == "" )
return false;
// locate block
_biter = _config.find( block );
if ( _biter != _config.end() ) {
// locate name
value_def valuelist = _biter->second;
_viter = valuelist.find( name );
if ( _viter != valuelist.end() )
return true;
}
return false;
}
/// 检查配置块是否存在
/// \param block 配置块名,为空表示全局配置块
/// \retval true 存在
/// \retval false 不存在
bool ConfigFile::block_exist( const string &block ) {
if ( block == "" )
return true;
_biter = _config.find( block );
if ( _biter != _config.end() )
return true;
else
return false;
}
/// 读取配置项参数值
/// \param block 配置块名,为空表示全局配置块
/// \param name 配置项名
/// \param default_value 默认参数值,配置项不存在时返回
/// \return 指定配置项参数值
string ConfigFile::get_value( const string &block, const string &name,
const string &default_value ) {
if ( name == "" )
return default_value;
// locate block
_biter = _config.find( block );
if ( _biter == _config.end() )
return default_value;
// locate value
value_def valuelist = _biter->second;
_viter = valuelist.find( name );
if ( _viter == valuelist.end() )
return default_value;
return _viter->second;
}
/// 读取指定配置块的全部配置项参数值
/// \param block 配置块名,为空表示全局配置块
/// \return 指定配置块的全部配置项参数值列表
map<string,string> ConfigFile::get_block( const string &block ) {
value_def valuelist;
if ( block_exist(block) )
valuelist = _config[block];
return valuelist;
}
/// 读取全部配置块列表
/// \return 全部配置块列表,包括全局配置块(block值为空)
vector<string> ConfigFile::block_list() {
vector<string> blocklist;
_biter = _config.begin();
for ( ; _biter!=_config.end(); _biter )
blocklist.push_back( _biter->first );
return blocklist;
}
/// 更新配置项
/// \param block 配置块名,为空表示全局配置块
/// \param name 配置项名
/// \param value 配置参数值
/// \retval true 更新成功
/// \retval false 更新失败
bool ConfigFile::set_value( const string &block, const string &name,
const string &value )
{
if ( name == "" )
return false;
_config[ block ][ name ] = value;
_unsaved = true;
return _unsaved;
}
/// 更新指定配置块的配置项列表
/// \param block 配置块名,为空表示全局配置块
/// \param valuelist 配置参数值对列表
/// \retval true 更新成功
/// \retval false 更新失败
bool ConfigFile::set_block( const string &block,
const map<string,string> &valuelist )
{
if ( valuelist.size() <= 0 )
return false;
value_def::const_iterator i = valuelist.begin();
for ( ; i!=valuelist.end(); i )
_config[ block ][ i->first ] = i->second;
_unsaved = true;
return _unsaved;
}
/// 删除配置项
/// \param block 配置块名,为空表示全局配置块
/// \param name 配置项名
void ConfigFile::del_value( const string &block, const string &name ) {
_biter = _config.find( block );
if ( _biter != _config.end() ) {
_viter = (_biter->second).find( name );
if ( _viter != (_biter->second).end() ) {
(_biter->second).erase( _viter );
_unsaved = true;
return;
}
}
}
/// 删除配置块
/// \param block 配置块名,为空表示全局配置块
void ConfigFile::del_block( const string &block ) {
_biter = _config.find( block );
if ( _biter != _config.end() ) {
_config.erase( _biter );
_unsaved = true;
return;
}
}
} // namespace