WIP: fix build compilation error with windows platform about event_mgr management
[openjpeg.git] / libopenjpeg / 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 opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
66         if(cinfo) {
67                 opj_event_mgr_t *previous = cinfo->event_mgr;
68                 cinfo->event_mgr = event_mgr;
69                 cinfo->client_data = context;
70                 return previous;
71         }
72
73         return NULL;
74 }
75
76 /* ----------------------------------------------------------------------- */
77 opj_bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {
78 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
79         opj_msg_callback msg_handler = NULL;
80
81         opj_event_mgr_t *event_mgr = cinfo->event_mgr;
82         if(event_mgr != NULL) {
83                 switch(event_type) {
84                         case EVT_ERROR:
85                                 msg_handler = event_mgr->error_handler;
86                                 break;
87                         case EVT_WARNING:
88                                 msg_handler = event_mgr->warning_handler;
89                                 break;
90                         case EVT_INFO:
91                                 msg_handler = event_mgr->info_handler;
92                                 break;
93                         default:
94                                 break;
95                 }
96                 if(msg_handler == NULL) {
97                         return OPJ_FALSE;
98                 }
99         } else {
100                 return OPJ_FALSE;
101         }
102
103         if ((fmt != NULL) && (event_mgr != NULL)) {
104                 va_list arg;
105                 int str_length/*, i, j*/; /* UniPG */
106                 char message[MSG_SIZE];
107                 memset(message, 0, MSG_SIZE);
108                 /* initialize the optional parameter list */
109                 va_start(arg, fmt);
110                 /* check the length of the format string */
111                 str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
112                 /* parse the format string and put the result in 'message' */
113                 vsprintf(message, fmt, arg); /* UniPG */
114                 /* deinitialize the optional parameter list */
115                 va_end(arg);
116
117                 /* output the message to the user program */
118                 msg_handler(message, cinfo->client_data);
119         }
120
121         return OPJ_TRUE;
122 }
123
124 /* ----------------------------------------------------------------------- */
125 opj_bool opj_event_msg_v2(opj_event_mgr_t* event_mgr, int event_type, const char *fmt, ...) {
126 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
127         opj_msg_callback msg_handler = NULL;
128
129         if(event_mgr != NULL) {
130                 switch(event_type) {
131                         case EVT_ERROR:
132                                 msg_handler = event_mgr->error_handler;
133                                 break;
134                         case EVT_WARNING:
135                                 msg_handler = event_mgr->warning_handler;
136                                 break;
137                         case EVT_INFO:
138                                 msg_handler = event_mgr->info_handler;
139                                 break;
140                         default:
141                                 break;
142                 }
143                 if(msg_handler == NULL) {
144                         return OPJ_FALSE;
145                 }
146         } else {
147                 return OPJ_FALSE;
148         }
149
150         if ((fmt != NULL) && (event_mgr != NULL)) {
151                 va_list arg;
152                 int str_length/*, i, j*/; /* UniPG */
153                 char message[MSG_SIZE];
154                 memset(message, 0, MSG_SIZE);
155                 /* initialize the optional parameter list */
156                 va_start(arg, fmt);
157                 /* check the length of the format string */
158                 str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
159                 /* parse the format string and put the result in 'message' */
160                 vsprintf(message, fmt, arg); /* UniPG */
161                 /* deinitialize the optional parameter list */
162                 va_end(arg);
163
164                 /* output the message to the user program */
165                 msg_handler(message, event_mgr->client_data);
166         }
167
168         return OPJ_TRUE;
169 }
170
171 /* ----------------------------------------------------------------------- */
172 void OPJ_CALLCONV opj_set_default_event_handler(opj_event_mgr_t * p_manager, opj_bool verbose)
173 {
174         p_manager->client_data = NULL;
175         p_manager->error_handler = opj_error_default_callback;
176
177         if (verbose) {
178                 p_manager->info_handler = opj_info_default_callback;
179                 p_manager->warning_handler = opj_warning_default_callback;
180         }
181         else {
182                 /* FIXME (MSD) This message should be remove when the documentation will be updated */
183                 fprintf(stdout, "[INFO] Verbose mode = OFF => no other info/warning output.\n");
184                 p_manager->info_handler = opj_default_callback ;
185                 p_manager->warning_handler = opj_default_callback ;
186         }
187 }
188
189 /* ---------------------------------------------------------------------- */
190 /* Default callback functions                                             */
191
192 /**
193  * Default callback function.
194  * Do nothing.
195  */
196 void opj_default_callback (const char *msg, void *client_data)
197 {
198 }
199
200 /**
201  * Default info callback function.
202  * Output = stdout.
203  */
204 void opj_info_default_callback (const char *msg, void *client_data)
205 {
206         (void)client_data;
207         fprintf(stdout, "[INFO] %s", msg);
208 }
209
210 /**
211  * Default warning callback function.
212  * Output = stderr.
213  */
214 void opj_warning_default_callback (const char *msg, void *client_data)
215 {
216         (void)client_data;
217         fprintf(stderr, "[WARNING] %s", msg);
218 }
219
220 /**
221  * Default error callback function.
222  * Output = stderr.
223  */
224 void opj_error_default_callback (const char *msg, void *client_data)
225 {
226         (void)client_data;
227         fprintf(stderr, "[ERROR] %s", msg);
228 }