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