Mercurial > hg > th-libs
annotate th_util.c @ 726:29e44a58bc73
Bump copyrights.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 01 Jan 2022 16:17:31 +0200 |
parents | 4ca6a3b30fe8 |
children | 31bc1ed07cf5 |
rev | line source |
---|---|
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Generic utility-functions, macros and defaults |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
726 | 4 * (C) Copyright 2002-2022 Tecnic Software productions (TNSP) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_util.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
96 | 10 #include <errno.h> |
589
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
11 #ifdef TH_PLAT_UNIX |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
12 # include <sys/ioctl.h> |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
13 #endif |
96 | 14 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
16 /* Default settings |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 */ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
18 static BOOL th_initialized = FALSE; |
469
fe5b803ae449
Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents:
468
diff
changeset
|
19 int th_verbosity = 2; |
60 | 20 char *th_prog_name = NULL, |
21 *th_prog_desc = NULL, | |
22 *th_prog_version = NULL, | |
23 *th_prog_author = NULL, | |
24 *th_prog_license = NULL; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 /* Initialize th_util-library and global variables |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 */ |
60 | 29 void th_init(char *name, char *desc, char *version, |
30 char *author, char *license) | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 { |
60 | 32 th_prog_name = name; |
33 th_prog_desc = desc; | |
34 th_prog_version = version; | |
471
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
35 |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
36 #ifdef TH_PROG_AUTHOR |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
37 th_prog_author = author == NULL ? TH_PROG_AUTHOR : NULL; |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
38 #else |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
39 th_prog_author = author; |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
40 #endif |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
41 |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
42 #ifdef TH_PROG_LICENSE |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
43 th_prog_license = license == NULL ? TH_PROG_LICENSE : NULL; |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
44 #else |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
45 th_prog_license = license; |
02a2a46f5485
Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents:
470
diff
changeset
|
46 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
48 th_initialized = TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
64 | 52 void th_print_banner(FILE *outFile, const char *name, const char *usage) |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
53 { |
470
167b66942c24
Remove newline from start of the banner.
Matti Hamalainen <ccr@tnsp.org>
parents:
469
diff
changeset
|
54 fprintf(outFile, "%s", th_prog_name); |
177
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
55 if (th_prog_version != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
56 fprintf(outFile, " v%s", th_prog_version); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
57 if (th_prog_desc != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
58 fprintf(outFile, " (%s)", th_prog_desc); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
59 fprintf(outFile, "\n"); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
60 |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
61 if (th_prog_author != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
62 fprintf(outFile, "%s\n", th_prog_author); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
63 |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
64 if (th_prog_license != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
65 fprintf(outFile, "%s\n", th_prog_license); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
66 |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
67 fprintf(outFile, "Usage: %s %s\n", name, usage); |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
68 } |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
69 |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
70 |
489 | 71 void th_print_version(FILE *outFile) |
72 { | |
73 fprintf(outFile, "%s", th_prog_name); | |
74 if (th_prog_version != NULL) | |
75 fprintf(outFile, " v%s", th_prog_version); | |
76 fprintf(outFile, "\n"); | |
77 } | |
78 | |
79 | |
143 | 80 int th_term_width() |
81 { | |
589
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
82 #ifdef TH_PLAT_UNIX |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
83 struct winsize cwz; |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
84 ioctl(0, TIOCGWINSZ, &cwz); |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
85 if (cwz.ws_col < 5) |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
86 cwz.ws_col = 80; |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
87 return cwz.ws_col; |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
88 #else |
143 | 89 char *var = getenv("COLUMNS"); |
90 int res = (var != NULL) ? atoi(var) : 80; | |
91 if (res < 5) res = 80; | |
92 return res; | |
589
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
93 #endif |
143 | 94 } |
95 | |
96 | |
97 int th_term_height() | |
98 { | |
589
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
99 #ifdef TH_PLAT_UNIX |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
100 struct winsize cwz; |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
101 ioctl(0, TIOCGWINSZ, &cwz); |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
102 if (cwz.ws_row < 5) |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
103 cwz.ws_row = 25; |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
104 return cwz.ws_row; |
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
105 #else |
467
b92b18500bf9
Oops. We've been trying to get number of terminal lines from environment
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
106 char *var = getenv("LINES"); |
143 | 107 int res = (var != NULL) ? atoi(var) : 25; |
108 if (res < 1) res = 1; | |
109 return res; | |
589
f2aa3c809247
Use TIOCGWINSZ ioctl on *NIX to get terminal dimensions.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
110 #endif |
143 | 111 } |
112 | |
113 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 /* Print formatted error, warning and information messages |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 * TODO: Implement th_vfprintf() and friends? |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 */ |
11 | 117 void THERR_V(const char *fmt, va_list ap) |
118 { | |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
119 assert(th_initialized == TRUE); |
11 | 120 |
121 fprintf(stderr, "%s: ", th_prog_name); | |
122 vfprintf(stderr, fmt, ap); | |
123 } | |
124 | |
125 | |
126 void THMSG_V(int level, const char *fmt, va_list ap) | |
127 { | |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
128 assert(th_initialized == TRUE); |
11 | 129 |
469
fe5b803ae449
Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents:
468
diff
changeset
|
130 if (th_verbosity >= level) |
48 | 131 { |
11 | 132 fprintf(stderr, "%s: ", th_prog_name); |
133 vfprintf(stderr, fmt, ap); | |
134 } | |
135 } | |
136 | |
137 | |
138 void THPRINT_V(int level, const char *fmt, va_list ap) | |
139 { | |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
140 assert(th_initialized == TRUE); |
11 | 141 |
469
fe5b803ae449
Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents:
468
diff
changeset
|
142 if (th_verbosity >= level) |
48 | 143 { |
11 | 144 vfprintf(stderr, fmt, ap); |
145 } | |
146 } | |
147 | |
148 | |
149 void THERR(const char *fmt, ...) | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 { |
10 | 151 va_list ap; |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
152 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 |
11 | 154 va_start(ap, fmt); |
155 THERR_V(fmt, ap); | |
10 | 156 va_end(ap); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 |
11 | 160 void THMSG(int level, const char *fmt, ...) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 { |
10 | 162 va_list ap; |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
163 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 |
11 | 165 va_start(ap, fmt); |
166 THMSG_V(level, fmt, ap); | |
167 va_end(ap); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 |
11 | 171 void THPRINT(int level, const char *fmt, ...) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 { |
10 | 173 va_list ap; |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
453
diff
changeset
|
174 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 |
11 | 176 va_start(ap, fmt); |
177 THPRINT_V(level, fmt, ap); | |
178 va_end(ap); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 |
96 | 182 /* Error handling |
183 */ | |
101
a8f732601fdc
Rename th_get_errno() to th_get_error(), it's more appropriate since it does
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
184 int th_get_error() |
96 | 185 { |
676 | 186 return th_errno_to_error(errno); |
96 | 187 } |
188 | |
189 | |
99 | 190 int th_errno_to_error(int error) |
191 { | |
676 | 192 if (error == 0) |
193 return THERR_OK; | |
194 else | |
195 return TH_SYSTEM_ERRORS + error; | |
99 | 196 } |
197 | |
198 | |
96 | 199 const char *th_error_str(int error) |
200 { | |
201 if (error >= TH_SYSTEM_ERRORS) | |
202 return strerror(error - TH_SYSTEM_ERRORS); | |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
203 |
96 | 204 switch (error) |
205 { | |
206 case THERR_OK: return "No error"; | |
644
562de49f8b4b
Add some missing error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
589
diff
changeset
|
207 |
562de49f8b4b
Add some missing error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
589
diff
changeset
|
208 case THERR_PROGRESS: return "Operation in progress"; |
562de49f8b4b
Add some missing error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
589
diff
changeset
|
209 |
562de49f8b4b
Add some missing error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
589
diff
changeset
|
210 case THERR_INTERNAL: return "Internal error"; |
562de49f8b4b
Add some missing error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
589
diff
changeset
|
211 |
96 | 212 case THERR_FOPEN: return "File open error"; |
213 case THERR_FREAD: return "Read error"; | |
214 case THERR_FWRITE: return "Write error"; | |
215 case THERR_FSEEK: return "Seek error"; | |
216 case THERR_NOT_FOUND: return "Resource not found"; | |
217 | |
218 case THERR_INVALID_DATA: return "Invalid data"; | |
219 case THERR_MALLOC: return "Memory allocation failure"; | |
220 case THERR_ALREADY_INIT: return "Already initialized"; | |
221 case THERR_INIT_FAIL: return "Initialization failed"; | |
222 case THERR_INVALID_ARGS: return "Invalid arguments"; | |
223 | |
224 case THERR_NULLPTR: return "NULL pointer"; | |
225 case THERR_NOT_SUPPORTED: return "Operation not supported"; | |
226 case THERR_OUT_OF_DATA: return "Out of data"; | |
227 case THERR_EXTRA_DATA: return "Extra data"; | |
228 case THERR_BOUNDS: return "Bounds check failed"; | |
229 | |
230 case THERR_TIMED_OUT: return "Operation timed out"; | |
644
562de49f8b4b
Add some missing error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
589
diff
changeset
|
231 case THERR_BUSY: return "Resource busy"; |
562de49f8b4b
Add some missing error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
589
diff
changeset
|
232 case THERR_IO_ERROR: return "Input/ouput error"; |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
233 |
96 | 234 case THERR_AUTH_FAILED: return "Authentication failed"; |
235 | |
236 default: return "Unknown error"; | |
237 } | |
238 } | |
239 | |
240 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 /* Memory handling routines |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 */ |
462 | 243 void *th_malloc(size_t len) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 { |
462 | 245 return malloc(len); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 |
462 | 249 void *th_malloc0(size_t len) |
68
29f9651465c6
Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents:
66
diff
changeset
|
250 { |
462 | 251 return calloc(1, len); |
68
29f9651465c6
Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents:
66
diff
changeset
|
252 } |
29f9651465c6
Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents:
66
diff
changeset
|
253 |
29f9651465c6
Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents:
66
diff
changeset
|
254 |
462 | 255 void *th_calloc(size_t n, size_t len) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 { |
462 | 257 return calloc(n, len); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 |
462 | 261 void *th_realloc(void *ptr, size_t len) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 { |
462 | 263 return realloc(ptr, len); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
267 void th_free(void *ptr) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 { |
10 | 269 /* Check for NULL pointers for portability due to some libc |
270 * implementations not handling free(NULL) too well. | |
271 */ | |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
272 if (ptr != NULL) free(ptr); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
273 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
275 |
197
88986d0b56ef
Rename th_free_r() to th_free_r_real() and #define a macro wrapper for
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
276 void th_free_r_real(void **ptr) |
192
7d25d43a3ce2
Add new function th_free_r(void **ptr) that calls th_free(*ptr) and sets *ptr to NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
177
diff
changeset
|
277 { |
195 | 278 if (ptr != NULL) |
192
7d25d43a3ce2
Add new function th_free_r(void **ptr) that calls th_free(*ptr) and sets *ptr to NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
177
diff
changeset
|
279 { |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
280 th_free(*ptr); |
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
281 *ptr = NULL; |
192
7d25d43a3ce2
Add new function th_free_r(void **ptr) that calls th_free(*ptr) and sets *ptr to NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
177
diff
changeset
|
282 } |
7d25d43a3ce2
Add new function th_free_r(void **ptr) that calls th_free(*ptr) and sets *ptr to NULL.
Matti Hamalainen <ccr@tnsp.org>
parents:
177
diff
changeset
|
283 } |