home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / jpeg / jerror.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.3 KB  |  250 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. #if defined(DEBUG) && defined(__MWERKS__)
  27. #       include "xp_trace.h"
  28. #endif
  29.  
  30. /*
  31.  * Create the message string table.
  32.  * We do this from the master message list in jerror.h by re-reading
  33.  * jerror.h with a suitable definition for macro JMESSAGE.
  34.  * The message table is made an external symbol just in case any applications
  35.  * want to refer to it directly.
  36.  */
  37.  
  38. #ifdef NEED_SHORT_EXTERNAL_NAMES
  39. #define jpeg_std_message_table    jMsgTable
  40. #endif
  41.  
  42. #define JMESSAGE(code,string)    string ,
  43.  
  44. const char * const jpeg_std_message_table[] = {
  45. #include "jerror.h"
  46.   NULL
  47. };
  48.  
  49.  
  50. /*
  51.  * Error exit handler: must not return to caller.
  52.  *
  53.  * Applications may override this if they want to get control back after
  54.  * an error.  Typically one would longjmp somewhere instead of exiting.
  55.  * The setjmp buffer can be made a private field within an expanded error
  56.  * handler object.  Note that the info needed to generate an error message
  57.  * is stored in the error object, so you can generate the message now or
  58.  * later, at your convenience.
  59.  * You should make sure that the JPEG object is cleaned up (with jpeg_abort
  60.  * or jpeg_destroy) at some point.
  61.  */
  62.  
  63. METHODDEF void
  64. error_exit (j_common_ptr cinfo)
  65. {
  66.   /* Always display the message */
  67.   (*cinfo->err->output_message) (cinfo);
  68.  
  69.   /* Let the memory manager delete any temp files before we die */
  70.   jpeg_destroy(cinfo);
  71.  
  72. #if !defined(_WINDOWS) || defined(_WIN32)
  73.   exit(EXIT_FAILURE);
  74. #else
  75.   /* XXX */
  76. #endif
  77. }
  78.  
  79.  
  80. /*
  81.  * Actual output of an error or trace message.
  82.  * Applications may override this method to send JPEG messages somewhere
  83.  * other than stderr.
  84.  */
  85.  
  86. METHODDEF void
  87. output_message (j_common_ptr cinfo)
  88. {
  89. #ifdef DEBUG
  90.   char buffer[JMSG_LENGTH_MAX];
  91.   /* Create the message */
  92.   (*cinfo->err->format_message) (cinfo, buffer);
  93.  
  94.  /* Send it to stderr, adding a newline */
  95.  
  96. #if defined(_WINDOWS) && !defined(_WIN32)
  97.   printf("%s\n", buffer); 
  98. #elif __MWERKS__ 
  99.   XP_TRACE(("%s\n", buffer)); 
  100. #else
  101.   fprintf(stderr, "%s\n", buffer); 
  102. #endif 
  103. #endif 
  104. }
  105.  
  106.  
  107. /*
  108.  * Decide whether to emit a trace or warning message.
  109.  * msg_level is one of:
  110.  *   -1: recoverable corrupt-data warning, may want to abort.
  111.  *    0: important advisory messages (always display to user).
  112.  *    1: first level of tracing detail.
  113.  *    2,3,...: successively more detailed tracing messages.
  114.  * An application might override this method if it wanted to abort on warnings
  115.  * or change the policy about which messages to display.
  116.  */
  117.  
  118. METHODDEF void
  119. emit_message (j_common_ptr cinfo, int msg_level)
  120. {
  121. #ifdef DEBUG
  122.   struct jpeg_error_mgr * err = cinfo->err;
  123.  
  124.   if (msg_level < 0) {
  125.     /* It's a warning message.  Since corrupt files may generate many warnings,
  126.      * the policy implemented here is to show only the first warning,
  127.      * unless trace_level >= 3.
  128.      */
  129. #ifndef __MWERKS__ /* Mark Lanett is a pinhead. */
  130.     if (err->num_warnings == 0 || err->trace_level >= 3)
  131. #endif
  132.       (*err->output_message) (cinfo);
  133.     /* Always count warnings in num_warnings. */
  134.     err->num_warnings++;
  135.   } else {
  136.     /* It's a trace message.  Show it if trace_level >= msg_level. */
  137. #ifndef __MWERKS__ /* Mark Lanett is a pinhead. */
  138.     if (err->trace_level >= msg_level)
  139. #endif
  140.       (*err->output_message) (cinfo);
  141.   }
  142. #endif
  143. }
  144.  
  145.  
  146. /*
  147.  * Format a message string for the most recent JPEG error or message.
  148.  * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
  149.  * characters.  Note that no '\n' character is added to the string.
  150.  * Few applications should need to override this method.
  151.  */
  152.  
  153. METHODDEF void
  154. format_message (j_common_ptr cinfo, char * buffer)
  155. {
  156.   struct jpeg_error_mgr * err = cinfo->err;
  157.   int msg_code = err->msg_code;
  158.   const char * msgtext = NULL;
  159.   const char * msgptr;
  160.   char ch;
  161.   boolean isstring;
  162.  
  163.   /* Look up message string in proper table */
  164.   if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
  165.     msgtext = err->jpeg_message_table[msg_code];
  166.   } else if (err->addon_message_table != NULL &&
  167.          msg_code >= err->first_addon_message &&
  168.          msg_code <= err->last_addon_message) {
  169.     msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  170.   }
  171.  
  172.   /* Defend against bogus message number */
  173.   if (msgtext == NULL) {
  174.     err->msg_parm.i[0] = msg_code;
  175.     msgtext = err->jpeg_message_table[0];
  176.   }
  177.  
  178.   /* Check for string parameter, as indicated by %s in the message text */
  179.   isstring = FALSE;
  180.   msgptr = msgtext;
  181.   while ((ch = *msgptr++) != '\0') {
  182.     if (ch == '%') {
  183.       if (*msgptr == 's') isstring = TRUE;
  184.       break;
  185.     }
  186.   }
  187.  
  188.   /* Format the message into the passed buffer */
  189.   if (isstring)
  190.     sprintf(buffer, msgtext, err->msg_parm.s);
  191.   else
  192.     sprintf(buffer, msgtext,
  193.         err->msg_parm.i[0], err->msg_parm.i[1],
  194.         err->msg_parm.i[2], err->msg_parm.i[3],
  195.         err->msg_parm.i[4], err->msg_parm.i[5],
  196.         err->msg_parm.i[6], err->msg_parm.i[7]);
  197. }
  198.  
  199.  
  200. /*
  201.  * Reset error state variables at start of a new image.
  202.  * This is called during compression startup to reset trace/error
  203.  * processing to default state, without losing any application-specific
  204.  * method pointers.  An application might possibly want to override
  205.  * this method if it has additional error processing state.
  206.  */
  207.  
  208. METHODDEF void
  209. reset_error_mgr (j_common_ptr cinfo)
  210. {
  211.   cinfo->err->num_warnings = 0;
  212.   /* trace_level is not reset since it is an application-supplied parameter */
  213.   cinfo->err->msg_code = 0;    /* may be useful as a flag for "no error" */
  214. }
  215.  
  216.  
  217. /*
  218.  * Fill in the standard error-handling methods in a jpeg_error_mgr object.
  219.  * Typical call is:
  220.  *    struct jpeg_compress_struct cinfo;
  221.  *    struct jpeg_error_mgr err;
  222.  *
  223.  *    cinfo.err = jpeg_std_error(&err);
  224.  * after which the application may override some of the methods.
  225.  */
  226.  
  227. GLOBAL JRI_PUBLIC_API(struct jpeg_error_mgr *)
  228. jpeg_std_error (struct jpeg_error_mgr * err)
  229. {
  230.   err->error_exit = error_exit;
  231.   err->emit_message = emit_message;
  232.   err->output_message = output_message;
  233.   err->format_message = format_message;
  234.   err->reset_error_mgr = reset_error_mgr;
  235.  
  236.   err->trace_level = 0;        /* default = no tracing */
  237.   err->num_warnings = 0;    /* no warnings emitted yet */
  238.   err->msg_code = 0;        /* may be useful as a flag for "no error" */
  239.  
  240.   /* Initialize message table pointers */
  241.   err->jpeg_message_table = jpeg_std_message_table;
  242.   err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
  243.  
  244.   err->addon_message_table = NULL;
  245.   err->first_addon_message = 0;    /* for safety */
  246.   err->last_addon_message = 0;
  247.  
  248.   return err;
  249. }
  250.