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