Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

jerror.cpp (6879B)


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