Optimize opj_t1_update_flags()
[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 {
42     if (i / r > 0) {
43         a = i2a(i / r, a, r);
44     }
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 {
60     r = ((r < 2) || (r > 36)) ? 10 : r;
61     if (i < 0) {
62         *a = '-';
63         *i2a(-i, a + 1, r) = 0;
64     } else {
65         *i2a(i, a, r) = 0;
66     }
67     return a;
68 }
69
70 #endif /* !_WIN32 */
71
72 /* ----------------------------------------------------------------------- */
73
74 opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo,
75         opj_event_mgr_t *event_mgr, void *context)
76 {
77     if (cinfo) {
78         opj_event_mgr_t *previous = cinfo->event_mgr;
79         cinfo->event_mgr = event_mgr;
80         cinfo->client_data = context;
81         return previous;
82     }
83
84     return NULL;
85 }
86
87 bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...)
88 {
89 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
90     opj_msg_callback msg_handler = NULL;
91
92     opj_event_mgr_t *event_mgr = cinfo->event_mgr;
93     if (event_mgr != NULL) {
94         switch (event_type) {
95         case EVT_ERROR:
96             msg_handler = event_mgr->error_handler;
97             break;
98         case EVT_WARNING:
99             msg_handler = event_mgr->warning_handler;
100             break;
101         case EVT_INFO:
102             msg_handler = event_mgr->info_handler;
103             break;
104         default:
105             break;
106         }
107         if (msg_handler == NULL) {
108             return false;
109         }
110     } else {
111         return false;
112     }
113
114     if ((fmt != NULL) && (event_mgr != NULL)) {
115         va_list arg;
116         int str_length, i, j;
117         char message[MSG_SIZE];
118         memset(message, 0, MSG_SIZE);
119         /* initialize the optional parameter list */
120         va_start(arg, fmt);
121         /* check the length of the format string */
122         str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
123         /* parse the format string and put the result in 'message' */
124         for (i = 0, j = 0; i < str_length; ++i) {
125             if (fmt[i] == '%') {
126                 if (i + 1 < str_length) {
127                     switch (tolower(fmt[i + 1])) {
128                     case '%' :
129                         message[j++] = '%';
130                         break;
131                     case 'o' : { /* octal numbers */
132                         char tmp[16];
133                         _itoa(va_arg(arg, int), tmp, 8);
134                         strcat(message, tmp);
135                         j += strlen(tmp);
136                         ++i;
137                         break;
138                     }
139                     case 'i' : /* decimal numbers */
140                     case 'd' : {
141                         char tmp[16];
142                         _itoa(va_arg(arg, int), tmp, 10);
143                         strcat(message, tmp);
144                         j += strlen(tmp);
145                         ++i;
146                         break;
147                     }
148                     case 'x' : { /* hexadecimal numbers */
149                         char tmp[16];
150                         _itoa(va_arg(arg, int), tmp, 16);
151                         strcat(message, tmp);
152                         j += strlen(tmp);
153                         ++i;
154                         break;
155                     }
156                     case 's' : { /* strings */
157                         char *tmp = va_arg(arg, char*);
158                         strcat(message, tmp);
159                         j += strlen(tmp);
160                         ++i;
161                         break;
162                     }
163                     case 'f' : { /* floats */
164                         char tmp[16];
165                         double value = va_arg(arg, double);
166                         sprintf(tmp, "%f", value);
167                         strcat(message, tmp);
168                         j += strlen(tmp);
169                         ++i;
170                         break;
171                     }
172                     };
173                 } else {
174                     message[j++] = fmt[i];
175                 }
176             } else {
177                 message[j++] = fmt[i];
178             };
179         }
180         /* deinitialize the optional parameter list */
181         va_end(arg);
182
183         /* output the message to the user program */
184         msg_handler(message, cinfo->client_data);
185     }
186
187     return true;
188 }
189