fixed minor bugs which were triggering warnings at compilation (different signedness...
[openjpeg.git] / libopenjpeg / event.c
1 /*
2  * Copyright (c) 2005, Herv� 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 "event.h"
28 #include "openjpeg.h"
29 #include "opj_includes.h"
30
31
32 /* ==========================================================
33      Utility functions
34    ==========================================================*/
35
36 #if !defined(_MSC_VER) && !defined(__MINGW32__)
37 static OPJ_CHAR*
38 i2a(OPJ_UINT32 i, OPJ_CHAR *a, OPJ_UINT32 r) {
39         if (i/r > 0) a = i2a(i/r,a,r);
40         *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
41         return a+1;
42 }
43 #endif
44 /* ----------------------------------------------------------------------- */
45
46 bool opj_event_msg(opj_event_mgr_t * p_event_mgr, OPJ_INT32 event_type, const OPJ_CHAR *fmt, ...) {
47 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
48         opj_msg_callback msg_handler = 00;
49         void * l_data = 00;
50
51         
52         if(p_event_mgr != 00) {
53                 switch(event_type) {
54                         case EVT_ERROR:
55                                 msg_handler = p_event_mgr->error_handler;
56                                 l_data = p_event_mgr->m_error_data;
57                                 break;
58                         case EVT_WARNING:
59                                 msg_handler = p_event_mgr->warning_handler;
60                                 l_data = p_event_mgr->m_warning_data;
61                                 break;
62                         case EVT_INFO:
63                                 msg_handler = p_event_mgr->info_handler;
64                                 l_data = p_event_mgr->m_info_data;
65                                 break;
66                         default:
67                                 break;
68                 }
69                 if(msg_handler == 00) {
70                         return false;
71                 }
72         } else {
73                 return false;
74         }
75
76         if ((fmt != 00) && (p_event_mgr != 00)) {
77                 va_list arg;
78                 OPJ_INT32 str_length/*, i, j*/; /* UniPG */
79                 OPJ_CHAR message[MSG_SIZE];
80                 memset(message, 0, MSG_SIZE);
81                 /* initialize the optional parameter list */
82                 va_start(arg, fmt);
83                 /* check the length of the format string */
84                 str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
85                 /* parse the format string and put the result in 'message' */
86                 vsprintf(message, fmt, arg); /* UniPG */
87                 /* deinitialize the optional parameter list */
88                 va_end(arg);
89
90                 /* output the message to the user program */
91                 msg_handler(message, l_data);
92         }
93
94         return true;
95 }
96