add a few scripts to clean the files and indent using emacs.
[hkl.git] / hkl / hkl-error.c
blob7e0fd3f1910a3f769e6802c8a4e77aa455b651df
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 */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <hkl/hkl-error.h>
34 /**
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
44 * Since: 2.22
46 HklError* hkl_error_new_valist (const char *format, va_list args)
48 HklError *error;
50 error = HKL_MALLOC(HklError);
52 vasprintf (&error->message, format, args);
54 return error;
57 /**
58 * hkl_error_new:
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, ...)
69 HklError* error;
70 va_list args;
72 if (!format)
73 return NULL;
75 va_start (args, format);
76 error = hkl_error_new_valist (format, args);
77 va_end (args);
79 return error;
82 /**
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
92 **/
93 HklError* hkl_error_new_literal (const char *message)
95 HklError* err;
97 if(!message)
98 return NULL;
100 err = HKL_MALLOC (HklError);
102 err->message = strdup (message);
104 return err;
108 * hkl_error_free:
109 * @error: a #HklError
111 * Frees a #HklError and associated resources.
113 void hkl_error_free (HklError *error)
115 if (!error)
116 return;
118 free (error->message);
119 free (error);
123 * hkl_error_copy:
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)
132 HklError *copy;
134 if(!error)
135 return NULL;
138 copy = HKL_MALLOC (HklError);
140 *copy = *error;
142 copy->message = strdup (error->message);
144 return copy;
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"
152 * hkl_error_set:
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, ...)
162 HklError *new;
163 va_list args;
165 if (err == NULL)
166 return;
168 va_start (args, format);
169 new = hkl_error_new_valist (format, args);
170 va_end (args);
172 if (*err == NULL)
173 *err = new;
174 else
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.
189 * Since: 2.18
191 void hkl_error_set_literal (HklError **err, const char *message)
193 HklError *new;
195 if (err == NULL)
196 return;
198 new = hkl_error_new_literal (message);
199 if (*err == NULL)
200 *err = new;
201 else
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)
215 if(!src)
216 return;
218 if (dest == NULL){
219 if (src)
220 hkl_error_free (src);
221 return;
222 }else{
223 if (*dest != NULL)
224 fprintf (stderr, ERROR_OVERWRITTEN_WARNING, src->message);
225 else
226 *dest = src;
231 * hkl_clear_error:
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)
239 if (err && *err){
240 hkl_error_free (*err);
241 *err = NULL;
245 static void hkl_error_add_prefix (char **string, const char *format, va_list ap)
247 char *oldstring;
248 char *prefix;
249 size_t len;
250 size_t len_prefix;
251 size_t len_oldstring;
253 len_prefix = vasprintf (&prefix, format, ap);
254 oldstring = *string;
255 len_oldstring = strlen(*string);
257 len = len_prefix + len_oldstring;
258 *string = malloc (len *sizeof (char) + 1);
259 #if _MSC_VER
260 strncpy_s (*string, len_prefix + 1, prefix, len_prefix);
261 strncat_s (*string, len + 1, oldstring, len_oldstring);
262 #else
263 *string = strncpy (*string, prefix, len_prefix + 1);
264 *string = strncat (*string, oldstring, len_oldstring);
265 #endif
266 free (oldstring);
267 free (prefix);
271 * hkl_prefix_error:
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
279 * nothing.
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
285 * to you.
287 * Since: 2.16
289 void hkl_error_prefix (HklError **err, const char *format, ...)
291 if (err && *err){
292 va_list ap;
294 va_start (ap, format);
295 hkl_error_add_prefix (&(*err)->message, format, ap);
296 va_end (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().
312 * Since: 2.16
314 void hkl_error_propagate_prefixed (HklError **dest, HklError *src, const char *format, ...)
316 hkl_error_propagate (dest, src);
318 if (dest && *dest){
319 va_list ap;
321 va_start (ap, format);
322 hkl_error_add_prefix (&(*dest)->message, format, ap);
323 va_end (ap);