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/.
35 * @domain: error domain
37 * @format: printf()-style format for error message
38 * @args: #va_list of parameters for the message format
40 * Creates a new #GError with the given @domain and @code,
41 * and a message formatted with @format.
43 * Returns: a new #GError
48 g_error_new_valist (GQuark domain,
55 error = g_slice_new (GError);
57 error->domain = domain;
59 error->message = g_strdup_vprintf (format, args);
66 * @domain: error domain
68 * @format: printf()-style format for error message
69 * @Varargs: parameters for message format
71 * Creates a new #GError with the given @domain and @code,
72 * and a message formatted with @format.
74 * Return value: a new #GError
77 g_error_new (GQuark domain,
85 g_return_val_if_fail (format != NULL, NULL);
86 g_return_val_if_fail (domain != 0, NULL);
88 va_start (args, format);
89 error = g_error_new_valist (domain, code, format, args);
96 * g_error_new_literal:
97 * @domain: error domain
99 * @message: error message
101 * Creates a new #GError; unlike g_error_new(), @message is
102 * not a printf()-style format string. Use this function if
103 * @message contains text you don't have control over,
104 * that could include printf() escape sequences.
106 * Return value: a new #GError
109 g_error_new_literal (GQuark domain,
111 const gchar *message)
115 g_return_val_if_fail (message != NULL, NULL);
116 g_return_val_if_fail (domain != 0, NULL);
118 err = g_slice_new (GError);
120 err->domain = domain;
122 err->message = g_strdup (message);
131 * Frees a #GError and associated resources.
134 g_error_free (GError *error)
136 g_return_if_fail (error != NULL);
138 g_free (error->message);
140 g_slice_free (GError, error);
147 * Makes a copy of @error.
149 * Return value: a new #GError
152 g_error_copy (const GError *error)
156 g_return_val_if_fail (error != NULL, NULL);
158 copy = g_slice_new (GError);
162 copy->message = g_strdup (error->message);
169 * @error: a #GError or %NULL
170 * @domain: an error domain
171 * @code: an error code
173 * Returns %TRUE if @error matches @domain and @code, %FALSE
174 * otherwise. In particular, when @error is %NULL, %FALSE will
177 * Return value: whether @error has @domain and @code
180 g_error_matches (const GError *error,
185 error->domain == domain &&
189 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
190 "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
191 "The overwriting error message was: %s"
195 * @err: a return location for a #GError, or %NULL
196 * @domain: error domain
198 * @format: printf()-style format
199 * @Varargs: args for @format
201 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
202 * must be %NULL. A new #GError is created and assigned to *@err.
205 g_set_error (GError **err,
218 va_start (args, format);
219 new = g_error_new_valist (domain, code, format, args);
225 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
229 * g_set_error_literal:
230 * @err: a return location for a #GError, or %NULL
231 * @domain: error domain
233 * @message: error message
235 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
236 * must be %NULL. A new #GError is created and assigned to *@err.
237 * Unlike g_set_error(), @message is not a printf()-style format string.
238 * Use this function if @message contains text you don't have control over,
239 * that could include printf() escape sequences.
244 g_set_error_literal (GError **err,
247 const gchar *message)
254 new = g_error_new_literal (domain, code, message);
258 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
263 * @dest: error return location
264 * @src: error to move into the return location
266 * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
267 * The error variable @dest points to must be %NULL.
270 g_propagate_error (GError **dest,
273 g_return_if_fail (src != NULL);
284 g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
292 * @err: a #GError return location
294 * If @err is %NULL, does nothing. If @err is non-%NULL,
295 * calls g_error_free() on *@err and sets *@err to %NULL.
298 g_clear_error (GError **err)
308 g_error_add_prefix (gchar **string,
315 prefix = g_strdup_vprintf (format, ap);
317 *string = g_strconcat (prefix, oldstring, NULL);
324 * @err: a return location for a #GError, or %NULL
325 * @format: printf()-style format string
326 * @...: arguments to @format
328 * Formats a string according to @format and
329 * prefix it to an existing error message. If
330 * @err is %NULL (ie: no error variable) then do
333 * If *@err is %NULL (ie: an error variable is
334 * present but there is no error condition) then
335 * also do nothing. Whether or not it makes
336 * sense to take advantage of this feature is up
342 g_prefix_error (GError **err,
350 va_start (ap, format);
351 g_error_add_prefix (&(*err)->message, format, ap);
357 * g_propagate_prefixed_error:
358 * @dest: error return location
359 * @src: error to move into the return location
360 * @format: printf()-style format string
361 * @...: arguments to @format
363 * If @dest is %NULL, free @src; otherwise,
364 * moves @src into *@dest. *@dest must be %NULL.
365 * After the move, add a prefix as with
371 g_propagate_prefixed_error (GError **dest,
376 g_propagate_error (dest, src);
382 va_start (ap, format);
383 g_error_add_prefix (&(*dest)->message, format, ap);
388 #define __G_ERROR_C__
389 #include "galiasdef.c"