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 #define _GNU_SOURCE /* need for vasprintf */
32 #include <hkl/hkl-error.h>
35 * hkl_error_new_valist:
36 * @format: printf()-style format for error message
37 * @args: #va_list of parameters for the message format
39 * Creates a new #HklError with the given message
40 * formatted with @format.
42 * Returns: a new #HklError
46 HklError
* hkl_error_new_valist (const char *format
, va_list args
)
50 error
= HKL_MALLOC(HklError
);
52 vasprintf (&error
->message
, format
, args
);
59 * @format: printf()-style format for error message
60 * @Varargs: parameters for message format
62 * Creates a new #HklError with the given,
63 * and a message formatted with @format.
65 * Return value: a new #HklError
67 HklError
* hkl_error_new (const char *format
, ...)
75 va_start (args
, format
);
76 error
= hkl_error_new_valist (format
, args
);
83 * hkl_error_new_literal:
84 * @message: error message
86 * Creates a new #HklError; unlike hkl_error_new(), @message is
87 * not a printf()-style format string. Use this function if
88 * @message contains text you don't have control over,
89 * that could include printf() escape sequences.
91 * Return value: a new #HklError
93 HklError
* hkl_error_new_literal (const char *message
)
100 err
= HKL_MALLOC (HklError
);
102 err
->message
= strdup (message
);
109 * @error: a #HklError
111 * Frees a #HklError and associated resources.
113 void hkl_error_free (HklError
*error
)
118 free (error
->message
);
124 * @error: a #HklError
126 * Makes a copy of @error.
128 * Return value: a new #HklError
130 HklError
* hkl_error_new_copy (const HklError
*error
)
138 copy
= HKL_MALLOC (HklError
);
142 copy
->message
= strdup (error
->message
);
147 #define ERROR_OVERWRITTEN_WARNING "HklError set over the top of a previous HklError or uninitialized memory.\n" \
148 "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
149 "The overwriting error message was: %s"
153 * @err: a return location for a #HklError, or %NULL
154 * @format: printf()-style format
155 * @Varargs: args for @format
157 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
158 * must be %NULL. A new #HklError is created and assigned to *@err.
160 void hkl_error_set (HklError
**err
, const char *format
, ...)
168 va_start (args
, format
);
169 new = hkl_error_new_valist (format
, args
);
175 fprintf (stderr
, ERROR_OVERWRITTEN_WARNING
, new->message
);
179 * hkl_error_set_literal:
180 * @err: a return location for a #HklError, or %NULL
181 * @message: error message
183 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
184 * must be %NULL. A new #HklError is created and assigned to *@err.
185 * Unlike hkl_set_error(), @message is not a printf()-style format string.
186 * Use this function if @message contains text you don't have control over,
187 * that could include printf() escape sequences.
191 void hkl_error_set_literal (HklError
**err
, const char *message
)
198 new = hkl_error_new_literal (message
);
202 fprintf (stderr
, ERROR_OVERWRITTEN_WARNING
, new->message
);
206 * hkl_propagate_error:
207 * @dest: error return location
208 * @src: error to move into the return location
210 * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
211 * The error variable @dest points to must be %NULL.
213 void hkl_error_propagate (HklError
**dest
, HklError
*src
)
220 hkl_error_free (src
);
224 fprintf (stderr
, ERROR_OVERWRITTEN_WARNING
, src
->message
);
232 * @err: a #HklError return location
234 * If @err is %NULL, does nothing. If @err is non-%NULL,
235 * calls hkl_error_free() on *@err and sets *@err to %NULL.
237 void hkl_error_clear (HklError
**err
)
240 hkl_error_free (*err
);
245 static void hkl_error_add_prefix (char **string
, const char *format
, va_list ap
)
251 size_t len_oldstring
;
253 len_prefix
= vasprintf (&prefix
, format
, ap
);
255 len_oldstring
= strlen(*string
);
257 len
= len_prefix
+ len_oldstring
;
258 *string
= malloc (len
*sizeof (char) + 1);
260 strncpy_s (*string
, len_prefix
+ 1, prefix
, len_prefix
);
261 strncat_s (*string
, len
+ 1, oldstring
, len_oldstring
);
263 *string
= strncpy (*string
, prefix
, len_prefix
+ 1);
264 *string
= strncat (*string
, oldstring
, len_oldstring
);
272 * @err: a return location for a #HklError, or %NULL
273 * @format: printf()-style format string
274 * @...: arguments to @format
276 * Formats a string according to @format and
277 * prefix it to an existing error message. If
278 * @err is %NULL (ie: no error variable) then do
281 * If *@err is %NULL (ie: an error variable is
282 * present but there is no error condition) then
283 * also do nothing. Whether or not it makes
284 * sense to take advantage of this feature is up
289 void hkl_error_prefix (HklError
**err
, const char *format
, ...)
294 va_start (ap
, format
);
295 hkl_error_add_prefix (&(*err
)->message
, format
, ap
);
301 * hkl_propagate_prefixed_error:
302 * @dest: error return location
303 * @src: error to move into the return location
304 * @format: printf()-style format string
305 * @...: arguments to @format
307 * If @dest is %NULL, free @src; otherwise,
308 * moves @src into *@dest. *@dest must be %NULL.
309 * After the move, add a prefix as with
310 * hkl_prefix_error().
314 void hkl_error_propagate_prefixed (HklError
**dest
, HklError
*src
, const char *format
, ...)
316 hkl_error_propagate (dest
, src
);
321 va_start (ap
, format
);
322 hkl_error_add_prefix (&(*dest
)->message
, format
, ap
);