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