Mercurial > hg > th-libs
annotate th_util.c @ 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 | a16e8272bdda |
children | fcbdc12f5866 |
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 | 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 | 10 #include <errno.h> |
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 */ |
10 | 15 static BOOL th_initialized = FALSE; |
60 | 16 int th_verbosityLevel = 2; |
17 char *th_prog_name = NULL, | |
18 *th_prog_desc = NULL, | |
19 *th_prog_version = NULL, | |
20 *th_prog_author = NULL, | |
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 | 26 void th_init(char *name, char *desc, char *version, |
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 | 29 th_prog_name = name; |
30 th_prog_desc = desc; | |
31 th_prog_version = version; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
60 | 33 if (author) |
34 th_prog_author = author; | |
10 | 35 else |
36 th_prog_author = TH_PROG_AUTHOR; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
60 | 38 if (license) |
39 th_prog_license = license; | |
10 | 40 else |
41 th_prog_license = TH_PROG_LICENSE; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 |
10 | 43 th_initialized = TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 |
64 | 47 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
|
48 { |
177
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
49 fprintf(outFile, "\n%s", th_prog_name); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
50 if (th_prog_version != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
51 fprintf(outFile, " v%s", th_prog_version); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
52 if (th_prog_desc != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
53 fprintf(outFile, " (%s)", th_prog_desc); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
54 fprintf(outFile, "\n"); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
55 |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
56 if (th_prog_author != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
57 fprintf(outFile, "%s\n", th_prog_author); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
58 |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
59 if (th_prog_license != NULL) |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
60 fprintf(outFile, "%s\n", th_prog_license); |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
61 |
2b07e452fd78
Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
62 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
|
63 } |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
64 |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
65 |
143 | 66 int th_term_width() |
67 { | |
68 char *var = getenv("COLUMNS"); | |
69 int res = (var != NULL) ? atoi(var) : 80; | |
70 if (res < 5) res = 80; | |
71 return res; | |
72 } | |
73 | |
74 | |
75 int th_term_height() | |
76 { | |
77 char *var = getenv("LINESS"); | |
78 int res = (var != NULL) ? atoi(var) : 25; | |
79 if (res < 1) res = 1; | |
80 return res; | |
81 } | |
82 | |
83 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 /* 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
|
85 * 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
|
86 */ |
11 | 87 void THERR_V(const char *fmt, va_list ap) |
88 { | |
89 assert(th_initialized == TRUE); | |
90 | |
91 fprintf(stderr, "%s: ", th_prog_name); | |
92 vfprintf(stderr, fmt, ap); | |
93 } | |
94 | |
95 | |
96 void THMSG_V(int level, const char *fmt, va_list ap) | |
97 { | |
98 assert(th_initialized == TRUE); | |
99 | |
48 | 100 if (th_verbosityLevel >= level) |
101 { | |
11 | 102 fprintf(stderr, "%s: ", th_prog_name); |
103 vfprintf(stderr, fmt, ap); | |
104 } | |
105 } | |
106 | |
107 | |
108 void THPRINT_V(int level, const char *fmt, va_list ap) | |
109 { | |
110 assert(th_initialized == TRUE); | |
111 | |
48 | 112 if (th_verbosityLevel >= level) |
113 { | |
11 | 114 vfprintf(stderr, fmt, ap); |
115 } | |
116 } | |
117 | |
118 | |
119 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
|
120 { |
10 | 121 va_list ap; |
11 | 122 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 |
11 | 124 va_start(ap, fmt); |
125 THERR_V(fmt, ap); | |
10 | 126 va_end(ap); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 |
11 | 130 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
|
131 { |
10 | 132 va_list ap; |
11 | 133 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 |
11 | 135 va_start(ap, fmt); |
136 THMSG_V(level, fmt, ap); | |
137 va_end(ap); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 |
11 | 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 { |
10 | 143 va_list ap; |
11 | 144 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 |
11 | 146 va_start(ap, fmt); |
147 THPRINT_V(level, fmt, ap); | |
148 va_end(ap); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 |
96 | 152 /* Error handling |
153 */ | |
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
|
154 int th_get_error() |
96 | 155 { |
156 return TH_SYSTEM_ERRORS + errno; | |
157 } | |
158 | |
159 | |
99 | 160 int th_errno_to_error(int error) |
161 { | |
162 return TH_SYSTEM_ERRORS + error; | |
163 } | |
164 | |
165 | |
96 | 166 const char *th_error_str(int error) |
167 { | |
168 if (error >= TH_SYSTEM_ERRORS) | |
169 return strerror(error - TH_SYSTEM_ERRORS); | |
170 | |
171 switch (error) | |
172 { | |
173 case THERR_OK: return "No error"; | |
174 case THERR_FOPEN: return "File open error"; | |
175 case THERR_FREAD: return "Read error"; | |
176 case THERR_FWRITE: return "Write error"; | |
177 case THERR_FSEEK: return "Seek error"; | |
178 case THERR_NOT_FOUND: return "Resource not found"; | |
179 | |
180 case THERR_INVALID_DATA: return "Invalid data"; | |
181 case THERR_MALLOC: return "Memory allocation failure"; | |
182 case THERR_ALREADY_INIT: return "Already initialized"; | |
183 case THERR_INIT_FAIL: return "Initialization failed"; | |
184 case THERR_INVALID_ARGS: return "Invalid arguments"; | |
185 | |
186 case THERR_NULLPTR: return "NULL pointer"; | |
187 case THERR_NOT_SUPPORTED: return "Operation not supported"; | |
188 case THERR_OUT_OF_DATA: return "Out of data"; | |
189 case THERR_EXTRA_DATA: return "Extra data"; | |
190 case THERR_BOUNDS: return "Bounds check failed"; | |
191 | |
192 case THERR_TIMED_OUT: return "Operation timed out"; | |
193 | |
194 case THERR_AUTH_FAILED: return "Authentication failed"; | |
195 | |
196 default: return "Unknown error"; | |
197 } | |
198 } | |
199 | |
200 | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 /* Memory handling routines |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 void *th_malloc(size_t l) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 { |
10 | 205 return malloc(l); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 |
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
|
209 void *th_malloc0(size_t l) |
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
|
210 { |
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
|
211 return calloc(1, l); |
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 } |
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
|
213 |
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 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 void *th_calloc(size_t n, size_t l) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 { |
10 | 217 return calloc(n, l); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
219 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
221 void *th_realloc(void *ptr, size_t l) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 { |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
223 return realloc(ptr, l); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
226 |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
227 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
|
228 { |
10 | 229 /* Check for NULL pointers for portability due to some libc |
230 * implementations not handling free(NULL) too well. | |
231 */ | |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
232 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
|
233 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 |
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
|
236 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
|
237 { |
195 | 238 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
|
239 { |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
240 th_free(*ptr); |
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
241 *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
|
242 } |
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
|
243 } |
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 |
16 | 246 /* Doubly linked list handling |
247 * | |
248 * In this implementation first node's prev points to last node of the list, | |
249 * and last node's next is NULL. This way we can semi-efficiently traverse to | |
250 * beginning and end of the list, assuming user does not do weird things. | |
251 */ | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
252 th_llist_t * th_llist_new(void *data) |
16 | 253 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
254 th_llist_t *res = th_malloc0(sizeof(th_llist_t)); |
16 | 255 res->data = data; |
256 return res; | |
257 } | |
258 | |
154 | 259 |
156
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
260 void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *)) |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
261 { |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
262 th_llist_t *curr = list; |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
263 |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
264 while (curr != NULL) |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
265 { |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
266 th_llist_t *next = curr->next; |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
267 freefunc(curr); |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
268 curr = next; |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
269 } |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
270 } |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
271 |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
272 |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
273 void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data)) |
16 | 274 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
275 th_llist_t *curr = list; |
16 | 276 |
40 | 277 while (curr != NULL) |
278 { | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
279 th_llist_t *next = curr->next; |
156
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
280 if (curr->data != NULL) |
16 | 281 freefunc(curr->data); |
282 th_free(curr); | |
283 curr = next; | |
284 } | |
285 } | |
286 | |
287 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
288 void th_llist_free(th_llist_t *list) |
16 | 289 { |
156
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
290 th_llist_t *curr = list; |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
291 |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
292 while (curr != NULL) |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
293 { |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
294 th_llist_t *next = curr->next; |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
295 th_free(curr); |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
296 curr = next; |
f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents:
152
diff
changeset
|
297 } |
16 | 298 } |
299 | |
53
2c90b33d3617
Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
300 |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
301 void th_llist_append_node(th_llist_t **list, th_llist_t *node) |
16 | 302 { |
40 | 303 if (*list != NULL) |
304 { | |
16 | 305 node->prev = (*list)->prev; |
306 (*list)->prev->next = node; | |
307 (*list)->prev = node; | |
308 (*list)->num++; | |
40 | 309 } |
310 else | |
311 { | |
16 | 312 *list = node; |
59 | 313 node->prev = node; |
16 | 314 (*list)->num = 1; |
315 } | |
316 | |
317 node->next = NULL; | |
318 } | |
319 | |
320 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
321 th_llist_t *th_llist_append(th_llist_t **list, void *data) |
16 | 322 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
323 th_llist_t *node = th_llist_new(data); |
16 | 324 |
325 th_llist_append_node(list, node); | |
326 | |
327 return node; | |
328 } | |
329 | |
330 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
331 void th_llist_prepend_node(th_llist_t **list, th_llist_t *node) |
16 | 332 { |
40 | 333 if (*list != NULL) |
334 { | |
16 | 335 node->prev = (*list)->prev; |
336 node->next = *list; | |
337 (*list)->prev = node; | |
338 node->num = (*list)->num + 1; | |
339 *list = node; | |
40 | 340 } |
341 else | |
342 { | |
16 | 343 *list = node->prev = node; |
344 node->next = NULL; | |
345 (*list)->num = 1; | |
346 } | |
347 | |
348 } | |
349 | |
350 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
351 th_llist_t *th_llist_prepend(th_llist_t **list, void *data) |
16 | 352 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
353 th_llist_t *node = th_llist_new(data); |
16 | 354 |
355 th_llist_prepend_node(list, node); | |
356 | |
357 return node; | |
358 } | |
359 | |
360 /* | |
361 1) Remove a middle node | |
362 | |
363 node0->prev->next = node->next (node1) | |
364 node0->next->prev = node->prev (list) | |
365 | |
366 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL | |
367 node2 <- list <=> node1 <=> node2 -> NULL | |
368 | |
369 2) Remove first node when many items | |
370 | |
371 | |
372 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL | |
373 node2 <- node0 <=> node1 <=> node2 -> NULL | |
374 | |
375 *list = node0 | |
376 | |
377 3) Remove last node in list | |
378 | |
379 if (node->next == NULL) { | |
380 list->prev = node->prev; | |
381 node->prev->next = NULL; | |
382 } | |
383 | |
384 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL | |
385 node1 <- list <=> node0 <=> node1 -> NULL | |
386 | |
387 4) Remove last | |
388 | |
389 list <- list -> NULL | |
390 | |
391 | |
392 */ | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
393 void th_llist_delete_node_fast(th_llist_t **list, th_llist_t *node) |
16 | 394 { |
40 | 395 if (node == *list) |
396 { | |
129 | 397 // First node in list |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
398 th_llist_t *tmp = (*list)->next; |
40 | 399 if (tmp != NULL) |
400 { | |
16 | 401 tmp->num = (*list)->num - 1; |
402 tmp->prev = (*list)->prev; | |
403 } | |
404 *list = tmp; | |
40 | 405 } |
406 else | |
407 { | |
129 | 408 // Somewhere in middle or end |
16 | 409 if (node->prev != NULL) |
410 node->prev->next = node->next; | |
411 | |
412 if (node->next != NULL) | |
413 node->next->prev = node->prev; | |
414 else | |
129 | 415 (*list)->prev = node; // Last node |
16 | 416 |
417 (*list)->num--; | |
418 } | |
419 | |
420 node->next = node->prev = NULL; | |
421 } | |
422 | |
423 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
424 void th_llist_delete_node(th_llist_t **list, th_llist_t *node) |
16 | 425 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
426 th_llist_t *curr = *list; |
16 | 427 |
40 | 428 while (curr != NULL) |
429 { | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
430 th_llist_t *next = curr->next; |
40 | 431 if (curr == node) |
432 { | |
16 | 433 th_llist_delete_node_fast(list, curr); |
434 th_free(node); | |
435 break; | |
436 } | |
437 curr = next; | |
438 } | |
439 } | |
440 | |
441 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
442 void th_llist_delete(th_llist_t **list, const void *data) |
16 | 443 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
444 th_llist_t *curr = *list; |
16 | 445 |
40 | 446 while (curr != NULL) |
447 { | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
448 th_llist_t *next = curr->next; |
40 | 449 if (curr->data == data) |
450 { | |
16 | 451 th_llist_delete_node_fast(list, curr); |
452 th_free(curr); | |
453 break; | |
454 } | |
455 curr = next; | |
456 } | |
457 } | |
458 | |
459 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
460 th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n) |
16 | 461 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
462 th_llist_t *curr = list; |
16 | 463 size_t i; |
464 | |
465 for (i = 0; curr != NULL && i < n; curr = curr->next, i++); | |
466 | |
467 return curr; | |
468 } | |
469 | |
470 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
471 size_t th_llist_length(const th_llist_t *list) |
16 | 472 { |
473 if (list == NULL) | |
474 return 0; | |
475 else | |
476 return list->num; | |
477 } | |
478 | |
479 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
480 ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node) |
16 | 481 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
482 const th_llist_t *curr = list; |
16 | 483 ssize_t i = 0; |
484 | |
40 | 485 while (curr != NULL) |
486 { | |
16 | 487 if (curr == node) |
488 return i; | |
489 else | |
490 i++; | |
491 | |
492 curr = curr->next; | |
493 } | |
494 | |
495 return -1; | |
496 } | |
497 | |
498 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
499 void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data) |
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
|
500 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
501 th_llist_t *curr = list; |
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
|
502 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
503 while (curr != NULL) |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
504 { |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
505 func(curr, data); |
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
|
506 curr = curr->next; |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
507 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
508 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
509 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
510 |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
511 int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_t **ret) |
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
|
512 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
513 th_llist_t *curr = list; |
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
|
514 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
515 while (curr != NULL) |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
516 { |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
517 int res = func(curr, data); |
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
|
518 if (res != 0) |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
519 { |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
520 *ret = curr; |
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
521 return 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
|
522 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
523 curr = curr->next; |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
524 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
525 |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
526 return 0; |
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
|
527 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
528 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
529 |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
530 th_llist_t * th_llist_find(th_llist_t *list, const void *data) |
16 | 531 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
532 th_llist_t *curr = list; |
16 | 533 |
40 | 534 while (curr != NULL) |
535 { | |
16 | 536 if (curr->data == data) |
537 return curr; | |
538 curr = curr->next; | |
539 } | |
540 | |
541 return NULL; | |
542 } | |
543 | |
544 | |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
545 th_llist_t * th_llist_find_func(th_llist_t *list, const void *userdata, int (compare)(const void *, const void *)) |
16 | 546 { |
152
b4e1b15a64e1
Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
547 th_llist_t *curr = list; |
16 | 548 |
40 | 549 while (curr != NULL) |
550 { | |
16 | 551 if (compare(curr->data, userdata) == 0) |
552 return curr; | |
553 curr = curr->next; | |
554 } | |
555 | |
556 return NULL; | |
557 } | |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
558 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
559 |
26 | 560 /* |
561 * Ringbuffers | |
562 */ | |
157
e96c900ca6f1
Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
156
diff
changeset
|
563 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data)) |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
564 { |
157
e96c900ca6f1
Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
156
diff
changeset
|
565 th_ringbuf_t *res = th_malloc0(sizeof(th_ringbuf_t)); |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
566 |
33 | 567 res->data = (char **) th_calloc(size, sizeof(char *)); |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
568 res->size = size; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
569 res->n = 0; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
570 res->deallocator = mdeallocator; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
571 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
572 return res; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
573 } |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
574 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
575 |
157
e96c900ca6f1
Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
156
diff
changeset
|
576 BOOL th_ringbuf_grow(th_ringbuf_t *buf, const size_t n) |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
577 { |
33 | 578 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *)); |
40 | 579 if (buf->data != NULL) |
580 { | |
98 | 581 memset(buf->data + buf->size, 0, sizeof(char *) * n); |
33 | 582 buf->size += n; |
583 return TRUE; | |
584 } else | |
585 return FALSE; | |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
586 } |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
587 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
588 |
157
e96c900ca6f1
Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
156
diff
changeset
|
589 void th_ringbuf_free(th_ringbuf_t *buf) |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
590 { |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
591 int i; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
592 |
33 | 593 for (i = 0; i < buf->size; i++) |
58
c12b66ec0362
Add braces, mostly cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
594 { |
25
2f7270a9d593
Make ringbuffer deallocator function call only happen if data pointer is != NULL
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
595 if (buf->data[i] != NULL) |
2f7270a9d593
Make ringbuffer deallocator function call only happen if data pointer is != NULL
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
596 buf->deallocator(buf->data[i]); |
58
c12b66ec0362
Add braces, mostly cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
597 } |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
598 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
599 th_free(buf->data); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
600 th_free(buf); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
601 } |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
602 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
603 |
157
e96c900ca6f1
Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
156
diff
changeset
|
604 void th_ringbuf_add(th_ringbuf_t *buf, void *ptr) |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
605 { |
28
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
606 if (buf->n < buf->size) |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
607 buf->n++; |
28
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
608 |
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
609 th_free(buf->data[0]); |
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
610 memmove(&(buf->data[0]), &(buf->data[1]), (buf->size - 1) * sizeof(void *)); |
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
611 buf->data[buf->size - 1] = ptr; |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
612 } |
109
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
613 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
614 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
615 /* |
115
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
616 * Simple legacy string growing buffer |
109
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
617 */ |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
618 BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
619 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
620 if (*buf == NULL) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
621 *bufsize = *len = 0; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
622 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
623 if (*buf == NULL || *len + grow >= *bufsize) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
624 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
625 *bufsize += grow + TH_BUFGROW; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
626 *buf = th_realloc(*buf, *bufsize); |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
627 if (*buf == NULL) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
628 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
629 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
630 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
631 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
632 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
633 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
634 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
|
635 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
636 if (!th_strbuf_grow(buf, bufsize, len, 1)) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
637 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
638 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
639 (*buf)[*len] = ch; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
640 (*len)++; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
641 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
642 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
643 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
644 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
645 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
646 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
|
647 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
648 size_t slen; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
649 if (str == NULL) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
650 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
651 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
652 slen = strlen(str); |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
653 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
654 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
655 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
656 memcpy(*buf + *len, str, slen + 1); |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
657 (*len) += slen; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
658 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
659 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
660 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
661 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
662 |
115
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
663 /* |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
664 * Growing buffer |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
665 */ |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
666 void th_growbuf_clear(th_growbuf_t *buf) |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
667 { |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
668 // Simply reset the current "length" |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
669 buf->len = 0; |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
670 } |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
671 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
672 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
673 void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow) |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
674 { |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
675 // Initialize the buffer structure |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
676 memset(buf, 0, sizeof(th_growbuf_t)); |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
677 buf->mingrow = mingrow; |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
678 } |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
679 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
680 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
681 th_growbuf_t *th_growbuf_new(const size_t mingrow) |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
682 { |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
683 th_growbuf_t *buf; |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
684 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
685 if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
686 return NULL; |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
687 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
688 th_growbuf_init(buf, mingrow); |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
689 buf->allocated = TRUE; |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
690 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
691 return buf; |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
692 } |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
693 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
694 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
695 void th_growbuf_free(th_growbuf_t *buf) |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
696 { |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
697 th_free(buf->data); |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
698 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
699 if (buf->allocated) |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
700 th_free(buf); |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
701 } |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
702 |
13f001f1ad6e
More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
703 |
109
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
704 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
|
705 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
706 if (buf == NULL) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
707 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
708 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
709 if (buf->data == NULL || buf->len + grow >= buf->size) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
710 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
711 buf->size += grow + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW); |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
712 buf->data = (uint8_t *) th_realloc(buf->data, buf->size); |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
713 if (buf->data == NULL) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
714 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
715 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
716 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
717 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
718 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
719 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
720 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
|
721 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
722 size_t slen; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
723 if (str == NULL) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
724 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
725 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
726 slen = strlen(str); |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
727 if (!th_growbuf_grow(buf, slen + 1)) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
728 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
729 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
730 memcpy(buf->data + buf->len, str, slen + 1); |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
731 buf->len += eos ? (slen + 1) : slen; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
732 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
733 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
734 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
735 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
736 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
737 BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
738 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
739 if (!th_growbuf_grow(buf, sizeof(char))) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
740 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
741 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
742 buf->data[buf->len++] = (uint8_t) ch; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
743 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
744 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
745 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
746 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
747 |
120
41c2638ee537
Use void* instead of uint8_t* in th_growbuf_put_str()
Matti Hamalainen <ccr@tnsp.org>
parents:
115
diff
changeset
|
748 BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len) |
111
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
749 { |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
750 if (s == NULL) |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
751 return FALSE; |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
752 |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
753 if (!th_growbuf_grow(buf, len + 1)) |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
754 return FALSE; |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
755 |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
756 memcpy(buf->data + buf->len, s, len + 1); |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
757 buf->len += len; |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
758 |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
759 return TRUE; |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
760 } |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
761 |
cddfc43a4e45
More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
109
diff
changeset
|
762 |
109
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
763 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
|
764 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
765 if (!th_growbuf_grow(buf, sizeof(uint8_t))) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
766 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
767 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
768 buf->data[buf->len++] = val; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
769 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
770 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
771 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
772 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
773 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
774 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
|
775 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
776 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
777 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
778 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
779 buf->data[buf->len++] = (val >> 8) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
780 buf->data[buf->len++] = val & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
781 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
782 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
783 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
784 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
785 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
786 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
|
787 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
788 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
789 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
790 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
791 buf->data[buf->len++] = val & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
792 buf->data[buf->len++] = (val >> 8) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
793 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
794 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
795 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
796 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
797 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
798 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
|
799 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
800 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
801 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
802 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
803 buf->data[buf->len++] = (val >> 24) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
804 buf->data[buf->len++] = (val >> 16) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
805 buf->data[buf->len++] = (val >> 8) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
806 buf->data[buf->len++] = val & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
807 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
808 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
809 } |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
810 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
811 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
812 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
|
813 { |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
814 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
815 return FALSE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
816 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
817 buf->data[buf->len++] = val & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
818 buf->data[buf->len++] = (val >> 8) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
819 buf->data[buf->len++] = (val >> 16) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
820 buf->data[buf->len++] = (val >> 24) & 0xff; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
821 |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
822 return TRUE; |
fedad0ed894f
Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
823 } |