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