2 * "$Id: error.c 6274 2007-02-13 21:05:28Z mike $"
4 * Raster error handling for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * _cupsRasterAddError() - Add an error message to the error buffer.
29 * _cupsRasterClearError() - Clear the error buffer.
30 * cupsRasterErrorString() - Return the last error from a raster function.
31 * get_error_buffer() - Return a pointer to thread local storage.
32 * raster_init() - Initialize error buffer once.
33 * raster_destructor() - Free memory allocated by get_error_buffer().
37 * Include necessary headers...
40 #include "image-private.h"
48 typedef struct _cups_raster_error_s
/**** Error buffer structure ****/
50 char *start
, /* Start of buffer */
51 *current
, /* Current position in buffer */
52 *end
; /* End of buffer */
53 } _cups_raster_error_t
;
60 static _cups_raster_error_t
*get_error_buffer(void);
64 * '_cupsRasterAddError()' - Add an error message to the error buffer.
68 _cupsRasterAddError(const char *f
, /* I - Printf-style error message */
69 ...) /* I - Additional arguments as needed */
71 _cups_raster_error_t
*buf
= get_error_buffer();
73 va_list ap
; /* Pointer to additional arguments */
74 char s
[2048]; /* Message string */
75 size_t bytes
; /* Bytes in message string */
79 bytes
= vsnprintf(s
, sizeof(s
), f
, ap
);
87 if (bytes
>= sizeof(s
))
90 if (bytes
> (buf
->end
- buf
->current
))
93 * Allocate more memory...
96 char *temp
; /* New buffer */
97 size_t size
; /* Size of buffer */
100 size
= buf
->end
- buf
->start
+ 2 * bytes
+ 1024;
103 temp
= realloc(buf
->start
, size
);
114 buf
->end
= temp
+ size
;
115 buf
->current
= temp
+ (buf
->current
- buf
->start
);
120 * Append the message to the end of the current string...
123 memcpy(buf
->current
, s
, bytes
);
124 buf
->current
+= bytes
- 1;
129 * '_cupsRasterClearError()' - Clear the error buffer.
133 _cupsRasterClearError(void)
135 _cups_raster_error_t
*buf
= get_error_buffer();
139 buf
->current
= buf
->start
;
142 *(buf
->start
) = '\0';
147 * 'cupsRasterErrorString()' - Return the last error from a raster function.
149 * If there are no recent errors, NULL is returned.
154 const char * /* O - Last error */
155 cupsRasterErrorString(void)
157 _cups_raster_error_t
*buf
= get_error_buffer();
161 if (buf
->current
== buf
->start
)
168 #ifdef HAVE_PTHREAD_H
170 * Implement per-thread globals...
173 # include <pthread.h>
180 static pthread_key_t raster_key
= -1;
181 /* Thread local storage key */
182 static pthread_once_t raster_key_once
= PTHREAD_ONCE_INIT
;
183 /* One-time initialization object */
190 static void raster_init(void);
191 static void raster_destructor(void *value
);
195 * 'get_error_buffer()' - Return a pointer to thread local storage.
198 _cups_raster_error_t
* /* O - Pointer to error buffer */
199 get_error_buffer(void)
201 _cups_raster_error_t
*buf
; /* Pointer to error buffer */
205 * Initialize the global data exactly once...
208 DEBUG_printf(("get_error_buffer(): raster_key_once=%d\n",
211 pthread_once(&raster_key_once
, raster_init
);
214 * See if we have allocated the data yet...
217 if ((buf
= (_cups_raster_error_t
*)pthread_getspecific(raster_key
))
220 DEBUG_puts("get_error_buffer: allocating memory for thread...");
223 * No, allocate memory as set the pointer for the key...
226 buf
= calloc(1, sizeof(_cups_raster_error_t
));
227 pthread_setspecific(raster_key
, buf
);
229 DEBUG_printf((" buf=%p\n", buf
));
233 * Return the pointer to the data...
241 * 'raster_init()' - Initialize error buffer once.
247 pthread_key_create(&raster_key
, raster_destructor
);
249 DEBUG_printf(("raster_init(): raster_key=%x(%u)\n", raster_key
,
255 * 'raster_destructor()' - Free memory allocated by get_error_buffer().
259 raster_destructor(void *value
) /* I - Data to free */
261 _cups_raster_error_t
*buf
= (_cups_raster_error_t
*)value
;
265 DEBUG_printf(("raster_destructor(value=%p)\n", value
));
276 * Implement static globals...
280 * 'get_error_buffer()' - Return a pointer to thread local storage.
283 _cups_raster_error_t
* /* O - Pointer to error buffer */
284 get_error_buffer(void)
286 static _cups_raster_error_t buf
= { 0, 0, 0 };
292 #endif /* HAVE_PTHREAD_H */
296 * End of "$Id: error.c 6274 2007-02-13 21:05:28Z mike $".