annotate th_util.c @ 99:0313fabd8049

Added varargs versions of some functions.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 17 Nov 2009 23:08:15 +0200
parents 69aed051f84d
children 4ec36204d34e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Generic utility-functions, macros and defaults
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #ifdef HAVE_CONFIG_H
2
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
9 #include "config.h"
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #endif
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "th_util.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <stdio.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
20
323c98360d8b Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
14 /* Default settings
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 */
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
16 static BOOL th_initialized = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
17 int th_verbosityLevel = 2;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
18 char *th_prog_name = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
19 *th_prog_fullname = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
20 *th_prog_version = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
21 *th_prog_author = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
22 *th_prog_license = NULL;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 /* Initialize th_util-library and global variables
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 void th_init(char *progName, char *progFullName, char *progVersion,
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
28 char *progAuthor, char *progLicense)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
30 th_prog_name = progName;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
31 th_prog_fullname = progFullName;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
32 th_prog_version = progVersion;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
34 if (progAuthor)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
35 th_prog_author = progAuthor;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
36 else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
37 th_prog_author = TH_PROG_AUTHOR;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
39 if (progLicense)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
40 th_prog_license = progLicense;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
41 else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
42 th_prog_license = TH_PROG_LICENSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
44 th_initialized = TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 /* Print formatted error, warning and information messages
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 * TODO: Implement th_vfprintf() and friends?
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 */
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
51 void THERR_V(const char *fmt, va_list ap)
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
52 {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
53 assert(th_initialized == TRUE);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
54
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
55 fprintf(stderr, "%s: ", th_prog_name);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
56 vfprintf(stderr, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
57 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
58
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
59
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
60 void THMSG_V(int level, const char *fmt, va_list ap)
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
61 {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
62 assert(th_initialized == TRUE);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
63
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
64 if (th_verbosityLevel >= level) {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
65 fprintf(stderr, "%s: ", th_prog_name);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
66 vfprintf(stderr, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
67 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
68 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
69
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
70
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
71 void THPRINT_V(int level, const char *fmt, va_list ap)
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
72 {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
73 assert(th_initialized == TRUE);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
74
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
75 if (th_verbosityLevel >= level) {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
76 vfprintf(stderr, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
77 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
78 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
79
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
80
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
81 void THERR(const char *fmt, ...)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
83 va_list ap;
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
84 assert(th_initialized == TRUE);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
86 va_start(ap, fmt);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
87 THERR_V(fmt, ap);
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
88 va_end(ap);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
92 void THMSG(int level, const char *fmt, ...)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
94 va_list ap;
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
95 assert(th_initialized == TRUE);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
97 va_start(ap, fmt);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
98 THMSG_V(level, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
99 va_end(ap);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
103 void THPRINT(int level, const char *fmt, ...)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
105 va_list ap;
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
106 assert(th_initialized == TRUE);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
108 va_start(ap, fmt);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
109 THPRINT_V(level, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
110 va_end(ap);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113
2
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
114 /* Memory handling routines
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 void *th_malloc(size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
118 return malloc(l);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 void *th_calloc(size_t n, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
124 return calloc(n, l);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 void *th_realloc(void *p, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
130 return realloc(p, l);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 void th_free(void *p)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
136 /* Check for NULL pointers for portability due to some libc
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
137 * implementations not handling free(NULL) too well.
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
138 */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
139 if (p) free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 #ifndef HAVE_MEMSET
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 void *th_memset(void *p, int c, size_t n)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
146 unsigned char *dp = (unsigned char *) p;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
147
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
148 while (n--)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
149 *(dp++) = c;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
151 return p;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 #endif