在开发实践中,积累了一些通用的C++ 类库,在此写出来给大家分享。也希望能给出更好的建议。工具库的名字是xtl——Properties 类。分两部分介绍。这篇介绍类的定义。下一篇将介绍类的实现。 。这篇介绍其中的读配置文件的类
下面是类的定义的头文件:
1
2 /*xtl/Properties.h
3 Author: ZhangTao
4 Date: Nov 6, 2008
5 */
6
7 # ifndef Properties_h
8 # define Properties_h
9
10 # include <algorithm>
11 # include <iterator>
12
13 # include <iostream>
14 # include <string>
15 # include <map>
16
17 namespace std {
18 // <map> member output operator
19 template<typename _U, typename _V>
20 ostream& operator<< (ostream& os, const pair<_U, _V>& val) {
21 return os << val.first << " = " << val.second;
22 }
23
24 // <map> output operator, ie. all members in <map> output to <ostream>
25 template<typename _U, typename _V>
26 ostream& operator<< (ostream& os, const map<_U, _V>& val) {
27 copy(val.begin(), val.end(), ostream_iterator< pair<_U, _V> >(os, "/n"));
28 return os;
29 }
30 } // end of <namespace std>
31
32 namespace xtl {
33
34 class Properties : public std::map<std::string, std::string> {
35 public:
36 Properties() {};
37 Properties(const char* fname, const char* section = "") {
38 load(fname, section);
39 }
40 Properties(std::istream& is, const char* section = "") {
41 load(is, section);
42 }
43
44 void load(const char* fname, const char* section = "");
45 void load(std::istream& is, const char* section = "");
46 void loadXML(const char* fname, const char* section = "");
47 void loadXML(std::istream& is, const char* section = "");
48
49 const std::string& getProperty(const std::string& key) const;
50
51 void list(std::ostream& os) const {
52 os << *this;
53 }
54 }; // end of <class Properties>
55
56 const char* const XML_ENTRY = "entry";
57 const char* const XML_KEY = "key";
58
59 } // end of <namespace xtl>
60
61 # endif /* end of <ifndef Properties_h> */
62
Properties 继承了stl 库的map 容器类。这样可以使用map 的各个接口实现。尤其是输出输入的重载的实现。
18 到22 行定义了map 单个元素的输出重载;25 到29 行则定义了map 全部元素的输出重载。正是有了这些铺垫,才使得51 行的list 函数显得那么简练。实际上list 函数完全可以不需要,可以使用<< 重载输出。例如:
Properties prop;
cout << prop;
等同于:
prop.list(cout);
Properties 的load 是用于从文件中读入数据的。它可以读入下面格式的文件:
# props.conf
# for Properties class test
TEST1=TEST1VALUE
TEST2=TEST2VALUE
TEST3=TEST3VALUE
TEST4 = 100
[Section0]
TEST01=TEST01VALUE
TEST02=TEST02VALUE
TEST03=TEST03VALUE
TEST04 = 200
[Section1]
TEST11=TEST11VALUE
TEST12=TEST12VALUE
TEST13=TEST13VALUE
TEST14 = 300
下面是测试程序:
/* tst-properties
test class Properties
*/
# include "xtl/Properties.h"
int
main(int argc, char* argv[])
{
const char* sec;
if ( argc > 1 )
sec = argv[1];
else
sec = "";
xtl::Properties prop(std::cin, sec);
prop.list(std::cout);
if ( argc > 2 )
std::cout << "Key:<" << argv[2] << "> Value:" <<
prop.getProperty(argv[2]) << "/n";
return 0;
}
上面的测试数据文件为 props.conf ,测试程序编译连接后的执行文件为 tst-properties 。
以下是运行结果。
$ tst-properties <props.conf
TEST1 = TEST1VALUE
TEST2 = TEST2VALUE
TEST3 = TEST3VALUE
TEST4 = 100
$ tst-properties Section0 ITEM04 <props.conf
TEST01 = TEST01VALUE
TEST02 = TEST02VALUE
TEST03 = TEST03VALUE
TEST04 = 200
Key:<TEST04> Value:200
由于继承了map类,相应的操作异常简练。最复杂的应该是load函数了。
load的实现,请阅下一篇文章。