BSTTree.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. #pragma once
  2. #include <vector>
  3. #include <list>
  4. #include <string>
  5. #include <functional>
  6. #include <memory>
  7. #pragma warning (disable:4150)
  8. // 删除指向不完整“boost::property_tree::basic_ptree<std::string, std::string, std::less<Key>>”类型的指针;没有调用析构函数
  9. #pragma warning (disable:4251)
  10. // ECOM::Utility::BSTTree::m_Root: class“XDWHolder<boost::property_tree::ptree>”需要有 dll 接口由 class“ECOM::Utility::BSTTree”的客户端使用
  11. namespace boost
  12. {
  13. namespace property_tree
  14. {
  15. template < class Key, class Data, class KeyCompare = std::less<Key> >
  16. class basic_ptree;
  17. typedef basic_ptree <std::string, std::string> ptree;
  18. typedef ptree tTree;
  19. // typedef basic_ptree <std::string, DString> tTree;
  20. };
  21. };
  22. using namespace boost::property_tree;
  23. #include "XDWHolder.tlh"
  24. #include "XDWHolder.tli"
  25. #include "DString.hpp"
  26. #include "WString.hpp"
  27. #ifndef BSTTree_EXPORTS
  28. #define _BSTTree_API _declspec(dllimport)
  29. #else
  30. #define _BSTTree_API _declspec(dllexport)
  31. #endif
  32. //-----------------------------------------------------------------------------
  33. // BSTTree
  34. // 对 tTree 的封装
  35. //
  36. // 不能用 std::unique_ptr, 因为 unique_ptr 需要知道 tTree 才能析构
  37. // 因此这里用 XDWHolder
  38. //-----------------------------------------------------------------------------
  39. namespace ECOM
  40. {
  41. namespace Utility
  42. {
  43. class _BSTTree_API BSTTree
  44. {
  45. private:
  46. tTree * m_Tree;
  47. XDWHolder <tTree> m_Root;
  48. bool m_bAttrib;
  49. private:
  50. BSTTree (XDWHolder <tTree> && from);
  51. public:
  52. BSTTree ();
  53. BSTTree (BSTTree && from);
  54. BSTTree (const BSTTree & from) = delete;
  55. BSTTree & operator = (BSTTree && from);
  56. BSTTree & operator = (const BSTTree & from) = delete;
  57. public:
  58. bool IsEmpty () const;
  59. operator bool () const { return ! IsEmpty (); }
  60. // 返回儿子节点的数量
  61. int Size () const;
  62. // 数一数其中有多少个路径
  63. int Count (const std::string & path) const;
  64. protected:
  65. BSTTree __Into (const std::string & path) const;
  66. BSTTree __Into (const std::string & path, char sep) const;
  67. public:
  68. BSTTree Into (const std::string & path) const;
  69. BSTTree Into (const DString & path) const;
  70. BSTTree Into (PCTSTR path) const { return Into (std::string (path)); }
  71. public:
  72. BSTTree Into (const std::string & path, char sep) const
  73. {
  74. return __Into (path, sep);
  75. }
  76. public:
  77. BSTTree Attrib ();
  78. protected:
  79. std::string TryAttribOf (const std::string & path, PCTSTR Def) const;
  80. std::string TryAttribOf (const std::string & path, const std::string & Def) const;
  81. public:
  82. template <typename T> T AttribOf (const std::string & path) const
  83. {
  84. // also equal to Attrib ().Get <T> (path);
  85. return AttribOf (path).To <T> ();
  86. }
  87. // 如果不写类型转换, 默认返回 DString 类型
  88. DString AttribOf (const std::string & path) const
  89. {
  90. return DString (TryAttribOf (path, std::string ()).c_str ());
  91. }
  92. // 给定路径, 获取 Value
  93. public:
  94. ///
  95. // 类型转换, 默认用的就是 DString 的转换函数/模版
  96. template <typename T> T Get (const std::string & path) const
  97. {
  98. return Get <DString> (path).To <T> ();
  99. }
  100. template <typename T> T Get (const DString & path) const
  101. {
  102. return Get <T> (path.constBuffer ());
  103. }
  104. template <typename T> T Get (PCTSTR path) const
  105. {
  106. return Get <T> (std::string (path));
  107. }
  108. // 偏特化
  109. template <> std::string Get (const std::string & path) const
  110. {
  111. auto rc = TryGet (path, std::string ());
  112. return std::move (rc);
  113. }
  114. template <> DString Get (const std::string & path) const
  115. {
  116. auto rc = TryGet (path, std::string ());
  117. return DString (rc.c_str ());
  118. }
  119. // 如下奇怪的写法, 只是为了让引用者的编译器能确切知道错误在哪里
  120. #ifdef BSTTree_EXPORTS
  121. template <> WString Get (const std::string & path) const { return WString (); }
  122. #else
  123. template <> WString Get (const std::string & path) const = delete;
  124. #endif
  125. /*
  126. {
  127. auto rc = Get <DString> (path);
  128. WString wrc (rc, CP_UTF8);
  129. return std::move (wrc);
  130. }
  131. */
  132. // 如果不写类型转换, 默认返回 DString 类型
  133. DString Get (const std::string & path) const { return Get <DString> (path); }
  134. DString Get (PCTSTR path) const { return Get <DString> (path); }
  135. DString Get (const DString & path) const { return Get <DString> (path); }
  136. ///
  137. public:
  138. // 带默认值的函数操作符重载
  139. DString operator () (const std::string & path, PCTSTR Def) const { return DString (TryGet (path, Def).c_str ()); }
  140. bool operator () (const std::string & path, bool Def) const { return TryGet (path, Def); }
  141. int operator () (const std::string & path, int Def) const { return TryGet (path, Def); }
  142. unsigned int operator () (const std::string & path, unsigned int Def) const { return TryGet (path, Def); }
  143. __int64 operator () (const std::string & path, __int64 Def) const { return TryGet (path, Def); }
  144. long operator () (const std::string & path, long Def) const { return TryGet (path, Def); }
  145. float operator () (const std::string & path, float Def) const { return TryGet (path, Def); }
  146. double operator () (const std::string & path, double Def) const { return TryGet (path, Def); }
  147. #if 0
  148. // 重载下标操作符
  149. public:
  150. DString operator [] (const std::string & path) { return Get <DString> (path); }
  151. DString operator [] (PCTSTR path) { return Get <DString> (path); }
  152. DString operator [] (const DString & path) { return Get <DString> (path); }
  153. // 重载函数操作符
  154. public:
  155. DString operator () (const std::string & path) { return Get <DString> (path); }
  156. DString operator () (PCTSTR path) { return Get <DString> (path); }
  157. DString operator () (const DString & path) { return Get <DString> (path); }
  158. #endif
  159. protected:
  160. /// 如果找不到, 就用 Def 代替
  161. std::string TryGet (const std::string & path, PCTSTR Def) const;
  162. std::string TryGet (const std::string & path, const std::string & Def) const;
  163. int TryGet (const std::string & path, int Def) const;
  164. unsigned int TryGet (const std::string & path, unsigned int Def) const;
  165. __int64 TryGet (const std::string & path, __int64 Def) const;
  166. unsigned __int64 TryGet (const std::string & path, unsigned __int64 Def) const;
  167. long TryGet (const std::string & path, long Def) const;
  168. double TryGet (const std::string & path, double Def) const;
  169. float TryGet (const std::string & path, float Def) const;
  170. bool TryGet (const std::string & path, bool Def) const;
  171. ///
  172. public:
  173. template <typename T> T GetIf (const std::string & path, const T & Def) const
  174. {
  175. auto rc = TryGet (path, Def);
  176. return rc;
  177. }
  178. /// 针对 PCTSTR Def 的特殊处理
  179. template <typename T> T GetIf (const std::string & path, PCTSTR Def) const
  180. {
  181. auto rc = TryGet (path, Def);
  182. return rc;
  183. }
  184. template <> DString GetIf (const std::string & path, PCTSTR Def) const
  185. {
  186. auto rc = TryGet (path, Def);
  187. return DString (rc.c_str ());
  188. }
  189. // 如下奇怪的写法, 只是为了让引用者的编译器能确切知道错误在哪里
  190. #ifdef BSTTree_EXPORTS
  191. template <> WString GetIf (const std::string & path, PCTSTR Def) const { return WString (); }
  192. #else
  193. template <> WString GetIf (const std::string & path, PCTSTR Def) const = delete;
  194. #endif
  195. /* {
  196. auto rc = TryGet (path, Def);
  197. DString drc (rc.c_str ());
  198. return WString (drc, CP_UTF8);
  199. }
  200. */
  201. DString GetIf (const std::string & path, PCTSTR Def) const
  202. {
  203. return GetIf <DString> (path, Def);
  204. }
  205. DString GetIf (const std::string & path, const DString & Def) const
  206. {
  207. return GetIf <DString> (path, Def.constBuffer ());
  208. }
  209. ///
  210. #if 0
  211. /// 针对 const wchar_t Def 的特殊处理, 只返回 WString
  212. template <typename T> T Get (const std::string & path, const wchar_t * Def)
  213. {
  214. assert (false);
  215. }
  216. template <> WString Get (const std::string & path, const wchar_t * Def)
  217. {
  218. WString tmp (Def);
  219. return Get <WString> (path, tmp.ToDString ());
  220. }
  221. WString Get (const std::string & path, const wchar_t * Def)
  222. {
  223. return Get <WString> (path, Def);
  224. }
  225. ///
  226. #endif
  227. public:
  228. DString GetData () const;
  229. DString GetValue () const;
  230. public:
  231. // 如果原来没有这一项, 就增加, 如果原来有, 就更新. 返回 this
  232. BSTTree & SetValue (PCTSTR value);
  233. BSTTree & SetValue (const std::string & value);
  234. BSTTree & SetValue (const DString & value);
  235. BSTTree & SetValue (int value);
  236. BSTTree & SetValue (unsigned int value);
  237. BSTTree & SetValue (__int64 value);
  238. BSTTree & SetValue (unsigned __int64 value);
  239. BSTTree & SetValue (double value);
  240. BSTTree & SetValue (bool value);
  241. BSTTree & SetValue (DWORD value);
  242. BSTTree & SetValue (void * ptr);
  243. public:
  244. // 如果原来没有这一项, 就增加, 如果原来有, 就更新. 返回 this
  245. BSTTree & Put (const std::string & path, PCTSTR value);
  246. BSTTree & Put (const std::string & path, const std::string & value);
  247. BSTTree & Put (const std::string & path, const DString & value);
  248. BSTTree & Put (const std::string & path, int value);
  249. BSTTree & Put (const std::string & path, unsigned int value);
  250. BSTTree & Put (const std::string & path, __int64 value);
  251. BSTTree & Put (const std::string & path, unsigned __int64 value);
  252. BSTTree & Put (const std::string & path, double value);
  253. BSTTree & Put (const std::string & path, bool value);
  254. BSTTree & Put (const std::string & path, DWORD value);
  255. BSTTree & Put (const std::string & path, void * ptr);
  256. public:
  257. // 添加新项, 返回 this
  258. BSTTree & Add (const std::string & path, PCTSTR value);
  259. BSTTree & Add (const std::string & path, const std::string & value);
  260. BSTTree & Add (const std::string & path, const DString & value);
  261. BSTTree & Add (const std::string & path, int value);
  262. BSTTree & Add (const std::string & path, unsigned int value);
  263. BSTTree & Add (const std::string & path, __int64 value);
  264. BSTTree & Add (const std::string & path, unsigned __int64 value);
  265. BSTTree & Add (const std::string & path, double value);
  266. BSTTree & Add (const std::string & path, bool value);
  267. BSTTree & Add (const std::string & path, DWORD value);
  268. BSTTree & Add (const std::string & path, void * ptr);
  269. protected:
  270. class Proxy
  271. {
  272. public:
  273. Proxy (BSTTree * Tree, PCTSTR Key)
  274. {
  275. m_Tree = Tree;
  276. m_Key = Key;
  277. }
  278. Proxy (BSTTree * Tree, const std::string & Key)
  279. {
  280. m_Tree = Tree;
  281. m_Key = Key;
  282. }
  283. ~Proxy () { }
  284. public:
  285. inline DString Get () const
  286. {
  287. return m_Tree->Get (m_Key);
  288. }
  289. inline operator DString () const
  290. {
  291. return m_Tree->Get (m_Key);
  292. }
  293. inline operator void * () const
  294. {
  295. return reinterpret_cast <void *> (Get ().To <UINT_PTR> ());
  296. }
  297. inline operator DWORD () const
  298. {
  299. return m_Tree->Get <DWORD> (m_Key);
  300. }
  301. inline operator __int64 () const
  302. {
  303. return m_Tree->Get <__int64> (m_Key);
  304. }
  305. inline operator unsigned __int64 () const
  306. {
  307. return m_Tree->Get <unsigned __int64> (m_Key);
  308. }
  309. inline operator int () const
  310. {
  311. return m_Tree->Get <int> (m_Key);
  312. }
  313. inline operator unsigned int () const
  314. {
  315. return m_Tree->Get <unsigned int> (m_Key);
  316. }
  317. inline operator long () const
  318. {
  319. return m_Tree->Get <long> (m_Key);
  320. }
  321. inline operator bool () const
  322. {
  323. return m_Tree->Get <bool> (m_Key);
  324. }
  325. inline operator float () const
  326. {
  327. return m_Tree->Get <float> (m_Key);
  328. }
  329. inline operator double () const
  330. {
  331. return m_Tree->Get <double> (m_Key);
  332. }
  333. public:
  334. inline Proxy & operator = (LPCSTR Value)
  335. {
  336. m_Tree->Put (m_Key, Value);
  337. return (*this);
  338. }
  339. #if 0
  340. inline Proxy & operator = (PCWSTR Value)
  341. {
  342. m_Tree->Put (m_Key, Value);
  343. return (*this);
  344. }
  345. #endif
  346. inline Proxy & operator = (const DString & Value)
  347. {
  348. m_Tree->Put (m_Key, Value);
  349. return (*this);
  350. }
  351. // inline Proxy & operator = (const WString & Value)
  352. // {
  353. // m_JsonRoot->SetValue (Value);
  354. // return (*this);
  355. // }
  356. inline Proxy & operator = (__int64 Value)
  357. {
  358. m_Tree->Put (m_Key, Value);
  359. return (*this);
  360. }
  361. inline Proxy & operator = (UINT_PTR Value)
  362. {
  363. m_Tree->Put (m_Key, Value);
  364. return (*this);
  365. }
  366. inline Proxy & operator = (DWORD Value)
  367. {
  368. m_Tree->Put (m_Key, Value);
  369. return (*this);
  370. }
  371. inline Proxy & operator = (int Value)
  372. {
  373. m_Tree->Put (m_Key, Value);
  374. return (*this);
  375. }
  376. inline Proxy & operator = (bool Value)
  377. {
  378. m_Tree->Put (m_Key, Value);
  379. return (*this);
  380. }
  381. inline Proxy & operator = (void * Value)
  382. {
  383. m_Tree->Put (m_Key, Value);
  384. return (*this);
  385. }
  386. inline Proxy & operator = (float Value)
  387. {
  388. m_Tree->Put (m_Key, Value);
  389. return (*this);
  390. }
  391. inline Proxy & operator = (double Value)
  392. {
  393. m_Tree->Put (m_Key, Value);
  394. return (*this);
  395. }
  396. protected:
  397. BSTTree * m_Tree;
  398. std::string m_Key;
  399. };
  400. // 函数操作符重载, 下标操作符重载
  401. public:
  402. Proxy operator [] (PCTSTR Key)
  403. {
  404. return Proxy (this, Key);
  405. }
  406. Proxy operator () (PCTSTR Key)
  407. {
  408. return Proxy (this, Key);
  409. }
  410. public:
  411. // 添加一个子节点, 返回的是这个子节点
  412. BSTTree AddChild (const std::string & path);
  413. BSTTree AddChild (const DString & path)
  414. {
  415. return AddChild (std::string (path.constBuffer ()));
  416. }
  417. BSTTree AddChild (const WString & path)
  418. {
  419. return AddChild (std::string (path.ToDString ().constBuffer ()));
  420. }
  421. BSTTree AddChild (PCTSTR path)
  422. {
  423. return AddChild (std::string (path));
  424. }
  425. public:
  426. // 如果原来没有这一项, 就增加, 如果原来有, 就更新, 返回的是这个子节点
  427. BSTTree PutChild (const std::string & path);
  428. BSTTree PutChild (const DString & path)
  429. {
  430. return PutChild (std::string (path.constBuffer ()));
  431. }
  432. BSTTree PutChild (const WString & path)
  433. {
  434. return PutChild (std::string (path.ToDString ().constBuffer ()));
  435. }
  436. BSTTree PutChild (PCTSTR path)
  437. {
  438. return PutChild (std::string (path));
  439. }
  440. public:
  441. // 无条件删除指定的项, 当然包括下面的全部子项
  442. bool Delete (const std::string & path);
  443. bool Delete (PCTSTR path)
  444. {
  445. return Delete (std::string (path));
  446. }
  447. // 当 Pred 返回 true 时, 删除
  448. bool Delete (std::function <bool (const std::string & path, BSTTree & xml)> pred);
  449. public:
  450. // 查找, 判断某个路径是否存在
  451. bool IsExist (const std::string & path);
  452. public:
  453. // 如果非空, 就执行指定的函数
  454. template <typename Fun, typename... arg>
  455. inline bool Do (const Fun & fun, arg... e)
  456. {
  457. if (IsEmpty ()) return false;
  458. fun (*this, e...);
  459. return true;
  460. }
  461. public:
  462. // 获取第一个/最后一个, 如果存在, 就调用指定指定的函数
  463. // bool OnFirst (std::function <void (const std::string & path, BSTTree & xml)> fun);
  464. // bool OnLast (std::function <void (const std::string & path, BSTTree & xml)> fun);
  465. // 返回第一个子节点/最后一个子节点. 如果不存在, 或者为空, 返回的 XML 将是空白的
  466. BSTTree First ();
  467. BSTTree Last ();
  468. // 类似迭代器的调用, 返回调用次数
  469. template <typename Fun, typename... arg>
  470. inline int ForEach (const Fun & fun, arg... e)
  471. {
  472. if (IsEmpty ())
  473. return 0;
  474. auto Iter = GetIterator ();
  475. int nCount = 0;
  476. for (; ; nCount ++)
  477. {
  478. auto & pair = Iter ();
  479. auto & ptr = pair.second;
  480. BSTTree * xml = ptr.get ();
  481. if (xml == nullptr) // 迭代结束
  482. break;
  483. auto & name = pair.first;
  484. if (! m_bAttrib)
  485. if (name == XMLATTR)
  486. continue;
  487. #ifdef _DEBUG
  488. auto d = xml->GetData ();
  489. auto v = xml->GetValue ();
  490. auto a = xml->Attrib ().GetData ();
  491. auto x = xml->Attrib ().GetValue ();
  492. #endif
  493. fun (name, *xml, e...);
  494. }
  495. return nCount;
  496. }
  497. // 搜索指定的 tagname, 如果找到, 就执行对应的函数
  498. template <typename Fun, typename... arg>
  499. inline int ForEachOn (const std::string & tagname, const Fun & fun, arg... e)
  500. {
  501. if (IsEmpty ())
  502. return 0;
  503. auto Iter = GetIterator ();
  504. int nCount = 0;
  505. while (true)
  506. {
  507. auto & pair = Iter ();
  508. auto & ptr = pair.second;
  509. BSTTree * xml = ptr.get ();
  510. if (xml == nullptr) // 迭代结束
  511. break;
  512. auto & name = pair.first;
  513. if (name != tagname)
  514. continue;
  515. fun (*xml, e...);
  516. nCount ++;
  517. }
  518. return nCount;
  519. }
  520. // 寻找满足条件的第一个, 如果找到, 就执行对应的函数
  521. template <typename Pred, typename... arg>
  522. inline bool First (const Pred & pred, arg... e)
  523. {
  524. if (IsEmpty ())
  525. return false;
  526. auto Iter = GetIterator ();
  527. int nCount = 0;
  528. for (; ; nCount ++)
  529. {
  530. auto & pair = Iter ();
  531. auto & name = pair.first;
  532. auto & ptr = pair.second;
  533. BSTTree * xml = ptr.get ();
  534. if (xml == nullptr) // 迭代结束
  535. return false;
  536. bool rc = pred (name, *xml, e...);
  537. if (rc)
  538. return true;
  539. }
  540. return false;
  541. }
  542. /*
  543. // 查找指定的 tagname, 如果不存在, 或者为空, 返回的 XML 将是空白的
  544. BSTTree Find (const std::string & tagname)
  545. {
  546. if (IsEmpty ())
  547. return BSTTree ();
  548. auto Iter = GetIterator ();
  549. int nCount = 0;
  550. for (; ; nCount ++)
  551. {
  552. auto & pair = Iter ();
  553. auto & name = pair.first;
  554. auto & ptr = pair.second;
  555. BSTTree * xml = ptr.get ();
  556. if (xml == nullptr) // 迭代结束
  557. return BSTTree ();
  558. if (name == tagname)
  559. {
  560. }
  561. }
  562. return BSTTree ();
  563. }
  564. */
  565. public:
  566. // 转换成 容器, 比如 std::list <BSTTree>
  567. template <typename T>
  568. inline T To ()
  569. {
  570. T rc;
  571. if (IsEmpty ())
  572. return rc;
  573. auto Iter = GetIterator ();
  574. for (;;)
  575. {
  576. auto & pair = Iter ();
  577. auto & name = pair.first;
  578. auto & ptr = pair.second;
  579. BSTTree * xml = ptr.get ();
  580. if (xml == nullptr) // 迭代结束
  581. break;
  582. rc.push_back (std::move (*xml));
  583. }
  584. return rc;
  585. }
  586. // 转换成 vector <BSTTree>
  587. inline std::vector <BSTTree> ToVector ()
  588. {
  589. return To <std::vector <BSTTree> > ();
  590. }
  591. // 转换成 list <BSTTree>
  592. inline std::list <BSTTree> ToList ()
  593. {
  594. return To <std::list <BSTTree> > ();
  595. }
  596. private:
  597. // 迭代函数返回 pair, 参数 2 用指针, 其目的是让上层知道: 何时迭代结束
  598. using IterArgType = std::pair <std::string, std::unique_ptr <BSTTree>>;
  599. using IterFuncType = std::function <IterArgType ()>;
  600. IterFuncType GetIterator ();
  601. static BSTTree _TreeNodeToMe (tTree & node);
  602. static const std::string XMLATTR_DOT; // = "<xmlattr>.";
  603. static const std::string XMLATTR; // = "<xmlattr>";
  604. friend class BSTXML;
  605. friend class BSTJSON;
  606. friend class BSTINI;
  607. };
  608. class _BSTTree_API BSTXML
  609. {
  610. public:
  611. // 文件和字符串, 默认是 UTF8 编码
  612. static BSTTree LoadFile (const std::wstring & szFileName);
  613. static BSTTree LoadFile (const WString & szFileName)
  614. {
  615. return LoadFile (std::wstring (szFileName.constBuffer ()));
  616. }
  617. static BSTTree LoadFile (const wchar_t * szFileName)
  618. {
  619. return LoadFile (std::wstring (szFileName));
  620. }
  621. static BSTTree LoadFile (const std::string & szFileName);
  622. static BSTTree LoadFile (const DString & szFileName)
  623. {
  624. return LoadFile (std::string (szFileName.constBuffer ()));
  625. }
  626. static BSTTree LoadFile (PCTSTR szFileName)
  627. {
  628. return LoadFile (std::string (szFileName));
  629. }
  630. static BSTTree FromString (const std::string & doc);
  631. static BSTTree FromString (const DString & doc);
  632. static BSTTree FromString (PCTSTR doc);
  633. // 返回: 写入的字节数
  634. static int SaveFile (BSTTree & Tree, const std::wstring & szFileName);
  635. static int SaveFile (BSTTree & Tree, const WString & szFileName)
  636. {
  637. return SaveFile (Tree, std::wstring (szFileName.constBuffer ()));
  638. }
  639. static int SaveFile (BSTTree & Tree, const wchar_t * szFileName)
  640. {
  641. return SaveFile (Tree, std::wstring (szFileName));
  642. }
  643. static int SaveFile (BSTTree & Tree, const std::string & szFileName);
  644. static int SaveFile (BSTTree & Tree, const DString & szFileName)
  645. {
  646. return SaveFile (Tree, std::string (szFileName.constBuffer ()));
  647. }
  648. static int SaveFile (BSTTree & Tree, PCTSTR szFileName)
  649. {
  650. return SaveFile (Tree, std::string (szFileName));
  651. }
  652. public:
  653. // 转换成字符串, 默认是 UTF8 编码
  654. static DString ToString (BSTTree & Tree);
  655. };
  656. class _BSTTree_API BSTJSON
  657. {
  658. public:
  659. // 文件和字符串, 默认是 UTF8 编码
  660. static BSTTree LoadFile (const std::wstring & szFileName);
  661. static BSTTree LoadFile (const WString & szFileName)
  662. {
  663. return LoadFile (std::wstring (szFileName.constBuffer ()));
  664. }
  665. static BSTTree LoadFile (const wchar_t * szFileName)
  666. {
  667. return LoadFile (std::wstring (szFileName));
  668. }
  669. static BSTTree LoadFile (const std::string & szFileName);
  670. static BSTTree LoadFile (const DString & szFileName)
  671. {
  672. return LoadFile (std::string (szFileName.constBuffer ()));
  673. }
  674. static BSTTree LoadFile (PCTSTR szFileName)
  675. {
  676. return LoadFile (std::string (szFileName));
  677. }
  678. static BSTTree FromString (const std::string & doc);
  679. static BSTTree FromString (const DString & doc);
  680. static BSTTree FromString (PCTSTR doc);
  681. // 返回: 写入的字节数
  682. static int SaveFile (BSTTree & Tree, const std::wstring & szFileName);
  683. static int SaveFile (BSTTree & Tree, const WString & szFileName)
  684. {
  685. return SaveFile (Tree, std::wstring (szFileName.constBuffer ()));
  686. }
  687. static int SaveFile (BSTTree & Tree, const wchar_t * szFileName)
  688. {
  689. return SaveFile (Tree, std::wstring (szFileName));
  690. }
  691. static int SaveFile (BSTTree & Tree, const std::string & szFileName);
  692. static int SaveFile (BSTTree & Tree, const DString & szFileName)
  693. {
  694. return SaveFile (Tree, std::string (szFileName.constBuffer ()));
  695. }
  696. static int SaveFile (BSTTree & Tree, PCTSTR szFileName)
  697. {
  698. return SaveFile (Tree, std::string (szFileName));
  699. }
  700. public:
  701. // 转换成字符串, 默认是 UTF8 编码
  702. static DString ToString (BSTTree & Tree);
  703. };
  704. class _BSTTree_API BSTINI
  705. {
  706. public:
  707. // 文件和字符串, 默认是 UTF8 编码
  708. static BSTTree LoadFile (const std::wstring & szFileName);
  709. static BSTTree LoadFile (const WString & szFileName)
  710. {
  711. return LoadFile (std::wstring (szFileName.constBuffer ()));
  712. }
  713. static BSTTree LoadFile (const wchar_t * szFileName)
  714. {
  715. return LoadFile (std::wstring (szFileName));
  716. }
  717. static BSTTree LoadFile (const std::string & szFileName);
  718. static BSTTree LoadFile (const DString & szFileName)
  719. {
  720. return LoadFile (std::string (szFileName.constBuffer ()));
  721. }
  722. static BSTTree LoadFile (PCTSTR szFileName)
  723. {
  724. return LoadFile (std::string (szFileName));
  725. }
  726. static BSTTree FromString (const std::string & doc);
  727. static BSTTree FromString (const DString & doc);
  728. static BSTTree FromString (PCTSTR doc);
  729. // 返回: 写入的字节数
  730. static int SaveFile (BSTTree & Tree, const std::wstring & szFileName);
  731. static int SaveFile (BSTTree & Tree, const WString & szFileName)
  732. {
  733. return SaveFile (Tree, std::wstring (szFileName.constBuffer ()));
  734. }
  735. static int SaveFile (BSTTree & Tree, const wchar_t * szFileName)
  736. {
  737. return SaveFile (Tree, std::wstring (szFileName));
  738. }
  739. static int SaveFile (BSTTree & Tree, const std::string & szFileName);
  740. static int SaveFile (BSTTree & Tree, const DString & szFileName)
  741. {
  742. return SaveFile (Tree, std::string (szFileName.constBuffer ()));
  743. }
  744. static int SaveFile (BSTTree & Tree, PCTSTR szFileName)
  745. {
  746. return SaveFile (Tree, std::string (szFileName));
  747. }
  748. public:
  749. // 转换成字符串, 默认是 UTF8 编码
  750. static DString ToString (BSTTree & Tree);
  751. };
  752. }
  753. }