]> Creatis software - gdcm.git/blob - src/gdcmopenjpeg/libopenjpeg/event.c
ENH: This time for real. Install is ok. STYLE: some minor stuff
[gdcm.git] / src / gdcmopenjpeg / 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 "opj_includes.h"
28
29 /* ==========================================================
30      Utility functions
31    ==========================================================*/
32
33 #if !defined(_MSC_VER) && !defined(__MINGW32__)
34 static char*
35 i2a(unsigned i, char *a, unsigned r) {
36   if (i/r > 0) a = i2a(i/r,a,r);
37   *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
38   return a+1;
39 }
40
41 /** 
42  Transforms integer i into an ascii string and stores the result in a; 
43  string is encoded in the base indicated by r.
44  @param i Number to be converted
45  @param a String result
46  @param r Base of value; must be in the range 2 - 36
47  @return Returns a
48 */
49 static char *
50 _itoa(int i, char *a, int r) {
51   r = ((r < 2) || (r > 36)) ? 10 : r;
52   if(i < 0) {
53     *a = '-';
54     *i2a(-i, a+1, r) = 0;
55   }
56   else *i2a(i, a, r) = 0;
57   return a;
58 }
59
60 #endif /* !WIN32 */
61
62 /* ----------------------------------------------------------------------- */
63
64 opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
65   if(cinfo) {
66     opj_event_mgr_t *previous = cinfo->event_mgr;
67     cinfo->event_mgr = event_mgr;
68     cinfo->client_data = context;
69     return previous;
70   }
71
72   return NULL;
73 }
74
75 bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {
76 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
77   opj_msg_callback msg_handler = NULL;
78
79   opj_event_mgr_t *event_mgr = cinfo->event_mgr;
80   if(event_mgr != NULL) {
81     switch(event_type) {
82       case EVT_ERROR:
83         msg_handler = event_mgr->error_handler;
84         break;
85       case EVT_WARNING:
86         msg_handler = event_mgr->warning_handler;
87         break;
88       case EVT_INFO:
89         msg_handler = event_mgr->info_handler;
90         break;
91       default:
92         break;
93     }
94     if(msg_handler == NULL) {
95       return false;
96     }
97   } else {
98     return false;
99   }
100
101   if ((fmt != NULL) && (event_mgr != NULL)) {
102     va_list arg;
103     int str_length, i, j;
104     char message[MSG_SIZE];
105     memset(message, 0, MSG_SIZE);
106     /* initialize the optional parameter list */
107     va_start(arg, fmt);
108     /* check the length of the format string */
109     str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt);
110     /* parse the format string and put the result in 'message' */
111     for (i = 0, j = 0; i < str_length; ++i) {
112       if (fmt[i] == '%') {
113         if (i + 1 < str_length) {
114           switch(tolower(fmt[i + 1])) {
115             case '%' :
116               message[j++] = '%';
117               break;
118             case 'o' : /* octal numbers */
119             {
120               char tmp[16];
121               _itoa(va_arg(arg, int), tmp, 8);
122               strcat(message, tmp);
123               j += strlen(tmp);
124               ++i;
125               break;
126             }
127             case 'i' : /* decimal numbers */
128             case 'd' :
129             {
130               char tmp[16];
131               _itoa(va_arg(arg, int), tmp, 10);
132               strcat(message, tmp);
133               j += strlen(tmp);
134               ++i;
135               break;
136             }
137             case 'x' : /* hexadecimal numbers */
138             {
139               char tmp[16];
140               _itoa(va_arg(arg, int), tmp, 16);
141               strcat(message, tmp);
142               j += strlen(tmp);
143               ++i;
144               break;
145             }
146             case 's' : /* strings */
147             {
148               char *tmp = va_arg(arg, char*);
149               strcat(message, tmp);
150               j += strlen(tmp);
151               ++i;
152               break;
153             }
154             case 'f' :  /* floats */
155             {
156               char tmp[16];
157               double value = va_arg(arg, double);
158               sprintf(tmp, "%f", value);
159               strcat(message, tmp);
160               j += strlen(tmp);
161               ++i;
162               break;
163             }
164           };
165         } else {
166           message[j++] = fmt[i];
167         }
168       } else {
169         message[j++] = fmt[i];
170       };
171     }
172     /* deinitialize the optional parameter list */
173     va_end(arg);
174
175     /* output the message to the user program */
176     msg_handler(message, cinfo->client_data);
177   }
178
179   return true;
180 }
181