Added function g_propagte_error to hand over local errors to the calling
[platform/upstream/glib.git] / 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 GError*
47 g_error_new (GQuark       domain,
48              gint         code,
49              const gchar *format,
50              ...)
51 {
52   GError* error;
53   va_list args;
54
55   g_return_val_if_fail (format != NULL, NULL);
56   g_return_val_if_fail (domain != 0, NULL);
57
58   va_start (args, format);
59   error = g_error_new_valist (domain, code, format, args);
60   va_end (args);
61
62   return error;
63 }
64
65 GError*
66 g_error_new_literal (GQuark         domain,
67                      gint           code,
68                      const gchar   *message)
69 {
70   GError* err;
71
72   g_return_val_if_fail (message != NULL, NULL);
73   g_return_val_if_fail (domain != 0, NULL);
74
75   err = g_new (GError, 1);
76
77   err->domain = domain;
78   err->code = code;
79   err->message = g_strdup (message);
80   
81   return err;
82 }
83
84 void
85 g_error_free (GError *error)
86 {
87   g_return_if_fail (error != NULL);  
88
89   g_free (error->message);
90
91   g_free (error);
92 }
93
94 GError*
95 g_error_copy (const GError *error)
96 {
97   GError *copy;
98   
99   g_return_val_if_fail (error != NULL, NULL);
100
101   copy = g_new (GError, 1);
102
103   *copy = *error;
104
105   copy->message = g_strdup (error->message);
106
107   return copy;
108 }
109
110 gboolean
111 g_error_matches (const GError *error,
112                  GQuark        domain,
113                  gint          code)
114 {
115   return error &&
116     error->domain == domain &&
117     error->code == code;
118 }
119
120 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
121                "This indicates a bug in someone's code. You must ensure an error is NULL before it's set."
122
123 void
124 g_set_error (GError      **err,
125              GQuark        domain,
126              gint          code,
127              const gchar  *format,
128              ...)
129 {
130   va_list args;
131
132   if (err == NULL)
133     return;
134
135   if (*err != NULL)
136     g_warning (ERROR_OVERWRITTEN_WARNING);
137   
138   va_start (args, format);
139   *err = g_error_new_valist (domain, code, format, args);
140   va_end (args);
141 }
142
143 void    
144 g_propagate_error (GError       **dest,
145                    GError        *src)
146 {
147   g_return_if_fail (src != NULL);
148
149   if (dest == NULL)
150     return;
151   
152   if (*dest != NULL)
153     g_warning (ERROR_OVERWRITTEN_WARNING);
154  
155   *dest = src;
156 }
157
158 void
159 g_clear_error (GError **err)
160 {
161   if (err && *err)
162     {
163       g_error_free (*err);
164       *err = NULL;
165     }
166 }