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