forked from pjkui/webapplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaConfigFile.h
99 lines (72 loc) · 2.49 KB
/
waConfigFile.h
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
/// \file waConfigFile.h
/// INI格式配置文件解析类头文件
/// 依赖于 webapp::String, webapp::TextFile
#ifndef _WEBAPPLIB_CONFIGFILE_H_
#define _WEBAPPLIB_CONFIGFILE_H_
#include <string>
#include <vector>
#include <map>
using namespace std;
/// Web Application Library namaspace
namespace webapp {
/// INI格式配置文件解析类
class ConfigFile
{
public:
/// 默认构造函数
ConfigFile(): _file(""), _unsaved(false) {};
/// 参数为配置文件名的构造函数
ConfigFile( const string &file )
: _file(""), _unsaved(false) {
this->load( file );
}
/// 析构函数
~ConfigFile() {
if ( _unsaved ) this->save();
};
////////////////////////////////////////////////////////////////////////////
/// 读取解析配置文件
bool load( const string &file );
/// 保存配置文件
bool save( const string &file = "" );
/// 检查配置项是否存在
bool value_exist( const string &block, const string &name );
/// 检查配置块是否存在
bool block_exist( const string &block );
////////////////////////////////////////////////////////////////////////////
/// 读取配置项参数值
inline string operator[] ( const string &name ) {
return this->get_value( "", name );
}
/// 读取配置项参数值
string get_value( const string &block, const string &name, const string &default_value = "" );
/// 读取指定配置块的全部配置项参数值
map<string,string> get_block( const string &block );
/// 读取全部配置块列表
vector<string> block_list();
////////////////////////////////////////////////////////////////////////////
/// 更新配置项
inline bool set_value( const string &name, const string &value ) {
return this->set_value( "", name, value );
}
/// 更新配置项
bool set_value( const string &block, const string &name, const string &value );
/// 更新指定配置块的配置项列表
bool set_block( const string &block, const map<string,string> &valuelist );
////////////////////////////////////////////////////////////////////////////
/// 删除配置项
void del_value( const string &block, const string &name );
/// 删除配置块
void del_block( const string &block );
////////////////////////////////////////////////////////////////////////////
private:
typedef map<string,string> value_def; // name -> value
typedef map<string,value_def> block_def; // block -> ( name -> value )
string _file;
bool _unsaved;
block_def _config;
block_def::iterator _biter;
value_def::iterator _viter;
};
} // namespace
#endif //_WEBAPPLIB_CONFIGFILE_H_