Minor doc fix
[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 #include "galias.h"
31
32
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
39  *
40  * Creates a new #GError with the given @domain and @code,
41  * and a message formatted with @format.
42  *
43  * Returns: a new #GError
44  *
45  * Since: 2.22
46  */
47 GError*
48 g_error_new_valist (GQuark       domain,
49                     gint         code,
50                     const gchar *format,
51                     va_list      args)
52 {
53   GError *error;
54
55   error = g_slice_new (GError);
56
57   error->domain = domain;
58   error->code = code;
59   error->message = g_strdup_vprintf (format, args);
60
61   return error;
62 }
63
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
70  *
71  * Creates a new #GError with the given @domain and @code,
72  * and a message formatted with @format.
73  *
74  * Return value: a new #GError
75  */
76 GError*
77 g_error_new (GQuark       domain,
78              gint         code,
79              const gchar *format,
80              ...)
81 {
82   GError* error;
83   va_list args;
84
85   g_return_val_if_fail (format != NULL, NULL);
86   g_return_val_if_fail (domain != 0, NULL);
87
88   va_start (args, format);
89   error = g_error_new_valist (domain, code, format, args);
90   va_end (args);
91
92   return error;
93 }
94
95 /**
96  * g_error_new_literal:
97  * @domain: error domain
98  * @code: error code
99  * @message: error message
100  *
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.
105  *
106  * Return value: a new #GError
107  **/
108 GError*
109 g_error_new_literal (GQuark         domain,
110                      gint           code,
111                      const gchar   *message)
112 {
113   GError* err;
114
115   g_return_val_if_fail (message != NULL, NULL);
116   g_return_val_if_fail (domain != 0, NULL);
117
118   err = g_slice_new (GError);
119
120   err->domain = domain;
121   err->code = code;
122   err->message = g_strdup (message);
123
124   return err;
125 }
126
127 /**
128  * g_error_free:
129  * @error: a #GError
130  *
131  * Frees a #GError and associated resources.
132  */
133 void
134 g_error_free (GError *error)
135 {
136   g_return_if_fail (error != NULL);
137
138   g_free (error->message);
139
140   g_slice_free (GError, error);
141 }
142
143 /**
144  * g_error_copy:
145  * @error: a #GError
146  *
147  * Makes a copy of @error.
148  *
149  * Return value: a new #GError
150  */
151 GError*
152 g_error_copy (const GError *error)
153 {
154   GError *copy;
155  
156   g_return_val_if_fail (error != NULL, NULL);
157
158   copy = g_slice_new (GError);
159
160   *copy = *error;
161
162   copy->message = g_strdup (error->message);
163
164   return copy;
165 }
166
167 /**
168  * g_error_matches:
169  * @error: a #GError
170  * @domain: an error domain
171  * @code: an error code
172  *
173  * Returns %TRUE if @error matches @domain and @code, %FALSE
174  * otherwise.
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 }
386
387 #define __G_ERROR_C__
388 #include "galiasdef.c"