9b87f734d6a4444af5590611bf1a9bf27924ee57
[openjpeg.git] / libopenjpeg / event.c
1
2 /*
3  * Copyright (c) 2005, Herv� Drolon, FreeImage Team
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27
28
29 #include "opj_includes.h"
30
31 opj_event_mgr_t* opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
32   if(cinfo) {
33     opj_event_mgr_t *previous = cinfo->event_mgr;
34     cinfo->event_mgr = event_mgr;
35     cinfo->client_data = context;
36     return previous;
37   }
38
39   return NULL;
40 }
41
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;
45
46   opj_event_mgr_t *event_mgr = cinfo->event_mgr;
47   if(event_mgr != NULL) {
48     switch(event_type) {
49       case EVT_ERROR:
50         msg_handler = event_mgr->error_handler;
51         break;
52       case EVT_WARNING:
53         msg_handler = event_mgr->warning_handler;
54         break;
55       case EVT_INFO:
56         msg_handler = event_mgr->info_handler;
57         break;
58       default:
59         break;
60     }
61     if(msg_handler == NULL) {
62       return false;
63     }
64   } else {
65     return false;
66   }
67
68   if ((fmt != NULL) && (event_mgr != NULL)) {
69     va_list arg;
70     int str_length, i, j;
71     char message[MSG_SIZE];
72     memset(message, 0, MSG_SIZE);
73     /* initialize the optional parameter list */
74     va_start(arg, fmt);
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) {
79       if (fmt[i] == '%') {
80         if (i + 1 < str_length) {
81           switch(tolower(fmt[i + 1])) {
82             case '%' :
83               message[j++] = '%';
84               break;
85             case 'o' : /* octal numbers */
86             {
87               char tmp[16];
88               _itoa(va_arg(arg, int), tmp, 8);
89               strcat(message, tmp);
90               j += strlen(tmp);
91               ++i;
92               break;
93             }
94             case 'i' : /* decimal numbers */
95             case 'd' :
96             {
97               char tmp[16];
98               _itoa(va_arg(arg, int), tmp, 10);
99               strcat(message, tmp);
100               j += strlen(tmp);
101               ++i;
102               break;
103             }
104             case 'x' : /* hexadecimal numbers */
105             {
106               char tmp[16];
107               _itoa(va_arg(arg, int), tmp, 16);
108               strcat(message, tmp);
109               j += strlen(tmp);
110               ++i;
111               break;
112             }
113             case 's' : /* strings */
114             {
115               char *tmp = va_arg(arg, char*);
116               strcat(message, tmp);
117               j += strlen(tmp);
118               ++i;
119               break;
120             }
121             case 'f' :  /* floats */
122             {
123               char tmp[16];
124               double value = va_arg(arg, double);
125               sprintf(tmp, "%f", value);
126               strcat(message, tmp);
127               j += strlen(tmp);
128               ++i;
129               break;
130             }
131           };
132         } else {
133           message[j++] = fmt[i];
134         }
135       } else {
136         message[j++] = fmt[i];
137       };
138     }
139     /* deinitialize the optional parameter list */
140     va_end(arg);
141
142     /* output the message to the user program */
143     msg_handler(message, cinfo->client_data);
144   }
145
146   return true;
147 }
148