source: golgotha/src/i4/loaders/jpg/jerror.cc @ 80

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