From f2a5aa6700b5a139472410226008b43544917d92 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Fri, 16 Nov 2007 03:05:45 +0000 Subject: [PATCH] new functions g_prefix_error and g_propagate_prefixed_error. 2006-11-15 Ryan Lortie * docs/reference/glib/glib-sections.txt: * glib/glib.symbols: * glib/gerror.h: * glib/gerror.c: new functions g_prefix_error and g_propagate_prefixed_error. svn path=/trunk/; revision=5859 --- ChangeLog | 8 +++ docs/reference/glib/glib-sections.txt | 2 + glib/gerror.c | 77 +++++++++++++++++++++++++++ glib/gerror.h | 10 ++++ glib/glib.symbols | 2 + 5 files changed, 99 insertions(+) diff --git a/ChangeLog b/ChangeLog index 67f1e1366..8a7a42a5a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2006-11-15 Ryan Lortie + + * docs/reference/glib/glib-sections.txt: + * glib/glib.symbols: + * glib/gerror.h: + * glib/gerror.c: new functions g_prefix_error and + g_propagate_prefixed_error. + 2007-11-13 Cody Russell * docs/reference/gobject/gobject-docs.sgml: diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index d07ae531f..94e026f25 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -386,6 +386,8 @@ g_error_matches g_set_error g_propagate_error g_clear_error +g_prefix_error +g_propagate_prefixed_error
diff --git a/glib/gerror.c b/glib/gerror.c index 0b0f98200..6d4794989 100644 --- a/glib/gerror.c +++ b/glib/gerror.c @@ -257,5 +257,82 @@ g_clear_error (GError **err) } } +static void +g_error_add_prefix (gchar **string, + const gchar *format, + va_list ap) +{ + gchar *oldstring; + gchar *prefix; + + prefix = g_strdup_vprintf (format, ap); + oldstring = *string; + *string = g_strjoin ("", prefix, oldstring, NULL); + g_free (oldstring); + g_free (prefix); +} + +/** + * g_prefix_error: + * @err: a return location for a #GError, or %NULL + * @format: printf()-style format string + * ...: arguments to @format + * + * Formats a string according to @format and + * prefix it to an existing error message. If + * @err is %NULL (ie: no error variable) then do + * nothing. + * + * If *@err is %NULL (ie: an error variable is + * present but there is no error condition) then + * also do nothing. Whether or not it makes + * sense to take advantage of this feature is up + * to you. + **/ +void +g_prefix_error (GError **err, + const gchar *format, + ...) +{ + if (err && *err) + { + va_list ap; + + va_start (ap, format); + g_error_add_prefix (&(*err)->message, format, ap); + va_end (ap); + } +} + +/** + * g_propagate_prefixed_error: + * @dest: error return location + * @src: error to move into the return location + * @format: printf()-style format string + * ...: arguments to @format + * + * If @dest is %NULL, free @src; otherwise, + * moves @src into *@dest. *@dest must be %NULL. + * After the move, add a prefix as with + * g_prefix_error(). + **/ +void +g_propagate_prefixed_error (GError **dest, + GError *src, + const gchar *format, + ...) +{ + g_propagate_error (dest, src); + + if (dest && *dest) + { + va_list ap; + + va_start (ap, format); + g_error_add_prefix (&(*dest)->message, format, ap); + va_end (ap); + } +} + #define __G_ERROR_C__ #include "galiasdef.c" diff --git a/glib/gerror.h b/glib/gerror.h index ea0d25aaf..7fa8ef0fb 100644 --- a/glib/gerror.h +++ b/glib/gerror.h @@ -67,6 +67,16 @@ void g_propagate_error (GError **dest, /* if (err && *err) { g_error_free(*err); *err = NULL; } */ void g_clear_error (GError **err); +/* if (err) prefix the formatted string to the ->message */ +void g_prefix_error (GError **err, + const gchar *format, + ...) G_GNUC_PRINTF (2, 3); + +/* g_propagate_error then g_error_prefix on dest */ +void g_propagate_prefixed_error (GError **dest, + GError *src, + const gchar *format, + ...) G_GNUC_PRINTF (3, 4); G_END_DECLS diff --git a/glib/glib.symbols b/glib/glib.symbols index 6e4e7876d..deb7d8db9 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -320,6 +320,8 @@ g_error_new G_GNUC_PRINTF(3,4) g_error_new_literal g_propagate_error g_set_error G_GNUC_PRINTF(4,5) +g_prefix_error G_GNUC_PRINTF(2,3) +g_propagate_prefixed_error G_GNUC_PRINTF(3,4) #endif #endif