struct opj_j2k: remove unused fields, and add some documentation
[openjpeg.git] / src / lib / openjp2 / 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  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
9  * Copyright (c) 2012, CS Systemes d'Information, France
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "opj_includes.h"
35
36 /* ==========================================================
37      Utility functions
38    ==========================================================*/
39
40 #ifdef OPJ_CODE_NOT_USED
41 #ifndef _WIN32
42 static char*
43 i2a(unsigned i, char *a, unsigned r)
44 {
45     if (i / r > 0) {
46         a = i2a(i / r, a, r);
47     }
48     *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % r];
49     return a + 1;
50 }
51
52 /**
53  Transforms integer i into an ascii string and stores the result in a;
54  string is encoded in the base indicated by r.
55  @param i Number to be converted
56  @param a String result
57  @param r Base of value; must be in the range 2 - 36
58  @return Returns a
59 */
60 static char *
61 _itoa(int i, char *a, int r)
62 {
63     r = ((r < 2) || (r > 36)) ? 10 : r;
64     if (i < 0) {
65         *a = '-';
66         *i2a(-i, a + 1, r) = 0;
67     } else {
68         *i2a(i, a, r) = 0;
69     }
70     return a;
71 }
72
73 #endif /* !_WIN32 */
74 #endif
75
76 /* ----------------------------------------------------------------------- */
77 /**
78  * Default callback function.
79  * Do nothing.
80  */
81 static void opj_default_callback(const char *msg, void *client_data)
82 {
83     OPJ_ARG_NOT_USED(msg);
84     OPJ_ARG_NOT_USED(client_data);
85 }
86
87 /* ----------------------------------------------------------------------- */
88
89
90 /* ----------------------------------------------------------------------- */
91 OPJ_BOOL opj_event_msg(opj_event_mgr_t* p_event_mgr, OPJ_INT32 event_type,
92                        const char *fmt, ...)
93 {
94 #define OPJ_MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
95     opj_msg_callback msg_handler = 00;
96     void * l_data = 00;
97
98     if (p_event_mgr != 00) {
99         switch (event_type) {
100         case EVT_ERROR:
101             msg_handler = p_event_mgr->error_handler;
102             l_data = p_event_mgr->m_error_data;
103             break;
104         case EVT_WARNING:
105             msg_handler = p_event_mgr->warning_handler;
106             l_data = p_event_mgr->m_warning_data;
107             break;
108         case EVT_INFO:
109             msg_handler = p_event_mgr->info_handler;
110             l_data = p_event_mgr->m_info_data;
111             break;
112         default:
113             break;
114         }
115         if (msg_handler == 00) {
116             return OPJ_FALSE;
117         }
118     } else {
119         return OPJ_FALSE;
120     }
121
122     if ((fmt != 00) && (p_event_mgr != 00)) {
123         va_list arg;
124         char message[OPJ_MSG_SIZE];
125         memset(message, 0, OPJ_MSG_SIZE);
126         /* initialize the optional parameter list */
127         va_start(arg, fmt);
128         /* parse the format string and put the result in 'message' */
129         vsnprintf(message, OPJ_MSG_SIZE, fmt, arg);
130         /* force zero termination for Windows _vsnprintf() of old MSVC */
131         message[OPJ_MSG_SIZE - 1] = '\0';
132         /* deinitialize the optional parameter list */
133         va_end(arg);
134
135         /* output the message to the user program */
136         msg_handler(message, l_data);
137     }
138
139     return OPJ_TRUE;
140 }
141
142 void opj_set_default_event_handler(opj_event_mgr_t * p_manager)
143 {
144     p_manager->m_error_data = 00;
145     p_manager->m_warning_data = 00;
146     p_manager->m_info_data = 00;
147     p_manager->error_handler = opj_default_callback;
148     p_manager->info_handler = opj_default_callback;
149     p_manager->warning_handler = opj_default_callback;
150 }
151