Debian Codegraph @

converter/other/fiasco/lib/error.c

raw
1 /*
2 * error.c: Error handling
3 *
4 * Written by: Stefan Frank
5 * Ullrich Hafner
6 *
7 * Credits: Modelled after variable argument routines from Jef
8 * Poskanzer's pbmplus package.
9 *
10 * This file is part of FIASCO (Fractal Image And Sequence COdec)
11 * Copyright (C) 1994-2000 Ullrich Hafner
12 */
13
14 #define _ERROR_C
15
16 #include "config.h"
17
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <errno.h>
21
22 #include <stdarg.h>
23 #define VA_START(args, lastarg) va_start(args, lastarg)
24 #include <string.h>
25
26 #if HAVE_SETJMP_H
27 # include <setjmp.h>
28 #endif /* HAVE_SETJMP_H */
29
30 #include "types.h"
31 #include "macros.h"
32 #include "error.h"
33
34 #include "misc.h"
35 #include "fiasco.h"
36
37 /*****************************************************************************
38
39 local variables
40
41 *****************************************************************************/
42
43 static fiasco_verbosity_e verboselevel = FIASCO_SOME_VERBOSITY;
44 static char *error_message = NULL;
45
46 #if HAVE_SETJMP_H
47 jmp_buf env;
48 #endif /* HAVE_SETJMP_H */
49
50 /*****************************************************************************
51
52 public code
53
54 *****************************************************************************/
55
56 void
57 set_error(const char * const format, ...) {
58 /*----------------------------------------------------------------------------
59 Set error text to given string.
60 -----------------------------------------------------------------------------*/
61 va_list args;
62 unsigned len;
63 bool error;
64 const char * str;
65
66 VA_START (args, format);
67
68 /* Compute how long the error text will be: 'len' */
69
70 for (len = strlen(format), str = &format[0], error = false;
71 *str && !error; ) {
72
73 str = strchr(str, '%');
74
75 if (*str) {
76 ++str; /* Move past % */
77 if (*str == 's') {
78 char * const vstring = va_arg (args, char *);
79 len += strlen(vstring);
80 } else if (*str == 'd') {
81 (void)va_arg(args, int);
82 len += 10;
83 } else if (*str == 'c') {
84 (void)va_arg(args, int);
85 len += 1;
86 } else
87 error = true;
88 if (!error)
89 ++str;
90 }
91 }
92 va_end(args);
93
94 if (!error) {
95 VA_START(args, format);
96
97 if (error_message)
98 Free(error_message);
99 error_message = Calloc(len, sizeof (char));
100
101 vsprintf(error_message, format, args);
102
103 va_end(args);
104 }
105 }
106
107
108
109 void
110 error(const char * const format, ...) {
111 /*----------------------------------------------------------------------------
112 Set error text to given string.
113 -----------------------------------------------------------------------------*/
114 va_list args;
115 unsigned len;
116 const char * str;
117
118 len = 0; /* initial value */
119 str = &format[0]; /* initial value */
120
121 VA_START (args, format);
122
123 len = strlen (format);
124 while ((str = strchr (str, '%'))) {
125 ++str;
126 if (*str == 's') {
127 char * const vstring = va_arg (args, char *);
128 len += strlen(vstring);
129 } else if (*str == 'd') {
130 (void)va_arg(args, int);
131 len += 10;
132 } else if (*str == 'c') {
133 (void)va_arg(args, int);
134 len += 1;
135 } else {
136 #if HAVE_SETJMP_H
137 longjmp(env, 1);
138 #else
139 exit(1);
140 #endif
141 };
142
143 ++str;
144 }
145 va_end(args);
146
147 VA_START(args, format);
148
149 if (error_message)
150 Free(error_message);
151 error_message = Calloc(len, sizeof (char));
152
153 vsprintf(error_message, format, args);
154
155 va_end(args);
156
157 #if HAVE_SETJMP_H
158 longjmp(env, 1);
159 #else
160 exit(1);
161 #endif
162 }
163
164
165
166 const char *
167 fiasco_get_error_message(void) {
168 /*----------------------------------------------------------------------------
169 Last error message of FIASCO library.
170 -----------------------------------------------------------------------------*/
171 return error_message ? error_message : "";
172 }
173
174
175
176 const char *
177 get_system_error(void) {
178 return strerror(errno);
179 }
180
181
182
183 void
184 file_error(const char * const filename) {
185 /*----------------------------------------------------------------------------
186 Print file error message and exit.
187 -----------------------------------------------------------------------------*/
188 error("File `%s': I/O Error - %s.", filename, get_system_error ());
189 }
190
191
192
193 void
194 warning(const char * const format, ...) {
195 /*----------------------------------------------------------------------------
196 Issue a warning.
197 -----------------------------------------------------------------------------*/
198 va_list args;
199
200 VA_START (args, format);
201
202 if (verboselevel == FIASCO_NO_VERBOSITY) {
203 /* User doesn't want warnings */
204 } else {
205 fprintf (stderr, "Warning: ");
206 vfprintf (stderr, format, args);
207 fputc ('\n', stderr);
208 }
209 va_end (args);
210 }
211
212
213
214 void
215 message(const char * const format, ...) {
216 /*----------------------------------------------------------------------------
217 Print a message to Standard Error
218 -----------------------------------------------------------------------------*/
219 va_list args;
220
221 VA_START (args, format);
222
223 if (verboselevel == FIASCO_NO_VERBOSITY) {
224 /* User doesn't want messages */
225 } else {
226 vfprintf (stderr, format, args);
227 fputc ('\n', stderr);
228 }
229 va_end (args);
230 }
231
232
233
234 void
235 debug_message(const char * const format, ...) {
236 /*----------------------------------------------------------------------------
237 Print a message to Standard Error if debug messages are enabled.
238 -----------------------------------------------------------------------------*/
239 va_list args;
240
241 VA_START (args, format);
242
243 if (verboselevel >= FIASCO_ULTIMATE_VERBOSITY) {
244 fprintf (stderr, "*** ");
245 vfprintf (stderr, format, args);
246 fputc ('\n', stderr);
247 }
248 va_end (args);
249 }
250
251
252
253 void
254 info(const char * const format, ...) {
255 /*----------------------------------------------------------------------------
256 Print a message to stderr. Do not append a newline.
257 -----------------------------------------------------------------------------*/
258 va_list args;
259
260 VA_START (args, format);
261
262 if (verboselevel == FIASCO_NO_VERBOSITY) {
263 /* User doesn't want informational messages */
264 } else {
265 vfprintf (stderr, format, args);
266 fflush (stderr);
267 }
268 va_end (args);
269 }
270
271
272
273 void
274 fiasco_set_verbosity(fiasco_verbosity_e const level) {
275 verboselevel = level;
276 }
277
278
279
280 fiasco_verbosity_e
281 fiasco_get_verbosity(void) {
282 return verboselevel;
283 }
284
285
286