文件读取解析boostproperty_tree,读取解析ini文件
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
boost::property_tree读取解析.xml文件
boost::property_tree读取解析.xml文件
1)read_xml 支持中文路径
boost::property_tree::wptree wpt;
std::locale::global(std::locale(""));
boost::property_tree::xml_parser::read_xml("E:\\测试\\test.xml",wpt);
2)get
ptree pt;
read_xml("D://test1.xml",pt); //读入一个xml文件
cout<<"ID is "<<pt.get<int>("con.id")<<endl;//读取节点中的信息
3)get_child
child = pt.get_child("con.urls");
for(BOOST_AUTO(pos,child.begin());pos != child.end();++pos) //boost中的auto
{
cout<<"\t"+pos->second.data()<<endl;
}
4)put
pt.put("","Sword"); //更改某个键值
5)add
pt.add("con.id","3"); //增加某个键值
6)write_xml
write_xml("D://test1.xml",pt); //写入XML
二boost::property_tree读取解析ini文件
[cpp]view plaincopy
1.#include "stdafx.h"
2.#include <iostream>
3.#include <boost/property_tree/ptree.hpp>
4.#include <boost/property_tree/ini_parser.hpp>
5.
6.int main()
7.{
8. boost::property_tree::ptree pt;
9. boost::property_tree::ini_parser::read_in
i("D:\\Overlay.ini", pt);
10.
11. std::cout << pt.get<std::string>("OVERLAY
.OverlayFontName") << std::endl;
12.
13. pt.put<std::string>("OVERLAY.OverlayFontN
ame","宋体");
14.
15. std::cout << pt.get<std::string>("OVERLAY
.OverlayFontName") << std::endl;
16.
17. boost::property_tree::ini_parser::write_i
ni("D:\\Overlay.ini",pt);
18.
19. return 0;
20.}
在C++11下,宽字节和单字节转换就简单了。
使用std::wstring_convert和
std::codecvt_utf8 来处理UTF8与WChar之间的互转.
[cpp]view plaincopy
1. #include <iostream>
2.#include <string>
3.#include <locale>
4.#include <codecvt>
5.#include <fstream>
6.
7.int main(int argc, char *argv[])
8.{
9. std::wstring str = L"123,宋体!";
10.
11. std::wstring_convert<std::codecvt_utf8<wch
ar_t>> conv;
12.
13. std::string narrowStr = conv.to_bytes(str)
;
14. {
15. std::ofstream ofs ("c:\\test.txt");
16. ofs << narrowStr;
17. }
18.
19. std::wstring wideStr = conv.from_bytes(nar
rowStr);
20. {
21. std::locale::global(std::locale("Chines
e-simplified"));
22. std::wofstream ofs (L"c:\\testW.txt");
23. ofs << wideStr;
24. }
25.}
另外可以保存函数,使用ptree:
1 struct debug_simple
2 {
3 int itsNumber;
4 std::string itsName; //这里使用string就可以
5 void load(const std::string& filename); //载入函数
6 void save(const std::string& filename); //保存函数
7 };
保存函数,使用ptree:
1 void debug_simple::save( const std::string& filename )
2 {
3 using boost::property_tree::ptree;
4 ptree pt;
5
6 pt.put("debug.number",itsNumber);
7 pt.put("",itsName);
8
9 write_xml(filename,pt);
10 }
载入函数使用的wptree,读取的值为wstring,需转换成string
1 void debug_simple::load( const std::string& filename )
2 {
3 using boost::property_tree::wptree;
4 wptree wpt;
5 read_xml(filename, wpt);
6
7 itsNumber = wpt.get<int>(L"debug.number");
8 std::wstring wStr = wpt.get<std::wstring>(L"");
9 itsName = std::string(wStr.begin(),wStr.end()); //wstring转string
10 }
main函数:
1 int _tmain(int argc, _TCHAR* argv[])
2 {
3
4 try
5 {
6 debug_simple ds,read;
7 ds.itsName = "汉字english";
8 ds.itsNumber = 20;
9
10 ds.save("simple.xml");
11 read.load("simple.xml");
12
13 std::cout<<read.itsNumber<<read.itsName;
14
15 }
16 catch (std::exception &e)
17 {
18 std::cout << "Error: " << e.what() << "\n";
19 }
20 return 0;
21 }
由于.ini文件是utf-8格式的,所以操作时要utf-8到unicode转换,或unicode到utf-8转换;
[cpp]view plaincopy
1.// PropertyTree.cpp : 定义控制台应用程序的入口
点。
2.//
3.
4.#include "stdafx.h"
5.#include <iostream>
ing namespace std;
7.
8.#include <boost/property_tree/ptree.hpp>
9.#include <boost/property_tree/ini_parser.hpp>
10.
11.//unicode 转UTF8
12.//参数1是UTF8字符串当前位置指针,这里必须要是指
针,因为必须要通过第1个字符进行判断才知道一个完整的字符的编码要向后取多少个字符
13.//参数2是返回的UCS-2编码的Unicode字符
14.inline int UTF82UnicodeOne(const char* utf8,
wchar_t& wch)
15.{
16. if (utf8==NULL)
17. {
18. return -1;
19. }
20.
21. //首字符的Ascii码大于0xC0才需要向后判断,否
则,就肯定是单个ANSI字符了
22. unsigned char firstCh = utf8[0];
23. if (firstCh >= 0xC0)
24. {
25. //根据首字符的高位判断这是几个字母的UTF8
编码
26. int afters, code;
27. if ((firstCh & 0xE0) == 0xC0)
28. {
29. afters = 2;
30. code = firstCh & 0x1F;
31. }
32. else if ((firstCh & 0xF0) == 0xE0)
33. {
34. afters = 3;
35. code = firstCh & 0xF;
36. }
37. else if ((firstCh & 0xF8) == 0xF0)
38. {
39. afters = 4;
40. code = firstCh & 0x7;
41. }
42. else if ((firstCh & 0xFC) == 0xF8)
43. {
44. afters = 5;
45. code = firstCh & 0x3;
46. }
47. else if ((firstCh & 0xFE) == 0xFC)
48. {
49. afters = 6;
50. code = firstCh & 0x1;
51. }
52. else
53. {
54. wch = firstCh;
55. return 1;
56. }
57.
58. //知道了字节数量之后,还需要向后检查一下,
如果检查失败,就简单的认为此UTF8编码有问题,或者
不是UTF8编码,于是当成一个ANSI来返回处理
59. for(int k = 1; k < afters; ++ k)
60. {
61. if ((utf8[k] & 0xC0) != 0x80)
62. {
63. //判断失败,不符合UTF8编码的规
则,直接当成一个ANSI字符返回
64. wch = firstCh;
65. return 1;
66. }
67.
68. code <<= 6;
69. code |= (unsigned char)utf8[k] &
0x3F;
70. }
71.
72. wch = code;
73. return afters;
74. }
75. else
76. {
77. wch = firstCh;
78. }
79. return 1;
80.}
81.
82.//参数1是UTF8编码的字符串
83.//参数2是输出的UCS-2的Unicode字符串
84.//参数3是参数1字符串的长度
85.//使用的时候需要注意参数2所指向的内存块足够用。
其
实安全的办法是判断一下pUniBuf是否为NULL,如果为NULL则只统计输出长度不写pUniBuf,这样
86.//通过两次函数调用就可以计算出实际所需要的
Unicode缓存输出长度。
当然,更简单的思路是:无论如何转换,UTF8的字符数量不可能比Unicode少,所87.//以可以简单的按照sizeof(wchar_t) * utf8Leng来
分配pUniBuf的内存……
88.int UTF82Unicode(const char* utf8Buf, wchar_t
*pUniBuf, int utf8Leng)
89.{
90.
91. if ((utf8Buf==NULL)||(pUniBuf==NULL))
92. {
93. return -1;
94. }
95.
96. int i = 0, count = 0;
97. while(i < utf8Leng)
98. {
99. i += UTF82UnicodeOne(utf8Buf + i, pUn
iBuf[count]);
100. count ++;
101. }
102.
103. return count;
104.}
105.
106.inline int Unicode2UTF8One(unsigned wchar , char *utf8)
107.{
108.
109. if (utf8==NULL)
110. {
111. return -1;
112. }
113.
114. int len = 0;
115. if (wchar < 0xC0)
116. {
117. utf8[len ++] = (char)wchar; 118. }
119. else if (wchar < 0x800)
120. {
121. utf8[len ++] = 0xc0 | (wchar >> 6 );
122. utf8[len ++] = 0x80 | (wchar & 0x 3f);
123. }
124. else if (wchar < 0x10000)
125. {
126. utf8[len ++] = 0xe0 | (wchar >> 1
2);
127. utf8[len ++] = 0x80 | ((wchar >>
6) & 0x3f);
128. utf8[len ++] = 0x80 | (wchar & 0x 3f);
129. }
130. else if (wchar < 0x200000) 131. {
132. utf8[len ++] = 0xf0 | ((int)wchar >> 18);
133. utf8[len ++] = 0x80 | ((wchar >>
12) & 0x3f);
134. utf8[len ++] = 0x80 | ((wchar >>
6) & 0x3f);
135. utf8[len ++] = 0x80 | (wchar & 0x 3f);
136. }
137. else if (wchar < 0x4000000) 138. {
139. utf8[len ++] = 0xf8 | ((int)wchar >> 24);
140. utf8[len ++] = 0x80 | ((wchar >>
18) & 0x3f);
141. utf8[len ++] = 0x80 | ((wchar >>
12) & 0x3f);
142. utf8[len ++] = 0x80 | ((wchar >>
6) & 0x3f);
143. utf8[len ++] = 0x80 | (wchar & 0x 3f);
144. }
145. else if (wchar < 0x80000000) 146. {
147. utf8[len ++] = 0xfc | ((int)wchar >> 30);
148. utf8[len ++] = 0x80 | ((wchar >>
24) & 0x3f);
149. utf8[len ++] = 0x80 | ((wchar >>
18) & 0x3f);
150. utf8[len ++] = 0x80 | ((wchar >>
12) & 0x3f);
151. utf8[len ++] = 0x80 | ((wchar >>
6) & 0x3f);
152. utf8[len ++] = 0x80 | (wchar & 0x 3f);
153. }
154.
155. return len;
156.}
157.
158.//
159.int Unicode2UTF8( const wchar_t *pUniBuf, char* utf8Buf, int UniLeng)
160.{
161. if ((utf8Buf==NULL)||(pUniBuf==NULL))
162. {
163. return -1;
164. }
165.
166. int count = 0, i = 0;
167. while(i < UniLeng)
168. {
169. count += Unicode2UTF8One(pUniBuf[ i], utf8Buf+count);
170. i ++;
171. }
172.
173. return count;
174.}
175.
176.int main()
177.{
178. boost::property_tree::ptree pt; 179. using boost::property_tree::wptree;
180. wptree wpt;
181.
182. boost::property_tree::ini_parser::rea d_ini("D:\\Overlay.ini", pt);
183.
184. std::string fontName=pt.get<std::stri ng>("OVERLAY.OverlayFontName") ;
185.
186. wchar_t wfontName[128]={0};
187.
188. UTF82Unicode(fontName.c_str(),wfontNa me,fontName.length());
189. std::wstring wstrfontName=wfontName;
190.
191. //std::wcout << wstrfontName.c_str()< < std::endl;
192.
193. /*std::wstring */
194. wstrfontName=_T("我是谁");
195. char cfontName[128]={0};
196.
197. Unicode2UTF8(wstrfontName.c_str(),cfo ntName,wstrfontName.length());
198.
199. pt.put<std::string>("OVERLAY.OverlayF ontName",cfontName);
200.
201. //std::cout << pt.get<std::string>("O VERLAY.OverlayFontName") << std::endl; 202.
203. boost::property_tree::ini_parser::wri te_ini("D:\\Overlay.ini",pt);
204.
205. return 0;
206.}。