annotate th_util.c @ 471:02a2a46f5485

Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override th_prog_author and th_prog_license variables if the provided values are NULL.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 04 Jun 2018 03:07:46 +0300
parents 167b66942c24
children 41c1d786ff08
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
144
51eec969b07a Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
4 * (C) Copyright 2002-2015 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
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
10 #include <errno.h>
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
11
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
13 /* Default settings
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
15 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
16 int th_verbosity = 2;
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
17 char *th_prog_name = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
18 *th_prog_desc = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
19 *th_prog_version = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
20 *th_prog_author = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
21 *th_prog_license = NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 /* 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
25 */
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
26 void th_init(char *name, char *desc, char *version,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
27 char *author, char *license)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 {
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
29 th_prog_name = name;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
30 th_prog_desc = desc;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
31 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
32
02a2a46f5485 Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents: 470
diff changeset
33 #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
34 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
35 #else
02a2a46f5485 Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents: 470
diff changeset
36 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
37 #endif
02a2a46f5485 Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents: 470
diff changeset
38
02a2a46f5485 Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents: 470
diff changeset
39 #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
40 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
41 #else
02a2a46f5485 Allow TH_PROG_AUTHOR and TH_PROG_LICENSE to be defined to override
Matti Hamalainen <ccr@tnsp.org>
parents: 470
diff changeset
42 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
43 #endif
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
45 th_initialized = TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
64
40b6e8dca885 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
49 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
50 {
470
167b66942c24 Remove newline from start of the banner.
Matti Hamalainen <ccr@tnsp.org>
parents: 469
diff changeset
51 fprintf(outFile, "%s", th_prog_name);
177
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
52 if (th_prog_version != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
53 fprintf(outFile, " v%s", th_prog_version);
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
54 if (th_prog_desc != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
55 fprintf(outFile, " (%s)", th_prog_desc);
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
56 fprintf(outFile, "\n");
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
57
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
58 if (th_prog_author != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
59 fprintf(outFile, "%s\n", th_prog_author);
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_license != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
62 fprintf(outFile, "%s\n", th_prog_license);
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 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
65 }
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
66
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
67
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
68 int th_term_width()
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
69 {
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
70 char *var = getenv("COLUMNS");
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
71 int res = (var != NULL) ? atoi(var) : 80;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
72 if (res < 5) res = 80;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
73 return res;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
74 }
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
75
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
76
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
77 int th_term_height()
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
78 {
467
b92b18500bf9 Oops. We've been trying to get number of terminal lines from environment
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
79 char *var = getenv("LINES");
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
80 int res = (var != NULL) ? atoi(var) : 25;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
81 if (res < 1) res = 1;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
82 return res;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
83 }
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
84
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
85
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 /* 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
87 * 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
88 */
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
89 void THERR_V(const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
90 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
91 assert(th_initialized == TRUE);
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
92
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
93 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
94 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
95 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
96
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
97
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
98 void THMSG_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
99 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
100 assert(th_initialized == TRUE);
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
101
469
fe5b803ae449 Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents: 468
diff changeset
102 if (th_verbosity >= level)
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
103 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
104 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
105 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
106 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
107 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
108
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
109
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
110 void THPRINT_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
111 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
112 assert(th_initialized == TRUE);
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
113
469
fe5b803ae449 Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents: 468
diff changeset
114 if (th_verbosity >= level)
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
115 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
116 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
117 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
118 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
119
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
120
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
121 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
122 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
123 va_list ap;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
124 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
126 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
127 THERR_V(fmt, ap);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
128 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
132 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
133 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
134 va_list ap;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
135 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
137 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
138 THMSG_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
139 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
143 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
144 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
145 va_list ap;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
146 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
148 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
149 THPRINT_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
150 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
154 /* Error handling
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
155 */
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
156 int th_get_error()
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
157 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
158 return TH_SYSTEM_ERRORS + errno;
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
159 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
160
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
161
99
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
162 int th_errno_to_error(int error)
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
163 {
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
164 return TH_SYSTEM_ERRORS + error;
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
165 }
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
166
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
167
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
168 const char *th_error_str(int error)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
169 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
170 if (error >= TH_SYSTEM_ERRORS)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
171 return strerror(error - TH_SYSTEM_ERRORS);
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 269
diff changeset
172
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
173 switch (error)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
174 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
175 case THERR_OK: return "No error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
176 case THERR_FOPEN: return "File open error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
177 case THERR_FREAD: return "Read error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
178 case THERR_FWRITE: return "Write error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
179 case THERR_FSEEK: return "Seek error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
180 case THERR_NOT_FOUND: return "Resource not found";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
181
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
182 case THERR_INVALID_DATA: return "Invalid data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
183 case THERR_MALLOC: return "Memory allocation failure";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
184 case THERR_ALREADY_INIT: return "Already initialized";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
185 case THERR_INIT_FAIL: return "Initialization failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
186 case THERR_INVALID_ARGS: return "Invalid arguments";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
187
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
188 case THERR_NULLPTR: return "NULL pointer";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
189 case THERR_NOT_SUPPORTED: return "Operation not supported";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
190 case THERR_OUT_OF_DATA: return "Out of data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
191 case THERR_EXTRA_DATA: return "Extra data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
192 case THERR_BOUNDS: return "Bounds check failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
193
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
194 case THERR_TIMED_OUT: return "Operation timed out";
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 269
diff changeset
195
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
196 case THERR_AUTH_FAILED: return "Authentication failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
197
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
198 default: return "Unknown error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
199 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
200 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
201
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
202
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 /* Memory handling routines
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 */
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
205 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
206 {
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
207 return malloc(len);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
211 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
212 {
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
213 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
214 }
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
215
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
216
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
217 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
218 {
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
219 return calloc(n, len);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
223 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
224 {
462
a90fe2c4c636 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
225 return realloc(ptr, len);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228
194
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
229 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
230 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
231 /* Check for NULL pointers for portability due to some libc
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
232 * implementations not handling free(NULL) too well.
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
233 */
194
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
234 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
235 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237
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
238 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
239 {
195
Matti Hamalainen <ccr@tnsp.org>
parents: 194
diff changeset
240 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
241 {
194
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
242 th_free(*ptr);
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
243 *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
244 }
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
245 }