annotate th_util.h @ 197:88986d0b56ef

Rename th_free_r() to th_free_r_real() and #define a macro wrapper for casting argument to (void **).
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 11 Feb 2016 20:26:47 +0200
parents 87dac812cac4
children e4d6bba7d7a4
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 */
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
8 #ifndef TH_UTIL_H
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
9 #define TH_UTIL_H
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
11 #ifdef HAVE_CONFIG_H
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
12 #include "config.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
183
8baec5a454c1 Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents: 163
diff changeset
15 #if defined(__WIN64) || defined(_WIN64) || defined(__WIN32) || defined(_WIN32)
8baec5a454c1 Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents: 163
diff changeset
16 # define TH_PLAT_WINDOWS 1
8baec5a454c1 Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents: 163
diff changeset
17 #else
8baec5a454c1 Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents: 163
diff changeset
18 # define TH_PLAT_UNIX 1
8baec5a454c1 Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents: 163
diff changeset
19 #endif
8baec5a454c1 Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents: 163
diff changeset
20
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 #include "th_types.h"
45
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
22 #include <stdio.h>
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 #include <stdarg.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 #include <stdlib.h>
75
cfb475e11b9f Add a missing include.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
25 #include <unistd.h>
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
26 #include <sys/types.h>
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 #ifndef HAVE_NO_ASSERT
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 #include <assert.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 #ifdef HAVE_STRING_H
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 #include <string.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 #else
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 #ifdef HAVE_STRINGS_H
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 #include <strings.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 #ifdef HAVE_MEMORY_H
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 #include <memory.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42
92
40568456eb5b Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 85
diff changeset
43
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
44 #ifdef __cplusplus
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
45 extern "C" {
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
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
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
48 #ifdef TH_NO_DEFAULTS
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
49 #define TH_PROG_AUTHOR NULL
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
50 #define TH_PROG_LICENSE NULL
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
51 #else
148
bca55caf5c37 Update copyright year.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
52 #define TH_PROG_AUTHOR "By Matti 'ccr' Hämäläinen (C) Copyright 2015 TNSP"
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
53 #define TH_PROG_LICENSE "This software is licensed under GNU GPL version 2"
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
54 #endif
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
55
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
56
95
d86ad68f326f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
57 // Replacement for assert()
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 #ifdef HAVE_NO_ASSERT
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 # ifdef NDEBUG
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 120
diff changeset
60 # define assert(NEXPR) // stub
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 # else
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 # define assert(NEXPR) do { if (!(NEXPR)) { fprintf(stderr, "[%s:%d] assert(" # NEXPR ") failed!\n", __FILE__, __LINE__); abort(); } } while (0)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 # endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
66
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
67 /* Error codes
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
68 */
97
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
69 enum
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
70 {
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
71 // General error codes
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
72 THERR_OK = 0,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
73 THERR_PROGRESS, // Status OK, but operation in progress
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
74
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
75 THERR_INTERNAL,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
76
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
77 THERR_FOPEN,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
78 THERR_FREAD,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
79 THERR_FWRITE,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
80 THERR_FSEEK,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
81 THERR_NOT_FOUND, // Resource/data not found
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
82
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
83 THERR_INVALID_DATA, // Some data was invalid
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
84 THERR_MALLOC, // Memory allocation failure
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
85 THERR_ALREADY_INIT, // Resource has already been initialized
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
86 THERR_INIT_FAIL, // General initialization failure
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
87 THERR_INVALID_ARGS,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
88
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
89 THERR_NULLPTR, // NULL pointer specified in critical argument
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
90 THERR_NOT_SUPPORTED,// Operation not supported
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
91 THERR_OUT_OF_DATA,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
92 THERR_EXTRA_DATA,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
93 THERR_BOUNDS,
163
44887a053df0 Add few error codes.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
94
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
95 THERR_TIMED_OUT,
163
44887a053df0 Add few error codes.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
96 THERR_BUSY,
44887a053df0 Add few error codes.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
97 THERR_IO_ERROR,
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
98
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
99 // Network errors
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
100 THERR_AUTH_FAILED,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
101 };
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
102
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
103 #define TH_SYSTEM_ERRORS 100000
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
104
97
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
105 /* Log levels
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
106 */
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
107 enum
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
108 {
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
109 THLOG_NONE = 0,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
110 THLOG_ERROR,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
111 THLOG_WARNING,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
112 THLOG_INFO,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
113 THLOG_DEBUG,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
114 };
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
115
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
116
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 /* Global variables
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 */
19
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
119 extern int th_verbosityLevel;
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
120 extern char *th_prog_name,
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
121 *th_prog_desc,
19
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
122 *th_prog_version,
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
123 *th_prog_author,
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
124 *th_prog_license;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 /* Functions
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 */
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
128 void th_init(char *name, char *desc, char *version,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
129 char *author, char *license);
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
130 void th_print_banner(FILE *outFile, const char *binName, const char *usage);
45
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
131
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
132 int th_term_width();
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
133 int th_term_height();
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
134
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
135 int th_get_error();
99
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
136 int th_errno_to_error(int error);
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
137 const char *th_error_str(int error);
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
138
194
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
139 void THERR(const char *fmt, ...);
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
140 void THMSG(int level, const char *fmt, ...);
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
141 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
142
194
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
143 void THERR_V(const char *fmt, va_list ap);
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
144 void THMSG_V(int level, const char *fmt, va_list ap);
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
145 void THPRINT_V(int level, const char *fmt, va_list ap);
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
146
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
147 void *th_malloc(size_t);
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
148 void *th_malloc0(size_t);
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
149 void *th_calloc(size_t, size_t);
194
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
150 void *th_realloc(void *ptr, size_t);
87dac812cac4 Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
151 void th_free(void *ptr);
197
88986d0b56ef Rename th_free_r() to th_free_r_real() and #define a macro wrapper for
Matti Hamalainen <ccr@tnsp.org>
parents: 194
diff changeset
152 void th_free_r_real(void **ptr);
88986d0b56ef Rename th_free_r() to th_free_r_real() and #define a macro wrapper for
Matti Hamalainen <ccr@tnsp.org>
parents: 194
diff changeset
153
88986d0b56ef Rename th_free_r() to th_free_r_real() and #define a macro wrapper for
Matti Hamalainen <ccr@tnsp.org>
parents: 194
diff changeset
154 #define th_free_r(ptr) th_free_r_real((void **) ptr)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
156
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
157 /* Doubly linked list handling
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
158 */
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
159 typedef struct _th_llist_t
69
28156333ef4c Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
160 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
161 void *data;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
162 size_t num;
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
163 struct _th_llist_t *prev, *next;
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
164 } th_llist_t;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
165
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
166
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
167 th_llist_t * th_llist_new(void *data);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
168 void th_llist_free(th_llist_t *list);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
169 void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data));
156
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
170 void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *node));
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
171
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
172 void th_llist_append_node(th_llist_t **list, th_llist_t *node);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
173 th_llist_t * th_llist_append(th_llist_t **list, void *data);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
174 void th_llist_prepend_node(th_llist_t **list, th_llist_t *node);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
175 th_llist_t * th_llist_prepend(th_llist_t **list, void *data);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
176 void th_llist_delete(th_llist_t **list, const void *data);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
177 void th_llist_delete_node(th_llist_t **list, th_llist_t *node);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
178 void th_llist_delete_node_fast(th_llist_t **list, th_llist_t *node);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
179
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
180 th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
181 size_t th_llist_length(const th_llist_t *list);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
182 ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
183
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
184 void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
185 int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_t **res);
54
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
186
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
187 th_llist_t * th_llist_find(th_llist_t *list, const void *data);
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 148
diff changeset
188 th_llist_t * th_llist_find_func(th_llist_t *list, const void *userdata, int (compare)(const void *, const void *));
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
189
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
190
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
191 /* Ringbuffer implementation
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
192 */
69
28156333ef4c Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
193 typedef struct
28156333ef4c Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
194 {
33
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
195 char **data;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
196 int n, size;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
197 void (*deallocator)(void *);
157
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
198 } th_ringbuf_t;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
199
157
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
200 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *));
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
201 BOOL th_ringbuf_grow(th_ringbuf_t *buf, const size_t n);
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
202 void th_ringbuf_free(th_ringbuf_t *buf);
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
203 void th_ringbuf_add(th_ringbuf_t *buf, void *ptr);
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
204
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
205
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
206 /* Growing buffers
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
207 */
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
208 #define TH_BUFGROW (32)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
209
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
210
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
211 typedef struct
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
212 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
213 BOOL allocated;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
214 uint8_t *data;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
215 size_t size, len, mingrow;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
216 } th_growbuf_t;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
217
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
218
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
219 /* Simple growing string buffer
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
220 */
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
221 BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, const size_t grow);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
222 BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
223 BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
224
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
225
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
226 /* Growing byte buffer
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
227 */
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
228 void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow);
115
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
229 void th_growbuf_clear(th_growbuf_t *buf);
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
230 th_growbuf_t *th_growbuf_new(const size_t mingrow);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
231 void th_growbuf_free(th_growbuf_t *buf);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
232
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
233
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
234 BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
235 BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
236 BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch);
120
41c2638ee537 Use void* instead of uint8_t* in th_growbuf_put_str()
Matti Hamalainen <ccr@tnsp.org>
parents: 115
diff changeset
237 BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len);
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
238 BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
239 BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
240 BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
241 BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
242 BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
243
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
244
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 #ifdef __cplusplus
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 #endif
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 120
diff changeset
248 #endif // TH_UTIL_H