annotate th_util.c @ 0:728243125263

Import.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 20 Mar 2008 00:15:03 +0000
parents
children ecfa4e3597e3
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
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <config.h>
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 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 #define TH_PROG_NAME "program"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 #define TH_PROG_FULLNAME "A Program"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 #define TH_PROG_VERSION "0.0.0"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 #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
21 #define TH_PROG_LICENSE "This software is licensed under GNU GPL version 2"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 BOOL th_isInitialized = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 int th_verbosityLevel = 2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 char *th_prog_name = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 *th_prog_fullname = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 *th_prog_version = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 *th_prog_author = NULL,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 *th_prog_license = NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 /* Initialize th_util-library and global variables
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 void th_init(char *progName, char *progFullName, char *progVersion,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 char *progAuthor, char *progLicense)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 /* Free previous values */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 if (progName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 th_prog_name = progName;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 th_prog_name = TH_PROG_NAME;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 if (progFullName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 th_prog_fullname = progFullName;
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_fullname = TH_PROG_FULLNAME;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 if (progVersion)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 th_prog_version = progVersion;
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_version = TH_PROG_VERSION;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 if (progAuthor)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 th_prog_author = progAuthor;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 th_prog_author = TH_PROG_AUTHOR;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 if (progLicense)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 th_prog_license = progLicense;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 th_prog_license = TH_PROG_LICENSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 th_isInitialized = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 /* Print formatted error, warning and information messages
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 * TODO: Implement th_vfprintf() and friends?
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 void THERR(const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 fprintf(stderr, "%s: ", th_prog_name);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 void THMSG(int verbLevel, const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 /* Check if the current verbosity level is enough */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 if (th_verbosityLevel >= verbLevel) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 fprintf(stderr, "%s: ", th_prog_name);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 void THPRINT(int verbLevel, const char *pcFormat, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 assert(th_isInitialized);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 /* Check if the current verbosity level is enough */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 if (th_verbosityLevel >= verbLevel) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 va_start(ap, pcFormat);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 vfprintf(stderr, pcFormat, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 va_end(ap);
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
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 * Memory handling routines
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 * TODO: Write alternate implementations for calloc and realloc,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 * to improve portability to platforms which don't implement them.
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 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 return malloc(l);
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 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 return calloc(n, l);
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 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 return realloc(p, l);
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 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 /* Check for NULL pointers for portability due to some libc
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 * implementations not handling free(NULL) too well.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 if (p) free(p);
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 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 unsigned char *dp = (unsigned char *) p;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 while (n--)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 *(dp++) = c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 return p;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 #endif