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