2 * gmakrup.c: Minimal XML markup reader.
4 * Unlike the GLib one, this can not be restarted with more text
5 * as the Mono use does not require it
8 * Miguel de Icaza (miguel@novell.com)
10 * (C) 2006 Novell, Inc.
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files (the
14 * "Software"), to deal in the Software without restriction, including
15 * without limitation the rights to use, copy, modify, merge, publish,
16 * distribute, sublicense, and/or sell copies of the Software, and to
17 * permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #define set_error(msg...) do { if (error != NULL) *error = g_error_new (1, 1, msg); } while (0);
40 struct _GMarkupParseContext {
43 GDestroyNotify user_data_dnotify;
48 g_markup_parse_context_new (const GMarkupParser *parser,
49 GMarkupParseFlags flags,
51 GDestroyNotify user_data_dnotify)
53 GMarkupParseContext *context = g_new0 (GMarkupParseContext, 1);
55 context->parser = *parser;
56 context->user_data = user_data;
57 context->user_data_dnotify = user_data_dnotify;
63 g_markup_parse_context_free (GMarkupParseContext *context)
69 g_markup_parse_context_parse (GMarkupParseContext *context,
70 const gchar *text, gssize text_len, GError **error)
74 g_return_val_if_fail (context != NULL, FALSE);
75 g_return_val_if_fail (text != NULL, FALSE);
76 g_return_val_if_fail (text_len >= 0, FALSE);
78 end = text + text_len;
80 for (p = text; p < end; p++){
83 switch (context->state){
85 if (c == ' ' || c == '\t' || c == '\f' || c == '\n')
88 context->state = START_ELEMENT;
91 set_error ("Expected < to start the document");
97 char *element_start = p;
98 char **names, *values;
100 if (!(isascii (*p) && isalpha (*p)))
101 set_error ("Must start with a letter");
103 for (++p; p < end && isalnum (*p); p++)
106 set_error ("Expected an element");
109 for (; p < end && isspace (*p); p++)
112 set_error ("Unfinished element");
115 p = parse_attributes (p, end, &names, &values);
117 set_error ("unfinished element");