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 kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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