]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jerror.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / jpeg6 / jerror.cpp
1 /*\r
2  * jerror.c\r
3  *\r
4  * Copyright (C) 1991-1994, Thomas G. Lane.\r
5  * This file is part of the Independent JPEG Group's software.\r
6  * For conditions of distribution and use, see the accompanying README file.\r
7  *\r
8  * This file contains simple error-reporting and trace-message routines.\r
9  * These are suitable for Unix-like systems and others where writing to\r
10  * stderr is the right thing to do.  Many applications will want to replace\r
11  * some or all of these routines.\r
12  *\r
13  * These routines are used by both the compression and decompression code.\r
14  */\r
15 \r
16 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */\r
17 #include "jinclude.h"\r
18 #include "radiant_jpeglib.h"\r
19 #include "jversion.h"\r
20 #include "jerror.h"\r
21 \r
22 #ifndef EXIT_FAILURE            /* define exit() codes if not provided */\r
23 #define EXIT_FAILURE  1\r
24 #endif\r
25 \r
26 \r
27 /*\r
28  * Create the message string table.\r
29  * We do this from the master message list in jerror.h by re-reading\r
30  * jerror.h with a suitable definition for macro JMESSAGE.\r
31  * The message table is made an external symbol just in case any applications\r
32  * want to refer to it directly.\r
33  */\r
34 \r
35 #ifdef NEED_SHORT_EXTERNAL_NAMES\r
36 #define jpeg_std_message_table  jMsgTable\r
37 #endif\r
38 \r
39 #define JMESSAGE(code,string)   string ,\r
40 \r
41 const char * const jpeg_std_message_table[] = {\r
42 #include "jerror.h"\r
43   NULL\r
44 };\r
45 \r
46 // Rad additions, longjmp out of the LoadJPGBuff\r
47 GLOBAL jmp_buf rad_loadfailed;\r
48 GLOBAL char rad_errormsg[JMSG_LENGTH_MAX];\r
49 \r
50 /*\r
51  * Error exit handler: must not return to caller.\r
52  *\r
53  * Applications may override this if they want to get control back after\r
54  * an error.  Typically one would longjmp somewhere instead of exiting.\r
55  * The setjmp buffer can be made a private field within an expanded error\r
56  * handler object.  Note that the info needed to generate an error message\r
57  * is stored in the error object, so you can generate the message now or\r
58  * later, at your convenience.\r
59  * You should make sure that the JPEG object is cleaned up (with jpeg_abort\r
60  * or jpeg_destroy) at some point.\r
61  */\r
62 \r
63 METHODDEF void\r
64 error_exit (j_common_ptr cinfo)\r
65 {\r
66 //  char buffer[JMSG_LENGTH_MAX];\r
67 \r
68   /* Create the message */\r
69   (*cinfo->err->format_message) (cinfo,rad_errormsg);\r
70 \r
71   /* Let the memory manager delete any temp files before we die */\r
72   jpeg_destroy(cinfo);\r
73 \r
74   longjmp( rad_loadfailed, -1 );\r
75 }\r
76 \r
77 \r
78 /*\r
79  * Actual output of an error or trace message.\r
80  * Applications may override this method to send JPEG messages somewhere\r
81  * other than stderr.\r
82  */\r
83 \r
84 METHODDEF void\r
85 output_message (j_common_ptr cinfo)\r
86 {\r
87   char buffer[JMSG_LENGTH_MAX];\r
88 \r
89   /* Create the message */\r
90   (*cinfo->err->format_message) (cinfo, buffer);\r
91 \r
92   /* Send it to stderr, adding a newline */\r
93   printf("%s\n", buffer);\r
94 }\r
95 \r
96 \r
97 /*\r
98  * Decide whether to emit a trace or warning message.\r
99  * msg_level is one of:\r
100  *   -1: recoverable corrupt-data warning, may want to abort.\r
101  *    0: important advisory messages (always display to user).\r
102  *    1: first level of tracing detail.\r
103  *    2,3,...: successively more detailed tracing messages.\r
104  * An application might override this method if it wanted to abort on warnings\r
105  * or change the policy about which messages to display.\r
106  */\r
107 \r
108 METHODDEF void\r
109 emit_message (j_common_ptr cinfo, int msg_level)\r
110 {\r
111   struct jpeg_error_mgr * err = cinfo->err;\r
112 \r
113   if (msg_level < 0) {\r
114     /* It's a warning message.  Since corrupt files may generate many warnings,\r
115      * the policy implemented here is to show only the first warning,\r
116      * unless trace_level >= 3.\r
117      */\r
118     if (err->num_warnings == 0 || err->trace_level >= 3)\r
119       (*err->output_message) (cinfo);\r
120     /* Always count warnings in num_warnings. */\r
121     err->num_warnings++;\r
122   } else {\r
123     /* It's a trace message.  Show it if trace_level >= msg_level. */\r
124     if (err->trace_level >= msg_level)\r
125       (*err->output_message) (cinfo);\r
126   }\r
127 }\r
128 \r
129 \r
130 /*\r
131  * Format a message string for the most recent JPEG error or message.\r
132  * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX\r
133  * characters.  Note that no '\n' character is added to the string.\r
134  * Few applications should need to override this method.\r
135  */\r
136 \r
137 METHODDEF void\r
138 format_message (j_common_ptr cinfo, char * buffer)\r
139 {\r
140   struct jpeg_error_mgr * err = cinfo->err;\r
141   int msg_code = err->msg_code;\r
142   const char * msgtext = NULL;\r
143   const char * msgptr;\r
144   char ch;\r
145   boolean isstring;\r
146 \r
147   /* Look up message string in proper table */\r
148   if (msg_code > 0 && msg_code <= err->last_jpeg_message) {\r
149     msgtext = err->jpeg_message_table[msg_code];\r
150   } else if (err->addon_message_table != NULL &&\r
151              msg_code >= err->first_addon_message &&\r
152              msg_code <= err->last_addon_message) {\r
153     msgtext = err->addon_message_table[msg_code - err->first_addon_message];\r
154   }\r
155 \r
156   /* Defend against bogus message number */\r
157   if (msgtext == NULL) {\r
158     err->msg_parm.i[0] = msg_code;\r
159     msgtext = err->jpeg_message_table[0];\r
160   }\r
161 \r
162   /* Check for string parameter, as indicated by %s in the message text */\r
163   isstring = FALSE;\r
164   msgptr = msgtext;\r
165   while ((ch = *msgptr++) != '\0') {\r
166     if (ch == '%') {\r
167       if (*msgptr == 's') isstring = TRUE;\r
168       break;\r
169     }\r
170   }\r
171 \r
172   /* Format the message into the passed buffer */\r
173   if (isstring)\r
174     sprintf(buffer, msgtext, err->msg_parm.s);\r
175   else\r
176     sprintf(buffer, msgtext,\r
177             err->msg_parm.i[0], err->msg_parm.i[1],\r
178             err->msg_parm.i[2], err->msg_parm.i[3],\r
179             err->msg_parm.i[4], err->msg_parm.i[5],\r
180             err->msg_parm.i[6], err->msg_parm.i[7]);\r
181 }\r
182 \r
183 \r
184 /*\r
185  * Reset error state variables at start of a new image.\r
186  * This is called during compression startup to reset trace/error\r
187  * processing to default state, without losing any application-specific\r
188  * method pointers.  An application might possibly want to override\r
189  * this method if it has additional error processing state.\r
190  */\r
191 \r
192 METHODDEF void\r
193 reset_error_mgr (j_common_ptr cinfo)\r
194 {\r
195   cinfo->err->num_warnings = 0;\r
196   /* trace_level is not reset since it is an application-supplied parameter */\r
197   cinfo->err->msg_code = 0;     /* may be useful as a flag for "no error" */\r
198 }\r
199 \r
200 \r
201 /*\r
202  * Fill in the standard error-handling methods in a jpeg_error_mgr object.\r
203  * Typical call is:\r
204  *      struct jpeg_compress_struct cinfo;\r
205  *      struct jpeg_error_mgr err;\r
206  *\r
207  *      cinfo.err = jpeg_std_error(&err);\r
208  * after which the application may override some of the methods.\r
209  */\r
210 \r
211 GLOBAL struct jpeg_error_mgr *\r
212 jpeg_std_error (struct jpeg_error_mgr * err)\r
213 {\r
214   err->error_exit = error_exit;\r
215   err->emit_message = emit_message;\r
216   err->output_message = output_message;\r
217   err->format_message = format_message;\r
218   err->reset_error_mgr = reset_error_mgr;\r
219 \r
220   err->trace_level = 0;         /* default = no tracing */\r
221   err->num_warnings = 0;        /* no warnings emitted yet */\r
222   err->msg_code = 0;            /* may be useful as a flag for "no error" */\r
223 \r
224   /* Initialize message table pointers */\r
225   err->jpeg_message_table = jpeg_std_message_table;\r
226   err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;\r
227 \r
228   err->addon_message_table = NULL;\r
229   err->first_addon_message = 0; /* for safety */\r
230   err->last_addon_message = 0;\r
231 \r
232   return err;\r
233 }\r