Merge pull request #647 from stweil/master
[openjpeg.git] / src / lib / openmj2 / 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 #ifdef OPJ_CODE_NOT_USED
39 #ifndef _WIN32
40 static char*
41 i2a(unsigned i, char *a, unsigned r) {
42         if (i/r > 0) a = i2a(i/r,a,r);
43         *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
44         return a+1;
45 }
46
47 /** 
48  Transforms integer i into an ascii string and stores the result in a; 
49  string is encoded in the base indicated by r.
50  @param i Number to be converted
51  @param a String result
52  @param r Base of value; must be in the range 2 - 36
53  @return Returns a
54 */
55 static char *
56 _itoa(int i, char *a, int r) {
57         r = ((r < 2) || (r > 36)) ? 10 : r;
58         if(i < 0) {
59                 *a = '-';
60                 *i2a(-i, a+1, r) = 0;
61         }
62         else *i2a(i, a, r) = 0;
63         return a;
64 }
65
66 #endif /* !_WIN32 */
67 #endif
68 /* ----------------------------------------------------------------------- */
69
70 opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) {
71         if(cinfo) {
72                 opj_event_mgr_t *previous = cinfo->event_mgr;
73                 cinfo->event_mgr = event_mgr;
74                 cinfo->client_data = context;
75                 return previous;
76         }
77
78         return NULL;
79 }
80
81 opj_bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) {
82 #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
83         opj_msg_callback msg_handler = NULL;
84
85         opj_event_mgr_t *event_mgr = cinfo->event_mgr;
86         if(event_mgr != NULL) {
87                 switch(event_type) {
88                         case EVT_ERROR:
89                                 msg_handler = event_mgr->error_handler;
90                                 break;
91                         case EVT_WARNING:
92                                 msg_handler = event_mgr->warning_handler;
93                                 break;
94                         case EVT_INFO:
95                                 msg_handler = event_mgr->info_handler;
96                                 break;
97                         default:
98                                 break;
99                 }
100                 if(msg_handler == NULL) {
101                         return OPJ_FALSE;
102                 }
103         } else {
104                 return OPJ_FALSE;
105         }
106
107         if ((fmt != NULL) && (event_mgr != NULL)) {
108                 va_list arg;
109                 int str_length/*, i, j*/; /* UniPG */
110                 char message[MSG_SIZE];
111                 /* initialize the optional parameter list */
112                 va_start(arg, fmt);
113                 /* parse the format string and put the result in 'message' */
114                 str_length = vsnprintf(message, MSG_SIZE, fmt, arg); /* UniPG */
115                 /* deinitialize the optional parameter list */
116                 va_end(arg);
117
118                 /* output the message to the user program */
119     if( str_length > -1 && str_length < MSG_SIZE )
120       msg_handler(message, cinfo->client_data);
121     else return OPJ_FALSE;
122         }
123
124         return OPJ_TRUE;
125 }
126