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