[trunk] Fix compilation:
[openjpeg.git] / src / lib / openjp2 / event.c
1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "opj_includes.h"
28
29 /* ==========================================================
30      Utility functions
31    ==========================================================*/
32
33 #ifdef OPJ_CODE_NOT_USED
34 #ifndef _WIN32
35 static char*
36 i2a(unsigned i, char *a, unsigned r) {
37         if (i/r > 0) a = i2a(i/r,a,r);
38         *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
39         return a+1;
40 }
41
42 /** 
43  Transforms integer i into an ascii string and stores the result in a; 
44  string is encoded in the base indicated by r.
45  @param i Number to be converted
46  @param a String result
47  @param r Base of value; must be in the range 2 - 36
48  @return Returns a
49 */
50 static char *
51 _itoa(int i, char *a, int r) {
52         r = ((r < 2) || (r > 36)) ? 10 : r;
53         if(i < 0) {
54                 *a = '-';
55                 *i2a(-i, a+1, r) = 0;
56         }
57         else *i2a(i, a, r) = 0;
58         return a;
59 }
60
61 #endif /* !_WIN32 */
62 #endif
63
64 /* ----------------------------------------------------------------------- */
65 /**
66  * Default callback function.
67  * Do nothing.
68  */
69 static void opj_default_callback (const char *msg, void *client_data){}
70
71 /* ----------------------------------------------------------------------- */
72 opj_bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {
73 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
74         opj_msg_callback msg_handler = NULL;
75
76         opj_event_mgr_t *event_mgr = cinfo->event_mgr;
77         if(event_mgr != NULL) {
78                 switch(event_type) {
79                         case EVT_ERROR:
80                                 msg_handler = event_mgr->error_handler;
81                                 break;
82                         case EVT_WARNING:
83                                 msg_handler = event_mgr->warning_handler;
84                                 break;
85                         case EVT_INFO:
86                                 msg_handler = event_mgr->info_handler;
87                                 break;
88                         default:
89                                 break;
90                 }
91                 if(msg_handler == NULL) {
92                         return OPJ_FALSE;
93                 }
94         } else {
95                 return OPJ_FALSE;
96         }
97
98         if ((fmt != NULL) && (event_mgr != NULL)) {
99                 va_list arg;
100                 size_t str_length/*, i, j*/; /* UniPG */
101                 char message[MSG_SIZE];
102                 memset(message, 0, MSG_SIZE);
103                 /* initialize the optional parameter list */
104                 va_start(arg, fmt);
105                 /* check the length of the format string */
106                 str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
107                 /* parse the format string and put the result in 'message' */
108                 vsprintf(message, fmt, arg); /* UniPG */
109                 /* deinitialize the optional parameter list */
110                 va_end(arg);
111
112                 /* output the message to the user program */
113                 msg_handler(message, cinfo->client_data);
114         }
115
116         return OPJ_TRUE;
117 }
118
119 /* ----------------------------------------------------------------------- */
120 opj_bool OPJ_CALLCONV opj_event_msg_v2(opj_event_mgr_t* p_event_mgr, int event_type, const char *fmt, ...) {
121 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
122         opj_msg_callback msg_handler = 00;
123         void * l_data = 00;
124
125         if(p_event_mgr != 00) {
126                 switch(event_type) {
127                         case EVT_ERROR:
128                                 msg_handler = p_event_mgr->error_handler;
129                                 l_data = p_event_mgr->m_error_data;
130                                 break;
131                         case EVT_WARNING:
132                                 msg_handler = p_event_mgr->warning_handler;
133                                 l_data = p_event_mgr->m_warning_data;
134                                 break;
135                         case EVT_INFO:
136                                 msg_handler = p_event_mgr->info_handler;
137                                 l_data = p_event_mgr->m_info_data;
138                                 break;
139                         default:
140                                 break;
141                 }
142                 if(msg_handler == 00) {
143                         return OPJ_FALSE;
144                 }
145         } else {
146                 return OPJ_FALSE;
147         }
148
149         if ((fmt != 00) && (p_event_mgr != 00)) {
150                 va_list arg;
151                 size_t str_length/*, i, j*/; /* UniPG */
152                 char message[MSG_SIZE];
153                 memset(message, 0, MSG_SIZE);
154                 /* initialize the optional parameter list */
155                 va_start(arg, fmt);
156                 /* check the length of the format string */
157                 str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
158                 /* parse the format string and put the result in 'message' */
159                 vsprintf(message, fmt, arg); /* UniPG */
160                 /* deinitialize the optional parameter list */
161                 va_end(arg);
162
163                 /* output the message to the user program */
164                 msg_handler(message, l_data);
165         }
166
167         return OPJ_TRUE;
168 }
169
170 void OPJ_CALLCONV opj_set_default_event_handler(opj_event_mgr_t * p_manager)
171 {
172         p_manager->m_error_data = 00;
173         p_manager->m_warning_data = 00;
174         p_manager->m_info_data = 00;
175         p_manager->error_handler = opj_default_callback;
176         p_manager->info_handler = opj_default_callback;
177         p_manager->warning_handler = opj_default_callback;
178 }
179