comparison th_util.c @ 10:a25f5d22483e

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