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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

197 rader
4.4 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. #ifndef HEADER_XML
  24. #define HEADER_XML
  25. /**
  26. * Includes
  27. */
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * Opaque structure holding the parsed xml document
  37. */
  38. struct xml_document;
  39. struct xml_node;
  40. struct xml_attribute;
  41. /**
  42. * Internal character sequence representation
  43. */
  44. struct xml_string;
  45. /**
  46. * Tries to parse the XML fragment in buffer
  47. *
  48. * @param buffer Chunk to parse
  49. * @param length Size of the buffer
  50. *
  51. * @warning `buffer` will be referenced by the document, you may not free it
  52. * until you free the xml_document
  53. * @warning You have to call xml_document_free after you finished using the
  54. * document
  55. *
  56. * @return The parsed xml fragment iff parsing was successful, 0 otherwise
  57. */
  58. struct xml_document* xml_parse_document(uint8_t* buffer, size_t length);
  59. /**
  60. * Tries to read an XML document from disk
  61. *
  62. * @param source File that will be read into an xml document. Will be closed
  63. *
  64. * @warning You have to call xml_document_free with free_buffer = true after you
  65. * finished using the document
  66. *
  67. * @return The parsed xml fragment iff parsing was successful, 0 otherwise
  68. */
  69. struct xml_document* xml_open_document(FILE* source);
  70. /**
  71. * Frees all resources associated with the document. All xml_node and xml_string
  72. * references obtained through the document will be invalidated
  73. *
  74. * @param document xml_document to free
  75. * @param free_buffer iff true the internal buffer supplied via xml_parse_buffer
  76. * will be freed with the `free` system call
  77. */
  78. void xml_document_free(struct xml_document* document, bool free_buffer);
  79. /**
  80. * @return xml_node representing the document root
  81. */
  82. struct xml_node* xml_document_root(struct xml_document* document);
  83. /**
  84. * @return The xml_node's tag name
  85. */
  86. struct xml_string* xml_node_name(struct xml_node* node);
  87. /**
  88. * @return The xml_node's string content (if available, otherwise NULL)
  89. */
  90. struct xml_string* xml_node_content(struct xml_node* node);
  91. /**
  92. * @return Number of child nodes
  93. */
  94. size_t xml_node_children(struct xml_node* node);
  95. /**
  96. * @return The n-th child or 0 if out of range
  97. */
  98. struct xml_node* xml_node_child(struct xml_node* node, size_t child);
  99. /**
  100. * @return Number of attribute nodes
  101. */
  102. size_t xml_node_attributes(struct xml_node* node);
  103. /**
  104. * @return the n-th attribute name or 0 if out of range
  105. */
  106. struct xml_string* xml_node_attribute_name(struct xml_node* node, size_t attribute);
  107. /**
  108. * @return the n-th attribute content or 0 if out of range
  109. */
  110. struct xml_string* xml_node_attribute_content(struct xml_node* node, size_t attribute);
  111. /**
  112. * @return The node described by the path or 0 if child cannot be found
  113. * @warning Each element on the way must be unique
  114. * @warning Last argument must be 0
  115. */
  116. struct xml_node* xml_easy_child(struct xml_node* node, uint8_t const* child, ...);
  117. /**
  118. * @return 0-terminated copy of node name
  119. * @warning User must free the result
  120. */
  121. uint8_t* xml_easy_name(struct xml_node* node);
  122. /**
  123. * @return 0-terminated copy of node content
  124. * @warning User must free the result
  125. */
  126. uint8_t* xml_easy_content(struct xml_node* node);
  127. /**
  128. * @return Length of the string
  129. */
  130. size_t xml_string_length(struct xml_string* string);
  131. /**
  132. * Copies the string into the supplied buffer
  133. *
  134. * @warning String will not be 0-terminated
  135. * @warning Will write at most length bytes, even if the string is longer
  136. */
  137. void xml_string_copy(struct xml_string* string, uint8_t* buffer, size_t length);
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif