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) {
41 if (i/r > 0) a = i2a(i/r,a,r);
42 *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
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
55 _itoa(int i, char *a, int r) {
56 r = ((r < 2) || (r > 36)) ? 10 : r;
61 else *i2a(i, a, r) = 0;
67 /* ----------------------------------------------------------------------- */
69 opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
71 opj_event_mgr_t *previous = cinfo->event_mgr;
72 cinfo->event_mgr = event_mgr;
73 cinfo->client_data = context;
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;
84 opj_event_mgr_t *event_mgr = cinfo->event_mgr;
85 if(event_mgr != NULL) {
88 msg_handler = event_mgr->error_handler;
91 msg_handler = event_mgr->warning_handler;
94 msg_handler = event_mgr->info_handler;
99 if(msg_handler == NULL) {
106 if ((fmt != NULL) && (event_mgr != NULL)) {
108 int str_length, i, j;
109 char message[MSG_SIZE];
110 memset(message, 0, MSG_SIZE);
111 /* initialize the optional parameter list */
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) {
118 if (i + 1 < str_length) {
119 switch(tolower(fmt[i + 1])) {
123 case 'o' : /* octal numbers */
126 _itoa(va_arg(arg, int), tmp, 8);
127 strcat(message, tmp);
132 case 'i' : /* decimal numbers */
136 _itoa(va_arg(arg, int), tmp, 10);
137 strcat(message, tmp);
142 case 'x' : /* hexadecimal numbers */
145 _itoa(va_arg(arg, int), tmp, 16);
146 strcat(message, tmp);
151 case 's' : /* strings */
153 char *tmp = va_arg(arg, char*);
154 strcat(message, tmp);
159 case 'f' : /* floats */
162 double value = va_arg(arg, double);
163 sprintf(tmp, "%f", value);
164 strcat(message, tmp);
171 message[j++] = fmt[i];
174 message[j++] = fmt[i];
177 /* deinitialize the optional parameter list */
180 /* output the message to the user program */
181 msg_handler(message, cinfo->client_data);