Simple XML subset parser comparable to glib's Markup parser, but without any dependencies in one self contained file. Forked from https://github.com/ooxi/xml.c
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

210 wiersze
6.7 KiB

  1. /**
  2. * Copyright (c) 2012 ooxi/xml.c
  3. * https://github.com/ooxi/xml.c
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * In no event will the authors be held liable for any damages arising from the
  7. * use of this software.
  8. *
  9. * Permission is granted to anyone to use this software for any purpose,
  10. * including commercial applications, and to alter it and redistribute it
  11. * freely, subject to the following restrictions:
  12. *
  13. * 1. The origin of this software must not be misrepresented; you must not
  14. * claim that you wrote the original software. If you use this software in a
  15. * product, an acknowledgment in the product documentation would be
  16. * appreciated but is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. */
  23. #include <stdbool.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <xml.h>
  27. /**
  28. * Will halt the program iff assertion fails
  29. */
  30. static void _assert_that(_Bool condition, char const* message, char const* func, char const* file, int line) {
  31. if (!condition) {
  32. fprintf(stderr, "Assertion failed: %s, in %s (%s:%i)\n", message, func, file, line);
  33. exit(EXIT_FAILURE);
  34. }
  35. }
  36. #define assert_that(condition, message) \
  37. _assert_that(condition, message, __func__, __FILE__, __LINE__)
  38. /**
  39. * @return true iff xml string equals the c string
  40. */
  41. static _Bool string_equals(struct xml_string* a, char const* b) {
  42. size_t a_length = xml_string_length(a);
  43. size_t b_length = strlen(b);
  44. uint8_t* a_buffer = alloca((a_length + 1) * sizeof(uint8_t));
  45. xml_string_copy(a, a_buffer, a_length);
  46. a_buffer[a_length] = 0;
  47. if (a_length != b_length) {
  48. fprintf(stderr, "string_equals: %s#%i <> %s#%i\n", a_buffer, (int)a_length, b, (int)b_length);
  49. return false;
  50. }
  51. size_t i = 0; for (; i < a_length; ++i) {
  52. if (a_buffer[i] != b[i]) {
  53. fprintf(stderr, "string_equals: %s <> %s\n", a_buffer, b);
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. /**
  60. * Converts a static character array to an uint8_t data source
  61. */
  62. #define SOURCE(source, content) \
  63. uint8_t* source = calloc(strlen(content) + 1, sizeof(uint8_t)); \
  64. { char const* content_string = content; \
  65. memcpy(source, content_string, strlen(content) + 1); \
  66. }
  67. /**
  68. * Tries to parse a simple document containing only one tag
  69. */
  70. static void test_xml_parse_document_0() {
  71. SOURCE(source, "<Hello>World</Hello>");
  72. // uint8_t* source = malloc((1 + strlen("<Hello>World</Hello>")) * sizeof(uint8_t));
  73. // { char const* content_string = "<Hello>World</Hello>";
  74. // memcpy(source, content_string, strlen("<Hello>World</Hello>") + 1);
  75. // }
  76. struct xml_document* document = xml_parse_document(source, strlen(source));
  77. assert_that(document, "Could not parse document");
  78. struct xml_node* root = xml_document_root(document);
  79. assert_that(string_equals(xml_node_name(root), "Hello"), "root node name must be `Hello'");
  80. assert_that(string_equals(xml_node_content(root), "World"), "root node content must be `World'");
  81. xml_document_free(document, true);
  82. }
  83. /**
  84. * Tries to parse a document containing multiple tags
  85. */
  86. static void test_xml_parse_document_1() {
  87. SOURCE(source, ""
  88. "<Parent>\n"
  89. "\t<Child>\n"
  90. "\t\tFirst content\n"
  91. "\t</Child>\n"
  92. "\t<Child>\n"
  93. "\t\tSecond content\n"
  94. "\t</Child>\n"
  95. "</Parent>\n"
  96. );
  97. struct xml_document* document = xml_parse_document(source, strlen(source));
  98. assert_that(document, "Could not parse document");
  99. struct xml_node* root = xml_document_root(document);
  100. assert_that(string_equals(xml_node_name(root), "Parent"), "root node name must be `Parent'");
  101. assert_that(2 == xml_node_children(root), "root must have two children");
  102. struct xml_node* first_child = xml_node_child(root, 0);
  103. struct xml_node* second_child = xml_node_child(root, 1);
  104. assert_that(first_child && second_child, "Failed retrieving the children of root");
  105. struct xml_node* third_child = xml_node_child(root, 2);
  106. assert_that(!third_child, "root has a third child where non should be");
  107. assert_that(string_equals(xml_node_name(first_child), "Child"), "first_child node name must be `Child'");
  108. assert_that(string_equals(xml_node_content(first_child), "First content"), "first_child node content must be `First content'");
  109. assert_that(string_equals(xml_node_name(second_child), "Child"), "second_child node name must be `Child'");
  110. assert_that(string_equals(xml_node_content(second_child), "Second content"), "second_child node content must be `tSecond content'");
  111. xml_document_free(document, true);
  112. }
  113. /**
  114. * Tests the eas functionality
  115. */
  116. static void test_xml_parse_document_2() {
  117. SOURCE(source, ""
  118. "<Parent>\n"
  119. "\t<Child>\n"
  120. "\t\tFirst content\n"
  121. "\t</Child>\n"
  122. "\t<This><Is>\n"
  123. "<A><Test>Content A</Test></A>\n"
  124. "<B><Test>Content B</Test></B>\n"
  125. "\t</Is></This>\n"
  126. "\t<Child>\n"
  127. "\t\tSecond content\n"
  128. "\t</Child>\n"
  129. "</Parent>\n"
  130. );
  131. struct xml_document* document = xml_parse_document(source, strlen(source));
  132. assert_that(document, "Could not parse document");
  133. struct xml_node* root = xml_document_root(document);
  134. assert_that(string_equals(xml_node_name(root), "Parent"), "root node name must be `Parent'");
  135. assert_that(3 == xml_node_children(root), "root must have two children");
  136. struct xml_node* test_a = xml_easy_child(root, "This", "Is", "A", "Test", 0);
  137. assert_that(test_a, "Cannot find Parent/This/Is/A/Test");
  138. assert_that(string_equals(xml_node_content(test_a), "Content A"), "Content of Parent/This/Is/A/Test must be `Content A'");
  139. struct xml_node* test_b = xml_easy_child(root, "This", "Is", "B", "Test", 0);
  140. assert_that(test_b, "Cannot find Parent/This/Is/B/Test");
  141. assert_that(string_equals(xml_node_content(test_b), "Content B"), "Content of Parent/This/Is/B/Test must be `Content B'");
  142. struct xml_node* test_c = xml_easy_child(root, "This", "Is", "C", "Test", 0);
  143. assert_that(!test_c, "Must not find Parent/This/Is/C/Test because no such path exists");
  144. struct xml_node* must_be_null = xml_easy_child(root, "Child");
  145. assert_that(!must_be_null, "Parent/Child cannot be a valid expression, because there are two children named `Child' in `Parent'");
  146. uint8_t* name_is = xml_easy_name(xml_easy_child(root, "This", "Is", 0));
  147. assert_that(!strcmp(name_is, "Is"), "Name of Parent/This/Is must be `Is'");
  148. free(name_is);
  149. uint8_t* content_a = xml_easy_content(test_a);
  150. assert_that(!strcmp(content_a, "Content A"), "Content of Parent/This/Is/A/Test must be `Content A'");
  151. free(content_a);
  152. xml_document_free(document, true);
  153. }
  154. /**
  155. * Console interface
  156. */
  157. int main(int argc, char** argv) {
  158. test_xml_parse_document_0();
  159. test_xml_parse_document_1();
  160. test_xml_parse_document_2();
  161. fprintf(stdout, "All tests passed :-)\n");
  162. exit(EXIT_SUCCESS);
  163. }