Minor markup fixes.
[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: <function>printf()</function>-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 <function>printf()</function>-style format string. Use this 
85  * function if @message contains text you don't have control over, 
86  * that could include <function>printf()</function> 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.\n" \
173                "The overwriting error message was: %s"
174
175 /**
176  * g_set_error:
177  * @err: a return location for a #GError, or %NULL
178  * @domain: error domain
179  * @code: error code 
180  * @format: <function>printf()</function>-style format
181  * @Varargs: args for @format 
182  * 
183  * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err must
184  * be %NULL. A new #GError is created and assigned to *@err.
185  **/
186 void
187 g_set_error (GError      **err,
188              GQuark        domain,
189              gint          code,
190              const gchar  *format,
191              ...)
192 {
193   GError *new;
194   
195   va_list args;
196
197   if (err == NULL)
198     return;
199   
200   va_start (args, format);
201   new = g_error_new_valist (domain, code, format, args);
202   va_end (args);
203
204   if (*err == NULL)
205     *err = new;
206   else
207     g_warning (ERROR_OVERWRITTEN_WARNING, new->message);    
208 }
209
210 /**
211  * g_propagate_error:
212  * @dest: error return location
213  * @src: error to move into the return location
214  * 
215  * If @dest is %NULL, free @src; otherwise,
216  * moves @src into *@dest. *@dest must be %NULL.
217  **/
218 void    
219 g_propagate_error (GError       **dest,
220                    GError        *src)
221 {
222   g_return_if_fail (src != NULL);
223   
224   if (dest == NULL)
225     {
226       if (src)
227         g_error_free (src);
228       return;
229     }
230   else
231     {
232       if (*dest != NULL)
233         g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
234       else
235         *dest = src;
236     }
237 }
238
239 /**
240  * g_clear_error:
241  * @err: a #GError return location
242  * 
243  * If @err is %NULL, does nothing. If @err is non-%NULL,
244  * calls g_error_free() on *@err and sets *@err to %NULL.
245  **/
246 void
247 g_clear_error (GError **err)
248 {
249   if (err && *err)
250     {
251       g_error_free (*err);
252       *err = NULL;
253     }
254 }