annotate th_util.h @ 148:bca55caf5c37

Update copyright year.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Jan 2015 16:56:29 +0200
parents fdea1c3acc53
children b4e1b15a64e1
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #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
16 #include <stdio.h>
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 #include <stdarg.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 #include <stdlib.h>
75
cfb475e11b9f Add a missing include.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
19 #include <unistd.h>
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
20 #include <sys/types.h>
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 #ifndef HAVE_NO_ASSERT
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 #include <assert.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 #ifdef HAVE_STRING_H
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 #include <string.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 #else
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 #ifdef HAVE_STRINGS_H
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 #include <strings.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 #ifdef HAVE_MEMORY_H
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 #include <memory.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
92
40568456eb5b Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 85
diff changeset
37
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
38 #ifdef __cplusplus
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
39 extern "C" {
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
40 #endif
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
42 #ifdef TH_NO_DEFAULTS
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
43 #define TH_PROG_AUTHOR NULL
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
44 #define TH_PROG_LICENSE NULL
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
45 #else
148
bca55caf5c37 Update copyright year.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
46 #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
47 #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
48 #endif
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
49
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
50
95
d86ad68f326f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
51 // Replacement for assert()
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 #ifdef HAVE_NO_ASSERT
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 # ifdef NDEBUG
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 120
diff changeset
54 # define assert(NEXPR) // stub
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 # else
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 # 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
57 # endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
60
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
61 /* Error codes
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
62 */
97
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
63 enum
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
64 {
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
65 // General error codes
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
66 THERR_OK = 0,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
67 THERR_PROGRESS, // Status OK, but operation in progress
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
68
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
69 THERR_INTERNAL,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
70
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
71 THERR_FOPEN,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
72 THERR_FREAD,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
73 THERR_FWRITE,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
74 THERR_FSEEK,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
75 THERR_NOT_FOUND, // Resource/data not found
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_INVALID_DATA, // Some data was invalid
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
78 THERR_MALLOC, // Memory allocation failure
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
79 THERR_ALREADY_INIT, // Resource has already been initialized
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
80 THERR_INIT_FAIL, // General initialization failure
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
81 THERR_INVALID_ARGS,
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_NULLPTR, // NULL pointer specified in critical argument
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
84 THERR_NOT_SUPPORTED,// Operation not supported
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
85 THERR_OUT_OF_DATA,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
86 THERR_EXTRA_DATA,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
87 THERR_BOUNDS,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
88 THERR_TIMED_OUT,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
89
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
90 // Network errors
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
91 THERR_AUTH_FAILED,
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
92 };
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
93
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
94 #define TH_SYSTEM_ERRORS 100000
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
95
97
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
96 /* Log levels
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
97 */
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
98 enum
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
99 {
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
100 THLOG_NONE = 0,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
101 THLOG_ERROR,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
102 THLOG_WARNING,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
103 THLOG_INFO,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
104 THLOG_DEBUG,
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
105 };
a285cdc05810 Add log level enums.
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
106
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
107
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 /* Global variables
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 */
19
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
110 extern int th_verbosityLevel;
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
111 extern char *th_prog_name,
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
112 *th_prog_desc,
19
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
113 *th_prog_version,
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
114 *th_prog_author,
bbb243eba688 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
115 *th_prog_license;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 /* Functions
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 */
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
119 void th_init(char *name, char *desc, char *version,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
120 char *author, char *license);
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
121 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
122
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
123 int th_term_width();
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
124 int th_term_height();
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
125
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
126 int th_get_error();
99
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
127 int th_errno_to_error(int error);
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
128 const char *th_error_str(int error);
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
129
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
130 void THERR(const char *, ...);
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
131 void THMSG(int, const char *, ...);
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
132 void THPRINT(int, const char *, ...);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
134 void THERR_V(const char *, va_list);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
135 void THMSG_V(int, const char *, va_list);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
136 void THPRINT_V(int, const char *, va_list);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
137
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
138 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
139 void *th_malloc0(size_t);
6
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
140 void *th_calloc(size_t, size_t);
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
141 void *th_realloc(void *, size_t);
2f92373537b5 Indentation cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
142 void th_free(void *);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
144
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
145 /* Doubly linked list handling
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
146 */
69
28156333ef4c Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
147 typedef struct _qlist_t
28156333ef4c Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
148 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
149 void *data;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
150 size_t num;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
151 struct _qlist_t *prev, *next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
152 } qlist_t;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
153
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
154
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
155 qlist_t * th_llist_new(void *data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
156 void th_llist_free(qlist_t *list);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
157 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data));
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
158
53
2c90b33d3617 Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
159 void th_llist_append_node(qlist_t **list, qlist_t *node);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
160 qlist_t * th_llist_append(qlist_t **list, void *data);
53
2c90b33d3617 Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
161 void th_llist_prepend_node(qlist_t **list, qlist_t *node);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
162 qlist_t * th_llist_prepend(qlist_t **list, void *data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
163 void th_llist_delete(qlist_t **list, const void *data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
164 void th_llist_delete_node(qlist_t **list, qlist_t *node);
93
029b3dea31ff Make th_llist_delete_node_fast() public.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
165 void th_llist_delete_node_fast(qlist_t **list, qlist_t *node);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
166
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
167 qlist_t * th_llist_get_nth(qlist_t *list, const size_t n);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
168 size_t th_llist_length(const qlist_t *list);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
169 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
170
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
171 void th_llist_foreach(qlist_t *list, void (*func)(qlist_t *node, void *userdata), void *data);
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
172 int th_llist_foreach_cond(qlist_t *list, int (*func)(qlist_t *node, void *userdata), void *data, qlist_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
173
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
174 qlist_t * th_llist_find(qlist_t *list, const void *data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
175 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *));
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
176
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
177
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
178 /* Ringbuffer implementation
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
179 */
69
28156333ef4c Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
180 typedef struct
28156333ef4c Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
181 {
33
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
182 char **data;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
183 int n, size;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
184 void (*deallocator)(void *);
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
185 } qringbuf_t;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
186
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
187 qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *));
95
d86ad68f326f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
188 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n);
d86ad68f326f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
189 void th_ringbuf_free(qringbuf_t *buf);
d86ad68f326f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
190 void th_ringbuf_add(qringbuf_t *buf, void *ptr);
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
191
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
192
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
193 /* Growing buffers
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
194 */
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
195 #define TH_BUFGROW (32)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
196
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
197
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
198 typedef struct
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
199 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
200 BOOL allocated;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
201 uint8_t *data;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
202 size_t size, len, mingrow;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
203 } th_growbuf_t;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
204
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
205
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
206 /* Simple growing string buffer
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 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
209 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
210 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
211
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 /* Growing byte buffer
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
214 */
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
215 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
216 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
217 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
218 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
219
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_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
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
231
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 #ifdef __cplusplus
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 #endif
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 120
diff changeset
235 #endif // TH_UTIL_H