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