0
|
1 /*
|
|
2 * Generic utility-functions, macros and defaults
|
|
3 * Programmed and designed by Matti 'ccr' Hamalainen
|
|
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP)
|
|
5 *
|
|
6 * Please read file 'COPYING' for information on license and distribution.
|
|
7 */
|
|
8 #ifdef HAVE_CONFIG_H
|
2
|
9 #include "config.h"
|
0
|
10 #endif
|
|
11 #include "th_util.h"
|
|
12 #include <stdio.h>
|
|
13
|
20
|
14 /* Default settings
|
0
|
15 */
|
81
|
16 static BOOL th_initialized = FALSE;
|
|
17 int th_verbosityLevel = 2;
|
|
18 char *th_prog_name = NULL,
|
|
19 *th_prog_fullname = NULL,
|
|
20 *th_prog_version = NULL,
|
|
21 *th_prog_author = NULL,
|
|
22 *th_prog_license = NULL;
|
0
|
23
|
|
24
|
|
25 /* Initialize th_util-library and global variables
|
|
26 */
|
|
27 void th_init(char *progName, char *progFullName, char *progVersion,
|
81
|
28 char *progAuthor, char *progLicense)
|
0
|
29 {
|
81
|
30 th_prog_name = progName;
|
|
31 th_prog_fullname = progFullName;
|
|
32 th_prog_version = progVersion;
|
0
|
33
|
81
|
34 if (progAuthor)
|
|
35 th_prog_author = progAuthor;
|
|
36 else
|
|
37 th_prog_author = TH_PROG_AUTHOR;
|
0
|
38
|
81
|
39 if (progLicense)
|
|
40 th_prog_license = progLicense;
|
|
41 else
|
|
42 th_prog_license = TH_PROG_LICENSE;
|
0
|
43
|
81
|
44 th_initialized = TRUE;
|
0
|
45 }
|
|
46
|
|
47
|
|
48 /* Print formatted error, warning and information messages
|
|
49 * TODO: Implement th_vfprintf() and friends?
|
|
50 */
|
|
51 void THERR(const char *pcFormat, ...)
|
|
52 {
|
81
|
53 va_list ap;
|
|
54 assert(th_initialized);
|
0
|
55
|
81
|
56 va_start(ap, pcFormat);
|
|
57 fprintf(stderr, "%s: ", th_prog_name);
|
|
58 vfprintf(stderr, pcFormat, ap);
|
|
59 va_end(ap);
|
0
|
60 }
|
|
61
|
|
62
|
|
63 void THMSG(int verbLevel, const char *pcFormat, ...)
|
|
64 {
|
81
|
65 va_list ap;
|
|
66 assert(th_initialized);
|
0
|
67
|
81
|
68 /* Check if the current verbosity level is enough */
|
|
69 if (th_verbosityLevel >= verbLevel) {
|
|
70 va_start(ap, pcFormat);
|
|
71 fprintf(stderr, "%s: ", th_prog_name);
|
|
72 vfprintf(stderr, pcFormat, ap);
|
|
73 va_end(ap);
|
|
74 }
|
0
|
75 }
|
|
76
|
|
77
|
|
78 void THPRINT(int verbLevel, const char *pcFormat, ...)
|
|
79 {
|
81
|
80 va_list ap;
|
|
81 assert(th_initialized);
|
0
|
82
|
81
|
83 /* Check if the current verbosity level is enough */
|
|
84 if (th_verbosityLevel >= verbLevel) {
|
|
85 va_start(ap, pcFormat);
|
|
86 vfprintf(stderr, pcFormat, ap);
|
|
87 va_end(ap);
|
|
88 }
|
0
|
89 }
|
|
90
|
|
91
|
2
|
92 /* Memory handling routines
|
0
|
93 */
|
|
94 void *th_malloc(size_t l)
|
|
95 {
|
81
|
96 return malloc(l);
|
0
|
97 }
|
|
98
|
|
99
|
|
100 void *th_calloc(size_t n, size_t l)
|
|
101 {
|
81
|
102 return calloc(n, l);
|
0
|
103 }
|
|
104
|
|
105
|
|
106 void *th_realloc(void *p, size_t l)
|
|
107 {
|
81
|
108 return realloc(p, l);
|
0
|
109 }
|
|
110
|
|
111
|
|
112 void th_free(void *p)
|
|
113 {
|
81
|
114 /* Check for NULL pointers for portability due to some libc
|
|
115 * implementations not handling free(NULL) too well.
|
|
116 */
|
|
117 if (p) free(p);
|
0
|
118 }
|
|
119
|
|
120
|
|
121 #ifndef HAVE_MEMSET
|
|
122 void *th_memset(void *p, int c, size_t n)
|
|
123 {
|
81
|
124 unsigned char *dp = (unsigned char *) p;
|
|
125
|
|
126 while (n--)
|
|
127 *(dp++) = c;
|
0
|
128
|
81
|
129 return p;
|
0
|
130 }
|
|
131 #endif
|