Add type_data_ref_U() and use it in g_type_class_ref()
[glib.git] / glib / gerror.c
blob4327f9acb885f0adcddceb5f7517c923fc125f63
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/.
27 #include "config.h"
29 #include "glib.h"
30 #include "galias.h"
33 /**
34 * g_error_new_valist:
35 * @domain: error domain
36 * @code: error code
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
45 * Since: 2.22
47 GError*
48 g_error_new_valist (GQuark domain,
49 gint code,
50 const gchar *format,
51 va_list args)
53 GError *error;
55 error = g_slice_new (GError);
57 error->domain = domain;
58 error->code = code;
59 error->message = g_strdup_vprintf (format, args);
61 return error;
64 /**
65 * g_error_new:
66 * @domain: error domain
67 * @code: error code
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
76 GError*
77 g_error_new (GQuark domain,
78 gint code,
79 const gchar *format,
80 ...)
82 GError* error;
83 va_list args;
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);
90 va_end (args);
92 return error;
95 /**
96 * g_error_new_literal:
97 * @domain: error domain
98 * @code: error code
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
108 GError*
109 g_error_new_literal (GQuark domain,
110 gint code,
111 const gchar *message)
113 GError* err;
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;
121 err->code = code;
122 err->message = g_strdup (message);
124 return err;
128 * g_error_free:
129 * @error: a #GError
131 * Frees a #GError and associated resources.
133 void
134 g_error_free (GError *error)
136 g_return_if_fail (error != NULL);
138 g_free (error->message);
140 g_slice_free (GError, error);
144 * g_error_copy:
145 * @error: a #GError
147 * Makes a copy of @error.
149 * Return value: a new #GError
151 GError*
152 g_error_copy (const GError *error)
154 GError *copy;
156 g_return_val_if_fail (error != NULL, NULL);
158 copy = g_slice_new (GError);
160 *copy = *error;
162 copy->message = g_strdup (error->message);
164 return copy;
168 * g_error_matches:
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
175 * be returned.
177 * Return value: whether @error has @domain and @code
179 gboolean
180 g_error_matches (const GError *error,
181 GQuark domain,
182 gint code)
184 return error &&
185 error->domain == domain &&
186 error->code == code;
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"
194 * g_set_error:
195 * @err: a return location for a #GError, or %NULL
196 * @domain: error domain
197 * @code: error code
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.
204 void
205 g_set_error (GError **err,
206 GQuark domain,
207 gint code,
208 const gchar *format,
209 ...)
211 GError *new;
213 va_list args;
215 if (err == NULL)
216 return;
218 va_start (args, format);
219 new = g_error_new_valist (domain, code, format, args);
220 va_end (args);
222 if (*err == NULL)
223 *err = new;
224 else
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
232 * @code: error code
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.
241 * Since: 2.18
243 void
244 g_set_error_literal (GError **err,
245 GQuark domain,
246 gint code,
247 const gchar *message)
249 GError *new;
251 if (err == NULL)
252 return;
254 new = g_error_new_literal (domain, code, message);
255 if (*err == NULL)
256 *err = new;
257 else
258 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
262 * g_propagate_error:
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.
269 void
270 g_propagate_error (GError **dest,
271 GError *src)
273 g_return_if_fail (src != NULL);
275 if (dest == NULL)
277 if (src)
278 g_error_free (src);
279 return;
281 else
283 if (*dest != NULL)
284 g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
285 else
286 *dest = src;
291 * g_clear_error:
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.
297 void
298 g_clear_error (GError **err)
300 if (err && *err)
302 g_error_free (*err);
303 *err = NULL;
307 static void
308 g_error_add_prefix (gchar **string,
309 const gchar *format,
310 va_list ap)
312 gchar *oldstring;
313 gchar *prefix;
315 prefix = g_strdup_vprintf (format, ap);
316 oldstring = *string;
317 *string = g_strconcat (prefix, oldstring, NULL);
318 g_free (oldstring);
319 g_free (prefix);
323 * g_prefix_error:
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
331 * nothing.
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
337 * to you.
339 * Since: 2.16
341 void
342 g_prefix_error (GError **err,
343 const gchar *format,
344 ...)
346 if (err && *err)
348 va_list ap;
350 va_start (ap, format);
351 g_error_add_prefix (&(*err)->message, format, ap);
352 va_end (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
366 * g_prefix_error().
368 * Since: 2.16
370 void
371 g_propagate_prefixed_error (GError **dest,
372 GError *src,
373 const gchar *format,
374 ...)
376 g_propagate_error (dest, src);
378 if (dest && *dest)
380 va_list ap;
382 va_start (ap, format);
383 g_error_add_prefix (&(*dest)->message, format, ap);
384 va_end (ap);
388 #define __G_ERROR_C__
389 #include "galiasdef.c"