applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[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 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 void
121 g_set_error (GError     **err,
122              GQuark       domain,
123              gint         code,
124              const gchar *format,
125              ...)
126 {
127   va_list args;
128
129   if (err == NULL)
130     return;
131
132   if (*err != NULL)
133     g_warning ("GError set over the top of a previous GError or uninitialized memory.\n"
134                "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.");
135   
136   va_start (args, format);
137   *err = g_error_new_valist (domain, code, format, args);
138   va_end (args);
139 }
140
141 void
142 g_clear_error (GError **err)
143 {
144   if (err && *err)
145     {
146       g_error_free (*err);
147       *err = NULL;
148     }
149 }