logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://hacktivis.me/git/libbulletml.git

ygg.h (60683B)


  1. /******************************************************************************
  2. 世界樹 -yggdrasil-
  3. 世界樹モジュールヘッダファイル
  4. Coded by Wraith in July 14, 2002.
  5. ******************************************************************************/
  6. // Tab幅を4文字に設定して表示させてください。
  7. ///////////////////////////////////////////////////////////////////////////////
  8. //
  9. // ■ ygg.h
  10. // http://tricklib.com/cxx/ex/yggdrasil/ygg.h
  11. //
  12. // □ 関連ファイル
  13. // 本モジュールの本体
  14. // http://tricklib.com/cxx/ex/yggdrasil/ygg.cpp
  15. // 本モジュールのチュートリアルソースファイル
  16. // http://tricklib.com/cxx/ex/yggdrasil/ygg_test.cpp
  17. // 全ファイルパック
  18. // http://tricklib.com/cxx/ex/yggdrasil/ygg.lzh
  19. // http://tricklib.com/cxx/ex/yggdrasil/ygg.zip
  20. //
  21. // □ リファレンス・サポートページ
  22. // http://tricklib.com/cxx/ex/yggdrasil/
  23. //
  24. // □ ライセンス情報
  25. // http://tricklib.com/license.htm
  26. //
  27. #ifndef __YGGDRASIL_YGG_H__
  28. #define __YGGDRASIL_YGG_H__
  29. #if !defined(__WITH_YGGDRASIL__)
  30. # define __WITH_YGGDRASIL__
  31. #endif
  32. #if !defined(__BABEL_BABEL_H__) && defined(__WITH_BABEL__)
  33. #include "babel.h"
  34. #endif
  35. #if defined(__WITH_BABEL__) && defined(__BBL_USE_SELECTORS__)
  36. #define __YGGDRASIL_WITH_BABEL__
  37. #endif
  38. #include <cassert>
  39. #include <iostream>
  40. #include <fstream>
  41. #include <list>
  42. #ifndef NDEBUG
  43. #ifndef _DEBUG
  44. #define _DEBUG
  45. #endif
  46. #endif
  47. //
  48. // ☆ベースエンコーディング指定マクロ
  49. //
  50. // コンパイル時に指定するのが面倒な場合は以下のコメント行から適切な指定を
  51. // (コメントを解除して)有効にしてください。いっさい指定がなされない場合で
  52. // も自動判別されますが、正確に判断されなっかたり判別の為に(余分)コードが
  53. // 生成されたりする可能性があります。
  54. //
  55. //#define __USING_ANSI__
  56. //#define __USING_SJIS__
  57. //#define __USING_EUC__
  58. //#define __USING_UTF8__
  59. #if !(defined(__USING_ANSI__) || defined(__USING_SJIS__) || defined(__USING_EUC__) || defined(__USING_UTF8__) || defined(__USING_UNKNOWN__))
  60. #define __USING_UNKNOWN__
  61. #endif
  62. #if defined(__BORLANDC__)
  63. # pragma warn -8022
  64. # pragma warn -8026
  65. # pragma warn -8027
  66. #endif
  67. /******************************************************************************
  68. □■□■ TrickPalace □■□■
  69. ■□■□ http://www.trickpalace.net/ ■□■□
  70. ******************************************************************************/
  71. //
  72. // ●yggdrasil
  73. //
  74. namespace yggdrasil {
  75. //
  76. // ▼使用する文字列クラス [ 〆 ]
  77. //
  78. }
  79. #include <string>
  80. namespace yggdrasil {
  81. typedef std::string ygg_string;
  82. // typedef std::wstring ygg_wstring;
  83. //
  84. // ●ノード型名 [ 〆 ]
  85. //
  86. namespace ygg_node_type {
  87. const ygg_string empty = "empty";
  88. //const ygg_string node = "node";
  89. const ygg_string text = "text";
  90. const ygg_string comment = "comment";
  91. const ygg_string attribute = "attribute";
  92. const ygg_string list = "list";
  93. const ygg_string root = "root";
  94. const ygg_string element = "element";
  95. }
  96. //
  97. // ●ターム [ 〆 ]
  98. //
  99. namespace ygg_term {
  100. const ygg_string broken_char = "?"; // 2文字以上でも可、ANSI以外は不可。
  101. const ygg_string empty = "";
  102. const ygg_string element_lead = "<";
  103. const ygg_string element_sign = "/";
  104. const ygg_string element_trail = ">";
  105. const ygg_string comment_lead = "<!--";
  106. const ygg_string comment_trail = "-->";
  107. const ygg_string cdata_lead = "<![CDATA[";
  108. const ygg_string cdata_trail = "]]>";
  109. const ygg_string equal = "=";
  110. const ygg_string attribute_prefix = "@";
  111. const ygg_string indent = "\t";
  112. const ygg_string return_code = "\n";
  113. const ygg_string white_space = "\n\r\t\v ";
  114. const ygg_string node = "node()";
  115. const ygg_string wildcard = "*";
  116. const ygg_string comment_node = "comment()";
  117. const ygg_string text_node = "text()";
  118. const ygg_string attribute_node = "attribute()";
  119. const ygg_string element_node = "element()";
  120. #ifndef __REJECT_PROCESSING_INSTRUCTION__
  121. const ygg_string processing_instruction_node = "processing-instruction()";
  122. #endif
  123. // const ygg_string doctype_node = "doctype()";
  124. // const ygg_string cdata_section_node = "cdata-section()";
  125. const ygg_string list_node = "list()";
  126. // const ygg_string root_node = "root()";
  127. const ygg_string path_dir = "/";
  128. const ygg_string path_root = path_dir;
  129. const ygg_string path_wherever = "//";
  130. const ygg_string path_this = ".";
  131. const ygg_string path_this_element = "./";
  132. const ygg_string path_this__wherever = ".//";
  133. const ygg_string path_parent = "..";
  134. const ygg_string path_parenthesis_begin = "[";
  135. const ygg_string path_parenthesis_end = "]";
  136. const ygg_string path_last_index = "last()";
  137. const ygg_string ygg_invalid_name_chars_a = " !\"#$%&'()*+,-./;<=>?@[\\]^`{|}~";
  138. const ygg_string ygg_invalid_name_chars_b = " !\"#$%&'()*+,/;<=>?@[\\]^`{|}~";
  139. const ygg_string yggdrasil_version = "information>\n"
  140. "\tmodule>\tid=yggdrasil\n"
  141. "\t\tname=世界樹 -yggdrasil- \n"
  142. "\t\turl=http://tricklib.com/cxx/ex/yggdrasil/\n"
  143. "\t\tversion=ベータIIII-I\n"
  144. "\tauthor>\tid=wraith\n"
  145. "\t\tname=道化師\n"
  146. "\t\turl=http://www.trickpalace.net/\n"
  147. "\t\tmail=wraith@trickpalace.net\n";
  148. }
  149. //
  150. // SOX path term
  151. //
  152. // term/node comment text attribute element
  153. // node() ○ ○ ○ ○
  154. // comment() ○ × × ×
  155. // text() × ○ × ×
  156. // attribute() × × ○ ×
  157. // @* × × ○ ×
  158. // element() × × × ○
  159. // * × × × ○
  160. //
  161. /******************************************************************************
  162. □■□■ cppll ML □■□■
  163. ■□■□ http://www.trickpalace.net/cppll/ ■□■□
  164. ******************************************************************************/
  165. //
  166. // ▽先行宣言
  167. //
  168. class ygg_node;
  169. class ygg_node_body;
  170. class ygg_ghost;
  171. class ygg_list;
  172. class ygg_root;
  173. class ygg_element;
  174. class ygg_iterator;
  175. class ygg_reverse_iterator;
  176. class ygg_const_iterator;
  177. class ygg_const_reverse_iterator;
  178. class sax_parser;
  179. class sax_handler;
  180. class ygg_error_code;
  181. class ygg_smart_base {
  182. public:
  183. typedef ygg_smart_base this_type;
  184. private:
  185. volatile int ref_count;
  186. protected:
  187. ygg_smart_base() :ref_count(0) {}
  188. virtual ~ygg_smart_base() { assert(0 == ref_count); }
  189. public:
  190. void inc_ref() volatile {
  191. if (NULL != this) {
  192. ++ref_count;
  193. }
  194. }
  195. void dec_ref() volatile {
  196. if (NULL != this) {
  197. if (--ref_count <= 0) {
  198. assert(0 == ref_count);
  199. delete this;
  200. }
  201. }
  202. }
  203. };
  204. template<class target_object>
  205. class ygg_smart_shell {
  206. public:
  207. typedef ygg_smart_shell<target_object> this_type;
  208. typedef target_object target_type;
  209. private:
  210. target_type *value;
  211. public:
  212. ygg_smart_shell(target_type *X_value = NULL) :value(X_value) {
  213. value->inc_ref();
  214. }
  215. ygg_smart_shell(const this_type &X) :value(X.value) {
  216. value->inc_ref();
  217. }
  218. ~ygg_smart_shell() {
  219. value->dec_ref();
  220. }
  221. this_type & operator = (target_type *X_value) {
  222. if (value != X_value) {
  223. value->dec_ref();
  224. value = X_value;
  225. value->inc_ref();
  226. }
  227. return *this;
  228. }
  229. this_type & operator = (const this_type &X) {
  230. if (value != X.value) {
  231. value->dec_ref();
  232. value = X.value;
  233. value->inc_ref();
  234. }
  235. return *this;
  236. }
  237. bool operator == (const this_type &X) const {
  238. return value == X.value;
  239. }
  240. bool operator != (const this_type &X) const {
  241. return value != X.value;
  242. }
  243. bool operator == (const target_type *X_value) const {
  244. return value == X_value;
  245. }
  246. bool operator != (const target_type *X_value) const {
  247. return value != X_value;
  248. }
  249. bool operator ! () const { return !value; }
  250. operator target_type* () { return value; }
  251. operator const target_type* () const { return value; }
  252. target_type& operator*() { return *value; }
  253. const target_type& operator*() const { return *value; }
  254. target_type* operator->() { return value; }
  255. const target_type* operator->() const { return value; }
  256. bool is_null() const { return NULL == value; }
  257. bool is_not_null() const { return NULL != value; }
  258. };
  259. //
  260. // ●utilities
  261. //
  262. namespace ygg_utility {
  263. ygg_string make_indent(int indent);
  264. ygg_string encode_xml(const ygg_string &X);
  265. ygg_string encode_attribute(const ygg_string &X);
  266. ygg_string encode_sox(const ygg_string &X);
  267. ygg_string encode_sox(int indent, const ygg_string &X);
  268. ygg_string encode_sox(const ygg_string &indent, const ygg_string &X);
  269. ygg_string decode_xml(const ygg_string &X);
  270. ygg_string decode_attribute(const ygg_string &X);
  271. ygg_string decode_sox(int indent, const ygg_string &X);
  272. ygg_node & format_tree(ygg_node &node, unsigned int max_row_length = 40);
  273. ygg_string xml_trim(const ygg_string &X);
  274. ygg_string both_trim(const ygg_string &X);
  275. ygg_error_code check_name(const ygg_string &X);
  276. ygg_string & ygg_string_replace(
  277. ygg_string &body,
  278. const ygg_string &search, const ygg_string &replace);
  279. ygg_string create_line(ygg_node X_list, const ygg_string &separator);
  280. namespace base_encoding {
  281. enum {
  282. ansi,
  283. sjis,
  284. jis,
  285. euc,
  286. utf8
  287. };
  288. };
  289. inline const int get_base_encoding() {
  290. using namespace base_encoding;
  291. #ifdef __USING_ANSI__
  292. return ansi;
  293. #endif
  294. #ifdef __USING_SJIS__
  295. return sjis;
  296. #endif
  297. #ifdef __USING_EUC__
  298. return euc;
  299. #endif
  300. #ifdef __USING_UTF8__
  301. return utf8;
  302. #endif
  303. #ifdef __USING_UNKNOWN__
  304. const int fingerprint = ((unsigned char*)("漢字"))[0];
  305. if (0x8A == fingerprint) {
  306. return sjis;
  307. }
  308. if (0x84 == fingerprint) {
  309. return euc;
  310. }
  311. if (0xE6 == fingerprint) {
  312. return utf8;
  313. }
  314. return ansi;
  315. #endif
  316. }
  317. }
  318. //
  319. // ■yggバリュークラス
  320. //
  321. class ygg_value {
  322. ygg_string value;
  323. public:
  324. ygg_value() {}
  325. ygg_value(const ygg_string &X_value) :value(X_value) {}
  326. ygg_value(const char *X_value) :value(X_value) {}
  327. ygg_value(int X) { operator=(X); }
  328. ygg_value(double X) { operator=(X); }
  329. ygg_value(const ygg_value &X) :value(X.value) {}
  330. ygg_value & operator=(const ygg_string &X_value) {
  331. return set_string(X_value);
  332. }
  333. ygg_value & operator=(const char *X_value) {
  334. return set_string(X_value);
  335. }
  336. ygg_value & operator=(int X) {
  337. return set_int(X);
  338. }
  339. ygg_value & operator=(double X) {
  340. return set_double(X);
  341. }
  342. ygg_value & operator=(const ygg_value &X) {
  343. return set_string(X.value);
  344. }
  345. operator const ygg_string & () const {
  346. return value;
  347. }
  348. operator const char * () const {
  349. return value.c_str();
  350. }
  351. operator const int () const {
  352. return atol(value.c_str());
  353. }
  354. operator const double () const {
  355. return atof(value.c_str());
  356. }
  357. const ygg_string & get_string() const {
  358. return value;
  359. }
  360. const char * get_primary() const {
  361. return value.c_str();
  362. }
  363. const char * c_str() const {
  364. return value.c_str();
  365. }
  366. const int get_int() const {
  367. return atol(value.c_str());
  368. }
  369. const double get_double() const {
  370. return atof(value.c_str());
  371. }
  372. ygg_value & set_string(const ygg_string &X_value) {
  373. value = X_value;
  374. return *this;
  375. }
  376. ygg_value & set_int(int X);
  377. ygg_value & set_double(double X);
  378. };
  379. /******************************************************************************
  380. □■□■ cuppa □■□■
  381. ■□■□ http://www.unittest.org/ ■□■□
  382. ******************************************************************************/
  383. //
  384. // ■yggノードホルダ
  385. //
  386. class ygg_node {
  387. friend class ygg_ghost;
  388. friend class ygg_proto_list;
  389. friend class ygg_root;
  390. typedef ygg_iterator iterator;
  391. typedef ygg_reverse_iterator reverse_iterator;
  392. ygg_smart_shell<ygg_node_body> body;
  393. // ygg_node_body *body;
  394. public:
  395. inline ygg_node(ygg_node_body *X_body = NULL);
  396. inline ygg_node(const ygg_node &X);
  397. inline ygg_node & operator = (ygg_node_body *X_body);
  398. inline ygg_node & operator = (const ygg_node &X);
  399. bool operator == (const ygg_node &X) const {
  400. return body == X.body;
  401. }
  402. bool operator != (const ygg_node &X) const {
  403. return body != X.body;
  404. }
  405. inline ygg_node get_parent() const;
  406. inline bool is_live() const; // ←こいつは将来的になくなるかもしれないので
  407. // is_valid()/is_empty() を使用してください。
  408. inline ygg_string get_type() const;
  409. inline ygg_string get_name() const;
  410. ygg_string get_path_name() const {
  411. if (ygg_node_type::attribute == get_type()) {
  412. return ygg_term::attribute_prefix +get_name();
  413. } else {
  414. return get_name();
  415. }
  416. }
  417. inline ygg_value get_value() const;
  418. inline ygg_string get_text() const;
  419. inline ygg_string get_xml(const ygg_string &indent = ygg_term::empty) const;
  420. inline ygg_string get_xml_attribute() const;
  421. inline ygg_string get_sox(const ygg_string &inden = ygg_term::empty) const;
  422. inline ygg_node operator[](const ygg_string &path);
  423. inline ygg_node operator[](const unsigned index);
  424. const ygg_node operator[](const ygg_string &path) const {
  425. return ((ygg_node*)this)->operator[](path);
  426. }
  427. const ygg_node operator[](const unsigned index) const {
  428. return ((ygg_node*)this)->operator[](index);
  429. }
  430. inline const int get_size() const;
  431. inline ygg_iterator begin();
  432. inline ygg_iterator end();
  433. inline ygg_reverse_iterator rbegin();
  434. inline ygg_reverse_iterator rend();
  435. inline ygg_const_iterator begin() const;
  436. inline ygg_const_iterator end() const;
  437. inline ygg_const_reverse_iterator rbegin() const;
  438. inline ygg_const_reverse_iterator rend() const;
  439. inline bool match_path(const ygg_string &path) const;
  440. inline ygg_node & set_name(const ygg_string &X);
  441. inline ygg_node & set_value(const ygg_value &X);
  442. inline ygg_node & adopt_node(ygg_node X);
  443. inline ygg_node & adopt_sox(const ygg_string &sox, sax_handler *sax = NULL) {
  444. return adopt_node(parse_sox(sox,sax));
  445. }
  446. inline ygg_node & adopt_xml(const ygg_string &xml, sax_handler *sax = NULL) {
  447. return adopt_node(parse_xml(xml,sax));
  448. }
  449. inline ygg_node & exile_node(ygg_node X);
  450. inline ygg_node & exile_path(const ygg_string &path) {
  451. return exile_node(operator[](path));
  452. }
  453. ygg_node & self_exile();
  454. inline ygg_node clone() const;
  455. inline const bool operator !() const {
  456. return !is_valid();
  457. }
  458. //inline operator const bool() const {
  459. // return is_valid();
  460. // }
  461. // alias functions
  462. bool is_valid() const {
  463. return is_live();
  464. }
  465. bool is_empty() const {
  466. return !is_live();
  467. }
  468. inline ygg_node & add_node(ygg_node X);
  469. inline ygg_node & sub_node(ygg_node X);
  470. #ifdef _DEBUG
  471. inline bool assert_other(const ygg_node &X) const;
  472. #endif
  473. ygg_node & vanish(); // 親ノードからの exile 及び、this(ygg_node)の空ノード化
  474. ygg_string get_path() const;
  475. ygg_node & purge(); // 結合可能なテキストノードの結合、除霊、空ノードの exile、空リストの空ノード化
  476. ygg_node enum_node(const ygg_string &path_where);
  477. static inline ygg_node parse_sox(const ygg_string &sox, sax_handler *sax = NULL);
  478. static inline ygg_node parse_xml(const ygg_string &xml, sax_handler *sax = NULL);
  479. protected:
  480. inline ygg_node & regist_parent(ygg_root *X);
  481. inline ygg_node & unregist_parent(ygg_root *X);
  482. };
  483. //
  484. // □yggノードクラス
  485. //
  486. class ygg_node_body :public ygg_smart_base {
  487. friend class ygg_node;
  488. protected:
  489. ygg_root *parent;
  490. ygg_node_body() :parent(NULL) {}
  491. virtual ~ygg_node_body() { }
  492. ygg_node & get_parent() {
  493. return *((ygg_node *)&parent);
  494. }
  495. const ygg_node & get_parent() const {
  496. return *((ygg_node *)&parent);
  497. }
  498. virtual bool is_live() const = 0;
  499. virtual ygg_string get_type() const = 0;
  500. virtual ygg_string get_name() const = 0;
  501. virtual ygg_string get_value() const = 0;
  502. virtual ygg_string get_text() const = 0;
  503. virtual ygg_string get_xml(const ygg_string &indent) const = 0;
  504. virtual ygg_string get_xml_attribute() const = 0;
  505. virtual ygg_string get_sox(const ygg_string &indent) const = 0;
  506. virtual ygg_node operator[](const ygg_string &path);
  507. virtual ygg_node operator[](const unsigned index);
  508. virtual const int get_size() const;
  509. virtual ygg_iterator begin();
  510. virtual ygg_iterator end();
  511. virtual ygg_reverse_iterator rbegin();
  512. virtual ygg_reverse_iterator rend();
  513. virtual ygg_const_iterator begin() const;
  514. virtual ygg_const_iterator end() const;
  515. virtual ygg_const_reverse_iterator rbegin() const;
  516. virtual ygg_const_reverse_iterator rend() const;
  517. virtual bool match_path(const ygg_string &path) const;
  518. virtual void set_name(const ygg_string &X);
  519. virtual void set_value(const ygg_string &X);
  520. virtual void adopt_node(ygg_node X);
  521. virtual void exile_node(ygg_node X);
  522. virtual ygg_node clone() const = 0;
  523. ygg_node get_shell() {
  524. return this;
  525. }
  526. const ygg_node get_shell() const {
  527. return (ygg_node_body*)this;
  528. }
  529. #ifdef _DEBUG
  530. virtual bool assert_other(const ygg_node &X) const;
  531. #endif
  532. inline
  533. void regist_parent(ygg_root *X);
  534. void unregist_parent(ygg_root *X);
  535. };
  536. inline ygg_node::ygg_node(ygg_node_body *X_body) :body(X_body) {}
  537. inline ygg_node::ygg_node(const ygg_node &X) :body(X.body) {}
  538. inline ygg_node & ygg_node::operator = (ygg_node_body *X_body) {
  539. body = X_body;
  540. return *this;
  541. }
  542. inline ygg_node & ygg_node::operator = (const ygg_node &X) {
  543. body = X.body;
  544. return *this;
  545. }
  546. inline ygg_node ygg_node::get_parent() const {
  547. assert(body.is_not_null());
  548. if (body.is_null()) {
  549. return NULL;
  550. } else {
  551. return body->get_parent();
  552. }
  553. }
  554. inline bool ygg_node::is_live() const {
  555. // assert(body.is_not_null());
  556. if (body.is_null()) {
  557. return false;
  558. } else {
  559. return body->is_live();
  560. }
  561. }
  562. inline ygg_string ygg_node::get_type() const {
  563. if (body.is_null()) {
  564. return ygg_node_type::empty;
  565. } else {
  566. return body->get_type();
  567. }
  568. }
  569. inline ygg_string ygg_node::get_name() const {
  570. assert(body.is_not_null());
  571. if (body.is_null()) {
  572. return ygg_term::empty;
  573. } else {
  574. return body->get_name();
  575. }
  576. }
  577. inline ygg_string ygg_node::get_text() const {
  578. assert(body.is_not_null());
  579. if (body.is_null()) {
  580. return ygg_term::empty;
  581. } else {
  582. return body->get_text();
  583. }
  584. }
  585. inline ygg_string ygg_node::get_xml(const ygg_string &indent) const {
  586. assert(body.is_not_null());
  587. if (body.is_null()) {
  588. return ygg_term::empty;
  589. } else {
  590. return body->get_xml(indent);
  591. }
  592. }
  593. inline ygg_string ygg_node::get_xml_attribute() const {
  594. assert(body.is_not_null());
  595. if (body.is_null()) {
  596. return ygg_term::empty;
  597. } else {
  598. return body->get_xml_attribute();
  599. }
  600. }
  601. inline ygg_string ygg_node::get_sox(const ygg_string &indent) const {
  602. assert(body.is_not_null());
  603. if (body.is_null()) {
  604. return ygg_term::empty;
  605. } else {
  606. return body->get_sox(indent);
  607. }
  608. }
  609. inline ygg_value ygg_node::get_value() const {
  610. if (body.is_null()) {
  611. return ygg_term::empty;
  612. } else {
  613. return body->get_value();
  614. }
  615. }
  616. inline ygg_node ygg_node::operator[](const ygg_string &path) {
  617. // assert(body.is_not_null());
  618. if (body.is_null()) {
  619. return NULL;
  620. } else {
  621. return body->operator[](path);
  622. }
  623. }
  624. inline ygg_node ygg_node::operator[](const unsigned index) {
  625. // assert(body.is_not_null());
  626. if (body.is_null()) {
  627. return NULL;
  628. } else {
  629. return body->operator[](index);
  630. }
  631. }
  632. inline const int ygg_node::get_size() const {
  633. // assert(body.is_not_null());
  634. if (body.is_null()) {
  635. return 0;
  636. } else {
  637. return body->get_size();
  638. }
  639. }
  640. inline bool ygg_node::match_path(const ygg_string &path) const {
  641. if (body.is_null()) {
  642. return false;
  643. } else {
  644. return body->match_path(path);
  645. }
  646. }
  647. inline ygg_node & ygg_node::set_name(const ygg_string &X) {
  648. assert(body.is_not_null());
  649. if (body.is_not_null()) {
  650. body->set_name(X);
  651. }
  652. return *this;
  653. }
  654. inline ygg_node & ygg_node::set_value(const ygg_value &X) {
  655. assert(body.is_not_null());
  656. if (body.is_not_null()) {
  657. body->set_value(X.get_string());
  658. }
  659. return *this;
  660. }
  661. inline ygg_node & ygg_node::adopt_node(ygg_node X) {
  662. assert(body.is_not_null());
  663. if (body.is_not_null()) {
  664. body->adopt_node(X);
  665. }
  666. return *this;
  667. }
  668. inline ygg_node & ygg_node::exile_node(ygg_node X) {
  669. assert(body.is_not_null());
  670. if (body.is_not_null()) {
  671. body->exile_node(X);
  672. }
  673. return *this;
  674. }
  675. inline ygg_node ygg_node::clone() const {
  676. if (body.is_null()) {
  677. return NULL;
  678. } else {
  679. return body->clone();
  680. }
  681. }
  682. inline ygg_node & ygg_node::add_node(ygg_node X) {
  683. return adopt_node(X);
  684. }
  685. inline ygg_node & ygg_node::sub_node(ygg_node X) {
  686. return exile_node(X);
  687. }
  688. #ifdef _DEBUG
  689. inline bool ygg_node::assert_other(const ygg_node &X) const {
  690. assert(body.is_not_null());
  691. return body->assert_other(X);
  692. }
  693. #endif
  694. inline ygg_node & ygg_node::regist_parent(ygg_root *X) {
  695. assert(body.is_not_null());
  696. if (body.is_not_null()) {
  697. body->regist_parent(X);
  698. }
  699. return *this;
  700. }
  701. inline ygg_node & ygg_node::unregist_parent(ygg_root *X) {
  702. assert(body.is_not_null());
  703. if (body.is_not_null()) {
  704. body->unregist_parent(X);
  705. }
  706. return *this;
  707. }
  708. //
  709. // ■yggゴーストクラス
  710. //
  711. class ygg_ghost :public ygg_node_body {
  712. ygg_string path;
  713. protected:
  714. ygg_ghost(ygg_root *X_parent, const ygg_string &X_path) {
  715. parent = X_parent;
  716. path = X_path;
  717. }
  718. ygg_node get_life() const;
  719. ygg_node realize() const;
  720. bool is_live() const;
  721. ygg_string get_type() const;
  722. ygg_string get_name() const;
  723. ygg_string get_value() const;
  724. ygg_string get_text() const;
  725. ygg_string get_xml(const ygg_string &indent = ygg_term::empty) const;
  726. ygg_string get_xml_attribute() const;
  727. ygg_string get_sox(const ygg_string &indent) const;
  728. ygg_node operator[](const ygg_string &path);
  729. ygg_node operator[](const unsigned index);
  730. const int get_size() const;
  731. bool match_path(const ygg_string &path) const;
  732. void set_name(const ygg_string &X);
  733. void set_value(const ygg_string &X);
  734. void adopt_node(ygg_node X);
  735. void exile_node(ygg_node X);
  736. ygg_node clone() const;
  737. #ifdef _DEBUG
  738. bool assert_other(const ygg_node &X) const;
  739. #endif
  740. public:
  741. static
  742. ygg_node create(ygg_root *X_parent, const ygg_string &X_path) {
  743. return new ygg_ghost(X_parent, X_path);
  744. }
  745. };
  746. //
  747. // ■yggコメントクラス
  748. //
  749. class ygg_comment :public ygg_node_body {
  750. ygg_string value;
  751. protected:
  752. ygg_comment() {}
  753. bool is_live() const;
  754. ygg_string get_type() const;
  755. ygg_string get_name() const;
  756. ygg_string get_value() const;
  757. ygg_string get_text() const;
  758. ygg_string get_xml(const ygg_string &indent = ygg_term::empty) const;
  759. ygg_string get_xml_attribute() const;
  760. ygg_string get_sox(const ygg_string &indent) const;
  761. bool match_path(const ygg_string &path) const;
  762. void set_value(const ygg_string &X);
  763. ygg_node clone() const;
  764. public:
  765. static
  766. ygg_node create() {
  767. return new ygg_comment();
  768. }
  769. };
  770. //
  771. // ■yggテキストクラス
  772. //
  773. class ygg_text :public ygg_node_body {
  774. ygg_string value;
  775. protected:
  776. ygg_text() {}
  777. bool is_live() const;
  778. ygg_string get_type() const;
  779. ygg_string get_name() const;
  780. ygg_string get_value() const;
  781. ygg_string get_text() const;
  782. ygg_string get_xml(const ygg_string &indent = ygg_term::empty) const;
  783. ygg_string get_xml_attribute() const;
  784. ygg_string get_sox(const ygg_string &indent) const;
  785. bool match_path(const ygg_string &path) const;
  786. void set_value(const ygg_string &X);
  787. ygg_node clone() const;
  788. public:
  789. static
  790. ygg_node create() {
  791. return new ygg_text();
  792. }
  793. };
  794. //
  795. // ■ygg属性クラス
  796. //
  797. class ygg_attribute :public ygg_node_body {
  798. ygg_string name;
  799. ygg_string value;
  800. protected:
  801. ygg_attribute() {}
  802. bool is_live() const;
  803. ygg_string get_type() const;
  804. ygg_string get_name() const;
  805. ygg_string get_value() const;
  806. ygg_string get_text() const;
  807. ygg_string get_xml(const ygg_string &indent = ygg_term::empty) const;
  808. ygg_string get_xml_attribute() const;
  809. ygg_string get_sox(const ygg_string &indent) const;
  810. bool match_path(const ygg_string &path) const;
  811. void set_name(const ygg_string &X);
  812. void set_value(const ygg_string &X);
  813. ygg_node clone() const;
  814. public:
  815. static
  816. ygg_node create() {
  817. return new ygg_attribute();
  818. }
  819. };
  820. //
  821. // ▼使用するコンテナクラス [ 〆 ]
  822. //
  823. typedef std::list<ygg_node> ygg_list_type;
  824. //typedef std::iterator<std::bidirectional_iterator_tag, ygg_node> ygg_iterator_base;
  825. //typedef std::bidirectional_iterator<ygg_node, ptrdiff_t> ygg_iterator_base;
  826. struct ygg_iterator_base {
  827. typedef std::bidirectional_iterator_tag iterator_category;
  828. typedef ygg_node value_type;
  829. typedef ptrdiff_t difference_type;
  830. typedef ygg_node* pointer;
  831. typedef ygg_node& reference;
  832. };
  833. class ygg_iterator :public ygg_iterator_base {
  834. friend class ygg_const_iterator;
  835. ygg_node list;
  836. ygg_list_type::iterator iterator;
  837. public:
  838. ygg_iterator(ygg_node X_list, ygg_list_type::iterator X_iterator)
  839. :list(X_list), iterator(X_iterator) {}
  840. ygg_iterator(const ygg_iterator &X)
  841. :list(X.list), iterator(X.iterator) {}
  842. const ygg_node & get_list() const {
  843. return list;
  844. }
  845. inline bool is_begin() const;
  846. inline bool is_not_begin() const;
  847. inline bool is_end() const;
  848. inline bool is_not_end() const;
  849. ygg_iterator & operator++() {
  850. ++iterator;
  851. return *this;
  852. }
  853. const ygg_iterator operator++(int) {
  854. return ygg_iterator(list, iterator++);
  855. }
  856. ygg_iterator & operator -- () {
  857. --iterator;
  858. return *this;
  859. }
  860. const ygg_iterator operator -- (int) {
  861. return ygg_iterator(list, iterator--);
  862. }
  863. ygg_node & operator*() {
  864. return *iterator;
  865. }
  866. ygg_node * operator->() {
  867. return iterator.operator->();
  868. }
  869. bool operator==(const ygg_iterator &X) const {
  870. assert(list == X.list);
  871. return iterator == X.iterator;
  872. }
  873. bool operator!=(const ygg_iterator &X) const {
  874. assert(list == X.list);
  875. return iterator != X.iterator;
  876. }
  877. };
  878. class ygg_reverse_iterator :public ygg_iterator_base {
  879. friend class ygg_const_reverse_iterator;
  880. ygg_node list;
  881. ygg_list_type::reverse_iterator iterator;
  882. public:
  883. ygg_reverse_iterator(ygg_node X_list, ygg_list_type::reverse_iterator X_iterator)
  884. :list(X_list), iterator(X_iterator) {}
  885. ygg_reverse_iterator(const ygg_reverse_iterator &X)
  886. :list(X.list), iterator(X.iterator) {}
  887. const ygg_node & get_list() const {
  888. return list;
  889. }
  890. inline bool is_rbegin() const;
  891. inline bool is_not_rbegin() const;
  892. inline bool is_rend() const;
  893. inline bool is_not_rend() const;
  894. ygg_reverse_iterator & operator++() {
  895. ++iterator; return *this;
  896. }
  897. const ygg_reverse_iterator operator++(int) {
  898. return ygg_reverse_iterator(list, iterator++);
  899. }
  900. ygg_reverse_iterator & operator--() {
  901. --iterator;
  902. return *this;
  903. }
  904. const ygg_reverse_iterator operator--(int) {
  905. return ygg_reverse_iterator(list, iterator--);
  906. }
  907. ygg_node & operator*() {
  908. return *iterator;
  909. }
  910. ygg_node * operator->() {
  911. return iterator.operator->();
  912. }
  913. bool operator==(const ygg_reverse_iterator &X) const {
  914. assert(list == X.list);
  915. return iterator == X.iterator;
  916. }
  917. bool operator!=(const ygg_reverse_iterator &X) const {
  918. assert(list == X.list);
  919. return iterator != X.iterator;
  920. }
  921. };
  922. class ygg_const_iterator :public ygg_iterator_base {
  923. friend class ygg_iterator;
  924. const ygg_node list;
  925. ygg_list_type::const_iterator iterator;
  926. public:
  927. ygg_const_iterator(const ygg_node X_list, ygg_list_type::const_iterator X_iterator)
  928. :list(X_list), iterator(X_iterator) {}
  929. ygg_const_iterator(const ygg_iterator &X)
  930. :list(X.list), iterator(X.iterator) {}
  931. ygg_const_iterator(const ygg_const_iterator &X)
  932. :list(X.list), iterator(X.iterator) {}
  933. const ygg_node & get_list() const {
  934. return list;
  935. }
  936. bool is_begin() const {
  937. return iterator == list.begin().iterator;
  938. }
  939. bool is_not_begin() const {
  940. return iterator != list.begin().iterator;
  941. }
  942. bool is_end() const {
  943. return iterator == list.end().iterator;
  944. }
  945. bool is_not_end() const {
  946. return iterator != list.end().iterator;
  947. }
  948. ygg_const_iterator & operator++() {
  949. ++iterator;
  950. return *this;
  951. }
  952. const ygg_const_iterator operator++(int) {
  953. return ygg_const_iterator(list, iterator++);
  954. }
  955. ygg_const_iterator & operator--() {
  956. --iterator;
  957. return *this;
  958. }
  959. const ygg_const_iterator operator--(int) {
  960. return ygg_const_iterator(list, iterator--);
  961. }
  962. const ygg_node & operator*() const {
  963. return *iterator;
  964. }
  965. const ygg_node * operator->() const {
  966. return iterator.operator->();
  967. }
  968. bool operator==(const ygg_const_iterator &X) const {
  969. assert(list == X.list);
  970. return iterator == X.iterator;
  971. }
  972. bool operator!=(const ygg_const_iterator &X) const {
  973. assert(list == X.list);
  974. return iterator != X.iterator;
  975. }
  976. };
  977. class ygg_const_reverse_iterator :public ygg_iterator_base {
  978. friend class ygg_reverse_iterator;
  979. const ygg_node list;
  980. ygg_list_type::const_reverse_iterator iterator;
  981. public:
  982. ygg_const_reverse_iterator(ygg_node X_list, ygg_list_type::const_reverse_iterator X_iterator)
  983. :list(X_list), iterator(X_iterator) {}
  984. ygg_const_reverse_iterator(const ygg_reverse_iterator &X)
  985. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  986. :list(X.list), iterator((ygg_list_type::const_reverse_iterator&)(X.iterator)) {}
  987. #else
  988. :list(X.list), iterator(X.iterator) {}
  989. #endif
  990. ygg_const_reverse_iterator(const ygg_const_reverse_iterator &X)
  991. :list(X.list), iterator(X.iterator) {}
  992. const ygg_node & get_list() const {
  993. return list;
  994. }
  995. bool is_rbegin() const {
  996. return iterator == list.rbegin().iterator;
  997. }
  998. bool is_not_rbegin() const {
  999. return iterator != list.rbegin().iterator;
  1000. }
  1001. bool is_rend() const {
  1002. return iterator == list.rend().iterator;
  1003. }
  1004. bool is_not_rend() const {
  1005. return iterator != list.rend().iterator;
  1006. }
  1007. ygg_const_reverse_iterator & operator++() {
  1008. ++iterator;
  1009. return *this;
  1010. }
  1011. const ygg_const_reverse_iterator operator++(int) {
  1012. return ygg_const_reverse_iterator(list, iterator++);
  1013. }
  1014. ygg_const_reverse_iterator & operator--() {
  1015. --iterator;
  1016. return *this;
  1017. }
  1018. const ygg_const_reverse_iterator operator--(int) {
  1019. return ygg_const_reverse_iterator(list, iterator--);
  1020. }
  1021. const ygg_node & operator*() const {
  1022. return *iterator;
  1023. }
  1024. const ygg_node * operator->() const {
  1025. return iterator.operator->();
  1026. }
  1027. bool operator==(const ygg_const_reverse_iterator &X) const {
  1028. assert(list == X.list);
  1029. return iterator == X.iterator;
  1030. }
  1031. bool operator!=(const ygg_const_reverse_iterator &X) const {
  1032. assert(list == X.list);
  1033. return iterator != X.iterator;
  1034. }
  1035. };
  1036. inline bool ygg_iterator::is_begin() const {
  1037. return *((ygg_const_iterator*)(this)) == list.begin();
  1038. }
  1039. inline bool ygg_iterator::is_not_begin() const {
  1040. return *((ygg_const_iterator*)(this)) != list.begin();
  1041. }
  1042. inline bool ygg_iterator::is_end() const {
  1043. return *((ygg_const_iterator*)(this)) == list.end();
  1044. }
  1045. inline bool ygg_iterator::is_not_end() const {
  1046. return *((ygg_const_iterator*)(this)) != list.end();
  1047. }
  1048. inline bool ygg_reverse_iterator::is_rbegin() const {
  1049. return *((ygg_const_reverse_iterator*)(this)) == list.rbegin();
  1050. }
  1051. inline bool ygg_reverse_iterator::is_not_rbegin() const {
  1052. return *((ygg_const_reverse_iterator*)(this)) != list.rbegin();
  1053. }
  1054. inline bool ygg_reverse_iterator::is_rend() const {
  1055. return *((ygg_const_reverse_iterator*)(this)) == list.rend();
  1056. }
  1057. inline bool ygg_reverse_iterator::is_not_rend() const {
  1058. return *((ygg_const_reverse_iterator*)(this)) != list.rend();
  1059. }
  1060. //
  1061. // ■yggリストクラス
  1062. //
  1063. class ygg_proto_list :public ygg_node_body {
  1064. friend class ygg_node_body;
  1065. protected:
  1066. ygg_list_type body;
  1067. ygg_proto_list() {}
  1068. bool is_live() const;
  1069. ygg_string get_value() const;
  1070. ygg_string get_text() const;
  1071. ygg_string get_attribute() const;
  1072. ygg_string get_xml(const ygg_string &indent = ygg_term::empty) const;
  1073. ygg_string get_xml_attribute() const;
  1074. ygg_string get_sox(const ygg_string &indent) const;
  1075. void adopt_node(ygg_node X);
  1076. void exile_node(ygg_node X);
  1077. ygg_proto_list * adopt_child(ygg_node X_node);
  1078. ygg_proto_list * exile_child(ygg_node X_node);
  1079. #ifdef _DEBUG
  1080. bool assert_other(const ygg_node &X) const;
  1081. #endif
  1082. inline ygg_node get_single() {
  1083. return operator[](0);
  1084. }
  1085. };
  1086. class ygg_list :public ygg_proto_list {
  1087. protected:
  1088. ygg_list() {}
  1089. ygg_string get_type() const;
  1090. ygg_string get_name() const;
  1091. ygg_node operator[](const ygg_string &path);
  1092. ygg_node operator[](const unsigned index);
  1093. const int get_size() const;
  1094. ygg_iterator begin();
  1095. ygg_iterator end();
  1096. ygg_reverse_iterator rbegin();
  1097. ygg_reverse_iterator rend();
  1098. ygg_const_iterator begin() const;
  1099. ygg_const_iterator end() const;
  1100. ygg_const_reverse_iterator rbegin() const;
  1101. ygg_const_reverse_iterator rend() const;
  1102. bool match_path(const ygg_string &path) const;
  1103. ygg_node clone() const;
  1104. public:
  1105. static
  1106. ygg_node create() {
  1107. return new ygg_list();
  1108. }
  1109. };
  1110. inline ygg_iterator ygg_node::begin() {
  1111. if (ygg_node_type::list == get_type()) {
  1112. return body->begin();
  1113. } else {
  1114. return ygg_list::create().adopt_node(*this).begin();
  1115. }
  1116. }
  1117. inline ygg_iterator ygg_node::end() {
  1118. if (ygg_node_type::list == get_type()) {
  1119. return body->end();
  1120. } else {
  1121. return ygg_list::create().adopt_node(*this).end();
  1122. }
  1123. }
  1124. inline ygg_reverse_iterator ygg_node::rbegin() {
  1125. if (ygg_node_type::list == get_type()) {
  1126. return body->rbegin();
  1127. } else {
  1128. return ygg_list::create().adopt_node(*this).rbegin();
  1129. }
  1130. }
  1131. inline ygg_reverse_iterator ygg_node::rend() {
  1132. if (ygg_node_type::list == get_type()) {
  1133. return body->rend();
  1134. } else {
  1135. return ygg_list::create().adopt_node(*this).rend();
  1136. }
  1137. }
  1138. inline ygg_const_iterator ygg_node::begin() const {
  1139. if (ygg_node_type::list == get_type()) {
  1140. return body->begin();
  1141. } else {
  1142. return ygg_list::create().adopt_node(*this).begin();
  1143. }
  1144. }
  1145. inline ygg_const_iterator ygg_node::end() const {
  1146. if (ygg_node_type::list == get_type()) {
  1147. return body->end();
  1148. } else {
  1149. return ygg_list::create().adopt_node(*this).end();
  1150. }
  1151. }
  1152. inline ygg_const_reverse_iterator ygg_node::rbegin() const {
  1153. if (ygg_node_type::list == get_type()) {
  1154. return body->rbegin();
  1155. } else {
  1156. return ygg_list::create().adopt_node(*this).rbegin();
  1157. }
  1158. }
  1159. inline ygg_const_reverse_iterator ygg_node::rend() const {
  1160. if (ygg_node_type::list == get_type()) {
  1161. return body->rend();
  1162. } else {
  1163. return ygg_list::create().adopt_node(*this).rend();
  1164. }
  1165. }
  1166. //
  1167. // ■ygg基底クラス
  1168. //
  1169. class ygg_root :public ygg_proto_list {
  1170. friend class ygg_ghost;
  1171. protected:
  1172. ygg_root() {}
  1173. ~ygg_root() {
  1174. exile_node(operator[](ygg_term::node));
  1175. }
  1176. ygg_string get_type() const;
  1177. ygg_string get_name() const;
  1178. ygg_node operator[](const ygg_string &path);
  1179. bool match_path(const ygg_string &path) const;
  1180. void adopt_node(ygg_node X);
  1181. void exile_node(ygg_node X);
  1182. ygg_node clone() const;
  1183. public:
  1184. static ygg_node create() {
  1185. return new ygg_root;
  1186. }
  1187. static inline ygg_node parse_sox(const ygg_string &sox, sax_handler *sax = NULL);
  1188. static inline ygg_node parse_xml(const ygg_string &xml, sax_handler *sax = NULL);
  1189. };
  1190. inline ygg_node ygg_node::parse_sox(const ygg_string &sox, sax_handler *sax) {
  1191. ygg_node node = ygg_root::parse_sox(sox, sax)[ygg_term::node];
  1192. // node.self_exile();
  1193. return node;
  1194. }
  1195. inline ygg_node ygg_node::parse_xml(const ygg_string &xml, sax_handler *sax) {
  1196. return ygg_root::parse_xml(xml, sax)[ygg_term::node];
  1197. }
  1198. //
  1199. // ■ygg要素クラス
  1200. //
  1201. class ygg_element :public ygg_root {
  1202. ygg_string name;
  1203. protected:
  1204. ygg_element() {}
  1205. ygg_string get_type() const;
  1206. ygg_string get_name() const;
  1207. // ygg_string get_value() const; use ygg_list::get_value()
  1208. // ygg_string get_text() const; use ygg_list::get_text()
  1209. ygg_string get_xml(const ygg_string &indent = ygg_term::empty) const;
  1210. ygg_string get_xml_attribute() const;
  1211. ygg_string get_sox(const ygg_string &indent) const;
  1212. bool match_path(const ygg_string &path) const;
  1213. void set_name(const ygg_string &X);
  1214. void set_value(const ygg_string &X);
  1215. ygg_node clone() const;
  1216. public:
  1217. static
  1218. ygg_node create() {
  1219. return new ygg_element;
  1220. }
  1221. };
  1222. inline void ygg_node_body::regist_parent(ygg_root *X) {
  1223. assert(NULL == parent);
  1224. //assert(("このノードには既に親がいます。いったん '親ノード.exile_node(このノード)' で絶縁するか、'このノード.close()' でクローンを作成してください。", NULL == parent));
  1225. if (NULL != this && ygg_node_type::empty != get_type()) {
  1226. parent = X;
  1227. X->adopt_child(this);
  1228. }
  1229. }
  1230. inline void ygg_node_body::unregist_parent(ygg_root *X) {
  1231. if (NULL != this && ygg_node_type::empty != get_type()) {
  1232. parent = NULL;
  1233. X->exile_child(this);
  1234. }
  1235. }
  1236. /******************************************************************************
  1237. □■□■ Trick Library 'dagger' □■□■
  1238. ■□■□ http://tricklib.com/cxx/dagger/ ■□■□
  1239. ******************************************************************************/
  1240. //
  1241. // ■ygg_position
  1242. //
  1243. class ygg_position {
  1244. public:
  1245. //struct ygg_position {
  1246. int line;
  1247. int row;
  1248. ygg_position(int X_line = 1, int X_row = 1)
  1249. :line(X_line), row(X_row) {}
  1250. ygg_position(const ygg_position &X)
  1251. :line(X.line), row(X.row) {}
  1252. ygg_position & set_line(int X_line = 1) {
  1253. line = X_line;
  1254. return *this;
  1255. }
  1256. ygg_position & set_row(int X_row = 1) {
  1257. row = X_row;
  1258. return *this;
  1259. }
  1260. ygg_position & set_position(int X_line = 1, int X_row = 1) {
  1261. line = X_line;
  1262. row = X_row;
  1263. return *this;
  1264. }
  1265. ygg_position & set_position(const ygg_position &X) {
  1266. line = X.line;
  1267. row = X.row;
  1268. return *this;
  1269. }
  1270. ygg_position & clear() {
  1271. return set_position();
  1272. }
  1273. const int get_line() const {
  1274. return line;
  1275. }
  1276. const int get_row() const {
  1277. return row;
  1278. }
  1279. ygg_position & next_line() {
  1280. ++line;
  1281. return set_row();
  1282. }
  1283. const ygg_position operator+(const ygg_string &X) const {
  1284. return ygg_position() += X;
  1285. }
  1286. ygg_position & operator+=(const ygg_string &X);
  1287. };
  1288. //
  1289. // ■ygg_error_term(1/2)
  1290. //
  1291. namespace ygg_error_term {
  1292. const int genre = 0x01000000;
  1293. const int category = 0x00010000;
  1294. const int sequence = 0x00000100;
  1295. const int sequence_case = 0x00000001;
  1296. const ygg_string default_message = "何らかのエラーが発生しました。";
  1297. };
  1298. class ygg_error_code {
  1299. protected:
  1300. int value;
  1301. public:
  1302. ygg_error_code(int X_value = 0) :value(X_value) {}
  1303. ygg_error_code(const ygg_error_code &X, int X_case = 0) :value(X.value +X_case) {
  1304. assert(0 == X.get_sequence_case() || 0 == X_case);
  1305. }
  1306. bool is_error() const {
  1307. return 0 != get_code();
  1308. }
  1309. const int get_strict_code() const {
  1310. return value;
  1311. }
  1312. const int get_code() const {
  1313. return get_strict_code() & -0x100;
  1314. }
  1315. const int get_spectrum(int spectrum) const {
  1316. return (get_strict_code() / spectrum) & 0xFF;
  1317. }
  1318. const int get_genre() const {
  1319. return get_spectrum(ygg_error_term::genre);
  1320. }
  1321. const int get_category() const {
  1322. return get_spectrum(ygg_error_term::category);
  1323. }
  1324. const int get_sequence() const {
  1325. return get_spectrum(ygg_error_term::sequence);
  1326. }
  1327. const int get_sequence_case() const {
  1328. return get_spectrum(ygg_error_term::sequence_case);
  1329. }
  1330. ygg_error_code & set_code(int X_value) {
  1331. value = X_value;
  1332. return *this;
  1333. }
  1334. ygg_error_code & set_code(const ygg_error_code &X) {
  1335. value = X.value;
  1336. return *this;
  1337. }
  1338. };
  1339. //
  1340. // ■ygg_error_term(2/2)
  1341. //
  1342. namespace ygg_error_term {
  1343. inline const ygg_error_code make_genre(int X_genre) {
  1344. return X_genre *genre;
  1345. }
  1346. inline const ygg_error_code make_category(const ygg_error_code &X_genre, int X_category) {
  1347. return X_genre.get_code() +X_category *category;
  1348. }
  1349. inline const ygg_error_code make_code(const ygg_error_code &X_category, int X_sequence, int X_sequence_case = 0) {
  1350. return X_category.get_code() +X_sequence *sequence +X_sequence_case *sequence_case;
  1351. }
  1352. inline const ygg_error_code make_strict_code(const ygg_error_code &X_sequence, int X_sequence_case) {
  1353. assert(0 == X_sequence.get_sequence_case());
  1354. return X_sequence.get_code() +X_sequence_case *sequence_case;
  1355. }
  1356. const ygg_error_code default_genre = make_genre(0x00);
  1357. const ygg_error_code default_category = make_category(default_genre, 0x00);
  1358. const ygg_error_code no_error = make_code(default_category, 0x00);
  1359. const ygg_error_code default_error = make_code(default_category, 0x01);
  1360. const ygg_error_code device = make_genre(0x01);
  1361. const ygg_error_code file = make_category(device, 0x01);
  1362. const ygg_error_code cannot_open_file = make_code(file, 0x01);
  1363. const ygg_error_code stream = make_category(device, 0x02);
  1364. const ygg_error_code invalid_stream = make_code(stream, 0x01);
  1365. const ygg_error_code syntax = make_genre(0x02);
  1366. const ygg_error_code ygg_syntax = make_category(syntax, 0x00);
  1367. const ygg_error_code ygg_broken_char = make_code(ygg_syntax, 0x01);
  1368. const ygg_error_code ygg_invalid_name = make_code(ygg_syntax, 0x02);
  1369. const ygg_error_code ygg_invalid_name_a = make_strict_code(ygg_invalid_name, 0x01);
  1370. const ygg_error_code ygg_invalid_name_b = make_strict_code(ygg_invalid_name, 0x02);
  1371. const ygg_error_code duplication_attribute
  1372. = make_code(ygg_syntax, 0x03);
  1373. const ygg_error_code many_roots = make_code(ygg_syntax, 0x04);
  1374. const ygg_error_code sox_syntax = make_category(syntax, 0x01);
  1375. const ygg_error_code sox_deep_indent = make_code(sox_syntax, 0x01);
  1376. const ygg_error_code xml_syntax = make_category(syntax, 0x02);
  1377. const ygg_error_code unmatch_tags = make_code(xml_syntax, 0x01);
  1378. const ygg_error_code unmatch_tags_a = make_strict_code(unmatch_tags, 0x01);
  1379. const ygg_error_code unmatch_tags_b = make_strict_code(unmatch_tags, 0x02);
  1380. const ygg_error_code csv_syntax = make_category(syntax, 0x03);
  1381. const ygg_error_code memory = make_genre(0x03);
  1382. const ygg_error_code user = make_genre(0x04);
  1383. };
  1384. //
  1385. // ■yggエラークラス
  1386. //
  1387. class ygg_error :public ygg_error_code, public ygg_position {
  1388. ygg_string message;
  1389. public:
  1390. ygg_error(const ygg_error &X)
  1391. :ygg_error_code(X), ygg_position(X), message(X.message) {}
  1392. ygg_error(const ygg_error_code &X = ygg_error_term::no_error, const ygg_position &X_position = ygg_position(0,0), const ygg_string &X_message = ygg_term::empty)
  1393. :ygg_error_code(X), ygg_position(X_position), message(X_message) {}
  1394. ygg_error & set_error(const ygg_error_code &X = ygg_error_term::no_error, const ygg_position &X_position = ygg_position(0,0), const ygg_string &X_message = ygg_term::empty) {
  1395. set_code(X);
  1396. set_position(X_position);
  1397. message = X_message;
  1398. return *this;
  1399. }
  1400. ygg_error & clear() {
  1401. return set_error();
  1402. }
  1403. const ygg_string & get_message() const {
  1404. return message;
  1405. }
  1406. ygg_error & set_message(const ygg_string & X_message) {
  1407. message = X_message;
  1408. return *this;
  1409. }
  1410. };
  1411. //
  1412. // ■SAXハンドラ基本クラス
  1413. //
  1414. class sax_handler {
  1415. friend class ygg_parser;
  1416. ygg_parser *parser;
  1417. void set_parser(ygg_parser *X_parser) {
  1418. parser = X_parser;
  1419. }
  1420. protected:
  1421. ygg_parser * get_parser();
  1422. public:
  1423. virtual void on_error(ygg_error* parse_error); // parse_error->clear() 可
  1424. virtual void start_document(ygg_node root); // コメントノードの付加のみ 可
  1425. virtual void end_document(ygg_node &root); // root.vanish() および、その他の全操作 可
  1426. virtual void start_element(ygg_node element); // 属性の全操作 可
  1427. virtual void end_element(ygg_node element); // element.vanish() および、同element内のその他の全操作 可
  1428. virtual void catch_text(ygg_node text); // text.set_value(...) 可
  1429. virtual void catch_comment(ygg_node comment); // comment.set_value(...) 可
  1430. };
  1431. //
  1432. //
  1433. //
  1434. class error_catcher :public sax_handler {
  1435. public:
  1436. ygg_error parse_error;
  1437. virtual ~error_catcher() {}
  1438. void on_error(ygg_error* X_parse_error) {
  1439. this->parse_error = *X_parse_error;
  1440. }
  1441. };
  1442. //
  1443. // □yggパーザクラス
  1444. //
  1445. class ygg_parser {
  1446. protected:
  1447. ygg_position anchor_position, hot_position;
  1448. sax_handler *sax;
  1449. ygg_parser (sax_handler *X_sax = NULL) :anchor_position(), hot_position(), sax(X_sax) {
  1450. if (sax) {
  1451. sax->set_parser(this);
  1452. }
  1453. }
  1454. const ygg_error_code check_name(const ygg_string &type, const ygg_string &X);
  1455. const ygg_error_code check_element_name(const ygg_string &X) {
  1456. return check_name("要素名", X);
  1457. }
  1458. const ygg_error_code check_attribute_name(const ygg_string &X) {
  1459. return check_name("属性名", X);
  1460. }
  1461. public:
  1462. ygg_error parse_error;
  1463. ygg_parser * set_sax_handler(sax_handler *X_sax);
  1464. const int get_line() const {
  1465. return anchor_position.get_line();
  1466. }
  1467. const int get_row() const {
  1468. return anchor_position.get_row();
  1469. }
  1470. void raise_error(const ygg_error_code &X_code = ygg_error_term::default_error, const ygg_string &X_message = ygg_error_term::default_message) {
  1471. raise_error(X_code, anchor_position, X_message);
  1472. }
  1473. void raise_error(const ygg_error_code &X_code, const ygg_position &X_position, const ygg_string &X_message = ygg_error_term::default_message) {
  1474. parse_error.set_error(X_code, X_position, X_message);
  1475. if (sax) {
  1476. sax->on_error(&parse_error);
  1477. }
  1478. }
  1479. };
  1480. //
  1481. // ■SOXパーザクラス
  1482. //
  1483. class sox_parser :public ygg_parser {
  1484. ygg_node &root;
  1485. ygg_node hot_element;
  1486. ygg_node last_node;
  1487. ygg_node indent_node_list;
  1488. int anchor_indent;
  1489. ygg_string unparsed_buffer;
  1490. public:
  1491. sox_parser(ygg_node &X_root, sax_handler *X_sax = NULL)
  1492. :ygg_parser(X_sax), root(X_root) {}
  1493. sox_parser & set_sax_handler(sax_handler *X_sax) { set_sax_handler(X_sax); return *this; }
  1494. #if defined(__YGGDRASIL_WITH_BABEL__)
  1495. sox_parser & read(std::istream &stream, int encoding = babel::base_encoding::unknown);
  1496. sox_parser & write(std::ostream &stream, int encoding = babel::base_encoding::unknown);
  1497. sox_parser & load(const ygg_string &X_filename, int encoding = babel::base_encoding::unknown);
  1498. sox_parser & save(const ygg_string &X_filename, int encoding = babel::base_encoding::unknown);
  1499. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1500. sox_parser & read(std::istream &stream);
  1501. sox_parser & write(std::ostream &stream);
  1502. sox_parser & load(const ygg_string &X_filename);
  1503. sox_parser & save(const ygg_string &X_filename);
  1504. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1505. sox_parser & init_root();
  1506. sox_parser & parse_line(const ygg_string &X_line);
  1507. sox_parser & parse_string(const ygg_string &X_text);
  1508. sox_parser & flush();
  1509. sox_parser & end_stream();
  1510. protected:
  1511. void parse(const ygg_string &X_line);
  1512. void catch_event(ygg_node node);
  1513. };
  1514. #if defined(__YGGDRASIL_WITH_BABEL__)
  1515. inline sox_parser & sox_parser::read(std::istream &stream, int encoding) {
  1516. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1517. inline sox_parser & sox_parser::read(std::istream &stream) {
  1518. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1519. init_root();
  1520. if (!stream) {
  1521. raise_error(ygg_error_term::invalid_stream, "無効なストリームが指定されました。");
  1522. } else {
  1523. #if defined(__YGGDRASIL_WITH_BABEL__)
  1524. using namespace babel;
  1525. ygg_string::value_type first_buffer[4096];
  1526. ygg_string second_buffer, third_buffer;
  1527. if (!stream.eof()) {
  1528. stream.read(first_buffer, 4096);
  1529. second_buffer.assign(first_buffer, stream.gcount());
  1530. const int determine_encoding = (babel::base_encoding::unknown == encoding) ? analyze_base_encoding(second_buffer): encoding;
  1531. bbl_translater<bbl_binary, ygg_string> translater = manual_translate_engine<bbl_binary, ygg_string>::order(determine_encoding);
  1532. while(true) {
  1533. translater << second_buffer;
  1534. translater >> third_buffer;
  1535. parse_string(third_buffer);
  1536. if (stream.eof() || parse_error.is_error()) {
  1537. break;
  1538. }
  1539. stream.read(first_buffer, 1024);
  1540. second_buffer.assign(first_buffer, stream.gcount());
  1541. }
  1542. }
  1543. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1544. ygg_string buffer;
  1545. while(!stream.eof() && !parse_error.is_error()) {
  1546. std::getline(stream, buffer);
  1547. parse_line(buffer);
  1548. }
  1549. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1550. }
  1551. end_stream();
  1552. return *this;
  1553. }
  1554. #if defined(__YGGDRASIL_WITH_BABEL__)
  1555. inline sox_parser & sox_parser::write(std::ostream &stream, int encoding) {
  1556. using namespace babel;
  1557. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1558. inline sox_parser & sox_parser::write(std::ostream &stream) {
  1559. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1560. if (!stream) {
  1561. raise_error(ygg_error_term::invalid_stream, "無効なストリームが指定されました。");
  1562. return *this; // return
  1563. }
  1564. #if defined(__YGGDRASIL_WITH_BABEL__)
  1565. stream << translate_to_binary(root.get_sox(), encoding);
  1566. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1567. stream << root.get_sox();
  1568. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1569. return *this; // return
  1570. }
  1571. #if defined(__YGGDRASIL_WITH_BABEL__)
  1572. inline sox_parser & sox_parser::load(const ygg_string &X_filename, int encoding) {
  1573. std::ifstream file(X_filename.c_str(), std::ios::binary);
  1574. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1575. inline sox_parser & sox_parser::load(const ygg_string &X_filename) {
  1576. std::ifstream file(X_filename.c_str());
  1577. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1578. if (!file) {
  1579. raise_error(ygg_error_term::cannot_open_file, "ファイル " +X_filename +" を開けませんでした。");
  1580. return *this; // return
  1581. }
  1582. #if defined(__YGGDRASIL_WITH_BABEL__)
  1583. return read(file, encoding); // return
  1584. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1585. return read(file); // return
  1586. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1587. }
  1588. #if defined(__YGGDRASIL_WITH_BABEL__)
  1589. inline sox_parser & sox_parser::save(const ygg_string &X_filename, int encoding) {
  1590. std::ofstream file(X_filename.c_str(), std::ios::binary);
  1591. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1592. inline sox_parser & sox_parser::save(const ygg_string &X_filename) {
  1593. std::ofstream file(X_filename.c_str());
  1594. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1595. if (!file) {
  1596. raise_error(ygg_error_term::cannot_open_file, "ファイル " +X_filename +" を開けませんでした。");
  1597. return *this; // return
  1598. }
  1599. #if defined(__YGGDRASIL_WITH_BABEL__)
  1600. return write(file, encoding); // return
  1601. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1602. return write(file); // return
  1603. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1604. }
  1605. //
  1606. //
  1607. //
  1608. class sox_file :public ygg_node {
  1609. ygg_string filename;
  1610. public:
  1611. ygg_error parse_error;
  1612. sox_file(const ygg_string &X_filename) :filename(X_filename) {}
  1613. ~sox_file() {}
  1614. #if defined(__YGGDRASIL_WITH_BABEL__)
  1615. sox_file & read(sax_handler *sax = NULL, int encoding = babel::base_encoding::unknown) {
  1616. parse_error = sox_parser(*this, sax).load(filename, encoding).parse_error;
  1617. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1618. sox_file & read(sax_handler *sax = NULL) {
  1619. parse_error = sox_parser(*this, sax).load(filename).parse_error;
  1620. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1621. return *this;
  1622. }
  1623. #if defined(__YGGDRASIL_WITH_BABEL__)
  1624. sox_file & write(int encoding = babel::base_encoding::unknown) {
  1625. sox_parser(*this).save(filename, encoding);
  1626. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1627. sox_file & write() {
  1628. sox_parser(*this).save(filename);
  1629. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1630. return *this;
  1631. }
  1632. sox_file & set_filename(const ygg_string &X_filename) {
  1633. filename = X_filename;
  1634. return *this;
  1635. }
  1636. ygg_string get_filename() {
  1637. return filename;
  1638. }
  1639. };
  1640. class sox_autofile :public sox_file {
  1641. public:
  1642. sox_autofile(const ygg_string &X_filename) :sox_file(X_filename) {
  1643. read();
  1644. }
  1645. ~sox_autofile() {
  1646. write();
  1647. }
  1648. };
  1649. class sox_stream :public ygg_node {
  1650. public:
  1651. ygg_error parse_error;
  1652. #if defined(__YGGDRASIL_WITH_BABEL__)
  1653. sox_stream & read(std::istream &stream, sax_handler *sax = NULL, int encoding = babel::base_encoding::unknown) {
  1654. parse_error = sox_parser(*this, sax).read(stream, encoding).parse_error;
  1655. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1656. sox_stream & read(std::istream &stream, sax_handler *sax = NULL) {
  1657. parse_error = sox_parser(*this, sax).read(stream).parse_error;
  1658. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1659. return *this;
  1660. }
  1661. #if defined(__YGGDRASIL_WITH_BABEL__)
  1662. sox_stream & write(std::ostream &stream, int encoding = babel::base_encoding::unknown) {
  1663. parse_error = sox_parser(*this).write(stream, encoding).parse_error;
  1664. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1665. sox_stream & write(std::ostream &stream) {
  1666. parse_error = sox_parser(*this).write(stream).parse_error;
  1667. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1668. return *this;
  1669. }
  1670. };
  1671. #if defined(__YGGDRASIL_WITH_BABEL__)
  1672. inline int get_encoding_from_label(const char *label) {
  1673. using namespace babel::base_encoding;
  1674. if (0 == stricmp(label, "shift-jis") ||
  1675. 0 == stricmp(label, "shift_jis") ||
  1676. 0 == stricmp(label, "x-sjis") ||
  1677. 0 == stricmp(label, "sjis") ||
  1678. 0 == stricmp(label, "shiftjis")) {
  1679. return sjis;
  1680. }
  1681. if (0 == stricmp(label, "jis")) {
  1682. return jis;
  1683. }
  1684. if (0 == stricmp(label, "iso-2022-jp")) {
  1685. return iso2022jp;
  1686. }
  1687. if (0 == stricmp(label, "euc") ||
  1688. 0 == stricmp(label, "euc-jp") ||
  1689. 0 == stricmp(label, "x-euc")) {
  1690. return euc;
  1691. }
  1692. if (0 == stricmp(label, "utf-8") ||
  1693. 0 == stricmp(label, "utf8")) {
  1694. return utf8;
  1695. }
  1696. if (0 == stricmp(label, "utf-16") ||
  1697. 0 == stricmp(label, "utf16")) {
  1698. return utf16;
  1699. }
  1700. if (0 == stricmp(label, "utf-16le") ||
  1701. 0 == stricmp(label, "utf16le")) {
  1702. return utf16le;
  1703. }
  1704. if (0 == stricmp(label, "utf-16be") ||
  1705. 0 == stricmp(label, "utf16be")) {
  1706. return utf16be;
  1707. }
  1708. if (0 == stricmp(label, "utf-32") ||
  1709. 0 == stricmp(label, "utf32")) {
  1710. return utf32;
  1711. }
  1712. if (0 == stricmp(label, "utf-32le") ||
  1713. 0 == stricmp(label, "utf32le")) {
  1714. return utf32le;
  1715. }
  1716. if (0 == stricmp(label, "utf-32be") ||
  1717. 0 == stricmp(label, "utf32be")) {
  1718. return utf32be;
  1719. }
  1720. return unknown;
  1721. }
  1722. inline babel::bbl_translater<babel::bbl_binary, ygg_string> get_translater(const ygg_string &header) {
  1723. // ホントならこのまわりのテンプレート引数は省略できるハズなのにぃ(ノД`) ... bcc のヽ(`Д´)ノばかぁ!
  1724. using namespace babel;
  1725. using namespace babel::base_encoding;
  1726. const ygg_string utf8_FEFF = "\xEF\xBB\xBF";
  1727. const int analyze_encoding = analyze_base_encoding(header);
  1728. bbl_translater<bbl_binary, ygg_string> translater = manual_translate_engine<bbl_binary, ygg_string>::order(analyze_encoding);
  1729. switch(analyze_encoding) {
  1730. case utf8: if (0 != header.find(utf8_FEFF)) break;
  1731. case utf16be:
  1732. case utf16le:
  1733. case utf32be:
  1734. case utf32le:
  1735. case jis:
  1736. return translater;
  1737. }
  1738. const ygg_string encoding_label = ygg_root::parse_xml(translater(header))["/?xml/@encoding"].get_value().get_string();
  1739. const int label_encoding = get_encoding_from_label(encoding_label.c_str());
  1740. // encoding_label を優先
  1741. if (unknown != label_encoding && analyze_encoding != label_encoding) {
  1742. return manual_translate_engine<bbl_binary, ygg_string>::order(label_encoding);
  1743. } else {
  1744. return translater.clear();
  1745. }
  1746. }
  1747. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1748. //
  1749. // ■XMLパーザクラス
  1750. //
  1751. class xml_parser :public ygg_parser {
  1752. ygg_node &root;
  1753. ygg_node hot_element;
  1754. ygg_node last_node;
  1755. ygg_node indent_node_list;
  1756. int anchor_indent;
  1757. ygg_string unparsed_buffer;
  1758. public:
  1759. xml_parser(ygg_node &X_root, sax_handler *X_sax = NULL)
  1760. :ygg_parser(X_sax), root(X_root) {}
  1761. xml_parser & set_sax_handler(sax_handler *X_sax) { set_sax_handler(X_sax); return *this; }
  1762. xml_parser & read(std::istream &stream);
  1763. xml_parser & write(std::ostream &stream);
  1764. xml_parser & load(const ygg_string &X_filename);
  1765. xml_parser & save(const ygg_string &X_filename);
  1766. xml_parser & init_root();
  1767. xml_parser & parse_line(const ygg_string &X_line);
  1768. xml_parser & parse_string(const ygg_string &X_text);
  1769. xml_parser & flush();
  1770. xml_parser & end_stream();
  1771. };
  1772. inline xml_parser & xml_parser::read(std::istream &stream) {
  1773. init_root();
  1774. if (!stream) {
  1775. raise_error(ygg_error_term::invalid_stream, "無効なストリームが指定されました。");
  1776. } else {
  1777. #if defined(__YGGDRASIL_WITH_BABEL__)
  1778. using namespace babel;
  1779. ygg_string::value_type first_buffer[1024];
  1780. ygg_string second_buffer, third_buffer;
  1781. if (!stream.eof()) {
  1782. stream.read(first_buffer, 1024);
  1783. second_buffer.assign(first_buffer, stream.gcount());
  1784. bbl_translater<bbl_binary, ygg_string> translater = get_translater(second_buffer);
  1785. while(true) {
  1786. translater << second_buffer;
  1787. translater >> third_buffer;
  1788. parse_string(third_buffer);
  1789. if (stream.eof() || parse_error.is_error()) {
  1790. break;
  1791. }
  1792. stream.read(first_buffer, 1024);
  1793. second_buffer.assign(first_buffer, stream.gcount());
  1794. }
  1795. }
  1796. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1797. ygg_string buffer;
  1798. while(!stream.eof() && !parse_error.is_error()) {
  1799. std::getline(stream, buffer);
  1800. parse_line(buffer);
  1801. }
  1802. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1803. }
  1804. end_stream();
  1805. return *this;
  1806. }
  1807. inline xml_parser & xml_parser::write(std::ostream &stream) {
  1808. #if defined(__YGGDRASIL_WITH_BABEL__)
  1809. using namespace babel;
  1810. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1811. if (!stream) {
  1812. raise_error(ygg_error_term::invalid_stream, "無効なストリームが指定されました。");
  1813. return *this;
  1814. }
  1815. #if defined(__YGGDRASIL_WITH_BABEL__)
  1816. const ygg_string encoding_label = root["/?xml/@encoding"].get_value().get_string();
  1817. const int label_encoding = get_encoding_from_label(encoding_label.c_str());
  1818. stream << translate_to_binary(root.get_xml(), label_encoding);
  1819. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1820. stream << root.get_xml();
  1821. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1822. return *this;
  1823. }
  1824. inline xml_parser & xml_parser::load(const ygg_string &X_filename) {
  1825. #if defined(__YGGDRASIL_WITH_BABEL__)
  1826. std::ifstream file(X_filename.c_str(), std::ios::binary);
  1827. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1828. std::ifstream file(X_filename.c_str());
  1829. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1830. if (!file) {
  1831. raise_error(ygg_error_term::cannot_open_file, "ファイル " +X_filename +" を開けませんでした。");
  1832. return *this;
  1833. }
  1834. return read(file);
  1835. }
  1836. inline xml_parser & xml_parser::save(const ygg_string &X_filename) {
  1837. #if defined(__YGGDRASIL_WITH_BABEL__)
  1838. std::ofstream file(X_filename.c_str(), std::ios::binary);
  1839. #else // defined(__YGGDRASIL_WITH_BABEL__)
  1840. std::ofstream file(X_filename.c_str());
  1841. #endif // defined(__YGGDRASIL_WITH_BABEL__)
  1842. if (!file) {
  1843. raise_error(ygg_error_term::cannot_open_file, "ファイル " +X_filename +" を開けませんでした。");
  1844. return *this;
  1845. }
  1846. return write(file);
  1847. }
  1848. //
  1849. //
  1850. //
  1851. class xml_file :public ygg_node {
  1852. ygg_string filename;
  1853. public:
  1854. ygg_error parse_error;
  1855. xml_file(const ygg_string &X_filename) :filename(X_filename) {}
  1856. ~xml_file() {}
  1857. xml_file & read(sax_handler *sax = NULL) {
  1858. parse_error = xml_parser(*this, sax).load(filename).parse_error;
  1859. return *this;
  1860. }
  1861. xml_file & write() {
  1862. xml_parser(*this).save(filename);
  1863. return *this;
  1864. }
  1865. xml_file & set_filename(const ygg_string &X_filename) {
  1866. filename = X_filename;
  1867. return *this;
  1868. }
  1869. ygg_string get_filename() {
  1870. return filename;
  1871. }
  1872. };
  1873. class xml_autofile :public xml_file {
  1874. public:
  1875. xml_autofile(const ygg_string &X_filename) :xml_file(X_filename) {
  1876. read();
  1877. }
  1878. ~xml_autofile() {
  1879. write();
  1880. }
  1881. };
  1882. class xml_stream :public ygg_node {
  1883. public:
  1884. ygg_error parse_error;
  1885. xml_stream & read(std::istream &stream, sax_handler *sax = NULL) {
  1886. parse_error = xml_parser(*this, sax).read(stream).parse_error;
  1887. return *this;
  1888. }
  1889. xml_stream & write(std::ostream &stream) {
  1890. parse_error = xml_parser(*this).write(stream).parse_error;
  1891. return *this;
  1892. }
  1893. };
  1894. inline ygg_node ygg_root::parse_sox(const ygg_string &sox, sax_handler *sax) {
  1895. ygg_node root;
  1896. sox_parser(root, sax).init_root().parse_string(sox).end_stream();
  1897. return root;
  1898. }
  1899. inline ygg_node ygg_root::parse_xml(const ygg_string &xml, sax_handler *sax) {
  1900. ygg_node root;
  1901. xml_parser(root, sax).init_root().parse_string(xml).end_stream();
  1902. return root;
  1903. }
  1904. } // namespace yggdrasil
  1905. #if defined(__BORLANDC__)
  1906. # pragma warn .8022
  1907. # pragma warn .8026
  1908. # pragma warn .8027
  1909. #endif
  1910. #endif // __YGGDRASIL_YGG_H__
  1911. /******************************************************************************
  1912. □■□■ Wraith the Trickster □■□■
  1913. ■□■□ 〜I'll go with heaven's advantage and fool's wisdom.〜 ■□■□
  1914. ******************************************************************************/