gmarkup.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* gmarkup.h - Simple XML-like string parser/writer
  2. *
  3. * Copyright 2000 Red Hat, Inc.
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef __G_MARKUP_H__
  21. #define __G_MARKUP_H__
  22. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  23. #error "Only <glib.h> can be included directly."
  24. #endif
  25. #include <stdarg.h>
  26. #include <glib/gerror.h>
  27. #include <glib/gslist.h>
  28. G_BEGIN_DECLS
  29. /**
  30. * GMarkupError:
  31. * @G_MARKUP_ERROR_BAD_UTF8: text being parsed was not valid UTF-8
  32. * @G_MARKUP_ERROR_EMPTY: document contained nothing, or only whitespace
  33. * @G_MARKUP_ERROR_PARSE: document was ill-formed
  34. * @G_MARKUP_ERROR_UNKNOWN_ELEMENT: error should be set by #GMarkupParser
  35. * functions; element wasn't known
  36. * @G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE: error should be set by #GMarkupParser
  37. * functions; attribute wasn't known
  38. * @G_MARKUP_ERROR_INVALID_CONTENT: error should be set by #GMarkupParser
  39. * functions; content was invalid
  40. * @G_MARKUP_ERROR_MISSING_ATTRIBUTE: error should be set by #GMarkupParser
  41. * functions; a required attribute was missing
  42. *
  43. * Error codes returned by markup parsing.
  44. */
  45. typedef enum
  46. {
  47. G_MARKUP_ERROR_BAD_UTF8,
  48. G_MARKUP_ERROR_EMPTY,
  49. G_MARKUP_ERROR_PARSE,
  50. /* The following are primarily intended for specific GMarkupParser
  51. * implementations to set.
  52. */
  53. G_MARKUP_ERROR_UNKNOWN_ELEMENT,
  54. G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
  55. G_MARKUP_ERROR_INVALID_CONTENT,
  56. G_MARKUP_ERROR_MISSING_ATTRIBUTE
  57. } GMarkupError;
  58. /**
  59. * G_MARKUP_ERROR:
  60. *
  61. * Error domain for markup parsing.
  62. * Errors in this domain will be from the #GMarkupError enumeration.
  63. * See #GError for information on error domains.
  64. */
  65. #define G_MARKUP_ERROR g_markup_error_quark ()
  66. GLIB_AVAILABLE_IN_ALL
  67. GQuark g_markup_error_quark (void);
  68. /**
  69. * GMarkupParseFlags:
  70. * @G_MARKUP_DEFAULT_FLAGS: No special behaviour. Since: 2.74
  71. * @G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG: flag you should not use
  72. * @G_MARKUP_TREAT_CDATA_AS_TEXT: When this flag is set, CDATA marked
  73. * sections are not passed literally to the @passthrough function of
  74. * the parser. Instead, the content of the section (without the
  75. * `<![CDATA[` and `]]>`) is
  76. * passed to the @text function. This flag was added in GLib 2.12
  77. * @G_MARKUP_PREFIX_ERROR_POSITION: Normally errors caught by GMarkup
  78. * itself have line/column information prefixed to them to let the
  79. * caller know the location of the error. When this flag is set the
  80. * location information is also prefixed to errors generated by the
  81. * #GMarkupParser implementation functions
  82. * @G_MARKUP_IGNORE_QUALIFIED: Ignore (don't report) qualified
  83. * attributes and tags, along with their contents. A qualified
  84. * attribute or tag is one that contains ':' in its name (ie: is in
  85. * another namespace). Since: 2.40.
  86. *
  87. * Flags that affect the behaviour of the parser.
  88. */
  89. typedef enum
  90. {
  91. G_MARKUP_DEFAULT_FLAGS GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 0,
  92. G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
  93. G_MARKUP_TREAT_CDATA_AS_TEXT = 1 << 1,
  94. G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2,
  95. G_MARKUP_IGNORE_QUALIFIED = 1 << 3
  96. } GMarkupParseFlags;
  97. /**
  98. * GMarkupParseContext:
  99. *
  100. * A parse context is used to parse a stream of bytes that
  101. * you expect to contain marked-up text.
  102. *
  103. * See g_markup_parse_context_new(), #GMarkupParser, and so
  104. * on for more details.
  105. */
  106. typedef struct _GMarkupParseContext GMarkupParseContext;
  107. typedef struct _GMarkupParser GMarkupParser;
  108. /**
  109. * GMarkupParser:
  110. * @start_element: Callback to invoke when the opening tag of an element
  111. * is seen. The callback's @attribute_names and @attribute_values parameters
  112. * are %NULL-terminated.
  113. * @end_element: Callback to invoke when the closing tag of an element
  114. * is seen. Note that this is also called for empty tags like
  115. * `<empty/>`.
  116. * @text: Callback to invoke when some text is seen (text is always
  117. * inside an element). Note that the text of an element may be spread
  118. * over multiple calls of this function. If the
  119. * %G_MARKUP_TREAT_CDATA_AS_TEXT flag is set, this function is also
  120. * called for the content of CDATA marked sections.
  121. * @passthrough: Callback to invoke for comments, processing instructions
  122. * and doctype declarations; if you're re-writing the parsed document,
  123. * write the passthrough text back out in the same position. If the
  124. * %G_MARKUP_TREAT_CDATA_AS_TEXT flag is not set, this function is also
  125. * called for CDATA marked sections.
  126. * @error: Callback to invoke when an error occurs.
  127. *
  128. * Any of the fields in #GMarkupParser can be %NULL, in which case they
  129. * will be ignored. Except for the @error function, any of these callbacks
  130. * can set an error; in particular the %G_MARKUP_ERROR_UNKNOWN_ELEMENT,
  131. * %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, and %G_MARKUP_ERROR_INVALID_CONTENT
  132. * errors are intended to be set from these callbacks. If you set an error
  133. * from a callback, g_markup_parse_context_parse() will report that error
  134. * back to its caller.
  135. */
  136. struct _GMarkupParser
  137. {
  138. /* Called for open tags <foo bar="baz"> */
  139. void (*start_element) (GMarkupParseContext *context,
  140. const gchar *element_name,
  141. const gchar **attribute_names,
  142. const gchar **attribute_values,
  143. gpointer user_data,
  144. GError **error);
  145. /* Called for close tags </foo> */
  146. void (*end_element) (GMarkupParseContext *context,
  147. const gchar *element_name,
  148. gpointer user_data,
  149. GError **error);
  150. /* Called for character data */
  151. /* text is not nul-terminated */
  152. void (*text) (GMarkupParseContext *context,
  153. const gchar *text,
  154. gsize text_len,
  155. gpointer user_data,
  156. GError **error);
  157. /* Called for strings that should be re-saved verbatim in this same
  158. * position, but are not otherwise interpretable. At the moment
  159. * this includes comments and processing instructions.
  160. */
  161. /* text is not nul-terminated. */
  162. void (*passthrough) (GMarkupParseContext *context,
  163. const gchar *passthrough_text,
  164. gsize text_len,
  165. gpointer user_data,
  166. GError **error);
  167. /* Called on error, including one set by other
  168. * methods in the vtable. The GError should not be freed.
  169. */
  170. void (*error) (GMarkupParseContext *context,
  171. GError *error,
  172. gpointer user_data);
  173. };
  174. GLIB_AVAILABLE_IN_ALL
  175. GMarkupParseContext *g_markup_parse_context_new (const GMarkupParser *parser,
  176. GMarkupParseFlags flags,
  177. gpointer user_data,
  178. GDestroyNotify user_data_dnotify);
  179. GLIB_AVAILABLE_IN_2_36
  180. GMarkupParseContext *g_markup_parse_context_ref (GMarkupParseContext *context);
  181. GLIB_AVAILABLE_IN_2_36
  182. void g_markup_parse_context_unref (GMarkupParseContext *context);
  183. GLIB_AVAILABLE_IN_ALL
  184. void g_markup_parse_context_free (GMarkupParseContext *context);
  185. GLIB_AVAILABLE_IN_ALL
  186. gboolean g_markup_parse_context_parse (GMarkupParseContext *context,
  187. const gchar *text,
  188. gssize text_len,
  189. GError **error);
  190. GLIB_AVAILABLE_IN_ALL
  191. void g_markup_parse_context_push (GMarkupParseContext *context,
  192. const GMarkupParser *parser,
  193. gpointer user_data);
  194. GLIB_AVAILABLE_IN_ALL
  195. gpointer g_markup_parse_context_pop (GMarkupParseContext *context);
  196. GLIB_AVAILABLE_IN_ALL
  197. gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context,
  198. GError **error);
  199. GLIB_AVAILABLE_IN_ALL
  200. const gchar * g_markup_parse_context_get_element (GMarkupParseContext *context);
  201. GLIB_AVAILABLE_IN_ALL
  202. const GSList * g_markup_parse_context_get_element_stack (GMarkupParseContext *context);
  203. /* For user-constructed error messages, has no precise semantics */
  204. GLIB_AVAILABLE_IN_ALL
  205. void g_markup_parse_context_get_position (GMarkupParseContext *context,
  206. gint *line_number,
  207. gint *char_number);
  208. GLIB_AVAILABLE_IN_ALL
  209. gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context);
  210. /* useful when saving */
  211. GLIB_AVAILABLE_IN_ALL
  212. gchar* g_markup_escape_text (const gchar *text,
  213. gssize length);
  214. GLIB_AVAILABLE_IN_ALL
  215. gchar *g_markup_printf_escaped (const char *format,
  216. ...) G_GNUC_PRINTF (1, 2);
  217. GLIB_AVAILABLE_IN_ALL
  218. gchar *g_markup_vprintf_escaped (const char *format,
  219. va_list args) G_GNUC_PRINTF(1, 0);
  220. typedef enum
  221. {
  222. G_MARKUP_COLLECT_INVALID,
  223. G_MARKUP_COLLECT_STRING,
  224. G_MARKUP_COLLECT_STRDUP,
  225. G_MARKUP_COLLECT_BOOLEAN,
  226. G_MARKUP_COLLECT_TRISTATE,
  227. G_MARKUP_COLLECT_OPTIONAL = (1 << 16)
  228. } GMarkupCollectType;
  229. /* useful from start_element */
  230. GLIB_AVAILABLE_IN_ALL
  231. gboolean g_markup_collect_attributes (const gchar *element_name,
  232. const gchar **attribute_names,
  233. const gchar **attribute_values,
  234. GError **error,
  235. GMarkupCollectType first_type,
  236. const gchar *first_attr,
  237. ...);
  238. G_END_DECLS
  239. #endif /* __G_MARKUP_H__ */