Mercurial > hg > nnchat
annotate th_util.c @ 110:8af4072dc31a
Fix Win32/MinGW/PDcurses issues.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 24 Oct 2010 18:28:22 +0300 |
parents | 0313fabd8049 |
children | 4ec36204d34e |
rev | line source |
---|---|
0 | 1 /* |
2 * Generic utility-functions, macros and defaults | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP) | |
5 * | |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #ifdef HAVE_CONFIG_H | |
2 | 9 #include "config.h" |
0 | 10 #endif |
11 #include "th_util.h" | |
12 #include <stdio.h> | |
13 | |
20 | 14 /* Default settings |
0 | 15 */ |
81 | 16 static BOOL th_initialized = FALSE; |
17 int th_verbosityLevel = 2; | |
18 char *th_prog_name = NULL, | |
19 *th_prog_fullname = NULL, | |
20 *th_prog_version = NULL, | |
21 *th_prog_author = NULL, | |
22 *th_prog_license = NULL; | |
0 | 23 |
24 | |
25 /* Initialize th_util-library and global variables | |
26 */ | |
27 void th_init(char *progName, char *progFullName, char *progVersion, | |
81 | 28 char *progAuthor, char *progLicense) |
0 | 29 { |
81 | 30 th_prog_name = progName; |
31 th_prog_fullname = progFullName; | |
32 th_prog_version = progVersion; | |
0 | 33 |
81 | 34 if (progAuthor) |
35 th_prog_author = progAuthor; | |
36 else | |
37 th_prog_author = TH_PROG_AUTHOR; | |
0 | 38 |
81 | 39 if (progLicense) |
40 th_prog_license = progLicense; | |
41 else | |
42 th_prog_license = TH_PROG_LICENSE; | |
0 | 43 |
81 | 44 th_initialized = TRUE; |
0 | 45 } |
46 | |
47 | |
48 /* Print formatted error, warning and information messages | |
49 * TODO: Implement th_vfprintf() and friends? | |
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 | 82 { |
81 | 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 | 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 | 88 va_end(ap); |
0 | 89 } |
90 | |
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 | 93 { |
81 | 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 | 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 | 100 } |
101 | |
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 | 104 { |
81 | 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 | 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 | 111 } |
112 | |
113 | |
2 | 114 /* Memory handling routines |
0 | 115 */ |
116 void *th_malloc(size_t l) | |
117 { | |
81 | 118 return malloc(l); |
0 | 119 } |
120 | |
121 | |
122 void *th_calloc(size_t n, size_t l) | |
123 { | |
81 | 124 return calloc(n, l); |
0 | 125 } |
126 | |
127 | |
128 void *th_realloc(void *p, size_t l) | |
129 { | |
81 | 130 return realloc(p, l); |
0 | 131 } |
132 | |
133 | |
134 void th_free(void *p) | |
135 { | |
81 | 136 /* Check for NULL pointers for portability due to some libc |
137 * implementations not handling free(NULL) too well. | |
138 */ | |
139 if (p) free(p); | |
0 | 140 } |
141 | |
142 | |
143 #ifndef HAVE_MEMSET | |
144 void *th_memset(void *p, int c, size_t n) | |
145 { | |
81 | 146 unsigned char *dp = (unsigned char *) p; |
147 | |
148 while (n--) | |
149 *(dp++) = c; | |
0 | 150 |
81 | 151 return p; |
0 | 152 } |
153 #endif |