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
|
|
14 /*
|
|
15 * Default settings
|
|
16 */
|
2
|
17 #ifdef TH_NO_DEFAULTS
|
|
18 #define TH_PROG_AUTHOR ""
|
|
19 #define TH_PROG_LICENSE ""
|
|
20 #else
|
0
|
21 #define TH_PROG_AUTHOR "By Matti 'ccr' Hämäläinen (C) Copyright 2008 TNSP"
|
|
22 #define TH_PROG_LICENSE "This software is licensed under GNU GPL version 2"
|
2
|
23 #endif
|
0
|
24
|
|
25 BOOL th_isInitialized = FALSE;
|
|
26 int th_verbosityLevel = 2;
|
|
27 char *th_prog_name = NULL,
|
|
28 *th_prog_fullname = NULL,
|
|
29 *th_prog_version = NULL,
|
|
30 *th_prog_author = NULL,
|
|
31 *th_prog_license = NULL;
|
|
32
|
|
33
|
|
34 /* Initialize th_util-library and global variables
|
|
35 */
|
|
36 void th_init(char *progName, char *progFullName, char *progVersion,
|
|
37 char *progAuthor, char *progLicense)
|
|
38 {
|
17
|
39 th_prog_name = progName;
|
|
40 th_prog_fullname = progFullName;
|
|
41 th_prog_version = progVersion;
|
0
|
42
|
|
43 if (progAuthor)
|
|
44 th_prog_author = progAuthor;
|
|
45 else
|
|
46 th_prog_author = TH_PROG_AUTHOR;
|
|
47
|
|
48 if (progLicense)
|
|
49 th_prog_license = progLicense;
|
|
50 else
|
|
51 th_prog_license = TH_PROG_LICENSE;
|
|
52
|
|
53 th_isInitialized = TRUE;
|
|
54 }
|
|
55
|
|
56
|
|
57 /* Print formatted error, warning and information messages
|
|
58 * TODO: Implement th_vfprintf() and friends?
|
|
59 */
|
|
60 void THERR(const char *pcFormat, ...)
|
|
61 {
|
|
62 va_list ap;
|
|
63 assert(th_isInitialized);
|
|
64
|
|
65 va_start(ap, pcFormat);
|
|
66 fprintf(stderr, "%s: ", th_prog_name);
|
|
67 vfprintf(stderr, pcFormat, ap);
|
|
68 va_end(ap);
|
|
69 }
|
|
70
|
|
71
|
|
72 void THMSG(int verbLevel, const char *pcFormat, ...)
|
|
73 {
|
|
74 va_list ap;
|
|
75 assert(th_isInitialized);
|
|
76
|
|
77 /* Check if the current verbosity level is enough */
|
|
78 if (th_verbosityLevel >= verbLevel) {
|
|
79 va_start(ap, pcFormat);
|
|
80 fprintf(stderr, "%s: ", th_prog_name);
|
|
81 vfprintf(stderr, pcFormat, ap);
|
|
82 va_end(ap);
|
|
83 }
|
|
84 }
|
|
85
|
|
86
|
|
87 void THPRINT(int verbLevel, const char *pcFormat, ...)
|
|
88 {
|
|
89 va_list ap;
|
|
90 assert(th_isInitialized);
|
|
91
|
|
92 /* Check if the current verbosity level is enough */
|
|
93 if (th_verbosityLevel >= verbLevel) {
|
|
94 va_start(ap, pcFormat);
|
|
95 vfprintf(stderr, pcFormat, ap);
|
|
96 va_end(ap);
|
|
97 }
|
|
98 }
|
|
99
|
|
100
|
2
|
101 /* Memory handling routines
|
0
|
102 */
|
|
103 void *th_malloc(size_t l)
|
|
104 {
|
|
105 return malloc(l);
|
|
106 }
|
|
107
|
|
108
|
|
109 void *th_calloc(size_t n, size_t l)
|
|
110 {
|
|
111 return calloc(n, l);
|
|
112 }
|
|
113
|
|
114
|
|
115 void *th_realloc(void *p, size_t l)
|
|
116 {
|
|
117 return realloc(p, l);
|
|
118 }
|
|
119
|
|
120
|
|
121 void th_free(void *p)
|
|
122 {
|
|
123 /* Check for NULL pointers for portability due to some libc
|
|
124 * implementations not handling free(NULL) too well.
|
|
125 */
|
|
126 if (p) free(p);
|
|
127 }
|
|
128
|
|
129
|
|
130 #ifndef HAVE_MEMSET
|
|
131 void *th_memset(void *p, int c, size_t n)
|
|
132 {
|
|
133 unsigned char *dp = (unsigned char *) p;
|
|
134
|
|
135 while (n--)
|
|
136 *(dp++) = c;
|
|
137
|
|
138 return p;
|
|
139 }
|
|
140 #endif
|