glib/: fully remove galias hacks
[platform/upstream/glib.git] / glib / gerror.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #include "config.h"
28
29 #include "glib.h"
30
31
32 /**
33  * g_error_new_valist:
34  * @domain: error domain
35  * @code: error code
36  * @format: printf()-style format for error message
37  * @args: #va_list of parameters for the message format
38  *
39  * Creates a new #GError with the given @domain and @code,
40  * and a message formatted with @format.
41  *
42  * Returns: a new #GError
43  *
44  * Since: 2.22
45  */
46 GError*
47 g_error_new_valist (GQuark       domain,
48                     gint         code,
49                     const gchar *format,
50                     va_list      args)
51 {
52   GError *error;
53
54   error = g_slice_new (GError);
55
56   error->domain = domain;
57   error->code = code;
58   error->message = g_strdup_vprintf (format, args);
59
60   return error;
61 }
62
63 /**
64  * g_error_new:
65  * @domain: error domain
66  * @code: error code
67  * @format: printf()-style format for error message
68  * @Varargs: parameters for message format
69  *
70  * Creates a new #GError with the given @domain and @code,
71  * and a message formatted with @format.
72  *
73  * Return value: a new #GError
74  */
75 GError*
76 g_error_new (GQuark       domain,
77              gint         code,
78              const gchar *format,
79              ...)
80 {
81   GError* error;
82   va_list args;
83
84   g_return_val_if_fail (format != NULL, NULL);
85   g_return_val_if_fail (domain != 0, NULL);
86
87   va_start (args, format);
88   error = g_error_new_valist (domain, code, format, args);
89   va_end (args);
90
91   return error;
92 }
93
94 /**
95  * g_error_new_literal:
96  * @domain: error domain
97  * @code: error code
98  * @message: error message
99  *
100  * Creates a new #GError; unlike g_error_new(), @message is
101  * not a printf()-style format string. Use this function if
102  * @message contains text you don't have control over,
103  * that could include printf() escape sequences.
104  *
105  * Return value: a new #GError
106  **/
107 GError*
108 g_error_new_literal (GQuark         domain,
109                      gint           code,
110                      const gchar   *message)
111 {
112   GError* err;
113
114   g_return_val_if_fail (message != NULL, NULL);
115   g_return_val_if_fail (domain != 0, NULL);
116
117   err = g_slice_new (GError);
118
119   err->domain = domain;
120   err->code = code;
121   err->message = g_strdup (message);
122
123   return err;
124 }
125
126 /**
127  * g_error_free:
128  * @error: a #GError
129  *
130  * Frees a #GError and associated resources.
131  */
132 void
133 g_error_free (GError *error)
134 {
135   g_return_if_fail (error != NULL);
136
137   g_free (error->message);
138
139   g_slice_free (GError, error);
140 }
141
142 /**
143  * g_error_copy:
144  * @error: a #GError
145  *
146  * Makes a copy of @error.
147  *
148  * Return value: a new #GError
149  */
150 GError*
151 g_error_copy (const GError *error)
152 {
153   GError *copy;
154  
155   g_return_val_if_fail (error != NULL, NULL);
156
157   copy = g_slice_new (GError);
158
159   *copy = *error;
160
161   copy->message = g_strdup (error->message);
162
163   return copy;
164 }
165
166 /**
167  * g_error_matches:
168  * @error: a #GError or %NULL
169  * @domain: an error domain
170  * @code: an error code
171  *
172  * Returns %TRUE if @error matches @domain and @code, %FALSE
173  * otherwise. In particular, when @error is %NULL, %FALSE will
174  * be returned.
175  *
176  * Return value: whether @error has @domain and @code
177  */
178 gboolean
179 g_error_matches (const GError *error,
180                  GQuark        domain,
181                  gint          code)
182 {
183   return error &&
184     error->domain == domain &&
185     error->code == code;
186 }
187
188 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
189                "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
190                "The overwriting error message was: %s"
191
192 /**
193  * g_set_error:
194  * @err: a return location for a #GError, or %NULL
195  * @domain: error domain
196  * @code: error code
197  * @format: printf()-style format
198  * @Varargs: args for @format
199  *
200  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
201  * must be %NULL. A new #GError is created and assigned to *@err.
202  */
203 void
204 g_set_error (GError      **err,
205              GQuark        domain,
206              gint          code,
207              const gchar  *format,
208              ...)
209 {
210   GError *new;
211
212   va_list args;
213
214   if (err == NULL)
215     return;
216
217   va_start (args, format);
218   new = g_error_new_valist (domain, code, format, args);
219   va_end (args);
220
221   if (*err == NULL)
222     *err = new;
223   else
224     g_warning (ERROR_OVERWRITTEN_WARNING, new->message); 
225 }
226
227 /**
228  * g_set_error_literal:
229  * @err: a return location for a #GError, or %NULL
230  * @domain: error domain
231  * @code: error code
232  * @message: error message
233  *
234  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
235  * must be %NULL. A new #GError is created and assigned to *@err.
236  * Unlike g_set_error(), @message is not a printf()-style format string.
237  * Use this function if @message contains text you don't have control over,
238  * that could include printf() escape sequences.
239  *
240  * Since: 2.18
241  */
242 void
243 g_set_error_literal (GError      **err,
244                      GQuark        domain,
245                      gint          code,
246                      const gchar  *message)
247 {
248   GError *new;
249
250   if (err == NULL)
251     return;
252
253   new = g_error_new_literal (domain, code, message);
254   if (*err == NULL)
255     *err = new;
256   else
257     g_warning (ERROR_OVERWRITTEN_WARNING, new->message); 
258 }
259
260 /**
261  * g_propagate_error:
262  * @dest: error return location
263  * @src: error to move into the return location
264  *
265  * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
266  * The error variable @dest points to must be %NULL.
267  */
268 void
269 g_propagate_error (GError **dest,
270                    GError  *src)
271 {
272   g_return_if_fail (src != NULL);
273  
274   if (dest == NULL)
275     {
276       if (src)
277         g_error_free (src);
278       return;
279     }
280   else
281     {
282       if (*dest != NULL)
283         g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
284       else
285         *dest = src;
286     }
287 }
288
289 /**
290  * g_clear_error:
291  * @err: a #GError return location
292  *
293  * If @err is %NULL, does nothing. If @err is non-%NULL,
294  * calls g_error_free() on *@err and sets *@err to %NULL.
295  */
296 void
297 g_clear_error (GError **err)
298 {
299   if (err && *err)
300     {
301       g_error_free (*err);
302       *err = NULL;
303     }
304 }
305
306 static void
307 g_error_add_prefix (gchar       **string,
308                     const gchar  *format,
309                     va_list       ap)
310 {
311   gchar *oldstring;
312   gchar *prefix;
313
314   prefix = g_strdup_vprintf (format, ap);
315   oldstring = *string;
316   *string = g_strconcat (prefix, oldstring, NULL);
317   g_free (oldstring);
318   g_free (prefix);
319 }
320
321 /**
322  * g_prefix_error:
323  * @err: a return location for a #GError, or %NULL
324  * @format: printf()-style format string
325  * @...: arguments to @format
326  *
327  * Formats a string according to @format and
328  * prefix it to an existing error message.  If
329  * @err is %NULL (ie: no error variable) then do
330  * nothing.
331  *
332  * If *@err is %NULL (ie: an error variable is
333  * present but there is no error condition) then
334  * also do nothing.  Whether or not it makes
335  * sense to take advantage of this feature is up
336  * to you.
337  *
338  * Since: 2.16
339  */
340 void
341 g_prefix_error (GError      **err,
342                 const gchar  *format,
343                 ...)
344 {
345   if (err && *err)
346     {
347       va_list ap;
348
349       va_start (ap, format);
350       g_error_add_prefix (&(*err)->message, format, ap);
351       va_end (ap);
352     }
353 }
354
355 /**
356  * g_propagate_prefixed_error:
357  * @dest: error return location
358  * @src: error to move into the return location
359  * @format: printf()-style format string
360  * @...: arguments to @format
361  *
362  * If @dest is %NULL, free @src; otherwise,
363  * moves @src into *@dest. *@dest must be %NULL.
364  * After the move, add a prefix as with
365  * g_prefix_error().
366  *
367  * Since: 2.16
368  **/
369 void
370 g_propagate_prefixed_error (GError      **dest,
371                             GError       *src,
372                             const gchar  *format,
373                             ...)
374 {
375   g_propagate_error (dest, src);
376
377   if (dest && *dest)
378     {
379       va_list ap;
380
381       va_start (ap, format);
382       g_error_add_prefix (&(*dest)->message, format, ap);
383       va_end (ap);
384     }
385 }