3 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 #include "opj_includes.h"
31 opj_event_mgr_t* opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
33 opj_event_mgr_t *previous = cinfo->event_mgr;
34 cinfo->event_mgr = event_mgr;
35 cinfo->client_data = context;
42 bool opg_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {
43 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
44 opj_msg_callback msg_handler = NULL;
46 opj_event_mgr_t *event_mgr = cinfo->event_mgr;
47 if(event_mgr != NULL) {
50 msg_handler = event_mgr->error_handler;
53 msg_handler = event_mgr->warning_handler;
56 msg_handler = event_mgr->info_handler;
61 if(msg_handler == NULL) {
68 if ((fmt != NULL) && (event_mgr != NULL)) {
71 char message[MSG_SIZE];
72 memset(message, 0, MSG_SIZE);
73 /* initialize the optional parameter list */
75 /* check the length of the format string */
76 str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
77 /* parse the format string and put the result in 'message' */
78 for (i = 0, j = 0; i < str_length; ++i) {
80 if (i + 1 < str_length) {
81 switch(tolower(fmt[i + 1])) {
85 case 'o' : /* octal numbers */
88 _itoa(va_arg(arg, int), tmp, 8);
94 case 'i' : /* decimal numbers */
98 _itoa(va_arg(arg, int), tmp, 10);
104 case 'x' : /* hexadecimal numbers */
107 _itoa(va_arg(arg, int), tmp, 16);
108 strcat(message, tmp);
113 case 's' : /* strings */
115 char *tmp = va_arg(arg, char*);
116 strcat(message, tmp);
121 case 'f' : /* floats */
124 double value = va_arg(arg, double);
125 sprintf(tmp, "%f", value);
126 strcat(message, tmp);
133 message[j++] = fmt[i];
136 message[j++] = fmt[i];
139 /* deinitialize the optional parameter list */
142 /* output the message to the user program */
143 msg_handler(message, cinfo->client_data);