annotate th_util.c @ 20:323c98360d8b

Synced th-libs.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Mar 2008 04:38:05 +0000
parents 19b2cbc91c2a
children dd59868059d5
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 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 BOOL th_isInitialized = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 int th_verbosityLevel = 2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 char *th_prog_name = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 *th_prog_fullname = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 *th_prog_version = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 *th_prog_author = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 *th_prog_license = NULL;
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,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 char *progAuthor, char *progLicense)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 {
17
19b2cbc91c2a Updated th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
30 th_prog_name = progName;
19b2cbc91c2a Updated th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
31 th_prog_fullname = progFullName;
19b2cbc91c2a Updated th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
32 th_prog_version = progVersion;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 if (progAuthor)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 th_prog_author = progAuthor;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 th_prog_author = TH_PROG_AUTHOR;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 if (progLicense)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 th_prog_license = progLicense;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 th_prog_license = TH_PROG_LICENSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 th_isInitialized = TRUE;
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 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 void THERR(const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 fprintf(stderr, "%s: ", th_prog_name);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 void THMSG(int verbLevel, const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 /* Check if the current verbosity level is enough */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 if (th_verbosityLevel >= verbLevel) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 fprintf(stderr, "%s: ", th_prog_name);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 void THPRINT(int verbLevel, const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 /* Check if the current verbosity level is enough */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 if (th_verbosityLevel >= verbLevel) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 }
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
2
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
92 /* Memory handling routines
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 void *th_malloc(size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 return malloc(l);
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 void *th_calloc(size_t n, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 return calloc(n, l);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 void *th_realloc(void *p, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 return realloc(p, l);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 void th_free(void *p)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 /* Check for NULL pointers for portability due to some libc
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 * implementations not handling free(NULL) too well.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 if (p) free(p);
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 #ifndef HAVE_MEMSET
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 void *th_memset(void *p, int c, size_t n)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 unsigned char *dp = (unsigned char *) p;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 while (n--)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 *(dp++) = c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 return p;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 #endif