annotate th_util.c @ 2:ecfa4e3597e3

Cleanups in th-libs.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 20 Mar 2008 01:06:03 +0000
parents 728243125263
children 19b2cbc91c2a
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_NAME ""
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
19 #define TH_PROG_FULLNAME ""
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
20 #define TH_PROG_VERSION ""
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
21 #define TH_PROG_AUTHOR ""
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
22 #define TH_PROG_LICENSE ""
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
23 #else
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
24 #define TH_PROG_NAME "program"
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
25 #define TH_PROG_FULLNAME "A Program"
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 #define TH_PROG_VERSION "0.0.0"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 #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
28 #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
29 #endif
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 BOOL th_isInitialized = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 int th_verbosityLevel = 2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 char *th_prog_name = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 *th_prog_fullname = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 *th_prog_version = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 *th_prog_author = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 *th_prog_license = NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 /* Initialize th_util-library and global variables
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 void th_init(char *progName, char *progFullName, char *progVersion,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 char *progAuthor, char *progLicense)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 if (progName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 th_prog_name = progName;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 th_prog_name = TH_PROG_NAME;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 if (progFullName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 th_prog_fullname = progFullName;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 th_prog_fullname = TH_PROG_FULLNAME;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 if (progVersion)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 th_prog_version = progVersion;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 th_prog_version = TH_PROG_VERSION;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 if (progAuthor)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 th_prog_author = progAuthor;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 th_prog_author = TH_PROG_AUTHOR;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 if (progLicense)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 th_prog_license = progLicense;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 th_prog_license = TH_PROG_LICENSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 th_isInitialized = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 /* Print formatted error, warning and information messages
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 * TODO: Implement th_vfprintf() and friends?
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 void THERR(const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 fprintf(stderr, "%s: ", th_prog_name);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 void THMSG(int verbLevel, const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 /* Check if the current verbosity level is enough */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 if (th_verbosityLevel >= verbLevel) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 fprintf(stderr, "%s: ", th_prog_name);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 va_end(ap);
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
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 void THPRINT(int verbLevel, const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 /* Check if the current verbosity level is enough */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 if (th_verbosityLevel >= verbLevel) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
2
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
118 /* Memory handling routines
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 void *th_malloc(size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 return malloc(l);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 void *th_calloc(size_t n, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 return calloc(n, l);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 void *th_realloc(void *p, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 return realloc(p, l);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 void th_free(void *p)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 /* Check for NULL pointers for portability due to some libc
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 * implementations not handling free(NULL) too well.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 if (p) free(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 #ifndef HAVE_MEMSET
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 void *th_memset(void *p, int c, size_t n)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 unsigned char *dp = (unsigned char *) p;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 while (n--)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 *(dp++) = c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 return p;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 #endif