Removed bashism in test for the pkg-config version.
[glib.git] / gerror.c
blobeff62588a85ec618a437041ad6f4b3c91f4f07bf
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "glib.h"
29 static GError*
30 g_error_new_valist(GQuark domain,
31 gint code,
32 const gchar *format,
33 va_list args)
35 GError *error;
37 error = g_new (GError, 1);
39 error->domain = domain;
40 error->code = code;
41 error->message = g_strdup_vprintf (format, args);
43 return error;
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
53 * Creates a new #GError with the given @domain and @code,
54 * and a message formatted with @format.
56 * Return value: a new #GError
57 **/
58 GError*
59 g_error_new (GQuark domain,
60 gint code,
61 const gchar *format,
62 ...)
64 GError* error;
65 va_list args;
67 g_return_val_if_fail (format != NULL, NULL);
68 g_return_val_if_fail (domain != 0, NULL);
70 va_start (args, format);
71 error = g_error_new_valist (domain, code, format, args);
72 va_end (args);
74 return error;
77 /**
78 * g_error_new_literal:
79 * @domain: error domain
80 * @code: error code
81 * @message: error message
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.
88 * Return value: a new #GError
89 **/
90 GError*
91 g_error_new_literal (GQuark domain,
92 gint code,
93 const gchar *message)
95 GError* err;
97 g_return_val_if_fail (message != NULL, NULL);
98 g_return_val_if_fail (domain != 0, NULL);
100 err = g_new (GError, 1);
102 err->domain = domain;
103 err->code = code;
104 err->message = g_strdup (message);
106 return err;
110 * g_error_free:
111 * @error: a #GError
113 * Frees a #GError and associated resources.
116 void
117 g_error_free (GError *error)
119 g_return_if_fail (error != NULL);
121 g_free (error->message);
123 g_free (error);
127 * g_error_copy:
128 * @error: a #GError
130 * Makes a copy of @error.
132 * Return value: a new #GError
134 GError*
135 g_error_copy (const GError *error)
137 GError *copy;
139 g_return_val_if_fail (error != NULL, NULL);
141 copy = g_new (GError, 1);
143 *copy = *error;
145 copy->message = g_strdup (error->message);
147 return copy;
151 * g_error_matches:
152 * @error: a #GError
153 * @domain: an error domain
154 * @code: an error code
156 * Returns TRUE if @error matches @domain and @code, FALSE
157 * otherwise.
159 * Return value: whether @error has @domain and @code
161 gboolean
162 g_error_matches (const GError *error,
163 GQuark domain,
164 gint code)
166 return error &&
167 error->domain == domain &&
168 error->code == code;
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"
176 * g_set_error:
177 * @err: a return location for a #GError, or NULL
178 * @domain: error domain
179 * @code: error code
180 * @format: printf()-style format
181 * @Varargs: args for @format
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.
186 void
187 g_set_error (GError **err,
188 GQuark domain,
189 gint code,
190 const gchar *format,
191 ...)
193 GError *new;
195 va_list args;
197 if (err == NULL)
198 return;
200 va_start (args, format);
201 new = g_error_new_valist (domain, code, format, args);
202 va_end (args);
204 if (*err == NULL)
205 *err = new;
206 else
207 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
211 * g_propagate_error:
212 * @dest: error return location
213 * @src: error to move into the return location
215 * If @dest is NULL, free @src; otherwise,
216 * moves @src into *@dest. *@dest must be NULL.
218 void
219 g_propagate_error (GError **dest,
220 GError *src)
222 g_return_if_fail (src != NULL);
224 if (dest == NULL)
226 if (src)
227 g_error_free (src);
228 return;
230 else
232 if (*dest != NULL)
233 g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
234 else
235 *dest = src;
240 * g_clear_error:
241 * @err: a #GError return location
243 * If @err is NULL, does nothing. If @err is non-NULL,
244 * calls g_error_free() on *@err and sets *@err to NULL.
246 void
247 g_clear_error (GError **err)
249 if (err && *err)
251 g_error_free (*err);
252 *err = NULL;