1 /* gmarkup.c - Simple XML-like parser
3 * Copyright 2000, 2003 Red Hat, Inc.
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.
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.
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.
34 g_markup_error_quark (void)
36 return g_quark_from_static_string ("g-markup-error-quark");
42 STATE_AFTER_OPEN_ANGLE,
43 STATE_AFTER_CLOSE_ANGLE,
44 STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
45 STATE_INSIDE_OPEN_TAG_NAME,
46 STATE_INSIDE_ATTRIBUTE_NAME,
47 STATE_AFTER_ATTRIBUTE_NAME,
48 STATE_BETWEEN_ATTRIBUTES,
49 STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
50 STATE_INSIDE_ATTRIBUTE_VALUE_SQ,
51 STATE_INSIDE_ATTRIBUTE_VALUE_DQ,
53 STATE_AFTER_CLOSE_TAG_SLASH,
54 STATE_INSIDE_CLOSE_TAG_NAME,
55 STATE_AFTER_CLOSE_TAG_NAME,
56 STATE_INSIDE_PASSTHROUGH,
60 struct _GMarkupParseContext
62 const GMarkupParser *parser;
64 GMarkupParseFlags flags;
70 GDestroyNotify dnotify;
72 /* A piece of character data or an element that
73 * hasn't "ended" yet so we haven't yet called
74 * the callback for it.
76 GString *partial_chunk;
78 GMarkupParseState state;
85 const gchar *current_text;
86 gssize current_text_len;
87 const gchar *current_text_end;
89 GString *leftover_char_portion;
91 /* used to save the start of the last interesting thingy */
96 guint document_empty : 1;
102 * g_markup_parse_context_new:
103 * @parser: a #GMarkupParser
104 * @flags: one or more #GMarkupParseFlags
105 * @user_data: user data to pass to #GMarkupParser functions
106 * @user_data_dnotify: user data destroy notifier called when the parse context is freed
108 * Creates a new parse context. A parse context is used to parse
109 * marked-up documents. You can feed any number of documents into
110 * a context, as long as no errors occur; once an error occurs,
111 * the parse context can't continue to parse text (you have to free it
112 * and create a new parse context).
114 * Return value: a new #GMarkupParseContext
116 GMarkupParseContext *
117 g_markup_parse_context_new (const GMarkupParser *parser,
118 GMarkupParseFlags flags,
120 GDestroyNotify user_data_dnotify)
122 GMarkupParseContext *context;
124 g_return_val_if_fail (parser != NULL, NULL);
126 context = g_new (GMarkupParseContext, 1);
128 context->parser = parser;
129 context->flags = flags;
130 context->user_data = user_data;
131 context->dnotify = user_data_dnotify;
133 context->line_number = 1;
134 context->char_number = 1;
136 context->partial_chunk = NULL;
138 context->state = STATE_START;
139 context->tag_stack = NULL;
140 context->attr_names = NULL;
141 context->attr_values = NULL;
142 context->cur_attr = -1;
143 context->alloc_attrs = 0;
145 context->current_text = NULL;
146 context->current_text_len = -1;
147 context->current_text_end = NULL;
148 context->leftover_char_portion = NULL;
150 context->start = NULL;
151 context->iter = NULL;
153 context->document_empty = TRUE;
154 context->parsing = FALSE;
156 context->balance = 0;
162 * g_markup_parse_context_free:
163 * @context: a #GMarkupParseContext
165 * Frees a #GMarkupParseContext. Can't be called from inside
166 * one of the #GMarkupParser functions.
170 g_markup_parse_context_free (GMarkupParseContext *context)
172 g_return_if_fail (context != NULL);
173 g_return_if_fail (!context->parsing);
175 if (context->dnotify)
176 (* context->dnotify) (context->user_data);
178 g_strfreev (context->attr_names);
179 g_strfreev (context->attr_values);
181 g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
182 g_slist_free (context->tag_stack);
184 if (context->partial_chunk)
185 g_string_free (context->partial_chunk, TRUE);
187 if (context->leftover_char_portion)
188 g_string_free (context->leftover_char_portion, TRUE);
194 mark_error (GMarkupParseContext *context,
197 context->state = STATE_ERROR;
199 if (context->parser->error)
200 (*context->parser->error) (context, error, context->user_data);
203 static void set_error (GMarkupParseContext *context,
207 ...) G_GNUC_PRINTF (4, 5);
210 set_error (GMarkupParseContext *context,
221 va_start (args, format);
222 s = g_strdup_vprintf (format, args);
225 /* Make sure that the GError message is valid UTF-8 even if it is
226 * complaining about invalid UTF-8 in the markup: */
227 s_valid = _g_utf8_make_valid (s);
228 tmp_error = g_error_new_literal (G_MARKUP_ERROR, code, s_valid);
233 g_prefix_error (&tmp_error,
234 _("Error on line %d char %d: "),
235 context->line_number,
236 context->char_number);
238 mark_error (context, tmp_error);
240 g_propagate_error (error, tmp_error);
244 propagate_error (GMarkupParseContext *context,
248 if (context->flags & G_MARKUP_PREFIX_ERROR_POSITION)
249 g_prefix_error (&src,
250 _("Error on line %d char %d: "),
251 context->line_number,
252 context->char_number);
254 mark_error (context, src);
256 g_propagate_error (dest, src);
259 /* To make these faster, we first use the ascii-only tests, then check
260 * for the usual non-alnum name-end chars, and only then call the
261 * expensive unicode stuff. Nobody uses non-ascii in XML tag/attribute
262 * names, so this is a reasonable hack that virtually always avoids
265 #define IS_COMMON_NAME_END_CHAR(c) \
266 ((c) == '=' || (c) == '/' || (c) == '>' || (c) == ' ')
269 is_name_start_char (const gchar *p)
271 if (g_ascii_isalpha (*p) ||
272 (!IS_COMMON_NAME_END_CHAR (*p) &&
275 g_unichar_isalpha (g_utf8_get_char (p)))))
282 is_name_char (const gchar *p)
284 if (g_ascii_isalnum (*p) ||
285 (!IS_COMMON_NAME_END_CHAR (*p) &&
290 g_unichar_isalpha (g_utf8_get_char (p)))))
298 char_str (gunichar c,
302 g_unichar_to_utf8 (c, buf);
307 utf8_str (const gchar *utf8,
310 char_str (g_utf8_get_char (utf8), buf);
315 set_unescape_error (GMarkupParseContext *context,
317 const gchar *remaining_text,
318 const gchar *remaining_text_end,
326 gint remaining_newlines;
329 remaining_newlines = 0;
331 while (p != remaining_text_end)
334 ++remaining_newlines;
338 va_start (args, format);
339 s = g_strdup_vprintf (format, args);
342 tmp_error = g_error_new (G_MARKUP_ERROR,
344 _("Error on line %d: %s"),
345 context->line_number - remaining_newlines,
350 mark_error (context, tmp_error);
352 g_propagate_error (error, tmp_error);
358 USTATE_AFTER_AMPERSAND,
359 USTATE_INSIDE_ENTITY_NAME,
360 USTATE_AFTER_CHARREF_HASH
365 GMarkupParseContext *context;
369 const gchar *text_end;
370 const gchar *entity_start;
374 unescape_text_state_inside_text (UnescapeContext *ucontext,
379 gboolean normalize_attribute;
381 if (ucontext->context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ ||
382 ucontext->context->state == STATE_INSIDE_ATTRIBUTE_VALUE_DQ)
383 normalize_attribute = TRUE;
385 normalize_attribute = FALSE;
389 while (p != ucontext->text_end)
395 else if (normalize_attribute && (*p == '\t' || *p == '\n'))
397 g_string_append_len (ucontext->str, start, p - start);
398 g_string_append_c (ucontext->str, ' ');
399 p = g_utf8_next_char (p);
404 g_string_append_len (ucontext->str, start, p - start);
405 g_string_append_c (ucontext->str, normalize_attribute ? ' ' : '\n');
406 p = g_utf8_next_char (p);
407 if (p != ucontext->text_end && *p == '\n')
408 p = g_utf8_next_char (p);
412 p = g_utf8_next_char (p);
416 g_string_append_len (ucontext->str, start, p - start);
418 if (p != ucontext->text_end && *p == '&')
420 p = g_utf8_next_char (p);
421 ucontext->state = USTATE_AFTER_AMPERSAND;
428 unescape_text_state_after_ampersand (UnescapeContext *ucontext,
432 ucontext->entity_start = NULL;
436 p = g_utf8_next_char (p);
438 ucontext->entity_start = p;
439 ucontext->state = USTATE_AFTER_CHARREF_HASH;
441 else if (!is_name_start_char (p))
445 set_unescape_error (ucontext->context, error,
446 p, ucontext->text_end,
447 G_MARKUP_ERROR_PARSE,
448 _("Empty entity '&;' seen; valid "
449 "entities are: & " < > '"));
455 set_unescape_error (ucontext->context, error,
456 p, ucontext->text_end,
457 G_MARKUP_ERROR_PARSE,
458 _("Character '%s' is not valid at "
459 "the start of an entity name; "
460 "the & character begins an entity; "
461 "if this ampersand isn't supposed "
462 "to be an entity, escape it as "
469 ucontext->entity_start = p;
470 ucontext->state = USTATE_INSIDE_ENTITY_NAME;
477 unescape_text_state_inside_entity_name (UnescapeContext *ucontext,
481 while (p != ucontext->text_end)
485 else if (!is_name_char (p))
489 set_unescape_error (ucontext->context, error,
490 p, ucontext->text_end,
491 G_MARKUP_ERROR_PARSE,
492 _("Character '%s' is not valid "
493 "inside an entity name"),
498 p = g_utf8_next_char (p);
501 if (ucontext->context->state != STATE_ERROR)
503 if (p != ucontext->text_end)
505 gint len = p - ucontext->entity_start;
507 /* move to after semicolon */
508 p = g_utf8_next_char (p);
509 ucontext->state = USTATE_INSIDE_TEXT;
511 if (strncmp (ucontext->entity_start, "lt", len) == 0)
512 g_string_append_c (ucontext->str, '<');
513 else if (strncmp (ucontext->entity_start, "gt", len) == 0)
514 g_string_append_c (ucontext->str, '>');
515 else if (strncmp (ucontext->entity_start, "amp", len) == 0)
516 g_string_append_c (ucontext->str, '&');
517 else if (strncmp (ucontext->entity_start, "quot", len) == 0)
518 g_string_append_c (ucontext->str, '"');
519 else if (strncmp (ucontext->entity_start, "apos", len) == 0)
520 g_string_append_c (ucontext->str, '\'');
525 name = g_strndup (ucontext->entity_start, len);
526 set_unescape_error (ucontext->context, error,
527 p, ucontext->text_end,
528 G_MARKUP_ERROR_PARSE,
529 _("Entity name '%s' is not known"),
536 set_unescape_error (ucontext->context, error,
537 /* give line number of the & */
538 ucontext->entity_start, ucontext->text_end,
539 G_MARKUP_ERROR_PARSE,
540 _("Entity did not end with a semicolon; "
541 "most likely you used an ampersand "
542 "character without intending to start "
543 "an entity - escape ampersand as &"));
552 unescape_text_state_after_charref_hash (UnescapeContext *ucontext,
556 gboolean is_hex = FALSE;
559 start = ucontext->entity_start;
564 p = g_utf8_next_char (p);
568 while (p != ucontext->text_end && *p != ';')
569 p = g_utf8_next_char (p);
571 if (p != ucontext->text_end)
573 g_assert (*p == ';');
575 /* digit is between start and p */
584 l = strtoul (start, &end, 16);
586 l = strtoul (start, &end, 10);
588 if (end != p || errno != 0)
590 set_unescape_error (ucontext->context, error,
591 start, ucontext->text_end,
592 G_MARKUP_ERROR_PARSE,
593 _("Failed to parse '%-.*s', which "
594 "should have been a digit "
595 "inside a character reference "
596 "(ê for example) - perhaps "
597 "the digit is too large"),
602 /* characters XML permits */
606 (l >= 0x20 && l <= 0xD7FF) ||
607 (l >= 0xE000 && l <= 0xFFFD) ||
608 (l >= 0x10000 && l <= 0x10FFFF))
611 g_string_append (ucontext->str, char_str (l, buf));
615 set_unescape_error (ucontext->context, error,
616 start, ucontext->text_end,
617 G_MARKUP_ERROR_PARSE,
618 _("Character reference '%-.*s' does not "
619 "encode a permitted character"),
624 /* Move to next state */
625 p = g_utf8_next_char (p); /* past semicolon */
626 ucontext->state = USTATE_INSIDE_TEXT;
630 set_unescape_error (ucontext->context, error,
631 start, ucontext->text_end,
632 G_MARKUP_ERROR_PARSE,
633 _("Empty character reference; "
634 "should include a digit such as "
640 set_unescape_error (ucontext->context, error,
641 start, ucontext->text_end,
642 G_MARKUP_ERROR_PARSE,
643 _("Character reference did not end with a "
645 "most likely you used an ampersand "
646 "character without intending to start "
647 "an entity - escape ampersand as &"));
654 unescape_text (GMarkupParseContext *context,
656 const gchar *text_end,
660 UnescapeContext ucontext;
663 ucontext.context = context;
664 ucontext.text = text;
665 ucontext.text_end = text_end;
666 ucontext.entity_start = NULL;
668 ucontext.str = g_string_sized_new (text_end - text);
670 ucontext.state = USTATE_INSIDE_TEXT;
673 while (p != text_end && context->state != STATE_ERROR)
675 g_assert (p < text_end);
677 switch (ucontext.state)
679 case USTATE_INSIDE_TEXT:
681 p = unescape_text_state_inside_text (&ucontext,
687 case USTATE_AFTER_AMPERSAND:
689 p = unescape_text_state_after_ampersand (&ucontext,
696 case USTATE_INSIDE_ENTITY_NAME:
698 p = unescape_text_state_inside_entity_name (&ucontext,
704 case USTATE_AFTER_CHARREF_HASH:
706 p = unescape_text_state_after_charref_hash (&ucontext,
713 g_assert_not_reached ();
718 if (context->state != STATE_ERROR)
720 switch (ucontext.state)
722 case USTATE_INSIDE_TEXT:
724 case USTATE_AFTER_AMPERSAND:
725 case USTATE_INSIDE_ENTITY_NAME:
726 set_unescape_error (context, error,
728 G_MARKUP_ERROR_PARSE,
729 _("Unfinished entity reference"));
731 case USTATE_AFTER_CHARREF_HASH:
732 set_unescape_error (context, error,
734 G_MARKUP_ERROR_PARSE,
735 _("Unfinished character reference"));
740 if (context->state == STATE_ERROR)
742 g_string_free (ucontext.str, TRUE);
748 *unescaped = ucontext.str;
753 static inline gboolean
754 advance_char (GMarkupParseContext *context)
756 context->iter = g_utf8_next_char (context->iter);
757 context->char_number += 1;
759 if (context->iter == context->current_text_end)
763 else if (*context->iter == '\n')
765 context->line_number += 1;
766 context->char_number = 1;
772 static inline gboolean
775 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
779 skip_spaces (GMarkupParseContext *context)
783 if (!xml_isspace (*context->iter))
786 while (advance_char (context));
790 advance_to_name_end (GMarkupParseContext *context)
794 if (!is_name_char (context->iter))
797 while (advance_char (context));
801 add_to_partial (GMarkupParseContext *context,
802 const gchar *text_start,
803 const gchar *text_end)
805 if (context->partial_chunk == NULL)
806 context->partial_chunk = g_string_sized_new (text_end - text_start);
808 if (text_start != text_end)
809 g_string_append_len (context->partial_chunk, text_start,
810 text_end - text_start);
812 /* Invariant here that partial_chunk exists */
816 truncate_partial (GMarkupParseContext *context)
818 if (context->partial_chunk != NULL)
820 context->partial_chunk = g_string_truncate (context->partial_chunk, 0);
825 current_element (GMarkupParseContext *context)
827 return context->tag_stack->data;
831 current_attribute (GMarkupParseContext *context)
833 g_assert (context->cur_attr >= 0);
834 return context->attr_names[context->cur_attr];
838 find_current_text_end (GMarkupParseContext *context)
840 /* This function must be safe (non-segfaulting) on invalid UTF8.
841 * It assumes the string starts with a character start
843 const gchar *end = context->current_text + context->current_text_len;
847 g_assert (context->current_text_len > 0);
849 p = g_utf8_find_prev_char (context->current_text, end);
851 g_assert (p != NULL); /* since current_text was a char start */
853 /* p is now the start of the last character or character portion. */
855 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
859 /* whole character */
860 context->current_text_end = end;
865 context->leftover_char_portion = g_string_new_len (p, end - p);
866 context->current_text_len -= (end - p);
867 context->current_text_end = p;
873 add_attribute (GMarkupParseContext *context, char *name)
875 if (context->cur_attr + 2 >= context->alloc_attrs)
877 context->alloc_attrs += 5; /* silly magic number */
878 context->attr_names = g_realloc (context->attr_names, sizeof(char*)*context->alloc_attrs);
879 context->attr_values = g_realloc (context->attr_values, sizeof(char*)*context->alloc_attrs);
882 context->attr_names[context->cur_attr] = name;
883 context->attr_values[context->cur_attr] = NULL;
884 context->attr_names[context->cur_attr+1] = NULL;
885 context->attr_values[context->cur_attr+1] = NULL;
889 * g_markup_parse_context_parse:
890 * @context: a #GMarkupParseContext
891 * @text: chunk of text to parse
892 * @text_len: length of @text in bytes
893 * @error: return location for a #GError
895 * Feed some data to the #GMarkupParseContext. The data need not
896 * be valid UTF-8; an error will be signaled if it's invalid.
897 * The data need not be an entire document; you can feed a document
898 * into the parser incrementally, via multiple calls to this function.
899 * Typically, as you receive data from a network connection or file,
900 * you feed each received chunk of data into this function, aborting
901 * the process if an error occurs. Once an error is reported, no further
902 * data may be fed to the #GMarkupParseContext; all errors are fatal.
904 * Return value: %FALSE if an error occurred, %TRUE on success
907 g_markup_parse_context_parse (GMarkupParseContext *context,
912 const gchar *first_invalid;
914 g_return_val_if_fail (context != NULL, FALSE);
915 g_return_val_if_fail (text != NULL, FALSE);
916 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
917 g_return_val_if_fail (!context->parsing, FALSE);
920 text_len = strlen (text);
925 context->parsing = TRUE;
927 if (context->leftover_char_portion)
929 const gchar *first_char;
931 if ((*text & 0xc0) != 0x80)
934 first_char = g_utf8_find_next_char (text, text + text_len);
938 /* leftover_char_portion was completed. Parse it. */
939 GString *portion = context->leftover_char_portion;
941 g_string_append_len (context->leftover_char_portion,
942 text, first_char - text);
944 /* hacks to allow recursion */
945 context->parsing = FALSE;
946 context->leftover_char_portion = NULL;
948 if (!g_markup_parse_context_parse (context,
949 portion->str, portion->len,
952 g_assert (context->state == STATE_ERROR);
955 g_string_free (portion, TRUE);
956 context->parsing = TRUE;
958 /* Skip the fraction of char that was in this text */
959 text_len -= (first_char - text);
964 /* another little chunk of the leftover char; geez
965 * someone is inefficient.
967 g_string_append_len (context->leftover_char_portion,
970 if (context->leftover_char_portion->len > 7)
972 /* The leftover char portion is too big to be
977 G_MARKUP_ERROR_BAD_UTF8,
978 _("Invalid UTF-8 encoded text - overlong sequence"));
985 context->current_text = text;
986 context->current_text_len = text_len;
987 context->iter = context->current_text;
988 context->start = context->iter;
990 /* Nothing left after finishing the leftover char, or nothing
991 * passed in to begin with.
993 if (context->current_text_len == 0)
996 /* find_current_text_end () assumes the string starts at
997 * a character start, so we need to validate at least
998 * that much. It doesn't assume any following bytes
1001 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
1005 G_MARKUP_ERROR_BAD_UTF8,
1006 _("Invalid UTF-8 encoded text - not a start char"));
1010 /* Initialize context->current_text_end, possibly adjusting
1011 * current_text_len, and add any leftover char portion
1013 find_current_text_end (context);
1015 /* Validate UTF8 (must be done after we find the end, since
1016 * we could have a trailing incomplete char)
1018 if (!g_utf8_validate (context->current_text,
1019 context->current_text_len,
1024 q = p = context->current_text;
1025 while (p != first_invalid)
1031 context->char_number = 1;
1036 context->line_number += newlines;
1037 context->char_number += g_utf8_strlen (q, first_invalid - q);
1041 G_MARKUP_ERROR_BAD_UTF8,
1042 _("Invalid UTF-8 encoded text - not valid '%s'"),
1043 g_strndup (context->current_text,
1044 context->current_text_len));
1048 while (context->iter != context->current_text_end)
1050 switch (context->state)
1053 /* Possible next state: AFTER_OPEN_ANGLE */
1055 g_assert (context->tag_stack == NULL);
1057 /* whitespace is ignored outside of any elements */
1058 skip_spaces (context);
1060 if (context->iter != context->current_text_end)
1062 if (*context->iter == '<')
1064 /* Move after the open angle */
1065 advance_char (context);
1067 context->state = STATE_AFTER_OPEN_ANGLE;
1069 /* this could start a passthrough */
1070 context->start = context->iter;
1072 /* document is now non-empty */
1073 context->document_empty = FALSE;
1079 G_MARKUP_ERROR_PARSE,
1080 _("Document must begin with an element (e.g. <book>)"));
1085 case STATE_AFTER_OPEN_ANGLE:
1086 /* Possible next states: INSIDE_OPEN_TAG_NAME,
1087 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
1089 if (*context->iter == '?' ||
1090 *context->iter == '!')
1092 /* include < in the passthrough */
1093 const gchar *openangle = "<";
1094 add_to_partial (context, openangle, openangle + 1);
1095 context->start = context->iter;
1096 context->balance = 1;
1097 context->state = STATE_INSIDE_PASSTHROUGH;
1099 else if (*context->iter == '/')
1102 advance_char (context);
1104 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
1106 else if (is_name_start_char (context->iter))
1108 context->state = STATE_INSIDE_OPEN_TAG_NAME;
1110 /* start of tag name */
1111 context->start = context->iter;
1119 G_MARKUP_ERROR_PARSE,
1120 _("'%s' is not a valid character following "
1121 "a '<' character; it may not begin an "
1123 utf8_str (context->iter, buf));
1127 /* The AFTER_CLOSE_ANGLE state is actually sort of
1128 * broken, because it doesn't correspond to a range
1129 * of characters in the input stream as the others do,
1130 * and thus makes things harder to conceptualize
1132 case STATE_AFTER_CLOSE_ANGLE:
1133 /* Possible next states: INSIDE_TEXT, STATE_START */
1134 if (context->tag_stack == NULL)
1136 context->start = NULL;
1137 context->state = STATE_START;
1141 context->start = context->iter;
1142 context->state = STATE_INSIDE_TEXT;
1146 case STATE_AFTER_ELISION_SLASH:
1147 /* Possible next state: AFTER_CLOSE_ANGLE */
1150 /* We need to pop the tag stack and call the end_element
1151 * function, since this is the close tag
1153 GError *tmp_error = NULL;
1155 g_assert (context->tag_stack != NULL);
1158 if (context->parser->end_element)
1159 (* context->parser->end_element) (context,
1160 context->tag_stack->data,
1166 mark_error (context, tmp_error);
1167 g_propagate_error (error, tmp_error);
1171 if (*context->iter == '>')
1173 /* move after the close angle */
1174 advance_char (context);
1175 context->state = STATE_AFTER_CLOSE_ANGLE;
1183 G_MARKUP_ERROR_PARSE,
1184 _("Odd character '%s', expected a '>' character "
1185 "to end the start tag of element '%s'"),
1186 utf8_str (context->iter, buf),
1187 current_element (context));
1191 g_free (context->tag_stack->data);
1192 context->tag_stack = g_slist_delete_link (context->tag_stack,
1193 context->tag_stack);
1197 case STATE_INSIDE_OPEN_TAG_NAME:
1198 /* Possible next states: BETWEEN_ATTRIBUTES */
1200 /* if there's a partial chunk then it's the first part of the
1201 * tag name. If there's a context->start then it's the start
1202 * of the tag name in current_text, the partial chunk goes
1203 * before that start though.
1205 advance_to_name_end (context);
1207 if (context->iter == context->current_text_end)
1209 /* The name hasn't necessarily ended. Merge with
1210 * partial chunk, leave state unchanged.
1212 add_to_partial (context, context->start, context->iter);
1216 /* The name has ended. Combine it with the partial chunk
1217 * if any; push it on the stack; enter next state.
1219 add_to_partial (context, context->start, context->iter);
1220 context->tag_stack =
1221 g_slist_prepend (context->tag_stack,
1222 g_string_free (context->partial_chunk,
1225 context->partial_chunk = NULL;
1227 context->state = STATE_BETWEEN_ATTRIBUTES;
1228 context->start = NULL;
1232 case STATE_INSIDE_ATTRIBUTE_NAME:
1233 /* Possible next states: AFTER_ATTRIBUTE_NAME */
1235 advance_to_name_end (context);
1236 add_to_partial (context, context->start, context->iter);
1238 /* read the full name, if we enter the equals sign state
1239 * then add the attribute to the list (without the value),
1240 * otherwise store a partial chunk to be prepended later.
1242 if (context->iter != context->current_text_end)
1243 context->state = STATE_AFTER_ATTRIBUTE_NAME;
1246 case STATE_AFTER_ATTRIBUTE_NAME:
1247 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1249 skip_spaces (context);
1251 if (context->iter != context->current_text_end)
1253 /* The name has ended. Combine it with the partial chunk
1254 * if any; push it on the stack; enter next state.
1256 add_attribute (context, g_string_free (context->partial_chunk, FALSE));
1258 context->partial_chunk = NULL;
1259 context->start = NULL;
1261 if (*context->iter == '=')
1263 advance_char (context);
1264 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1272 G_MARKUP_ERROR_PARSE,
1273 _("Odd character '%s', expected a '=' after "
1274 "attribute name '%s' of element '%s'"),
1275 utf8_str (context->iter, buf),
1276 current_attribute (context),
1277 current_element (context));
1283 case STATE_BETWEEN_ATTRIBUTES:
1284 /* Possible next states: AFTER_CLOSE_ANGLE,
1285 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1287 skip_spaces (context);
1289 if (context->iter != context->current_text_end)
1291 if (*context->iter == '/')
1293 advance_char (context);
1294 context->state = STATE_AFTER_ELISION_SLASH;
1296 else if (*context->iter == '>')
1299 advance_char (context);
1300 context->state = STATE_AFTER_CLOSE_ANGLE;
1302 else if (is_name_start_char (context->iter))
1304 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1305 /* start of attribute name */
1306 context->start = context->iter;
1314 G_MARKUP_ERROR_PARSE,
1315 _("Odd character '%s', expected a '>' or '/' "
1316 "character to end the start tag of "
1317 "element '%s', or optionally an attribute; "
1318 "perhaps you used an invalid character in "
1319 "an attribute name"),
1320 utf8_str (context->iter, buf),
1321 current_element (context));
1324 /* If we're done with attributes, invoke
1325 * the start_element callback
1327 if (context->state == STATE_AFTER_ELISION_SLASH ||
1328 context->state == STATE_AFTER_CLOSE_ANGLE)
1330 const gchar *start_name;
1331 /* Ugly, but the current code expects an empty array instead of NULL */
1332 const gchar *empty = NULL;
1333 const gchar **attr_names = ∅
1334 const gchar **attr_values = ∅
1337 /* Call user callback for element start */
1338 start_name = current_element (context);
1340 if (context->cur_attr >= 0)
1342 attr_names = (const gchar**)context->attr_names;
1343 attr_values = (const gchar**)context->attr_values;
1347 if (context->parser->start_element)
1348 (* context->parser->start_element) (context,
1350 (const gchar **)attr_names,
1351 (const gchar **)attr_values,
1355 /* Go ahead and free the attributes. */
1356 for (; context->cur_attr >= 0; context->cur_attr--)
1358 int pos = context->cur_attr;
1359 g_free (context->attr_names[pos]);
1360 g_free (context->attr_values[pos]);
1361 context->attr_names[pos] = context->attr_values[pos] = NULL;
1363 g_assert (context->cur_attr == -1);
1364 g_assert (context->attr_names == NULL ||
1365 context->attr_names[0] == NULL);
1366 g_assert (context->attr_values == NULL ||
1367 context->attr_values[0] == NULL);
1369 if (tmp_error != NULL)
1370 propagate_error (context, error, tmp_error);
1375 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1376 /* Possible next state: INSIDE_ATTRIBUTE_VALUE_[SQ/DQ] */
1378 skip_spaces (context);
1380 if (context->iter != context->current_text_end)
1382 if (*context->iter == '"')
1384 advance_char (context);
1385 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_DQ;
1386 context->start = context->iter;
1388 else if (*context->iter == '\'')
1390 advance_char (context);
1391 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_SQ;
1392 context->start = context->iter;
1400 G_MARKUP_ERROR_PARSE,
1401 _("Odd character '%s', expected an open quote mark "
1402 "after the equals sign when giving value for "
1403 "attribute '%s' of element '%s'"),
1404 utf8_str (context->iter, buf),
1405 current_attribute (context),
1406 current_element (context));
1411 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1412 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1413 /* Possible next states: BETWEEN_ATTRIBUTES */
1417 if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ)
1428 if (*context->iter == delim)
1431 while (advance_char (context));
1433 if (context->iter == context->current_text_end)
1435 /* The value hasn't necessarily ended. Merge with
1436 * partial chunk, leave state unchanged.
1438 add_to_partial (context, context->start, context->iter);
1442 /* The value has ended at the quote mark. Combine it
1443 * with the partial chunk if any; set it for the current
1448 add_to_partial (context, context->start, context->iter);
1450 g_assert (context->cur_attr >= 0);
1452 if (unescape_text (context,
1453 context->partial_chunk->str,
1454 context->partial_chunk->str +
1455 context->partial_chunk->len,
1459 /* success, advance past quote and set state. */
1460 context->attr_values[context->cur_attr] = g_string_free (unescaped, FALSE);
1461 advance_char (context);
1462 context->state = STATE_BETWEEN_ATTRIBUTES;
1463 context->start = NULL;
1466 truncate_partial (context);
1470 case STATE_INSIDE_TEXT:
1471 /* Possible next states: AFTER_OPEN_ANGLE */
1474 if (*context->iter == '<')
1477 while (advance_char (context));
1479 /* The text hasn't necessarily ended. Merge with
1480 * partial chunk, leave state unchanged.
1483 add_to_partial (context, context->start, context->iter);
1485 if (context->iter != context->current_text_end)
1487 GString *unescaped = NULL;
1489 /* The text has ended at the open angle. Call the text
1493 if (unescape_text (context,
1494 context->partial_chunk->str,
1495 context->partial_chunk->str +
1496 context->partial_chunk->len,
1500 GError *tmp_error = NULL;
1502 if (context->parser->text)
1503 (*context->parser->text) (context,
1509 g_string_free (unescaped, TRUE);
1511 if (tmp_error == NULL)
1513 /* advance past open angle and set state. */
1514 advance_char (context);
1515 context->state = STATE_AFTER_OPEN_ANGLE;
1516 /* could begin a passthrough */
1517 context->start = context->iter;
1520 propagate_error (context, error, tmp_error);
1523 truncate_partial (context);
1527 case STATE_AFTER_CLOSE_TAG_SLASH:
1528 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1529 if (is_name_start_char (context->iter))
1531 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1533 /* start of tag name */
1534 context->start = context->iter;
1542 G_MARKUP_ERROR_PARSE,
1543 _("'%s' is not a valid character following "
1544 "the characters '</'; '%s' may not begin an "
1546 utf8_str (context->iter, buf),
1547 utf8_str (context->iter, buf));
1551 case STATE_INSIDE_CLOSE_TAG_NAME:
1552 /* Possible next state: AFTER_CLOSE_TAG_NAME */
1553 advance_to_name_end (context);
1554 add_to_partial (context, context->start, context->iter);
1556 if (context->iter != context->current_text_end)
1557 context->state = STATE_AFTER_CLOSE_TAG_NAME;
1560 case STATE_AFTER_CLOSE_TAG_NAME:
1561 /* Possible next state: AFTER_CLOSE_TAG_SLASH */
1563 skip_spaces (context);
1565 if (context->iter != context->current_text_end)
1569 /* The name has ended. Combine it with the partial chunk
1570 * if any; check that it matches stack top and pop
1571 * stack; invoke proper callback; enter next state.
1573 close_name = g_string_free (context->partial_chunk, FALSE);
1574 context->partial_chunk = NULL;
1576 if (*context->iter != '>')
1582 G_MARKUP_ERROR_PARSE,
1583 _("'%s' is not a valid character following "
1584 "the close element name '%s'; the allowed "
1585 "character is '>'"),
1586 utf8_str (context->iter, buf),
1589 else if (context->tag_stack == NULL)
1593 G_MARKUP_ERROR_PARSE,
1594 _("Element '%s' was closed, no element "
1595 "is currently open"),
1598 else if (strcmp (close_name, current_element (context)) != 0)
1602 G_MARKUP_ERROR_PARSE,
1603 _("Element '%s' was closed, but the currently "
1604 "open element is '%s'"),
1606 current_element (context));
1611 advance_char (context);
1612 context->state = STATE_AFTER_CLOSE_ANGLE;
1613 context->start = NULL;
1615 /* call the end_element callback */
1617 if (context->parser->end_element)
1618 (* context->parser->end_element) (context,
1624 /* Pop the tag stack */
1625 g_free (context->tag_stack->data);
1626 context->tag_stack = g_slist_delete_link (context->tag_stack,
1627 context->tag_stack);
1630 propagate_error (context, error, tmp_error);
1633 g_free (close_name);
1637 case STATE_INSIDE_PASSTHROUGH:
1638 /* Possible next state: AFTER_CLOSE_ANGLE */
1641 if (*context->iter == '<')
1643 if (*context->iter == '>')
1649 add_to_partial (context, context->start, context->iter);
1650 context->start = context->iter;
1652 str = context->partial_chunk->str;
1653 len = context->partial_chunk->len;
1655 if (str[1] == '?' && str[len - 1] == '?')
1657 if (strncmp (str, "<!--", 4) == 0 &&
1658 strcmp (str + len - 2, "--") == 0)
1660 if (strncmp (str, "<![CDATA[", 9) == 0 &&
1661 strcmp (str + len - 2, "]]") == 0)
1663 if (strncmp (str, "<!DOCTYPE", 9) == 0 &&
1664 context->balance == 0)
1668 while (advance_char (context));
1670 if (context->iter == context->current_text_end)
1672 /* The passthrough hasn't necessarily ended. Merge with
1673 * partial chunk, leave state unchanged.
1675 add_to_partial (context, context->start, context->iter);
1679 /* The passthrough has ended at the close angle. Combine
1680 * it with the partial chunk if any. Call the passthrough
1681 * callback. Note that the open/close angles are
1682 * included in the text of the passthrough.
1684 GError *tmp_error = NULL;
1686 advance_char (context); /* advance past close angle */
1687 add_to_partial (context, context->start, context->iter);
1689 if (context->flags & G_MARKUP_TREAT_CDATA_AS_TEXT &&
1690 strncmp (context->partial_chunk->str, "<![CDATA[", 9) == 0)
1692 if (context->parser->text)
1693 (*context->parser->text) (context,
1694 context->partial_chunk->str + 9,
1695 context->partial_chunk->len - 12,
1699 else if (context->parser->passthrough)
1700 (*context->parser->passthrough) (context,
1701 context->partial_chunk->str,
1702 context->partial_chunk->len,
1706 truncate_partial (context);
1708 if (tmp_error == NULL)
1710 context->state = STATE_AFTER_CLOSE_ANGLE;
1711 context->start = context->iter; /* could begin text */
1714 propagate_error (context, error, tmp_error);
1723 g_assert_not_reached ();
1729 context->parsing = FALSE;
1731 return context->state != STATE_ERROR;
1735 * g_markup_parse_context_end_parse:
1736 * @context: a #GMarkupParseContext
1737 * @error: return location for a #GError
1739 * Signals to the #GMarkupParseContext that all data has been
1740 * fed into the parse context with g_markup_parse_context_parse().
1741 * This function reports an error if the document isn't complete,
1742 * for example if elements are still open.
1744 * Return value: %TRUE on success, %FALSE if an error was set
1747 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1750 g_return_val_if_fail (context != NULL, FALSE);
1751 g_return_val_if_fail (!context->parsing, FALSE);
1752 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1754 if (context->partial_chunk != NULL)
1756 g_string_free (context->partial_chunk, TRUE);
1757 context->partial_chunk = NULL;
1760 if (context->document_empty)
1762 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1763 _("Document was empty or contained only whitespace"));
1767 context->parsing = TRUE;
1769 switch (context->state)
1775 case STATE_AFTER_OPEN_ANGLE:
1776 set_error (context, error, G_MARKUP_ERROR_PARSE,
1777 _("Document ended unexpectedly just after an open angle bracket '<'"));
1780 case STATE_AFTER_CLOSE_ANGLE:
1781 if (context->tag_stack != NULL)
1783 /* Error message the same as for INSIDE_TEXT */
1784 set_error (context, error, G_MARKUP_ERROR_PARSE,
1785 _("Document ended unexpectedly with elements still open - "
1786 "'%s' was the last element opened"),
1787 current_element (context));
1791 case STATE_AFTER_ELISION_SLASH:
1792 set_error (context, error, G_MARKUP_ERROR_PARSE,
1793 _("Document ended unexpectedly, expected to see a close angle "
1794 "bracket ending the tag <%s/>"), current_element (context));
1797 case STATE_INSIDE_OPEN_TAG_NAME:
1798 set_error (context, error, G_MARKUP_ERROR_PARSE,
1799 _("Document ended unexpectedly inside an element name"));
1802 case STATE_INSIDE_ATTRIBUTE_NAME:
1803 case STATE_AFTER_ATTRIBUTE_NAME:
1804 set_error (context, error, G_MARKUP_ERROR_PARSE,
1805 _("Document ended unexpectedly inside an attribute name"));
1808 case STATE_BETWEEN_ATTRIBUTES:
1809 set_error (context, error, G_MARKUP_ERROR_PARSE,
1810 _("Document ended unexpectedly inside an element-opening "
1814 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1815 set_error (context, error, G_MARKUP_ERROR_PARSE,
1816 _("Document ended unexpectedly after the equals sign "
1817 "following an attribute name; no attribute value"));
1820 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1821 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1822 set_error (context, error, G_MARKUP_ERROR_PARSE,
1823 _("Document ended unexpectedly while inside an attribute "
1827 case STATE_INSIDE_TEXT:
1828 g_assert (context->tag_stack != NULL);
1829 set_error (context, error, G_MARKUP_ERROR_PARSE,
1830 _("Document ended unexpectedly with elements still open - "
1831 "'%s' was the last element opened"),
1832 current_element (context));
1835 case STATE_AFTER_CLOSE_TAG_SLASH:
1836 case STATE_INSIDE_CLOSE_TAG_NAME:
1837 case STATE_AFTER_CLOSE_TAG_NAME:
1838 set_error (context, error, G_MARKUP_ERROR_PARSE,
1839 _("Document ended unexpectedly inside the close tag for "
1840 "element '%s'"), current_element (context));
1843 case STATE_INSIDE_PASSTHROUGH:
1844 set_error (context, error, G_MARKUP_ERROR_PARSE,
1845 _("Document ended unexpectedly inside a comment or "
1846 "processing instruction"));
1851 g_assert_not_reached ();
1855 context->parsing = FALSE;
1857 return context->state != STATE_ERROR;
1861 * g_markup_parse_context_get_element:
1862 * @context: a #GMarkupParseContext
1863 * @returns: the name of the currently open element, or %NULL
1865 * Retrieves the name of the currently open element.
1867 * If called from the start_element or end_element handlers this will
1868 * give the element_name as passed to those functions. For the parent
1869 * elements, see g_markup_parse_context_get_element_stack().
1873 G_CONST_RETURN gchar *
1874 g_markup_parse_context_get_element (GMarkupParseContext *context)
1876 g_return_val_if_fail (context != NULL, NULL);
1878 if (context->tag_stack == NULL)
1881 return current_element (context);
1885 * g_markup_parse_context_get_element_stack:
1886 * @context: a #GMarkupParseContext
1888 * Retrieves the element stack from the internal state of the parser.
1889 * The returned #GSList is a list of strings where the first item is
1890 * the currently open tag (as would be returned by
1891 * g_markup_parse_context_get_element()) and the next item is its
1894 * This function is intended to be used in the start_element and
1895 * end_element handlers where g_markup_parse_context_get_element()
1896 * would merely return the name of the element that is being
1899 * Returns: the element stack, which must not be modified
1903 G_CONST_RETURN GSList *
1904 g_markup_parse_context_get_element_stack (GMarkupParseContext *context)
1906 g_return_val_if_fail (context != NULL, NULL);
1908 return context->tag_stack;
1912 * g_markup_parse_context_get_position:
1913 * @context: a #GMarkupParseContext
1914 * @line_number: return location for a line number, or %NULL
1915 * @char_number: return location for a char-on-line number, or %NULL
1917 * Retrieves the current line number and the number of the character on
1918 * that line. Intended for use in error messages; there are no strict
1919 * semantics for what constitutes the "current" line number other than
1920 * "the best number we could come up with for error messages."
1924 g_markup_parse_context_get_position (GMarkupParseContext *context,
1928 g_return_if_fail (context != NULL);
1931 *line_number = context->line_number;
1934 *char_number = context->char_number;
1938 append_escaped_text (GString *str,
1947 end = text + length;
1952 next = g_utf8_next_char (p);
1957 g_string_append (str, "&");
1961 g_string_append (str, "<");
1965 g_string_append (str, ">");
1969 g_string_append (str, "'");
1973 g_string_append (str, """);
1977 c = g_utf8_get_char (p);
1978 if ((0x1 <= c && c <= 0x8) ||
1979 (0xb <= c && c <= 0xc) ||
1980 (0xe <= c && c <= 0x1f) ||
1981 (0x7f <= c && c <= 0x84) ||
1982 (0x86 <= c && c <= 0x9f))
1983 g_string_append_printf (str, "&#x%x;", c);
1985 g_string_append_len (str, p, next - p);
1994 * g_markup_escape_text:
1995 * @text: some valid UTF-8 text
1996 * @length: length of @text in bytes, or -1 if the text is nul-terminated
1998 * Escapes text so that the markup parser will parse it verbatim.
1999 * Less than, greater than, ampersand, etc. are replaced with the
2000 * corresponding entities. This function would typically be used
2001 * when writing out a file to be parsed with the markup parser.
2003 * Note that this function doesn't protect whitespace and line endings
2004 * from being processed according to the XML rules for normalization
2005 * of line endings and attribute values.
2007 * Return value: a newly allocated string with the escaped text
2010 g_markup_escape_text (const gchar *text,
2015 g_return_val_if_fail (text != NULL, NULL);
2018 length = strlen (text);
2020 /* prealloc at least as long as original text */
2021 str = g_string_sized_new (length);
2022 append_escaped_text (str, text, length);
2024 return g_string_free (str, FALSE);
2029 * @format: a printf-style format string
2030 * @after: location to store a pointer to the character after
2031 * the returned conversion. On a %NULL return, returns the
2032 * pointer to the trailing NUL in the string
2034 * Find the next conversion in a printf-style format string.
2035 * Partially based on code from printf-parser.c,
2036 * Copyright (C) 1999-2000, 2002-2003 Free Software Foundation, Inc.
2038 * Return value: pointer to the next conversion in @format,
2039 * or %NULL, if none.
2042 find_conversion (const char *format,
2045 const char *start = format;
2048 while (*start != '\0' && *start != '%')
2065 /* Test for positional argument. */
2066 if (*cp >= '0' && *cp <= '9')
2070 for (np = cp; *np >= '0' && *np <= '9'; np++)
2076 /* Skip the flags. */
2090 /* Skip the field width. */
2095 /* Test for positional argument. */
2096 if (*cp >= '0' && *cp <= '9')
2100 for (np = cp; *np >= '0' && *np <= '9'; np++)
2108 for (; *cp >= '0' && *cp <= '9'; cp++)
2112 /* Skip the precision. */
2118 /* Test for positional argument. */
2119 if (*cp >= '0' && *cp <= '9')
2123 for (np = cp; *np >= '0' && *np <= '9'; np++)
2131 for (; *cp >= '0' && *cp <= '9'; cp++)
2136 /* Skip argument type/size specifiers. */
2137 while (*cp == 'h' ||
2146 /* Skip the conversion character. */
2154 * g_markup_vprintf_escaped:
2155 * @format: printf() style format string
2156 * @args: variable argument list, similar to vprintf()
2158 * Formats the data in @args according to @format, escaping
2159 * all string and character arguments in the fashion
2160 * of g_markup_escape_text(). See g_markup_printf_escaped().
2162 * Return value: newly allocated result from formatting
2163 * operation. Free with g_free().
2168 g_markup_vprintf_escaped (const char *format,
2173 GString *result = NULL;
2174 gchar *output1 = NULL;
2175 gchar *output2 = NULL;
2176 const char *p, *op1, *op2;
2179 /* The technique here, is that we make two format strings that
2180 * have the identical conversions in the identical order to the
2181 * original strings, but differ in the text in-between. We
2182 * then use the normal g_strdup_vprintf() to format the arguments
2183 * with the two new format strings. By comparing the results,
2184 * we can figure out what segments of the output come from
2185 * the the original format string, and what from the arguments,
2186 * and thus know what portions of the string to escape.
2188 * For instance, for:
2190 * g_markup_printf_escaped ("%s ate %d apples", "Susan & Fred", 5);
2192 * We form the two format strings "%sX%dX" and %sY%sY". The results
2193 * of formatting with those two strings are
2195 * "%sX%dX" => "Susan & FredX5X"
2196 * "%sY%dY" => "Susan & FredY5Y"
2198 * To find the span of the first argument, we find the first position
2199 * where the two arguments differ, which tells us that the first
2200 * argument formatted to "Susan & Fred". We then escape that
2201 * to "Susan & Fred" and join up with the intermediate portions
2202 * of the format string and the second argument to get
2203 * "Susan & Fred ate 5 apples".
2206 /* Create the two modified format strings
2208 format1 = g_string_new (NULL);
2209 format2 = g_string_new (NULL);
2214 const char *conv = find_conversion (p, &after);
2218 g_string_append_len (format1, conv, after - conv);
2219 g_string_append_c (format1, 'X');
2220 g_string_append_len (format2, conv, after - conv);
2221 g_string_append_c (format2, 'Y');
2226 /* Use them to format the arguments
2228 G_VA_COPY (args2, args);
2230 output1 = g_strdup_vprintf (format1->str, args);
2237 output2 = g_strdup_vprintf (format2->str, args2);
2242 result = g_string_new (NULL);
2244 /* Iterate through the original format string again,
2245 * copying the non-conversion portions and the escaped
2246 * converted arguments to the output string.
2254 const char *output_start;
2255 const char *conv = find_conversion (p, &after);
2258 if (!conv) /* The end, after points to the trailing \0 */
2260 g_string_append_len (result, p, after - p);
2264 g_string_append_len (result, p, conv - p);
2266 while (*op1 == *op2)
2272 escaped = g_markup_escape_text (output_start, op1 - output_start);
2273 g_string_append (result, escaped);
2282 g_string_free (format1, TRUE);
2283 g_string_free (format2, TRUE);
2288 return g_string_free (result, FALSE);
2294 * g_markup_printf_escaped:
2295 * @format: printf() style format string
2296 * @Varargs: the arguments to insert in the format string
2298 * Formats arguments according to @format, escaping
2299 * all string and character arguments in the fashion
2300 * of g_markup_escape_text(). This is useful when you
2301 * want to insert literal strings into XML-style markup
2302 * output, without having to worry that the strings
2303 * might themselves contain markup.
2306 * const char *store = "Fortnum & Mason";
2307 * const char *item = "Tea";
2310 * output = g_markup_printf_escaped ("<purchase>"
2311 * "<store>%s</store>"
2312 * "<item>%s</item>"
2313 * "</purchase>",
2317 * Return value: newly allocated result from formatting
2318 * operation. Free with g_free().
2323 g_markup_printf_escaped (const char *format, ...)
2328 va_start (args, format);
2329 result = g_markup_vprintf_escaped (format, args);
2336 g_markup_parse_boolean (const char *string,
2339 char const * const falses[] = { "false", "f", "no", "n", "0" };
2340 char const * const trues[] = { "true", "t", "yes", "y", "1" };
2343 for (i = 0; i < G_N_ELEMENTS (falses); i++)
2345 if (g_ascii_strcasecmp (string, falses[i]) == 0)
2354 for (i = 0; i < G_N_ELEMENTS (trues); i++)
2356 if (g_ascii_strcasecmp (string, trues[i]) == 0)
2369 * GMarkupCollectType:
2370 * @G_MARKUP_COLLECT_INVALID: used to terminate the list of attributes
2372 * @G_MARKUP_COLLECT_STRING: collect the string pointer directly from
2373 * the attribute_values[] array. Expects a
2374 * parameter of type (const char **). If
2375 * %G_MARKUP_COLLECT_OPTIONAL is specified
2376 * and the attribute isn't present then the
2377 * pointer will be set to %NULL.
2378 * @G_MARKUP_COLLECT_STRDUP: as with %G_MARKUP_COLLECT_STRING, but
2379 * expects a paramter of type (char **) and
2380 * g_strdup()s the returned pointer. The
2381 * pointer must be freed with g_free().
2382 * @G_MARKUP_COLLECT_BOOLEAN: expects a parameter of type (gboolean *)
2383 * and parses the attribute value as a
2384 * boolean. Sets %FALSE if the attribute
2385 * isn't present. Valid boolean values
2386 * consist of (case insensitive) "false",
2387 * "f", "no", "n", "0" and "true", "t",
2389 * @G_MARKUP_COLLECT_TRISTATE: as with %G_MARKUP_COLLECT_BOOLEAN, but
2390 * in the case of a missing attribute a
2391 * value is set that compares equal to
2392 * neither %FALSE nor %TRUE.
2393 * G_MARKUP_COLLECT_OPTIONAL is implied.
2394 * @G_MARKUP_COLLECT_OPTIONAL: can be bitwise ORed with the other
2395 * fields. If present, allows the
2396 * attribute not to appear. A default
2397 * value is set depending on what value
2400 * A mixed enumerated type and flags field. You must specify one type
2401 * (string, strdup, boolean, tristate). Additionally, you may
2402 * optionally bitwise OR the type with the flag
2403 * %G_MARKUP_COLLECT_OPTIONAL.
2405 * It is likely that this enum will be extended in the future to
2406 * support other types.
2410 * g_markup_collect_attributes:
2411 * @element_name: the current tag name
2412 * @attribute_names: the attribute names
2413 * @attribute_values: the attribute values
2414 * @error: a pointer to a #GError or %NULL
2415 * @first_type: the #GMarkupCollectType of the
2417 * @first_attr: the name of the first attribute
2418 * @...: a pointer to the storage location of the
2419 * first attribute (or %NULL), followed by
2420 * more types names and pointers, ending
2421 * with %G_MARKUP_COLLECT_INVALID.
2423 * Collects the attributes of the element from the
2424 * data passed to the #GMarkupParser start_element
2425 * function, dealing with common error conditions
2426 * and supporting boolean values.
2428 * This utility function is not required to write
2429 * a parser but can save a lot of typing.
2431 * The @element_name, @attribute_names,
2432 * @attribute_values and @error parameters passed
2433 * to the start_element callback should be passed
2434 * unmodified to this function.
2436 * Following these arguments is a list of
2437 * "supported" attributes to collect. It is an
2438 * error to specify multiple attributes with the
2439 * same name. If any attribute not in the list
2440 * appears in the @attribute_names array then an
2441 * unknown attribute error will result.
2443 * The #GMarkupCollectType field allows specifying
2444 * the type of collection to perform and if a
2445 * given attribute must appear or is optional.
2447 * The attribute name is simply the name of the
2448 * attribute to collect.
2450 * The pointer should be of the appropriate type
2451 * (see the descriptions under
2452 * #GMarkupCollectType) and may be %NULL in case a
2453 * particular attribute is to be allowed but
2456 * This function deals with issuing errors for missing attributes
2457 * (of type %G_MARKUP_ERROR_MISSING_ATTRIBUTE), unknown attributes
2458 * (of type %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE) and duplicate
2459 * attributes (of type %G_MARKUP_ERROR_INVALID_CONTENT) as well
2460 * as parse errors for boolean-valued attributes (again of type
2461 * %G_MARKUP_ERROR_INVALID_CONTENT). In all of these cases %FALSE
2462 * will be returned and @error will be set as appropriate.
2464 * Return value: %TRUE if successful
2469 g_markup_collect_attributes (const gchar *element_name,
2470 const gchar **attribute_names,
2471 const gchar **attribute_values,
2473 GMarkupCollectType first_type,
2474 const gchar *first_attr,
2477 GMarkupCollectType type;
2489 va_start (ap, first_attr);
2490 while (type != G_MARKUP_COLLECT_INVALID)
2495 mandatory = !(type & G_MARKUP_COLLECT_OPTIONAL);
2496 type &= (G_MARKUP_COLLECT_OPTIONAL - 1);
2498 /* tristate records a value != TRUE and != FALSE
2499 * for the case where the attribute is missing
2501 if (type == G_MARKUP_COLLECT_TRISTATE)
2504 for (i = 0; attribute_names[i]; i++)
2505 if (i >= 40 || !(collected & (G_GUINT64_CONSTANT(1) << i)))
2506 if (!strcmp (attribute_names[i], attr))
2509 /* ISO C99 only promises that the user can pass up to 127 arguments.
2510 * Subtracting the first 4 arguments plus the final NULL and dividing
2511 * by 3 arguments per collected attribute, we are left with a maximum
2512 * number of supported attributes of (127 - 5) / 3 = 40.
2514 * In reality, nobody is ever going to call us with anywhere close to
2515 * 40 attributes to collect, so it is safe to assume that if i > 40
2516 * then the user has given some invalid or repeated arguments. These
2517 * problems will be caught and reported at the end of the function.
2519 * We know at this point that we have an error, but we don't know
2520 * what error it is, so just continue...
2523 collected |= (G_GUINT64_CONSTANT(1) << i);
2525 value = attribute_values[i];
2527 if (value == NULL && mandatory)
2529 g_set_error (error, G_MARKUP_ERROR,
2530 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
2531 "element '%s' requires attribute '%s'",
2532 element_name, attr);
2540 case G_MARKUP_COLLECT_STRING:
2542 const char **str_ptr;
2544 str_ptr = va_arg (ap, const char **);
2546 if (str_ptr != NULL)
2551 case G_MARKUP_COLLECT_STRDUP:
2555 str_ptr = va_arg (ap, char **);
2557 if (str_ptr != NULL)
2558 *str_ptr = g_strdup (value);
2562 case G_MARKUP_COLLECT_BOOLEAN:
2563 case G_MARKUP_COLLECT_TRISTATE:
2568 bool_ptr = va_arg (ap, gboolean *);
2570 if (bool_ptr != NULL)
2572 if (type == G_MARKUP_COLLECT_TRISTATE)
2573 /* constructivists rejoice!
2574 * neither false nor true...
2578 else /* G_MARKUP_COLLECT_BOOLEAN */
2584 if (!g_markup_parse_boolean (value, va_arg (ap, gboolean *)))
2586 g_set_error (error, G_MARKUP_ERROR,
2587 G_MARKUP_ERROR_INVALID_CONTENT,
2588 "element '%s', attribute '%s', value '%s' "
2589 "cannot be parsed as a boolean value",
2590 element_name, attr, value);
2600 g_assert_not_reached ();
2603 type = va_arg (ap, GMarkupCollectType);
2604 attr = va_arg (ap, const char *);
2609 /* ensure we collected all the arguments */
2610 for (i = 0; attribute_names[i]; i++)
2611 if ((collected & (G_GUINT64_CONSTANT(1) << i)) == 0)
2613 /* attribute not collected: could be caused by two things.
2615 * 1) it doesn't exist in our list of attributes
2616 * 2) it existed but was matched by a duplicate attribute earlier
2622 for (j = 0; j < i; j++)
2623 if (strcmp (attribute_names[i], attribute_names[j]) == 0)
2627 /* j is now the first occurance of attribute_names[i] */
2629 g_set_error (error, G_MARKUP_ERROR,
2630 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2631 "attribute '%s' invalid for element '%s'",
2632 attribute_names[i], element_name);
2634 g_set_error (error, G_MARKUP_ERROR,
2635 G_MARKUP_ERROR_INVALID_CONTENT,
2636 "attribute '%s' given multiple times for element '%s'",
2637 attribute_names[i], element_name);
2645 /* replay the above to free allocations */
2649 va_start (ap, first_attr);
2650 while (type != G_MARKUP_COLLECT_INVALID)
2654 ptr = va_arg (ap, gpointer);
2659 switch (type & (G_MARKUP_COLLECT_OPTIONAL - 1))
2661 case G_MARKUP_COLLECT_STRDUP:
2663 g_free (*(char **) ptr);
2665 case G_MARKUP_COLLECT_STRING:
2666 *(char **) ptr = NULL;
2669 case G_MARKUP_COLLECT_BOOLEAN:
2670 *(gboolean *) ptr = FALSE;
2673 case G_MARKUP_COLLECT_TRISTATE:
2674 *(gboolean *) ptr = -1;
2678 type = va_arg (ap, GMarkupCollectType);
2679 attr = va_arg (ap, const char *);
2689 #define __G_MARKUP_C__
2690 #include "galiasdef.c"