1、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); //读入一个xm
2、l文件
cout<<"ID is "< 3、值
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 4、parser.hpp>
5.
6. int main()
7. {
8. boost::property_tree::ptree pt;
9. boost::property_tree::ini_parser::read_ini("D:\\Overlay.ini", pt);
10.
11. std::cout << pt.get 5、LAY.OverlayFontName","宋体");
14.
15. std::cout << pt.get 6、codecvt_utf8 来处理UTF8与WChar之间的互转.
[cpp] view plaincopy
1. #include 7、string_convert 8、1. std::locale::global(std::locale("Chinese-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(c 9、onst 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.p 10、ut("debug.name",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 itsNum 11、ber = wpt.get 12、read;
7 ds.itsName = "汉字english";
8 ds.itsNumber = 20;
9
10 ds.save("simple.xml");
11 read.load("simple.xml");
12
13 std::cout< 13、r: " << 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 14、 #include 15、ch)
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编码
16、
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; 17、
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. {
18、
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. { 19、
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 20、)
62. {
63. //判断失败,不符合UTF8编码的规则,直接当成一个ANSI字符返回
64. wch = firstCh;
65. return 1;
66. }
67.
68. code <<= 6;
69. code |= (unsigned char)utf8[k] & 0x3F;
70. 21、 }
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字符串的长度
22、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 23、 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, pUniBuf[count]);
10 24、0. 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; 25、
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 & 0x3f);
123. }
124. 26、 else if (wchar < 0x10000)
125. {
126. utf8[len ++] = 0xe0 | (wchar >> 12);
127. utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);
128. utf8[len ++] = 0x80 | (wchar & 0x3f);
129. }
130. else if (wchar < 0x200000)
131. {
27、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 & 0x3f);
136. }
137. else if (wchar < 0x4000000)
28、
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 29、 | (wchar & 0x3f);
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. 30、utf8[len ++] = 0x80 | ((wchar >> 12) & 0x3f);
151. utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);
152. utf8[len ++] = 0x80 | (wchar & 0x3f);
153. }
154.
155. return len;
156. }
157.
158. //
159. int Unicode2UTF8( const wchar_t *pUniBuf,c 31、har* 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], utf8Bu 32、f+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::i 33、ni_parser::read_ini("D:\\Overlay.ini", pt);
183.
184. std::string fontName=pt.get 34、ame=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(),cfontName,wstrfontName.length());
198. 35、
199. pt.put






