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