fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

jerror.c

Go to the documentation of this file.
00001 /*
00002  * jerror.c
00003  *
00004  * Copyright (C) 1991-1998, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains simple error-reporting and trace-message routines.
00009  * These are suitable for Unix-like systems and others where writing to
00010  * stderr is the right thing to do.  Many applications will want to replace
00011  * some or all of these routines.
00012  *
00013  * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
00014  * you get a Windows-specific hack to display error messages in a dialog box.
00015  * It ain't much, but it beats dropping error messages into the bit bucket,
00016  * which is what happens to output to stderr under most Windows C compilers.
00017  *
00018  * These routines are used by both the compression and decompression code.
00019  */
00020 
00021 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
00022 #include "jinclude.h"
00023 #include "jpeglib.h"
00024 #include "jversion.h"
00025 #include "jerror.h"
00026 #include <stdlib.h>
00027 
00028 #ifdef USE_WINDOWS_MESSAGEBOX
00029 #include <windows.h>
00030 #endif
00031 
00032 #ifndef EXIT_FAILURE            /* define exit() codes if not provided */
00033 #define EXIT_FAILURE  1
00034 #endif
00035 
00036 
00037 /*
00038  * Create the message string table.
00039  * We do this from the master message list in jerror.h by re-reading
00040  * jerror.h with a suitable definition for macro JMESSAGE.
00041  * The message table is made an external symbol just in case any applications
00042  * want to refer to it directly.
00043  */
00044 
00045 #ifdef NEED_SHORT_EXTERNAL_NAMES
00046 #define jpeg_std_message_table  jMsgTable
00047 #endif
00048 
00049 #define JMESSAGE(code,string)   string ,
00050 
00051 const char * const jpeg_std_message_table[] = {
00052 #include "jerror.h"
00053   NULL
00054 };
00055 
00056 
00057 /*
00058  * Error exit handler: must not return to caller.
00059  *
00060  * Applications may override this if they want to get control back after
00061  * an error.  Typically one would longjmp somewhere instead of exiting.
00062  * The setjmp buffer can be made a private field within an expanded error
00063  * handler object.  Note that the info needed to generate an error message
00064  * is stored in the error object, so you can generate the message now or
00065  * later, at your convenience.
00066  * You should make sure that the JPEG object is cleaned up (with jpeg_abort
00067  * or jpeg_destroy) at some point.
00068  */
00069 
00070 METHODDEF(void)
00071 error_exit (j_common_ptr cinfo)
00072 {
00073   /* Always display the message */
00074   (*cinfo->err->output_message) (cinfo);
00075 
00076   /* Let the memory manager delete any temp files before we die */
00077   jpeg_destroy(cinfo);
00078 
00079   exit(EXIT_FAILURE);
00080 }
00081 
00082 
00083 /*
00084  * Actual output of an error or trace message.
00085  * Applications may override this method to send JPEG messages somewhere
00086  * other than stderr.
00087  *
00088  * On Windows, printing to stderr is generally completely useless,
00089  * so we provide optional code to produce an error-dialog popup.
00090  * Most Windows applications will still prefer to override this routine,
00091  * but if they don't, it'll do something at least marginally useful.
00092  *
00093  * NOTE: to use the library in an environment that doesn't support the
00094  * C stdio library, you may have to delete the call to fprintf() entirely,
00095  * not just not use this routine.
00096  */
00097 
00098 METHODDEF(void)
00099 output_message (j_common_ptr cinfo)
00100 {
00101   char buffer[JMSG_LENGTH_MAX];
00102 
00103   /* Create the message */
00104   (*cinfo->err->format_message) (cinfo, buffer);
00105 
00106 #ifdef USE_WINDOWS_MESSAGEBOX
00107   /* Display it in a message dialog box */
00108   MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
00109              MB_OK | MB_ICONERROR);
00110 #else
00111   /* Send it to stderr, adding a newline */
00112   fprintf(stderr, "%s\n", buffer);
00113 #endif
00114 }
00115 
00116 
00117 /*
00118  * Decide whether to emit a trace or warning message.
00119  * msg_level is one of:
00120  *   -1: recoverable corrupt-data warning, may want to abort.
00121  *    0: important advisory messages (always display to user).
00122  *    1: first level of tracing detail.
00123  *    2,3,...: successively more detailed tracing messages.
00124  * An application might override this method if it wanted to abort on warnings
00125  * or change the policy about which messages to display.
00126  */
00127 
00128 METHODDEF(void)
00129 emit_message (j_common_ptr cinfo, int msg_level)
00130 {
00131   struct jpeg_error_mgr * err = cinfo->err;
00132 
00133   if (msg_level < 0) {
00134     /* It's a warning message.  Since corrupt files may generate many warnings,
00135      * the policy implemented here is to show only the first warning,
00136      * unless trace_level >= 3.
00137      */
00138     if (err->num_warnings == 0 || err->trace_level >= 3)
00139       (*err->output_message) (cinfo);
00140     /* Always count warnings in num_warnings. */
00141     err->num_warnings++;
00142   } else {
00143     /* It's a trace message.  Show it if trace_level >= msg_level. */
00144     if (err->trace_level >= msg_level)
00145       (*err->output_message) (cinfo);
00146   }
00147 }
00148 
00149 
00150 /*
00151  * Format a message string for the most recent JPEG error or message.
00152  * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
00153  * characters.  Note that no '\n' character is added to the string.
00154  * Few applications should need to override this method.
00155  */
00156 
00157 METHODDEF(void)
00158 format_message (j_common_ptr cinfo, char * buffer)
00159 {
00160   struct jpeg_error_mgr * err = cinfo->err;
00161   int msg_code = err->msg_code;
00162   const char * msgtext = NULL;
00163   const char * msgptr;
00164   char ch;
00165   boolean isstring;
00166 
00167   /* Look up message string in proper table */
00168   if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
00169     msgtext = err->jpeg_message_table[msg_code];
00170   } else if (err->addon_message_table != NULL &&
00171              msg_code >= err->first_addon_message &&
00172              msg_code <= err->last_addon_message) {
00173     msgtext = err->addon_message_table[msg_code - err->first_addon_message];
00174   }
00175 
00176   /* Defend against bogus message number */
00177   if (msgtext == NULL) {
00178     err->msg_parm.i[0] = msg_code;
00179     msgtext = err->jpeg_message_table[0];
00180   }
00181 
00182   /* Check for string parameter, as indicated by %s in the message text */
00183   isstring = FALSE;
00184   msgptr = msgtext;
00185   while ((ch = *msgptr++) != '\0') {
00186     if (ch == '%') {
00187       if (*msgptr == 's') isstring = TRUE;
00188       break;
00189     }
00190   }
00191 
00192   /* Format the message into the passed buffer */
00193   if (isstring)
00194     sprintf(buffer, msgtext, err->msg_parm.s);
00195   else
00196     sprintf(buffer, msgtext,
00197             err->msg_parm.i[0], err->msg_parm.i[1],
00198             err->msg_parm.i[2], err->msg_parm.i[3],
00199             err->msg_parm.i[4], err->msg_parm.i[5],
00200             err->msg_parm.i[6], err->msg_parm.i[7]);
00201 }
00202 
00203 
00204 /*
00205  * Reset error state variables at start of a new image.
00206  * This is called during compression startup to reset trace/error
00207  * processing to default state, without losing any application-specific
00208  * method pointers.  An application might possibly want to override
00209  * this method if it has additional error processing state.
00210  */
00211 
00212 METHODDEF(void)
00213 reset_error_mgr (j_common_ptr cinfo)
00214 {
00215   cinfo->err->num_warnings = 0;
00216   /* trace_level is not reset since it is an application-supplied parameter */
00217   cinfo->err->msg_code = 0;     /* may be useful as a flag for "no error" */
00218 }
00219 
00220 
00221 /*
00222  * Fill in the standard error-handling methods in a jpeg_error_mgr object.
00223  * Typical call is:
00224  *      struct jpeg_compress_struct cinfo;
00225  *      struct jpeg_error_mgr err;
00226  *
00227  *      cinfo.err = jpeg_std_error(&err);
00228  * after which the application may override some of the methods.
00229  */
00230 
00231 GLOBAL(struct jpeg_error_mgr *)
00232 jpeg_std_error (struct jpeg_error_mgr * err)
00233 {
00234   err->error_exit = error_exit;
00235   err->emit_message = emit_message;
00236   err->output_message = output_message;
00237   err->format_message = format_message;
00238   err->reset_error_mgr = reset_error_mgr;
00239 
00240   err->trace_level = 0;         /* default = no tracing */
00241   err->num_warnings = 0;        /* no warnings emitted yet */
00242   err->msg_code = 0;            /* may be useful as a flag for "no error" */
00243 
00244   /* Initialize message table pointers */
00245   err->jpeg_message_table = jpeg_std_message_table;
00246   err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
00247 
00248   err->addon_message_table = NULL;
00249   err->first_addon_message = 0; /* for safety */
00250   err->last_addon_message = 0;
00251 
00252   return err;
00253 }