gmarkup.h 11 KB

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