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