Free the src error if the dest location is NULL - I'm pretty sure that's
[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 "glib.h"
28
29 static GError* 
30 g_error_new_valist(GQuark         domain,
31                    gint           code,
32                    const gchar   *format,
33                    va_list        args)
34 {
35   GError *error;
36   
37   error = g_new (GError, 1);
38   
39   error->domain = domain;
40   error->code = code;
41   error->message = g_strdup_vprintf (format, args);
42   
43   return error;
44 }
45
46 /**
47  * g_error_new:
48  * @domain: error domain 
49  * @code: error code
50  * @format: printf()-style format for error message
51  * @Varargs: parameters for message format
52  * 
53  * Creates a new #GError with the given @domain and @code,
54  * and a message formatted with @format.
55  * 
56  * Return value: a new #GError
57  **/
58 GError*
59 g_error_new (GQuark       domain,
60              gint         code,
61              const gchar *format,
62              ...)
63 {
64   GError* error;
65   va_list args;
66
67   g_return_val_if_fail (format != NULL, NULL);
68   g_return_val_if_fail (domain != 0, NULL);
69
70   va_start (args, format);
71   error = g_error_new_valist (domain, code, format, args);
72   va_end (args);
73
74   return error;
75 }
76
77 /**
78  * g_error_new_literal:
79  * @domain: error domain
80  * @code: error code
81  * @message: error message
82  * 
83  * Creates a new #GError; unlike g_error_new(), @message is not
84  * a printf()-style format string. Use this function if @message
85  * contains text you don't have control over, that could include
86  * printf() escape sequences.
87  * 
88  * Return value: a new #GError
89  **/
90 GError*
91 g_error_new_literal (GQuark         domain,
92                      gint           code,
93                      const gchar   *message)
94 {
95   GError* err;
96
97   g_return_val_if_fail (message != NULL, NULL);
98   g_return_val_if_fail (domain != 0, NULL);
99
100   err = g_new (GError, 1);
101
102   err->domain = domain;
103   err->code = code;
104   err->message = g_strdup (message);
105   
106   return err;
107 }
108
109 /**
110  * g_error_free:
111  * @error: a #GError
112  *
113  * Frees a #GError and associated resources.
114  * 
115  **/
116 void
117 g_error_free (GError *error)
118 {
119   g_return_if_fail (error != NULL);  
120
121   g_free (error->message);
122
123   g_free (error);
124 }
125
126 /**
127  * g_error_copy:
128  * @error: a #GError
129  * 
130  * Makes a copy of @error.
131  * 
132  * Return value: a new #GError
133  **/
134 GError*
135 g_error_copy (const GError *error)
136 {
137   GError *copy;
138   
139   g_return_val_if_fail (error != NULL, NULL);
140
141   copy = g_new (GError, 1);
142
143   *copy = *error;
144
145   copy->message = g_strdup (error->message);
146
147   return copy;
148 }
149
150 /**
151  * g_error_matches:
152  * @error: a #GError
153  * @domain: an error domain
154  * @code: an error code
155  * 
156  * Returns TRUE if @error matches @domain and @code, FALSE
157  * otherwise.
158  * 
159  * Return value: whether @error has @domain and @code
160  **/
161 gboolean
162 g_error_matches (const GError *error,
163                  GQuark        domain,
164                  gint          code)
165 {
166   return error &&
167     error->domain == domain &&
168     error->code == code;
169 }
170
171 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
172                "This indicates a bug in someone's code. You must ensure an error is NULL before it's set."
173
174 /**
175  * g_set_error:
176  * @err: a return location for a #GError, or NULL
177  * @domain: error domain
178  * @code: error code 
179  * @format: printf()-style format
180  * @Varargs: args for @format 
181  * 
182  * Does nothing if @err is NULL; if @err is non-NULL, then *@err must
183  * be NULL. A new #GError is created and assigned to *@err.
184  **/
185 void
186 g_set_error (GError      **err,
187              GQuark        domain,
188              gint          code,
189              const gchar  *format,
190              ...)
191 {
192   va_list args;
193
194   if (err == NULL)
195     return;
196
197   if (*err != NULL)
198     g_warning (ERROR_OVERWRITTEN_WARNING);
199   
200   va_start (args, format);
201   *err = g_error_new_valist (domain, code, format, args);
202   va_end (args);
203 }
204
205 /**
206  * g_propagate_error:
207  * @dest: error return location
208  * @src: error to move into the return location
209  * 
210  * If @dest is NULL, free @src; otherwise,
211  * moves @src into *@dest. *@dest must be NULL.
212  **/
213 void    
214 g_propagate_error (GError       **dest,
215                    GError        *src)
216 {
217   g_return_if_fail (src != NULL);
218   
219   if (dest == NULL)
220     {
221       if (src)
222         g_error_free (src);
223       return;
224     }
225   else
226     {
227       if (*dest != NULL)
228         g_warning (ERROR_OVERWRITTEN_WARNING);
229       
230       *dest = src;
231     }
232 }
233
234 /**
235  * g_clear_error:
236  * @err: a #GError return location
237  * 
238  * If @err is NULL, does nothing. If @err is non-NULL,
239  * calls g_error_free() on *@err and sets *@err to NULL.
240  **/
241 void
242 g_clear_error (GError **err)
243 {
244   if (err && *err)
245     {
246       g_error_free (*err);
247       *err = NULL;
248     }
249 }