/src/xpdf-4.05/xpdf/Error.cc
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // Error.cc |
4 | | // |
5 | | // Copyright 1996-2013 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include <stdio.h> |
12 | | #include <stddef.h> |
13 | | #include <stdarg.h> |
14 | | #include "gmempp.h" |
15 | | #include "GString.h" |
16 | | #include "GlobalParams.h" |
17 | | #include "Error.h" |
18 | | |
19 | | const char *errorCategoryNames[] = { |
20 | | "Syntax Warning", |
21 | | "Syntax Error", |
22 | | "Config Error", |
23 | | "Command Line Error", |
24 | | "I/O Error", |
25 | | "Permission Error", |
26 | | "Unimplemented Feature", |
27 | | "Internal Error" |
28 | | }; |
29 | | |
30 | | static void (*errorCbk)(void *data, ErrorCategory category, |
31 | | int pos, char *msg) = NULL; |
32 | | static void *errorCbkData = NULL; |
33 | | |
34 | | void setErrorCallback(void (*cbk)(void *data, ErrorCategory category, |
35 | | int pos, char *msg), |
36 | 0 | void *data) { |
37 | 0 | errorCbk = cbk; |
38 | 0 | errorCbkData = data; |
39 | 0 | } |
40 | | |
41 | 0 | void *getErrorCallbackData() { |
42 | 0 | return errorCbkData; |
43 | 0 | } |
44 | | |
45 | | void CDECL error(ErrorCategory category, GFileOffset pos, |
46 | 55.5M | const char *msg, ...) { |
47 | 55.5M | va_list args; |
48 | 55.5M | GString *s, *sanitized; |
49 | 55.5M | char c; |
50 | 55.5M | int i; |
51 | | |
52 | | // NB: this can be called before the globalParams object is created |
53 | 55.5M | if (!errorCbk && globalParams && globalParams->getErrQuiet()) { |
54 | 55.5M | return; |
55 | 55.5M | } |
56 | 55.5M | va_start(args, msg); |
57 | 0 | s = GString::formatv(msg, args); |
58 | 0 | va_end(args); |
59 | | |
60 | | // remove non-printable characters, just in case they might cause |
61 | | // problems for the terminal program |
62 | 0 | sanitized = new GString(); |
63 | 0 | for (i = 0; i < s->getLength(); ++i) { |
64 | 0 | c = s->getChar(i); |
65 | 0 | if (c >= 0x20 && c <= 0x7e) { |
66 | 0 | sanitized->append(c); |
67 | 0 | } else { |
68 | 0 | sanitized->appendf("<{0:02x}>", c & 0xff); |
69 | 0 | } |
70 | 0 | } |
71 | |
|
72 | 0 | if (errorCbk) { |
73 | 0 | (*errorCbk)(errorCbkData, category, (int)pos, sanitized->getCString()); |
74 | 0 | } else { |
75 | 0 | fflush(stdout); |
76 | 0 | if (pos >= 0) { |
77 | 0 | fprintf(stderr, "%s (%d): %s\n", |
78 | 0 | errorCategoryNames[category], (int)pos, sanitized->getCString()); |
79 | 0 | } else { |
80 | 0 | fprintf(stderr, "%s: %s\n", |
81 | 0 | errorCategoryNames[category], sanitized->getCString()); |
82 | 0 | } |
83 | 0 | fflush(stderr); |
84 | 0 | } |
85 | |
|
86 | 0 | delete s; |
87 | 0 | delete sanitized; |
88 | 0 | } |