logo

libbulletml

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

ygg_test.cpp (10464B)


  1. /******************************************************************************
  2. 世界樹 -yggdrasil-
  3. 世界樹チュートリアルソースファイル
  4. Coded by Wraith in July 14, 2002.
  5. ******************************************************************************/
  6. // Tab幅を4文字に設定して表示させてください。
  7. ///////////////////////////////////////////////////////////////////////////////
  8. //
  9. // ■ ygg_test.cpp
  10. // http://tricklib.com/cxx/ex/yggdrasil/ygg_test.cpp
  11. //
  12. // □ 関連ファイル
  13. // yggモジュールのヘッダファイル
  14. // http://tricklib.com/cxx/ex/yggdrasil/ygg.h
  15. // yggモジュールの本体
  16. // http://tricklib.com/cxx/ex/yggdrasil/ygg.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. #include <stdio.h>
  28. //#include <iostream> // for debug
  29. //#define __USING_UTF8__
  30. //#include "..\babel\babel.cpp"
  31. #include "ygg.cpp"
  32. #include <stdio.h>
  33. #include <malloc.h>
  34. // VC対策
  35. #if defined(_MSC_VER)
  36. # define std_for if(0) ; else for
  37. #else
  38. # define std_for for
  39. #endif
  40. // gcc対策(一時オブジェクトの左辺値化)
  41. template<class T> T & vitalize(const T &X) { return (T&)X; }
  42. using namespace yggdrasil;
  43. //
  44. // SOX path term
  45. //
  46. // term/node comment text attribute element
  47. // node() ○ ○ ○ ○
  48. // comment() ○ × × ×
  49. // text() × ○ × ×
  50. // attribute() × × ○ ×
  51. // @* × × ○ ×
  52. // element() × × × ○
  53. // * × × × ○
  54. //
  55. class error_print: public sax_handler {
  56. public:
  57. void on_error(ygg_error *parse_error);
  58. };
  59. void error_print::on_error(ygg_error *parse_error) {
  60. if (parse_error->is_error()) {
  61. printf("On Error !!!\n"
  62. "\tlocation %d:%d\n"
  63. "\tcode %02x:%02x:%02x:%02x\n"
  64. "\tmessage %s\n",
  65. parse_error->get_line(),
  66. parse_error->get_row(),
  67. parse_error->get_genre(),
  68. parse_error->get_category(),
  69. parse_error->get_sequence(),
  70. parse_error->get_sequence_case(),
  71. parse_error->get_message().c_str());
  72. }
  73. }
  74. void test_enum() {
  75. sox_file root("test.sox");
  76. root.read(&vitalize(error_print()));
  77. ygg_node node_list = root["//node()"];
  78. // std_for(int i = 0; node_list[i].is_valid(); ++i) {
  79. // printf("path:%s\n", node_list[i].get_path().c_str());
  80. // }
  81. // std_for(ygg_iterator i = node_list.begin(); i != node_list.end(); ++i) {
  82. // printf("path:%s\n", i->get_path().c_str());
  83. // }
  84. std_for(ygg_iterator i = node_list.begin(); i.is_not_end(); ++i) {
  85. printf("path:%s\n", i->get_path().c_str());
  86. }
  87. }
  88. void test_enum_stream() {
  89. sox_stream root;
  90. std::ifstream fs("test.sox");
  91. root.read(fs, &vitalize(error_print()));
  92. ygg_node node_list = root["//node()"];
  93. std_for(int i = 0; node_list[i].is_valid(); ++i) {
  94. printf("path:%s\n", node_list[i].get_path().c_str());
  95. }
  96. }
  97. void test_path() {
  98. sox_file root("test.sox");
  99. root.read(&vitalize(error_print()));
  100. ygg_node length = root["/first/hige/@length"];
  101. std_for(int i = 0; length[i].is_valid(); ++i) {
  102. printf("%s\n", length[i].get_path().c_str());
  103. }
  104. ygg_node hige = root["/first/hige/p/b/c/d/..../.."];
  105. std_for(int i = 0; hige[i].is_valid(); ++i) {
  106. printf("%s\n", hige[i].get_path().c_str());
  107. }
  108. ygg_node p = root["/first/hige/p"];
  109. std_for(int i = 0; p[i].is_valid(); ++i) {
  110. printf("%s\n", p[i].get_path().c_str());
  111. }
  112. }
  113. void test_csv() {
  114. sox_file root("csv.sox");
  115. ygg_node item = root.read(&vitalize(error_print()))["/csv/item"];
  116. std_for(int i = 0; item[i].is_valid(); ++i) {
  117. printf("%s\n", ygg_utility::create_line(item[i]["node()"], ",").c_str());
  118. }
  119. }
  120. void test_text() {
  121. printf("%s\n", sox_file("test.sox").read(&vitalize(error_print())).get_text().c_str());
  122. }
  123. class sax_print: public error_print {
  124. public:
  125. void on_error(ygg_error* parse_error);
  126. void start_document(ygg_node root);
  127. void end_document(ygg_node &root);
  128. void start_element(ygg_node element);
  129. void end_element(ygg_node element);
  130. void catch_text(ygg_node text);
  131. void catch_comment(ygg_node comment);
  132. };
  133. void sax_print::on_error(ygg_error *parse_error) {
  134. printf("sax_print::on_error\n");
  135. error_print::on_error(parse_error);
  136. }
  137. void sax_print::start_document(ygg_node) {
  138. printf("sax_print::start_document\n");
  139. }
  140. void sax_print::end_document(ygg_node&) {
  141. printf("sax_print::end_document\n");
  142. }
  143. void sax_print::start_element(ygg_node element) {
  144. printf("sax_print::start_element[%s]\n", element.get_name().c_str());
  145. // ygg_node attributes = element["@*"];
  146. // std_for(int i = 0; attributes[i].is_valid(); ++i) {
  147. // printf("@%s=\"%s\"\n", attributes[i].get_name().c_str(), attributes[i].get_value().c_str());
  148. // }
  149. std_for(ygg_iterator i = element["@*"].begin(); i.is_not_end(); ++i) {
  150. printf("@%s=\"%s\"\n", i->get_name().c_str(), i->get_value().c_str());
  151. }
  152. }
  153. void sax_print::end_element(ygg_node element) {
  154. printf("sax_print::end_element[%s]\n", element.get_name().c_str());
  155. }
  156. void sax_print::catch_text(ygg_node text) {
  157. printf("sax_print::catch_text\n%s\n", text.get_text().c_str());
  158. }
  159. void sax_print::catch_comment(ygg_node) {
  160. printf("sax_print::catch_comment\n");
  161. }
  162. void test_handler() {
  163. sax_print sax;
  164. sox_file("test.sox").read(&sax);
  165. }
  166. void test_const() {
  167. const ygg_node node_list = sox_file("test.sox").read(&vitalize(error_print()))["//node()"];
  168. std_for(ygg_const_iterator i = node_list.begin(); i.is_not_end(); ++i) {
  169. printf("path:%s\n", i->get_path().c_str());
  170. }
  171. }
  172. void test_locacl_enum() {
  173. sox_file root("test.sox");
  174. root.read(&vitalize(error_print()));
  175. ygg_node node_list = root["/first/hige[0]/.//node()"];
  176. std_for(ygg_iterator i = node_list.begin(); i.is_not_end(); ++i) {
  177. printf("path:%s\n", i->get_path().c_str());
  178. }
  179. }
  180. void test_xml() {
  181. sax_print sax;
  182. xml_file root("test.xml");
  183. root.read(&sax);
  184. ygg_node node_list = root["//node()"];
  185. std_for(ygg_iterator i = node_list.begin(); i.is_not_end(); ++i) {
  186. printf("path:%s\n", i->get_path().c_str());
  187. }
  188. printf("xml:\n%s\n", root.get_xml().c_str());
  189. }
  190. void test_string() {
  191. ygg_node root = ygg_root::parse_sox(
  192. "sox>\n"
  193. "\traw>\n"
  194. "\t\ttext\n", &vitalize(error_print()));
  195. root["/sox"].adopt_sox("raw>\n\tadopt_sox\n");
  196. root["/sox"].adopt_xml("<raw>adopt_xml</raw>");
  197. printf("%s\n", root.get_xml().c_str());
  198. }
  199. void test_error() {
  200. error_catcher error_holder;
  201. ygg_root::parse_sox(
  202. "sox>\n"
  203. // "\t&raw>\n" // (02:00:02:01)
  204. // "\traw*>\n" // (02:00:02:02)
  205. "\t\traw>\n" // (02:01:01:00)
  206. "\traw>\n"
  207. "\t\thage=true\n"
  208. // "\t\thage=true\n" // (02:00:03:00)
  209. // "\t\t-hage=true\n" // (02:00:02:01)
  210. // "\t\thage?=true\n" // (02:00:02:02)
  211. "\t\ttext\n", &error_holder);
  212. ygg_error &error = error_holder.parse_error;
  213. if (error.is_error()) {
  214. printf("On Error !!!\n"
  215. "\tlocation %d:%d\n"
  216. "\tcode %02x:%02x:%02x:%02x\n"
  217. "\tmessage %s\n",
  218. error.get_line(),
  219. error.get_row(),
  220. error.get_genre(),
  221. error.get_category(),
  222. error.get_sequence(),
  223. error.get_sequence_case(),
  224. error.get_message().c_str());
  225. } else {
  226. printf("エラーはありませんでした。\n");
  227. }
  228. }
  229. int main() {
  230. // std::locale::global(std::locale("japanese"));
  231. #if defined(__WITH_BABEL__)
  232. babel::init_babel();
  233. #endif // defined(__WITH_BABEL__)
  234. while(true) {
  235. int test;
  236. std::cout << std::endl;
  237. std::cout << " *** ygg_test menu ***" << std::endl;
  238. std::cout << std::endl;
  239. std::cout << " 0 ... version information" << std::endl;
  240. std::cout << std::endl;
  241. std::cout << " 1 ... enum test" << std::endl;
  242. std::cout << " 2 ... enum test with stream" << std::endl;
  243. std::cout << " 3 ... path test" << std::endl;
  244. std::cout << " 4 ... csv test" << std::endl;
  245. std::cout << " 5 ... text test" << std::endl;
  246. std::cout << " 6 ... sax handle test" << std::endl;
  247. std::cout << " 7 ... const test" << std::endl;
  248. std::cout << " 8 ... local enum test" << std::endl;
  249. std::cout << " 9 ... xml test" << std::endl;
  250. std::cout << " 10 ... string test" << std::endl;
  251. std::cout << " 11 ... parse error test" << std::endl;
  252. std::cout << std::endl;
  253. std::cout << " other number ... exit" << std::endl;
  254. std::cout << std::endl;
  255. std::cout << " Please, input number." << std::endl;
  256. std::cout << std::endl;
  257. std::cin >> test;
  258. std::cout << std::endl;
  259. switch(test) {
  260. case 0:
  261. std::cout << "Version Information(SOX)" << std::endl;
  262. std::cout << ygg_node::parse_sox(ygg_term::yggdrasil_version).get_sox().c_str() << std::endl;
  263. std::cout << "Version Information(XML)" << std::endl;
  264. std::cout << ygg_node::parse_sox(ygg_term::yggdrasil_version).get_xml().c_str() << std::endl;
  265. break;
  266. case 1:
  267. std::cout << "Execute enum test!" << std::endl;
  268. test_enum();
  269. break;
  270. case 2:
  271. std::cout << "Execute enum test with stream!" << std::endl;
  272. test_enum_stream();
  273. break;
  274. case 3:
  275. std::cout << "Execute path test!" << std::endl;
  276. test_path();
  277. break;
  278. case 4:
  279. std::cout << "Execute csv test!" << std::endl;
  280. test_csv();
  281. break;
  282. case 5:
  283. std::cout << "Execute text test!" << std::endl;
  284. test_text();
  285. break;
  286. case 6:
  287. std::cout << "Execute sax handle test!" << std::endl;
  288. test_handler();
  289. break;
  290. case 7:
  291. std::cout << "Execute const test!" << std::endl;
  292. test_const();
  293. break;
  294. case 8:
  295. std::cout << "Execute local enum test!" << std::endl;
  296. test_locacl_enum();
  297. break;
  298. case 9:
  299. std::cout << "Execute xml test!" << std::endl;
  300. test_xml();
  301. break;
  302. case 10:
  303. std::cout << "Execute string test!" << std::endl;
  304. test_string();
  305. break;
  306. case 11:
  307. std::cout << "Execute parse error test!" << std::endl;
  308. test_error();
  309. break;
  310. default:
  311. std::cout << "Exit! Good-bye!" << std::endl;
  312. return 0;
  313. }
  314. }
  315. }
  316. /******************************************************************************
  317. □■□■ Wraith the Trickster □■□■
  318. ■□■□ 〜I'll go with heaven's advantage and fool's wisdom.〜 ■□■□
  319. ******************************************************************************/