annotate th_util.c @ 17:19b2cbc91c2a

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