Main MRPT website > C++ reference for MRPT 1.9.9
jerror.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
11 #include "jinclude.h"
12 #include "mrpt_jpeglib.h"
13 #include "jversion.h"
14 #include "jerror.h"
15 
16 #ifdef USE_WINDOWS_MESSAGEBOX
17 #include <windows.h>
18 #endif
19 
20 #ifndef EXIT_FAILURE /* define exit() codes if not provided */
21 #define EXIT_FAILURE 1
22 #endif
23 
24 /*
25  * Create the message string table.
26  * We do this from the master message list in jerror.h by re-reading
27  * jerror.h with a suitable definition for macro JMESSAGE.
28  * The message table is made an external symbol just in case any applications
29  * want to refer to it directly.
30  */
31 
32 #ifdef NEED_SHORT_EXTERNAL_NAMES
33 #define jpeg_std_message_table jMsgTable
34 #endif
35 
36 #define JMESSAGE(code, string) string,
37 
38 const char* const jpeg_std_message_table[] = {
39 #include "jerror.h"
40  NULL};
41 
42 /*
43  * Error exit handler: must not return to caller.
44  *
45  * Applications may override this if they want to get control back after
46  * an error. Typically one would longjmp somewhere instead of exiting.
47  * The setjmp buffer can be made a private field within an expanded error
48  * handler object. Note that the info needed to generate an error message
49  * is stored in the error object, so you can generate the message now or
50  * later, at your convenience.
51  * You should make sure that the JPEG object is cleaned up (with jpeg_abort
52  * or jpeg_destroy) at some point.
53  */
54 
55 METHODDEF(void)
57 {
58  /* Always display the message */
59  (*cinfo->err->output_message)(cinfo);
60 
61  /* Let the memory manager delete any temp files before we die */
62  jpeg_destroy(cinfo);
63 
64  fprintf(stderr, "[jpeg::error_exit] critical error\n");
65  // exit(EXIT_FAILURE);
66 }
67 
68 /*
69  * Actual output of an error or trace message.
70  * Applications may override this method to send JPEG messages somewhere
71  * other than stderr.
72  *
73  * On Windows, printing to stderr is generally completely useless,
74  * so we provide optional code to produce an error-dialog popup.
75  * Most Windows applications will still prefer to override this routine,
76  * but if they don't, it'll do something at least marginally useful.
77  *
78  * NOTE: to use the library in an environment that doesn't support the
79  * C stdio library, you may have to delete the call to fprintf() entirely,
80  * not just not use this routine.
81  */
82 
83 METHODDEF(void)
85 {
86  char buffer[JMSG_LENGTH_MAX];
87 
88  /* Create the message */
89  (*cinfo->err->format_message)(cinfo, buffer);
90 
91 #ifdef USE_WINDOWS_MESSAGEBOX
92  /* Display it in a message dialog box */
93  MessageBox(
94  GetActiveWindow(), buffer, "JPEG Library Error", MB_OK | MB_ICONERROR);
95 #else
96  /* Send it to stderr, adding a newline */
97  fprintf(stderr, "%s\n", buffer);
98 #endif
99 }
100 
101 /*
102  * Decide whether to emit a trace or warning message.
103  * msg_level is one of:
104  * -1: recoverable corrupt-data warning, may want to abort.
105  * 0: important advisory messages (always display to user).
106  * 1: first level of tracing detail.
107  * 2,3,...: successively more detailed tracing messages.
108  * An application might override this method if it wanted to abort on warnings
109  * or change the policy about which messages to display.
110  */
111 
112 METHODDEF(void)
113 emit_message(j_common_ptr cinfo, int msg_level)
114 {
115  struct jpeg_error_mgr* err = cinfo->err;
116 
117  if (msg_level < 0)
118  {
119  /* It's a warning message. Since corrupt files may generate many
120  * warnings,
121  * the policy implemented here is to show only the first warning,
122  * unless trace_level >= 3.
123  */
124  if (err->num_warnings == 0 || err->trace_level >= 3)
125  (*err->output_message)(cinfo);
126  /* Always count warnings in num_warnings. */
127  err->num_warnings++;
128  }
129  else
130  {
131  /* It's a trace message. Show it if trace_level >= msg_level. */
132  if (err->trace_level >= msg_level) (*err->output_message)(cinfo);
133  }
134 }
135 
136 /*
137  * Format a message string for the most recent JPEG error or message.
138  * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
139  * characters. Note that no '\n' character is added to the string.
140  * Few applications should need to override this method.
141  */
142 
143 METHODDEF(void)
145 {
146  struct jpeg_error_mgr* err = cinfo->err;
147  int msg_code = err->msg_code;
148  const char* msgtext = nullptr;
149  const char* msgptr;
150  char ch;
151  boolean isstring;
152 
153  /* Look up message string in proper table */
154  if (msg_code > 0 && msg_code <= err->last_jpeg_message)
155  {
156  msgtext = err->jpeg_message_table[msg_code];
157  }
158  else if (
159  err->addon_message_table != nullptr &&
160  msg_code >= err->first_addon_message &&
161  msg_code <= err->last_addon_message)
162  {
163  msgtext = err->addon_message_table[msg_code - err->first_addon_message];
164  }
165 
166  /* Defend against bogus message number */
167  if (msgtext == nullptr)
168  {
169  err->msg_parm.i[0] = msg_code;
170  msgtext = err->jpeg_message_table[0];
171  }
172 
173  /* Check for string parameter, as indicated by %s in the message text */
174  isstring = FALSE;
175  msgptr = msgtext;
176  while ((ch = *msgptr++) != '\0')
177  {
178  if (ch == '%')
179  {
180  if (*msgptr == 's') isstring = TRUE;
181  break;
182  }
183  }
184 
185  /* Format the message into the passed buffer */
186  if (isstring)
187  sprintf(buffer, msgtext, err->msg_parm.s);
188  else
189  sprintf(
190  buffer, msgtext, err->msg_parm.i[0], err->msg_parm.i[1],
191  err->msg_parm.i[2], err->msg_parm.i[3], err->msg_parm.i[4],
192  err->msg_parm.i[5], err->msg_parm.i[6], err->msg_parm.i[7]);
193 }
194 
195 /*
196  * Reset error state variables at start of a new image.
197  * This is called during compression startup to reset trace/error
198  * processing to default state, without losing any application-specific
199  * method pointers. An application might possibly want to override
200  * this method if it has additional error processing state.
201  */
202 
203 METHODDEF(void)
205 {
206  cinfo->err->num_warnings = 0;
207  /* trace_level is not reset since it is an application-supplied parameter */
208  cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
209 }
210 
211 /*
212  * Fill in the standard error-handling methods in a jpeg_error_mgr object.
213  * Typical call is:
214  * struct jpeg_compress_struct cinfo;
215  * struct jpeg_error_mgr err;
216  *
217  * cinfo.err = jpeg_std_error(&err);
218  * after which the application may override some of the methods.
219  */
220 
221 GLOBAL(struct jpeg_error_mgr*)
223 {
224  err->error_exit = error_exit;
225  err->emit_message = emit_message;
226  err->output_message = output_message;
227  err->format_message = format_message;
228  err->reset_error_mgr = reset_error_mgr;
229 
230  err->trace_level = 0; /* default = no tracing */
231  err->num_warnings = 0; /* no warnings emitted yet */
232  err->msg_code = 0; /* may be useful as a flag for "no error" */
233 
234  /* Initialize message table pointers */
235  err->jpeg_message_table = jpeg_std_message_table;
236  err->last_jpeg_message = (int)JMSG_LASTMSGCODE - 1;
237 
238  err->addon_message_table = nullptr;
239  err->first_addon_message = 0; /* for safety */
240  err->last_addon_message = 0;
241 
242  return err;
243 }
reset_error_mgr(j_common_ptr cinfo)
Definition: jerror.cpp:204
output_message(j_common_ptr cinfo)
Definition: jerror.cpp:84
GLuint buffer
Definition: glext.h:3917
char s[JMSG_STR_PARM_MAX]
Definition: mrpt_jpeglib.h:665
format_message(j_common_ptr cinfo, char *buffer)
Definition: jerror.cpp:144
jpeg_destroy(j_common_ptr cinfo)
Definition: jcomapi.cpp:68
const char *const * addon_message_table
Definition: mrpt_jpeglib.h:695
const char *const jpeg_std_message_table[]
Definition: jerror.cpp:38
#define FALSE
Definition: jmorecfg.h:216
error_exit(j_common_ptr cinfo)
Definition: jerror.cpp:56
#define TRUE
Definition: jmorecfg.h:219
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
#define GLOBAL(type)
Definition: jmorecfg.h:177
#define METHODDEF(type)
Definition: jmorecfg.h:173
const char *const * jpeg_message_table
Definition: mrpt_jpeglib.h:690
jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.cpp:222
#define JMSG_LENGTH_MAX
Definition: mrpt_jpeglib.h:654
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:188
emit_message(j_common_ptr cinfo, int msg_level)
Definition: jerror.cpp:113
union jpeg_error_mgr::@77 msg_parm



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019