Make g_error_new_valist public
[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  * @var_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 not
102  * a printf()-style format string. Use this 
103  * function if @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  **/
134 void
135 g_error_free (GError *error)
136 {
137   g_return_if_fail (error != NULL);  
138
139   g_free (error->message);
140
141   g_slice_free (GError, error);
142 }
143
144 /**
145  * g_error_copy:
146  * @error: a #GError
147  * 
148  * Makes a copy of @error.
149  * 
150  * Return value: a new #GError
151  **/
152 GError*
153 g_error_copy (const GError *error)
154 {
155   GError *copy;
156   
157   g_return_val_if_fail (error != NULL, NULL);
158
159   copy = g_slice_new (GError);
160
161   *copy = *error;
162
163   copy->message = g_strdup (error->message);
164
165   return copy;
166 }
167
168 /**
169  * g_error_matches:
170  * @error: a #GError
171  * @domain: an error domain
172  * @code: an error code
173  * 
174  * Returns %TRUE if @error matches @domain and @code, %FALSE
175  * otherwise.
176  * 
177  * Return value: whether @error has @domain and @code
178  **/
179 gboolean
180 g_error_matches (const GError *error,
181                  GQuark        domain,
182                  gint          code)
183 {
184   return error &&
185     error->domain == domain &&
186     error->code == code;
187 }
188
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"
192
193 /**
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 
200  * 
201  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err must
202  * be %NULL. A new #GError is created and assigned to *@err.
203  **/
204 void
205 g_set_error (GError      **err,
206              GQuark        domain,
207              gint          code,
208              const gchar  *format,
209              ...)
210 {
211   GError *new;
212   
213   va_list args;
214
215   if (err == NULL)
216     return;
217   
218   va_start (args, format);
219   new = g_error_new_valist (domain, code, format, args);
220   va_end (args);
221
222   if (*err == NULL)
223     *err = new;
224   else
225     g_warning (ERROR_OVERWRITTEN_WARNING, new->message);    
226 }
227
228 /**
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
234  *
235  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err must
236  * 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.
240  *
241  * Since: 2.18
242  **/
243 void
244 g_set_error_literal (GError      **err,
245                      GQuark        domain,
246                      gint          code,
247                      const gchar  *message)
248 {
249   GError *new;
250   
251   if (err == NULL)
252     return;
253
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);    
259 }
260
261 /**
262  * g_propagate_error:
263  * @dest: error return location
264  * @src: error to move into the return location
265  *
266  * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
267  * The error variable @dest points to must be %NULL.
268  **/
269 void
270 g_propagate_error (GError       **dest,
271                    GError        *src)
272 {
273   g_return_if_fail (src != NULL);
274   
275   if (dest == NULL)
276     {
277       if (src)
278         g_error_free (src);
279       return;
280     }
281   else
282     {
283       if (*dest != NULL)
284         g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
285       else
286         *dest = src;
287     }
288 }
289
290 /**
291  * g_clear_error:
292  * @err: a #GError return location
293  * 
294  * If @err is %NULL, does nothing. If @err is non-%NULL,
295  * calls g_error_free() on *@err and sets *@err to %NULL.
296  **/
297 void
298 g_clear_error (GError **err)
299 {
300   if (err && *err)
301     {
302       g_error_free (*err);
303       *err = NULL;
304     }
305 }
306
307 static void
308 g_error_add_prefix (gchar       **string,
309                     const gchar  *format,
310                     va_list       ap)
311 {
312   gchar *oldstring;
313   gchar *prefix;
314
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);
320 }
321
322 /**
323  * g_prefix_error:
324  * @err: a return location for a #GError, or %NULL
325  * @format: printf()-style format string
326  * @...: arguments to @format
327  *
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.
332  *
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.
338  *
339  * Since: 2.16
340  **/
341 void
342 g_prefix_error (GError      **err,
343                 const gchar  *format,
344                 ...)
345 {
346   if (err && *err)
347     {
348       va_list ap;
349
350       va_start (ap, format);
351       g_error_add_prefix (&(*err)->message, format, ap);
352       va_end (ap);
353     }
354 }
355
356 /**
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
362  * 
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().
367  *
368  * Since: 2.16
369  **/
370 void
371 g_propagate_prefixed_error (GError      **dest,
372                             GError       *src,
373                             const gchar  *format,
374                             ...)
375 {
376   g_propagate_error (dest, src);
377
378   if (dest && *dest)
379     {
380       va_list ap;
381
382       va_start (ap, format);
383       g_error_add_prefix (&(*dest)->message, format, ap);
384       va_end (ap);
385     }
386 }
387
388 #define __G_ERROR_C__
389 #include "galiasdef.c"