Debian Codegraph @

matio-1.5.0/src/io.c

raw
1 /** @file io.c
2 * MAT File I/O Utility Functions
3 */
4 /*
5 * Copyright (C) 2005-2011 Christopher C. Hulbert
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
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 CHRISTOPHER C. HULBERT ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER C. HULBERT 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 <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include "matio_private.h"
37
38 #if !defined(HAVE_VA_COPY) && defined(HAVE___VA_COPY)
39 # define va_copy(d,s) __va_copy(d,s)
40 #elif !defined(HAVE_VA_COPY)
41 # define va_copy(d,s) memcpy(&(d),&(s),sizeof(va_list))
42 #endif
43 #ifndef HAVE_VSNPRINTF
44 # define vsnprintf mat_vsnprintf
45 # ifdef __cplusplus
46 extern "C" int vsnprintf(char *,size_t,const char *,va_list);
47 # else
48 extern int vsnprintf(char *,size_t,const char *,va_list);
49 # endif
50 #endif
51 #ifndef HAVE_SNPRINTF
52 # define snprintf mat_snprintf
53 # ifdef __cplusplus
54 extern "C" int snprintf(char *str,size_t size,const char *format,...);
55 # else
56 extern int snprintf(char *str,size_t size,const char *format,...);
57 # endif
58 #endif
59 #ifndef HAVE_VASPRINTF
60 # define vasprintf mat_vasprintf
61 #endif
62 #ifndef HAVE_ASPRINTF
63 # define asprintf mat_asprintf
64 #endif
65
66 /** @cond 0 */
67 #define LOG_LEVEL_ERROR 1
68 #define LOG_LEVEL_CRITICAL 1 << 1
69 #define LOG_LEVEL_WARNING 1 << 2
70 #define LOG_LEVEL_MESSAGE 1 << 3
71 #define LOG_LEVEL_DEBUG 1 << 4
72 /** @endcond */
73
74 static void (*logfunc)(int log_level, char *message ) = NULL;
75 static const char *progname = NULL;
76
77 /** @brief Allocates and prints to a new string
78 *
79 * @ingroup mat_util
80 * @param format format string
81 * @param ap variable argument list
82 * @return Newly allocated string with format printed to it
83 */
84 char *
85 strdup_vprintf(const char* format, va_list ap)
86 {
87 va_list ap2;
88 int size;
89 char* buffer;
90
91 va_copy(ap2, ap);
92 size = vsnprintf(NULL, 0, format, ap2)+1;
93 va_end(ap2);
94
95 buffer = malloc(size+1);
96 if ( !buffer )
97 return NULL;
98
99 vsnprintf(buffer, size, format, ap);
100 return buffer;
101 }
102
103 /** @brief Allocates and prints to a new string using printf format
104 *
105 * @ingroup mat_util
106 * @param format format string
107 * @return Pointer to resulting string, or NULL if there was an error
108 */
109 char *
110 strdup_printf(const char* format, ...)
111 {
112 char* buffer;
113 va_list ap;
114 va_start(ap, format);
115 buffer = strdup_vprintf(format, ap);
116 va_end(ap);
117 return buffer;
118 }
119
120 static void
121 matio_error_func( int log_level, char *message )
122 {
123
124 if ( progname ) {
125 if ( log_level & LOG_LEVEL_CRITICAL) {
126 fprintf(stderr,"-E- %s: %s\n", progname, message);
127 fflush(stderr);
128 } else if ( log_level & LOG_LEVEL_ERROR ) {
129 fprintf(stderr,"-E- %s: %s\n", progname, message);
130 fflush(stderr);
131 abort();
132 } else if ( log_level & LOG_LEVEL_WARNING ) {
133 fprintf(stderr,"-W- %s: %s\n", progname, message);
134 fflush(stderr);
135 } else if ( log_level & LOG_LEVEL_DEBUG ) {
136 fprintf(stderr,"-D- %s: %s\n", progname, message);
137 fflush(stderr);
138 } else if ( log_level & LOG_LEVEL_MESSAGE ) {
139 fprintf(stdout,"%s\n", message);
140 fflush(stdout);
141 }
142 } else {
143 if ( log_level & LOG_LEVEL_CRITICAL) {
144 fprintf(stderr,"-E- : %s\n", message);
145 fflush(stderr);
146 } else if ( log_level & LOG_LEVEL_ERROR ) {
147 fprintf(stderr,"-E- : %s\n", message);
148 fflush(stderr);
149 abort();
150 } else if ( log_level & LOG_LEVEL_WARNING ) {
151 fprintf(stderr,"-W- : %s\n", message);
152 fflush(stderr);
153 } else if ( log_level & LOG_LEVEL_DEBUG ) {
154 fprintf(stderr,"-D- : %s\n", message);
155 fflush(stderr);
156 } else if ( log_level & LOG_LEVEL_MESSAGE ) {
157 fprintf(stdout,"%s\n", message);
158 fflush(stdout);
159 }
160 }
161
162 }
163
164 static void
165 mat_log(int loglevel, const char *format, va_list ap)
166 {
167 char* buffer;
168
169 if ( !logfunc ) return;
170 buffer = strdup_vprintf(format, ap);
171 (*logfunc)(loglevel,buffer);
172 free(buffer);
173 return;
174 }
175
176
177 /** @var debug
178 * @brief holds the verbose level set in @ref SetVerbose
179 * This variable is used to determine if information should be printed to
180 * the screen
181 * @ingroup mat_util
182 */
183 static int debug = 0;
184
185 /** @var verbose
186 * @brief holds the verbose level set in @ref SetVerbose
187 * This variable is used to determine if information should be printed to
188 * the screen
189 * @ingroup mat_util
190 */
191 static int verbose = 0;
192 /** @var silent
193 * @brief holds the silent level set in @ref SetVerbose
194 * If set, all output which is not an error is not displayed regardless
195 * of verbose level
196 * @ingroup mat_util
197 */
198 static int silent = 0;
199 /** @brief Sets verbose parameters
200 *
201 * Sets the verbose level and silent level. These values are used by
202 * programs to determine what information should be printed to the screen
203 * @ingroup mat_util
204 * @param verb sets logging verbosity level
205 * @param s sets logging silent level
206 */
207 int
208 Mat_SetVerbose( int verb, int s )
209 {
210
211 verbose = verb;
212 silent = s;
213
214 return 0;
215 }
216
217 /** @brief Sets verbose parameters
218 *
219 * Sets the verbose level and silent level. These values are used by
220 * programs to determine what information should be printed to the screen
221 * @ingroup mat_util
222 * @param verb sets logging verbosity level
223 * @param s sets logging silent level
224 */
225 int
226 Mat_SetDebug( int d )
227 {
228 debug = d;
229 return 0;
230 }
231
232 /** @brief Log a message unless silent
233 *
234 * Logs the message unless the silent option is set (See @ref SetVerbose).
235 * To log a message based on the verbose level, use @ref Mat_VerbMessage
236 * @ingroup mat_util
237 * @param format message format
238 */
239 int Mat_Message( const char *format, ... )
240 {
241 va_list ap;
242
243 if ( silent ) return 0;
244 if ( !logfunc ) return 0;
245
246 va_start(ap, format );
247 mat_log(LOG_LEVEL_MESSAGE, format, ap );
248 va_end(ap);
249 return 0;
250 }
251
252 /** @brief Log a message based on verbose level
253 *
254 * If @e level is less than or equal to the set verbose level, the message
255 * is printed. If the level is higher than the set verbose level nothing
256 * is displayed.
257 * @ingroup mat_util
258 * @param level verbose level
259 * @param format message format
260 */
261 int Mat_DebugMessage( int level, const char *format, ... )
262 {
263 va_list ap;
264
265 if ( silent ) return 0;
266 if ( level > debug ) return 0;
267
268 va_start(ap, format );
269 mat_log(LOG_LEVEL_DEBUG, format, ap );
270 va_end(ap);
271 return 0;
272 }
273
274 /** @brief Log a message based on verbose level
275 *
276 * If @e level is less than or equal to the set verbose level, the message
277 * is printed. If the level is higher than the set verbose level nothing
278 * is displayed.
279 * @ingroup mat_util
280 * @param level verbose level
281 * @param format message format
282 */
283 int Mat_VerbMessage( int level, const char *format, ... )
284 {
285 va_list ap;
286
287 if ( silent ) return 0;
288 if ( level > verbose ) return 0;
289
290 va_start(ap, format );
291 mat_log(LOG_LEVEL_MESSAGE, format, ap );
292 va_end(ap);
293 return 0;
294 }
295
296 /** @brief Logs a Critical message and returns to the user
297 *
298 * Logs a Critical message and returns to the user. If the program should
299 * stop running, use @ref Mat_Error
300 * @ingroup mat_util
301 * @param format format string identical to printf format
302 * @param ... arguments to the format string
303 */
304 void Mat_Critical( const char *format, ... )
305 {
306 va_list ap;
307
308 va_start(ap, format );
309 mat_log(LOG_LEVEL_CRITICAL, format, ap );
310 va_end(ap);
311 }
312
313 /** @brief Logs a Critical message and aborts the program
314 *
315 * Logs an Error message and aborts
316 * @ingroup mat_util
317 * @param format format string identical to printf format
318 * @param ... arguments to the format string
319 */
320 void Mat_Error( const char *format, ... )
321 {
322 va_list ap;
323
324 va_start(ap, format );
325 mat_log( LOG_LEVEL_ERROR, format, ap );
326 va_end(ap);
327 }
328
329 /** @brief Prints a helpstring to stdout and exits with status 1
330 *
331 * Prints the array of strings to stdout and exits with status 1. The array
332 * of strings should have NULL as its last element
333 * @code
334 * char *helpstr[] = {"My Help string line1","My help string line 2",NULL};
335 * Mat_Help(helpstr);
336 * @endcode
337 * @ingroup mat_util
338 * @param helpstr array of strings with NULL as its last element
339 */
340 void Mat_Help( const char *helpstr[] )
341 {
342 int i;
343 for (i = 0; helpstr[i] != NULL; i++)
344 printf("%s\n",helpstr[i]);
345 exit(EXIT_SUCCESS);
346 }
347
348 /** @brief Closes the logging system
349 *
350 * @ingroup mat_util
351 * @retval 1
352 */
353 int
354 Mat_LogClose( void )
355 {
356 logfunc = NULL;
357 return 1;
358 }
359
360 /** @brief Intializes the logging system
361 *
362 * @ingroup mat_util
363 * @param prog_name Name of the program initializing the logging functions
364 * @return 0 on success
365 */
366 int
367 Mat_LogInit( const char *prog_name )
368 {
369 logfunc = &matio_error_func;
370
371 verbose = 0;
372 silent = 0;
373
374 return 0;
375 }
376
377 /** @brief Intializes the logging system
378 *
379 * @ingroup mat_util
380 * @param prog_name Name of the program initializing the logging functions
381 * @param log_func pointer to the function to do the logging
382 * @return 0 on success
383 */
384 int
385 Mat_LogInitFunc(const char *prog_name,
386 void (*log_func)(int log_level,char *message))
387 {
388 logfunc = log_func;
389 progname = prog_name;
390
391 verbose = 0;
392 silent = 0;
393 return 0;
394 }
395
396 /** @brief Prints a warning message to stdout
397 *
398 * Logs a warning message then returns
399 * @ingroup mat_util
400 * @param format format string identical to printf format
401 * @param ... arguments to the format string
402 */
403 void
404 Mat_Warning( const char *format, ... )
405 {
406 va_list ap;
407
408 va_start(ap, format );
409 mat_log(LOG_LEVEL_WARNING, format, ap );
410 va_end(ap);
411 }
412
413 /** @brief Calculate the size of MAT data types
414 *
415 * @ingroup mat_util
416 * @param data_type Data type enumeration
417 * @return size of the data type in bytes
418 */
419 size_t
420 Mat_SizeOf(enum matio_types data_type)
421 {
422 switch (data_type) {
423 case MAT_T_DOUBLE:
424 return sizeof(double);
425 case MAT_T_SINGLE:
426 return sizeof(float);
427 #ifdef HAVE_MAT_INT64_T
428 case MAT_T_INT64:
429 return sizeof(mat_int64_t);
430 #endif
431 #ifdef HAVE_MAT_INT64_T
432 case MAT_T_UINT64:
433 return sizeof(mat_uint64_t);
434 #endif
435 case MAT_T_INT32:
436 return sizeof(mat_int32_t);
437 case MAT_T_UINT32:
438 return sizeof(mat_uint32_t);
439 case MAT_T_INT16:
440 return sizeof(mat_int16_t);
441 case MAT_T_UINT16:
442 return sizeof(mat_uint16_t);
443 case MAT_T_INT8:
444 return sizeof(mat_int8_t);
445 case MAT_T_UINT8:
446 return sizeof(mat_uint8_t);
447 default:
448 return 0;
449 }
450 }