Add g_prefix_error_literal()

Because sometimes you don't want a lone "%s", and you don't
want the compiler yelling at you about format strings that
don't have any format in them.

Closes #663
This commit is contained in:
Dan Williams 2021-05-27 13:29:54 +02:00 committed by Emmanuel Fleury
parent 0c8740dc83
commit 9cb1bb0fb2
3 changed files with 30 additions and 0 deletions

View File

@ -786,6 +786,7 @@ g_set_error_literal
g_propagate_error
g_clear_error
g_prefix_error
g_prefix_error_literal
g_propagate_prefixed_error
<SUBSECTION>
GErrorInitFunc

View File

@ -1100,6 +1100,30 @@ g_prefix_error (GError **err,
}
}
/**
* g_prefix_error_literal:
* @err: (allow-none): a return location for a #GError, or %NULL
* @prefix: string to prefix @err with
*
* Prefixes @prefix to an existing error message. If @err or *@err is
* %NULL (i.e.: no error variable) then do nothing.
*
* Since: 2.70
*/
void
g_prefix_error_literal (GError **err,
const gchar *prefix)
{
if (err && *err)
{
gchar *oldstring;
oldstring = (*err)->message;
(*err)->message = g_strconcat (prefix, oldstring, NULL);
g_free (oldstring);
}
}
/**
* g_propagate_prefixed_error:
* @dest: error return location

View File

@ -244,6 +244,11 @@ void g_prefix_error (GError **err,
const gchar *format,
...) G_GNUC_PRINTF (2, 3);
/* if (err) prefix the string to the ->message */
GLIB_AVAILABLE_IN_2_70
void g_prefix_error_literal (GError **err,
const gchar *prefix);
/* g_propagate_error then g_error_prefix on dest */
GLIB_AVAILABLE_IN_ALL
void g_propagate_prefixed_error (GError **dest,