mxml.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Header file for Mini-XML, a small XML file parsing library.
  3. *
  4. * https://www.msweet.org/mxml
  5. *
  6. * Copyright © 2003-2019 by Michael R Sweet.
  7. *
  8. * Licensed under Apache License v2.0. See the file "LICENSE" for more
  9. * information.
  10. */
  11. /*
  12. * Prevent multiple inclusion...
  13. */
  14. #ifndef _mxml_h_
  15. # define _mxml_h_
  16. /*
  17. * Include necessary headers...
  18. */
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. # include <ctype.h>
  23. # include <errno.h>
  24. # include "ql_fs.h"
  25. /*
  26. * Constants...
  27. */
  28. # define MXML_MAJOR_VERSION 3 /* Major version number */
  29. # define MXML_MINOR_VERSION 1 /* Minor version number */
  30. # define MXML_TAB 8 /* Tabs every N columns */
  31. # define MXML_NO_CALLBACK 0 /* Don't use a type callback */
  32. # define MXML_INTEGER_CALLBACK mxml_integer_cb
  33. /* Treat all data as integers */
  34. # define MXML_OPAQUE_CALLBACK mxml_opaque_cb
  35. /* Treat all data as opaque */
  36. # define MXML_REAL_CALLBACK mxml_real_cb
  37. /* Treat all data as real numbers */
  38. # define MXML_TEXT_CALLBACK 0 /* Treat all data as text */
  39. # define MXML_IGNORE_CALLBACK mxml_ignore_cb
  40. /* Ignore all non-element content */
  41. # define MXML_NO_PARENT 0 /* No parent for the node */
  42. # define MXML_DESCEND 1 /* Descend when finding/walking */
  43. # define MXML_NO_DESCEND 0 /* Don't descend when finding/walking */
  44. # define MXML_DESCEND_FIRST -1 /* Descend for first find */
  45. # define MXML_WS_BEFORE_OPEN 0 /* Callback for before open tag */
  46. # define MXML_WS_AFTER_OPEN 1 /* Callback for after open tag */
  47. # define MXML_WS_BEFORE_CLOSE 2 /* Callback for before close tag */
  48. # define MXML_WS_AFTER_CLOSE 3 /* Callback for after close tag */
  49. # define MXML_ADD_BEFORE 0 /* Add node before specified node */
  50. # define MXML_ADD_AFTER 1 /* Add node after specified node */
  51. # define MXML_ADD_TO_PARENT NULL /* Add node relative to parent */
  52. /*
  53. * Data types...
  54. */
  55. typedef enum mxml_sax_event_e /**** SAX event type. ****/
  56. {
  57. MXML_SAX_CDATA, /* CDATA node */
  58. MXML_SAX_COMMENT, /* Comment node */
  59. MXML_SAX_DATA, /* Data node */
  60. MXML_SAX_DIRECTIVE, /* Processing directive node */
  61. MXML_SAX_ELEMENT_CLOSE, /* Element closed */
  62. MXML_SAX_ELEMENT_OPEN /* Element opened */
  63. } mxml_sax_event_t;
  64. typedef enum mxml_type_e /**** The XML node type. ****/
  65. {
  66. MXML_IGNORE = -1, /* Ignore/throw away node @since Mini-XML 2.3@ */
  67. MXML_ELEMENT, /* XML element with attributes */
  68. MXML_INTEGER, /* Integer value */
  69. MXML_OPAQUE, /* Opaque string */
  70. MXML_REAL, /* Real value */
  71. MXML_TEXT, /* Text fragment */
  72. MXML_CUSTOM /* Custom data @since Mini-XML 2.1@ */
  73. } mxml_type_t;
  74. typedef void (*mxml_custom_destroy_cb_t)(void *);
  75. /**** Custom data destructor ****/
  76. typedef void (*mxml_error_cb_t)(const char *);
  77. /**** Error callback function ****/
  78. typedef struct _mxml_node_s mxml_node_t; /**** An XML node. ****/
  79. typedef struct _mxml_index_s mxml_index_t;
  80. /**** An XML node index. ****/
  81. typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *);
  82. /**** Custom data load callback function ****/
  83. typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *);
  84. /**** Custom data save callback function ****/
  85. typedef int (*mxml_entity_cb_t)(const char *);
  86. /**** Entity callback function */
  87. typedef mxml_type_t (*mxml_load_cb_t)(mxml_node_t *);
  88. /**** Load callback function ****/
  89. typedef const char *(*mxml_save_cb_t)(mxml_node_t *, int);
  90. /**** Save callback function ****/
  91. typedef void (*mxml_sax_cb_t)(mxml_node_t *, mxml_sax_event_t, void *);
  92. /**** SAX callback function ****/
  93. /*
  94. * C++ support...
  95. */
  96. # ifdef __cplusplus
  97. extern "C" {
  98. # endif /* __cplusplus */
  99. /*
  100. * Prototypes...
  101. */
  102. extern void mxmlAdd(mxml_node_t *parent, int where,
  103. mxml_node_t *child, mxml_node_t *node);
  104. extern void mxmlDelete(mxml_node_t *node);
  105. extern void mxmlElementDeleteAttr(mxml_node_t *node,
  106. const char *name);
  107. extern const char *mxmlElementGetAttr(mxml_node_t *node, const char *name);
  108. extern const char *mxmlElementGetAttrByIndex(mxml_node_t *node, int idx, const char **name);
  109. extern int mxmlElementGetAttrCount(mxml_node_t *node);
  110. extern void mxmlElementSetAttr(mxml_node_t *node, const char *name,
  111. const char *value);
  112. extern void mxmlElementSetAttrf(mxml_node_t *node, const char *name,
  113. const char *format, ...)
  114. # ifdef __GNUC__
  115. __attribute__ ((__format__ (__printf__, 3, 4)))
  116. # endif /* __GNUC__ */
  117. ;
  118. extern int mxmlEntityAddCallback(mxml_entity_cb_t cb);
  119. extern const char *mxmlEntityGetName(int val);
  120. extern int mxmlEntityGetValue(const char *name);
  121. extern void mxmlEntityRemoveCallback(mxml_entity_cb_t cb);
  122. extern mxml_node_t *mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
  123. const char *element, const char *attr,
  124. const char *value, int descend);
  125. extern mxml_node_t *mxmlFindPath(mxml_node_t *node, const char *path);
  126. extern const char *mxmlGetCDATA(mxml_node_t *node);
  127. extern const void *mxmlGetCustom(mxml_node_t *node);
  128. extern const char *mxmlGetElement(mxml_node_t *node);
  129. extern mxml_node_t *mxmlGetFirstChild(mxml_node_t *node);
  130. extern int mxmlGetInteger(mxml_node_t *node);
  131. extern mxml_node_t *mxmlGetLastChild(mxml_node_t *node);
  132. extern mxml_node_t *mxmlGetNextSibling(mxml_node_t *node);
  133. extern const char *mxmlGetOpaque(mxml_node_t *node);
  134. extern mxml_node_t *mxmlGetParent(mxml_node_t *node);
  135. extern mxml_node_t *mxmlGetPrevSibling(mxml_node_t *node);
  136. extern double mxmlGetReal(mxml_node_t *node);
  137. extern int mxmlGetRefCount(mxml_node_t *node);
  138. extern const char *mxmlGetText(mxml_node_t *node, int *whitespace);
  139. extern mxml_type_t mxmlGetType(mxml_node_t *node);
  140. extern void *mxmlGetUserData(mxml_node_t *node);
  141. extern void mxmlIndexDelete(mxml_index_t *ind);
  142. extern mxml_node_t *mxmlIndexEnum(mxml_index_t *ind);
  143. extern mxml_node_t *mxmlIndexFind(mxml_index_t *ind,
  144. const char *element,
  145. const char *value);
  146. extern int mxmlIndexGetCount(mxml_index_t *ind);
  147. extern mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element,
  148. const char *attr);
  149. extern mxml_node_t *mxmlIndexReset(mxml_index_t *ind);
  150. extern mxml_node_t *mxmlLoadFd(mxml_node_t *top, int fd,
  151. mxml_type_t (*cb)(mxml_node_t *));
  152. extern mxml_node_t *mxmlLoadFile(mxml_node_t *top, QFILE *fp,
  153. mxml_type_t (*cb)(mxml_node_t *));
  154. extern mxml_node_t *mxmlLoadString(mxml_node_t *top, const char *s,
  155. mxml_type_t (*cb)(mxml_node_t *));
  156. extern mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string);
  157. extern mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data,
  158. mxml_custom_destroy_cb_t destroy);
  159. extern mxml_node_t *mxmlNewElement(mxml_node_t *parent, const char *name);
  160. extern mxml_node_t *mxmlNewInteger(mxml_node_t *parent, int integer);
  161. extern mxml_node_t *mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
  162. extern mxml_node_t *mxmlNewOpaquef(mxml_node_t *parent, const char *format, ...)
  163. # ifdef __GNUC__
  164. __attribute__ ((__format__ (__printf__, 2, 3)))
  165. # endif /* __GNUC__ */
  166. ;
  167. extern mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real);
  168. extern mxml_node_t *mxmlNewText(mxml_node_t *parent, int whitespace, const char *string);
  169. extern mxml_node_t *mxmlNewTextf(mxml_node_t *parent, int whitespace, const char *format, ...)
  170. # ifdef __GNUC__
  171. __attribute__ ((__format__ (__printf__, 3, 4)))
  172. # endif /* __GNUC__ */
  173. ;
  174. extern mxml_node_t *mxmlNewXML(const char *version);
  175. extern int mxmlRelease(mxml_node_t *node);
  176. extern void mxmlRemove(mxml_node_t *node);
  177. extern int mxmlRetain(mxml_node_t *node);
  178. extern char *mxmlSaveAllocString(mxml_node_t *node,
  179. mxml_save_cb_t cb);
  180. extern int mxmlSaveFd(mxml_node_t *node, int fd,
  181. mxml_save_cb_t cb);
  182. extern int mxmlSaveFile(mxml_node_t *node, QFILE *fp,
  183. mxml_save_cb_t cb);
  184. extern int mxmlSaveString(mxml_node_t *node, char *buffer,
  185. int bufsize, mxml_save_cb_t cb);
  186. extern mxml_node_t *mxmlSAXLoadFd(mxml_node_t *top, int fd,
  187. mxml_type_t (*cb)(mxml_node_t *),
  188. mxml_sax_cb_t sax, void *sax_data);
  189. extern mxml_node_t *mxmlSAXLoadFile(mxml_node_t *top, QFILE *fp,
  190. mxml_type_t (*cb)(mxml_node_t *),
  191. mxml_sax_cb_t sax, void *sax_data);
  192. extern mxml_node_t *mxmlSAXLoadString(mxml_node_t *top, const char *s,
  193. mxml_type_t (*cb)(mxml_node_t *),
  194. mxml_sax_cb_t sax, void *sax_data);
  195. extern int mxmlSetCDATA(mxml_node_t *node, const char *data);
  196. extern int mxmlSetCustom(mxml_node_t *node, void *data,
  197. mxml_custom_destroy_cb_t destroy);
  198. extern void mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
  199. mxml_custom_save_cb_t save);
  200. extern int mxmlSetElement(mxml_node_t *node, const char *name);
  201. extern void mxmlSetErrorCallback(mxml_error_cb_t cb);
  202. extern int mxmlSetInteger(mxml_node_t *node, int integer);
  203. extern int mxmlSetOpaque(mxml_node_t *node, const char *opaque);
  204. extern int mxmlSetOpaquef(mxml_node_t *node, const char *format, ...)
  205. # ifdef __GNUC__
  206. __attribute__ ((__format__ (__printf__, 2, 3)))
  207. # endif /* __GNUC__ */
  208. ;
  209. extern int mxmlSetReal(mxml_node_t *node, double real);
  210. extern int mxmlSetText(mxml_node_t *node, int whitespace,
  211. const char *string);
  212. extern int mxmlSetTextf(mxml_node_t *node, int whitespace,
  213. const char *format, ...)
  214. # ifdef __GNUC__
  215. __attribute__ ((__format__ (__printf__, 3, 4)))
  216. # endif /* __GNUC__ */
  217. ;
  218. extern int mxmlSetUserData(mxml_node_t *node, void *data);
  219. extern void mxmlSetWrapMargin(int column);
  220. extern mxml_node_t *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
  221. int descend);
  222. extern mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
  223. int descend);
  224. /*
  225. * Semi-private functions...
  226. */
  227. extern void mxml_error(const char *format, ...)
  228. # ifdef __GNUC__
  229. __attribute__ ((__format__ (__printf__, 1, 2)))
  230. # endif /* __GNUC__ */
  231. ;
  232. extern mxml_type_t mxml_ignore_cb(mxml_node_t *node);
  233. extern mxml_type_t mxml_integer_cb(mxml_node_t *node);
  234. extern mxml_type_t mxml_opaque_cb(mxml_node_t *node);
  235. extern mxml_type_t mxml_real_cb(mxml_node_t *node);
  236. /*
  237. * C++ support...
  238. */
  239. # ifdef __cplusplus
  240. }
  241. # endif /* __cplusplus */
  242. #endif /* !_mxml_h_ */