1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gstrfuncs.h"
32 #include "gtestutils.h"
36 * @domain: error domain
38 * @format: printf()-style format for error message
39 * @args: #va_list of parameters for the message format
41 * Creates a new #GError with the given @domain and @code,
42 * and a message formatted with @format.
44 * Returns: a new #GError
49 g_error_new_valist (GQuark domain,
56 error = g_slice_new (GError);
58 error->domain = domain;
60 error->message = g_strdup_vprintf (format, args);
67 * @domain: error domain
69 * @format: printf()-style format for error message
70 * @Varargs: parameters for message format
72 * Creates a new #GError with the given @domain and @code,
73 * and a message formatted with @format.
75 * Return value: a new #GError
78 g_error_new (GQuark domain,
86 g_return_val_if_fail (format != NULL, NULL);
87 g_return_val_if_fail (domain != 0, NULL);
89 va_start (args, format);
90 error = g_error_new_valist (domain, code, format, args);
97 * g_error_new_literal:
98 * @domain: error domain
100 * @message: error message
102 * Creates a new #GError; unlike g_error_new(), @message is
103 * not a printf()-style format string. Use this function if
104 * @message contains text you don't have control over,
105 * that could include printf() escape sequences.
107 * Return value: a new #GError
110 g_error_new_literal (GQuark domain,
112 const gchar *message)
116 g_return_val_if_fail (message != NULL, NULL);
117 g_return_val_if_fail (domain != 0, NULL);
119 err = g_slice_new (GError);
121 err->domain = domain;
123 err->message = g_strdup (message);
132 * Frees a #GError and associated resources.
135 g_error_free (GError *error)
137 g_return_if_fail (error != NULL);
139 g_free (error->message);
141 g_slice_free (GError, error);
148 * Makes a copy of @error.
150 * Return value: a new #GError
153 g_error_copy (const GError *error)
157 g_return_val_if_fail (error != NULL, NULL);
159 copy = g_slice_new (GError);
163 copy->message = g_strdup (error->message);
170 * @error: a #GError or %NULL
171 * @domain: an error domain
172 * @code: an error code
174 * Returns %TRUE if @error matches @domain and @code, %FALSE
175 * otherwise. In particular, when @error is %NULL, %FALSE will
178 * Return value: whether @error has @domain and @code
181 g_error_matches (const GError *error,
186 error->domain == domain &&
190 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
191 "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
192 "The overwriting error message was: %s"
196 * @err: a return location for a #GError, or %NULL
197 * @domain: error domain
199 * @format: printf()-style format
200 * @Varargs: args for @format
202 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
203 * must be %NULL. A new #GError is created and assigned to *@err.
206 g_set_error (GError **err,
219 va_start (args, format);
220 new = g_error_new_valist (domain, code, format, args);
226 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
230 * g_set_error_literal:
231 * @err: a return location for a #GError, or %NULL
232 * @domain: error domain
234 * @message: error message
236 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
237 * must be %NULL. A new #GError is created and assigned to *@err.
238 * Unlike g_set_error(), @message is not a printf()-style format string.
239 * Use this function if @message contains text you don't have control over,
240 * that could include printf() escape sequences.
245 g_set_error_literal (GError **err,
248 const gchar *message)
255 new = g_error_new_literal (domain, code, message);
259 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
264 * @dest: error return location
265 * @src: error to move into the return location
267 * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
268 * The error variable @dest points to must be %NULL.
271 g_propagate_error (GError **dest,
274 g_return_if_fail (src != NULL);
285 g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
293 * @err: a #GError return location
295 * If @err is %NULL, does nothing. If @err is non-%NULL,
296 * calls g_error_free() on *@err and sets *@err to %NULL.
299 g_clear_error (GError **err)
309 g_error_add_prefix (gchar **string,
316 prefix = g_strdup_vprintf (format, ap);
318 *string = g_strconcat (prefix, oldstring, NULL);
325 * @err: a return location for a #GError, or %NULL
326 * @format: printf()-style format string
327 * @...: arguments to @format
329 * Formats a string according to @format and
330 * prefix it to an existing error message. If
331 * @err is %NULL (ie: no error variable) then do
334 * If *@err is %NULL (ie: an error variable is
335 * present but there is no error condition) then
336 * also do nothing. Whether or not it makes
337 * sense to take advantage of this feature is up
343 g_prefix_error (GError **err,
351 va_start (ap, format);
352 g_error_add_prefix (&(*err)->message, format, ap);
358 * g_propagate_prefixed_error:
359 * @dest: error return location
360 * @src: error to move into the return location
361 * @format: printf()-style format string
362 * @...: arguments to @format
364 * If @dest is %NULL, free @src; otherwise,
365 * moves @src into *@dest. *@dest must be %NULL.
366 * After the move, add a prefix as with
372 g_propagate_prefixed_error (GError **dest,
377 g_propagate_error (dest, src);
383 va_start (ap, format);
384 g_error_add_prefix (&(*dest)->message, format, ap);