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.
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include "opj_includes.h"
34 /* ==========================================================
36 // ==========================================================*/
40 i2a(unsigned i, char *a, unsigned r)
45 *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % r];
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
58 _itoa(int i, char *a, int r)
60 r = ((r < 2) || (r > 36)) ? 10 : r;
63 *i2a(-i, a + 1, r) = 0;
72 /* ----------------------------------------------------------------------- */
74 opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo,
75 opj_event_mgr_t *event_mgr, void *context)
78 opj_event_mgr_t *previous = cinfo->event_mgr;
79 cinfo->event_mgr = event_mgr;
80 cinfo->client_data = context;
87 bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...)
89 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
90 opj_msg_callback msg_handler = NULL;
92 opj_event_mgr_t *event_mgr = cinfo->event_mgr;
93 if (event_mgr != NULL) {
96 msg_handler = event_mgr->error_handler;
99 msg_handler = event_mgr->warning_handler;
102 msg_handler = event_mgr->info_handler;
107 if (msg_handler == NULL) {
114 if ((fmt != NULL) && (event_mgr != NULL)) {
116 int str_length, i, j;
117 char message[MSG_SIZE];
118 memset(message, 0, MSG_SIZE);
119 /* initialize the optional parameter list */
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) {
126 if (i + 1 < str_length) {
127 switch (tolower(fmt[i + 1])) {
131 case 'o' : { /* octal numbers */
133 _itoa(va_arg(arg, int), tmp, 8);
134 strcat(message, tmp);
139 case 'i' : /* decimal numbers */
142 _itoa(va_arg(arg, int), tmp, 10);
143 strcat(message, tmp);
148 case 'x' : { /* hexadecimal numbers */
150 _itoa(va_arg(arg, int), tmp, 16);
151 strcat(message, tmp);
156 case 's' : { /* strings */
157 char *tmp = va_arg(arg, char*);
158 strcat(message, tmp);
163 case 'f' : { /* floats */
165 double value = va_arg(arg, double);
166 sprintf(tmp, "%f", value);
167 strcat(message, tmp);
174 message[j++] = fmt[i];
177 message[j++] = fmt[i];
180 /* deinitialize the optional parameter list */
183 /* output the message to the user program */
184 msg_handler(message, cinfo->client_data);