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