diff --git a/ChangeLog b/ChangeLog index 8a7a42a5a..84271e428 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2006-11-15 Ryan Lortie + + * docs/reference/glib/tmpl/markup.sgml: + * glib/gmarkup.h: + * glib/gmarkup.c: new flag G_MARKUP_PREFIX_ERROR_POSITION to cause the + parser to prepend location information (ie: "Error on line %d, char + %d:") to errors generated by the GMarkupParser callbacks. + + Closes #496046. + 2006-11-15 Ryan Lortie * docs/reference/glib/glib-sections.txt: diff --git a/docs/reference/glib/tmpl/markup.sgml b/docs/reference/glib/tmpl/markup.sgml index e20c8f35f..b6cc0bdbd 100644 --- a/docs/reference/glib/tmpl/markup.sgml +++ b/docs/reference/glib/tmpl/markup.sgml @@ -102,7 +102,7 @@ Error codes returned by markup parsing. @G_MARKUP_ERROR_PARSE: document was ill-formed @G_MARKUP_ERROR_UNKNOWN_ELEMENT: error should be set by #GMarkupParser functions; element wasn't known @G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE: error should be set by #GMarkupParser functions; attribute wasn't known -@G_MARKUP_ERROR_INVALID_CONTENT: error should be set by #GMarkupParser functions; something was wrong with contents of the document, e.g. invalid attribute value +@G_MARKUP_ERROR_INVALID_POSITION: error should be set by #GMarkupParser functions; something was wrong with contents of the document, e.g. invalid attribute value @@ -124,6 +124,11 @@ Flags that affect the behaviour of the parser. the parser. Instead, the content of the section (without the <![CDATA[ and ]]>) is passed to the @text function. This flag was added in GLib 2.12. +@G_MARKUP_PREFIX_ERROR_POSITION: Normally errors caught by GMarkup + itself have line/column information prefixed to them to let the + caller know the location of the error. When this flag is set the + location information is also prefixed to errors generated by the + #GMarkupParser implementation functions. diff --git a/glib/gmarkup.c b/glib/gmarkup.c index 35c37f595..ca6c4453d 100644 --- a/glib/gmarkup.c +++ b/glib/gmarkup.c @@ -221,20 +221,34 @@ set_error (GMarkupParseContext *context, s = g_strdup_vprintf (format, args); va_end (args); - tmp_error = g_error_new (G_MARKUP_ERROR, - code, - _("Error on line %d char %d: %s"), - context->line_number, - context->char_number, - s); - + tmp_error = g_error_new_literal (G_MARKUP_ERROR, code, s); g_free (s); + g_prefix_error (&tmp_error, + _("Error on line %d char %d: "), + context->line_number, + context->char_number); + mark_error (context, tmp_error); g_propagate_error (error, tmp_error); } +static void +propagate_error (GMarkupParseContext *context, + GError **dest, + GError *src) +{ + if (context->flags & G_MARKUP_PREFIX_ERROR_POSITION) + g_prefix_error (&src, + _("Error on line %d char %d: "), + context->line_number, + context->char_number); + + mark_error (context, src); + + g_propagate_error (dest, src); +} /* To make these faster, we first use the ascii-only tests, then check * for the usual non-alnum name-end chars, and only then call the @@ -1347,10 +1361,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context, context->attr_values[0] == NULL); if (tmp_error != NULL) - { - mark_error (context, tmp_error); - g_propagate_error (error, tmp_error); - } + propagate_error (context, error, tmp_error); } } break; @@ -1500,10 +1511,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context, context->start = context->iter; } else - { - mark_error (context, tmp_error); - g_propagate_error (error, tmp_error); - } + propagate_error (context, error, tmp_error); } truncate_partial (context); @@ -1613,10 +1621,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context, context->tag_stack); if (tmp_error) - { - mark_error (context, tmp_error); - g_propagate_error (error, tmp_error); - } + propagate_error (context, error, tmp_error); } g_free (close_name); @@ -1700,10 +1705,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context, context->start = context->iter; /* could begin text */ } else - { - mark_error (context, tmp_error); - g_propagate_error (error, tmp_error); - } + propagate_error (context, error, tmp_error); } break; diff --git a/glib/gmarkup.h b/glib/gmarkup.h index 81c385238..5021b1ee5 100644 --- a/glib/gmarkup.h +++ b/glib/gmarkup.h @@ -48,7 +48,8 @@ GQuark g_markup_error_quark (void); typedef enum { G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0, - G_MARKUP_TREAT_CDATA_AS_TEXT = 1 << 1 + G_MARKUP_TREAT_CDATA_AS_TEXT = 1 << 1, + G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2 } GMarkupParseFlags; typedef struct _GMarkupParseContext GMarkupParseContext;