-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathraster-error.c
154 lines (115 loc) · 2.96 KB
/
raster-error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Raster error handling for CUPS.
//
// Copyright © 2022 by OpenPrinting.
// Copyright © 2007-2018 by Apple Inc.
// Copyright © 2007 by Easy Software Products.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Include necessary headers...
//
//#include "cups-private.h"
#include "raster-private.h"
#include <stdarg.h>
//#include "debug-internal.h"
//
// Local structures...
//
typedef struct _cups_raster_error_s /**** Error buffer structure ****/
{
char *start, /* Start of buffer */
*current, /* Current position in buffer */
*end; /* End of buffer */
} _cups_raster_error_t;
//
// Local functions...
//
static _cups_raster_error_t error_buffer;
//
// '_cupsRasterAddError()' - Add an error message to the error buffer.
//
void
_cupsRasterAddError(const char *f, // I - Printf-style error message
...) // I - Additional arguments as needed
{
// _cups_globals_t *cg = _cupsGlobals();
// Thread globals
// _cups_raster_error_t *buf = &cg->raster_error;
_cups_raster_error_t *buf = &error_buffer;
// Error buffer
va_list ap; // Pointer to additional arguments
char s[2048]; // Message string
ssize_t bytes; // Bytes in message string
DEBUG_printf(("_cupsRasterAddError(f=\"%s\", ...)", f));
va_start(ap, f);
bytes = vsnprintf(s, sizeof(s), f, ap);
va_end(ap);
if (bytes <= 0)
return;
DEBUG_printf(("1_cupsRasterAddError: %s", s));
bytes ++;
if ((size_t)bytes >= sizeof(s))
return;
if (bytes > (ssize_t)(buf->end - buf->current))
{
/*
* Allocate more memory...
*/
char *temp; // New buffer
size_t size; // Size of buffer
size = (size_t)(buf->end - buf->start + 2 * bytes + 1024);
if (buf->start)
temp = realloc(buf->start, size);
else
temp = malloc(size);
if (!temp)
return;
/*
* Update pointers...
*/
buf->end = temp + size;
buf->current = temp + (buf->current - buf->start);
buf->start = temp;
}
/*
* Append the message to the end of the current string...
*/
memcpy(buf->current, s, (size_t)bytes);
buf->current += bytes - 1;
}
//
// '_cupsRasterClearError()' - Clear the error buffer.
//
void
_cupsRasterClearError(void)
{
// _cups_globals_t *cg = _cupsGlobals();
// Thread globals
// _cups_raster_error_t *buf = &cg->raster_error;
_cups_raster_error_t *buf = &error_buffer;
// Error buffer
buf->current = buf->start;
if (buf->start)
*(buf->start) = '\0';
}
//
// 'cupsRasterErrorString()' - Return the last error from a raster function.
//
// If there are no recent errors, `NULL` is returned.
//
const char * // O - Last error
cupsRasterErrorString(void)
{
// _cups_globals_t *cg = _cupsGlobals();
// Thread globals
// _cups_raster_error_t *buf = &cg->raster_error;
_cups_raster_error_t *buf = &error_buffer;
// Error buffer
if (buf->current == buf->start)
return (NULL);
else
return (buf->start);
}