home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / jpeglib_5a.lzh / JPEG_5A / jerror.c < prev    next >
Text File  |  1995-01-14  |  7KB  |  229 lines

  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.   /* Always display the message */
  64.   (*cinfo->err->output_message) (cinfo);
  65.  
  66.   /* Let the memory manager delete any temp files before we die */
  67.   jpeg_destroy(cinfo);
  68.  
  69.   exit(EXIT_FAILURE);
  70. }
  71.  
  72.  
  73. /*
  74.  * Actual output of an error or trace message.
  75.  * Applications may override this method to send JPEG messages somewhere
  76.  * other than stderr.
  77.  */
  78.  
  79. METHODDEF void
  80. output_message (j_common_ptr cinfo)
  81. {
  82.   char buffer[JMSG_LENGTH_MAX];
  83.  
  84.   /* Create the message */
  85.   (*cinfo->err->format_message) (cinfo, buffer);
  86.  
  87.   /* Send it to stderr, adding a newline */
  88.   fprintf(stderr, "%s\n", buffer);
  89. }
  90.  
  91.  
  92. /*
  93.  * Decide whether to emit a trace or warning message.
  94.  * msg_level is one of:
  95.  *   -1: recoverable corrupt-data warning, may want to abort.
  96.  *    0: important advisory messages (always display to user).
  97.  *    1: first level of tracing detail.
  98.  *    2,3,...: successively more detailed tracing messages.
  99.  * An application might override this method if it wanted to abort on warnings
  100.  * or change the policy about which messages to display.
  101.  */
  102.  
  103. METHODDEF void
  104. emit_message (j_common_ptr cinfo, int msg_level)
  105. {
  106.   struct jpeg_error_mgr * err = cinfo->err;
  107.  
  108.   if (msg_level < 0) {
  109.     /* It's a warning message.  Since corrupt files may generate many warnings,
  110.      * the policy implemented here is to show only the first warning,
  111.      * unless trace_level >= 3.
  112.      */
  113.     if (err->num_warnings == 0 || err->trace_level >= 3)
  114.       (*err->output_message) (cinfo);
  115.     /* Always count warnings in num_warnings. */
  116.     err->num_warnings++;
  117.   } else {
  118.     /* It's a trace message.  Show it if trace_level >= msg_level. */
  119.     if (err->trace_level >= msg_level)
  120.       (*err->output_message) (cinfo);
  121.   }
  122. }
  123.  
  124.  
  125. /*
  126.  * Format a message string for the most recent JPEG error or message.
  127.  * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
  128.  * characters.  Note that no '\n' character is added to the string.
  129.  * Few applications should need to override this method.
  130.  */
  131.  
  132. METHODDEF void
  133. format_message (j_common_ptr cinfo, char * buffer)
  134. {
  135.   struct jpeg_error_mgr * err = cinfo->err;
  136.   int msg_code = err->msg_code;
  137.   const char * msgtext = NULL;
  138.   const char * msgptr;
  139.   char ch;
  140.   boolean isstring;
  141.  
  142.   /* Look up message string in proper table */
  143.   if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
  144.     msgtext = err->jpeg_message_table[msg_code];
  145.   } else if (err->addon_message_table != NULL &&
  146.          msg_code >= err->first_addon_message &&
  147.          msg_code <= err->last_addon_message) {
  148.     msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  149.   }
  150.  
  151.   /* Defend against bogus message number */
  152.   if (msgtext == NULL) {
  153.     err->msg_parm.i[0] = msg_code;
  154.     msgtext = err->jpeg_message_table[0];
  155.   }
  156.  
  157.   /* Check for string parameter, as indicated by %s in the message text */
  158.   isstring = FALSE;
  159.   msgptr = msgtext;
  160.   while ((ch = *msgptr++) != '\0') {
  161.     if (ch == '%') {
  162.       if (*msgptr == 's') isstring = TRUE;
  163.       break;
  164.     }
  165.   }
  166.  
  167.   /* Format the message into the passed buffer */
  168.   if (isstring)
  169.     sprintf(buffer, msgtext, err->msg_parm.s);
  170.   else
  171.     sprintf(buffer, msgtext,
  172.         err->msg_parm.i[0], err->msg_parm.i[1],
  173.         err->msg_parm.i[2], err->msg_parm.i[3],
  174.         err->msg_parm.i[4], err->msg_parm.i[5],
  175.         err->msg_parm.i[6], err->msg_parm.i[7]);
  176. }
  177.  
  178.  
  179. /*
  180.  * Reset error state variables at start of a new image.
  181.  * This is called during compression startup to reset trace/error
  182.  * processing to default state, without losing any application-specific
  183.  * method pointers.  An application might possibly want to override
  184.  * this method if it has additional error processing state.
  185.  */
  186.  
  187. METHODDEF void
  188. reset_error_mgr (j_common_ptr cinfo)
  189. {
  190.   cinfo->err->num_warnings = 0;
  191.   /* trace_level is not reset since it is an application-supplied parameter */
  192.   cinfo->err->msg_code = 0;    /* may be useful as a flag for "no error" */
  193. }
  194.  
  195.  
  196. /*
  197.  * Fill in the standard error-handling methods in a jpeg_error_mgr object.
  198.  * Typical call is:
  199.  *    struct jpeg_compress_struct cinfo;
  200.  *    struct jpeg_error_mgr err;
  201.  *
  202.  *    cinfo.err = jpeg_std_error(&err);
  203.  * after which the application may override some of the methods.
  204.  */
  205.  
  206. GLOBAL struct jpeg_error_mgr *
  207. jpeg_std_error (struct jpeg_error_mgr * err)
  208. {
  209.   err->error_exit = error_exit;
  210.   err->emit_message = emit_message;
  211.   err->output_message = output_message;
  212.   err->format_message = format_message;
  213.   err->reset_error_mgr = reset_error_mgr;
  214.  
  215.   err->trace_level = 0;        /* default = no tracing */
  216.   err->num_warnings = 0;    /* no warnings emitted yet */
  217.   err->msg_code = 0;        /* may be useful as a flag for "no error" */
  218.  
  219.   /* Initialize message table pointers */
  220.   err->jpeg_message_table = jpeg_std_message_table;
  221.   err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
  222.  
  223.   err->addon_message_table = NULL;
  224.   err->first_addon_message = 0;    /* for safety */
  225.   err->last_addon_message = 0;
  226.  
  227.   return err;
  228. }
  229.