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