Mercurial > hg > th-libs
annotate th_util.c @ 59:82cfbebaa8b9
Sync.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 15 Aug 2012 14:15:51 +0300 |
parents | 300fba04b7ad |
children | eef9c6312b88 |
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 |
16 | 4 * (C) Copyright 2002-2010 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> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
11 /* Default settings |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 */ |
10 | 13 static BOOL th_initialized = FALSE; |
14 int th_verbosityLevel = 2; | |
15 char *th_prog_name = NULL, | |
16 *th_prog_fullname = NULL, | |
17 *th_prog_version = NULL, | |
18 *th_prog_author = NULL, | |
19 *th_prog_license = NULL; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 /* 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
|
23 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 void th_init(char *progName, char *progFullName, char *progVersion, |
10 | 25 char *progAuthor, char *progLicense) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 { |
10 | 27 th_prog_name = progName; |
28 th_prog_fullname = progFullName; | |
29 th_prog_version = progVersion; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 |
10 | 31 if (progAuthor) |
32 th_prog_author = progAuthor; | |
33 else | |
34 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
|
35 |
10 | 36 if (progLicense) |
37 th_prog_license = progLicense; | |
38 else | |
39 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
|
40 |
10 | 41 th_initialized = TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
45 void th_print_banner(FILE *outFile, const char *binName, const char *progUsage) |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
46 { |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
47 fprintf(outFile, |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
48 "\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
|
49 "%s\n" |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
50 "%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 "Usage: %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
|
52 th_prog_name, th_prog_version, th_prog_fullname, |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
53 th_prog_author, th_prog_license, binName, progUsage); |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
54 } |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
55 |
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
40
diff
changeset
|
56 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 /* 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
|
58 * 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
|
59 */ |
11 | 60 void THERR_V(const char *fmt, va_list ap) |
61 { | |
62 assert(th_initialized == TRUE); | |
63 | |
64 fprintf(stderr, "%s: ", th_prog_name); | |
65 vfprintf(stderr, fmt, ap); | |
66 } | |
67 | |
68 | |
69 void THMSG_V(int level, const char *fmt, va_list ap) | |
70 { | |
71 assert(th_initialized == TRUE); | |
72 | |
48 | 73 if (th_verbosityLevel >= level) |
74 { | |
11 | 75 fprintf(stderr, "%s: ", th_prog_name); |
76 vfprintf(stderr, fmt, ap); | |
77 } | |
78 } | |
79 | |
80 | |
81 void THPRINT_V(int level, const char *fmt, va_list ap) | |
82 { | |
83 assert(th_initialized == TRUE); | |
84 | |
48 | 85 if (th_verbosityLevel >= level) |
86 { | |
11 | 87 vfprintf(stderr, fmt, ap); |
88 } | |
89 } | |
90 | |
91 | |
92 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
|
93 { |
10 | 94 va_list ap; |
11 | 95 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 |
11 | 97 va_start(ap, fmt); |
98 THERR_V(fmt, ap); | |
10 | 99 va_end(ap); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 |
11 | 103 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
|
104 { |
10 | 105 va_list ap; |
11 | 106 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 |
11 | 108 va_start(ap, fmt); |
109 THMSG_V(level, fmt, ap); | |
110 va_end(ap); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 |
11 | 114 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
|
115 { |
10 | 116 va_list ap; |
11 | 117 assert(th_initialized == TRUE); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 |
11 | 119 va_start(ap, fmt); |
120 THPRINT_V(level, fmt, ap); | |
121 va_end(ap); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 |
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 /* Memory handling routines |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 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
|
128 { |
10 | 129 return malloc(l); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 } |
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 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
|
134 { |
10 | 135 return calloc(n, l); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 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
|
140 { |
10 | 141 return realloc(p, l); |
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 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 void th_free(void *p) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 { |
10 | 147 /* Check for NULL pointers for portability due to some libc |
148 * implementations not handling free(NULL) too well. | |
149 */ | |
150 if (p) free(p); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 #ifndef HAVE_MEMSET |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 void *th_memset(void *p, int c, size_t n) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 { |
10 | 157 unsigned char *dp = (unsigned char *) p; |
158 | |
159 while (n--) | |
160 *(dp++) = c; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 |
10 | 162 return p; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 #endif |
16 | 165 |
166 /* Doubly linked list handling | |
167 * | |
168 * In this implementation first node's prev points to last node of the list, | |
169 * and last node's next is NULL. This way we can semi-efficiently traverse to | |
170 * beginning and end of the list, assuming user does not do weird things. | |
171 */ | |
172 qlist_t * th_llist_new(void *data) | |
173 { | |
174 qlist_t *res = th_calloc(sizeof(qlist_t), 1); | |
175 res->data = data; | |
176 return res; | |
177 } | |
178 | |
179 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data)) | |
180 { | |
181 qlist_t *curr = list; | |
182 | |
40 | 183 while (curr != NULL) |
184 { | |
16 | 185 qlist_t *next = curr->next; |
186 if (freefunc != NULL && curr->data != NULL) | |
187 freefunc(curr->data); | |
188 th_free(curr); | |
189 curr = next; | |
190 } | |
191 } | |
192 | |
193 | |
194 void th_llist_free(qlist_t *list) | |
195 { | |
196 th_llist_free_func(list, NULL); | |
197 } | |
198 | |
53
2c90b33d3617
Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
199 |
16 | 200 void th_llist_append_node(qlist_t **list, qlist_t *node) |
201 { | |
40 | 202 if (*list != NULL) |
203 { | |
16 | 204 node->prev = (*list)->prev; |
205 (*list)->prev->next = node; | |
206 (*list)->prev = node; | |
207 (*list)->num++; | |
40 | 208 } |
209 else | |
210 { | |
16 | 211 *list = node; |
59 | 212 node->prev = node; |
16 | 213 (*list)->num = 1; |
214 } | |
215 | |
216 node->next = NULL; | |
217 } | |
218 | |
219 | |
220 qlist_t *th_llist_append(qlist_t **list, void *data) | |
221 { | |
222 qlist_t *node = th_llist_new(data); | |
223 | |
224 th_llist_append_node(list, node); | |
225 | |
226 return node; | |
227 } | |
228 | |
229 | |
230 void th_llist_prepend_node(qlist_t **list, qlist_t *node) | |
231 { | |
40 | 232 if (*list != NULL) |
233 { | |
16 | 234 node->prev = (*list)->prev; |
235 node->next = *list; | |
236 (*list)->prev = node; | |
237 node->num = (*list)->num + 1; | |
238 *list = node; | |
40 | 239 } |
240 else | |
241 { | |
16 | 242 *list = node->prev = node; |
243 node->next = NULL; | |
244 (*list)->num = 1; | |
245 } | |
246 | |
247 } | |
248 | |
249 | |
250 qlist_t *th_llist_prepend(qlist_t **list, void *data) | |
251 { | |
252 qlist_t *node = th_llist_new(data); | |
253 | |
254 th_llist_prepend_node(list, node); | |
255 | |
256 return node; | |
257 } | |
258 | |
259 /* | |
260 1) Remove a middle node | |
261 | |
262 node0->prev->next = node->next (node1) | |
263 node0->next->prev = node->prev (list) | |
264 | |
265 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL | |
266 node2 <- list <=> node1 <=> node2 -> NULL | |
267 | |
268 2) Remove first node when many items | |
269 | |
270 | |
271 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL | |
272 node2 <- node0 <=> node1 <=> node2 -> NULL | |
273 | |
274 *list = node0 | |
275 | |
276 3) Remove last node in list | |
277 | |
278 if (node->next == NULL) { | |
279 list->prev = node->prev; | |
280 node->prev->next = NULL; | |
281 } | |
282 | |
283 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL | |
284 node1 <- list <=> node0 <=> node1 -> NULL | |
285 | |
286 4) Remove last | |
287 | |
288 list <- list -> NULL | |
289 | |
290 | |
291 */ | |
292 static void th_llist_delete_node_fast(qlist_t **list, qlist_t *node) | |
293 { | |
40 | 294 if (node == *list) |
295 { | |
16 | 296 /* First node in list */ |
297 qlist_t *tmp = (*list)->next; | |
40 | 298 if (tmp != NULL) |
299 { | |
16 | 300 tmp->num = (*list)->num - 1; |
301 tmp->prev = (*list)->prev; | |
302 } | |
303 *list = tmp; | |
40 | 304 } |
305 else | |
306 { | |
16 | 307 /* Somewhere in middle or end */ |
308 if (node->prev != NULL) | |
309 node->prev->next = node->next; | |
310 | |
311 if (node->next != NULL) | |
312 node->next->prev = node->prev; | |
313 else | |
314 (*list)->prev = node; /* Last node */ | |
315 | |
316 (*list)->num--; | |
317 } | |
318 | |
319 node->next = node->prev = NULL; | |
320 } | |
321 | |
322 | |
323 void th_llist_delete_node(qlist_t **list, qlist_t *node) | |
324 { | |
325 qlist_t *curr = *list; | |
326 | |
40 | 327 while (curr != NULL) |
328 { | |
16 | 329 qlist_t *next = curr->next; |
40 | 330 if (curr == node) |
331 { | |
16 | 332 th_llist_delete_node_fast(list, curr); |
333 th_free(node); | |
334 break; | |
335 } | |
336 curr = next; | |
337 } | |
338 } | |
339 | |
340 | |
341 void th_llist_delete(qlist_t **list, const void *data) | |
342 { | |
343 qlist_t *curr = *list; | |
344 | |
40 | 345 while (curr != NULL) |
346 { | |
16 | 347 qlist_t *next = curr->next; |
40 | 348 if (curr->data == data) |
349 { | |
16 | 350 th_llist_delete_node_fast(list, curr); |
351 th_free(curr); | |
352 break; | |
353 } | |
354 curr = next; | |
355 } | |
356 } | |
357 | |
358 | |
359 qlist_t * th_llist_get_nth(qlist_t *list, const size_t n) | |
360 { | |
361 qlist_t *curr = list; | |
362 size_t i; | |
363 | |
364 for (i = 0; curr != NULL && i < n; curr = curr->next, i++); | |
365 | |
366 return curr; | |
367 } | |
368 | |
369 | |
370 size_t th_llist_length(const qlist_t *list) | |
371 { | |
372 if (list == NULL) | |
373 return 0; | |
374 else | |
375 return list->num; | |
376 } | |
377 | |
378 | |
379 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node) | |
380 { | |
381 const qlist_t *curr = list; | |
382 ssize_t i = 0; | |
383 | |
40 | 384 while (curr != NULL) |
385 { | |
16 | 386 if (curr == node) |
387 return i; | |
388 else | |
389 i++; | |
390 | |
391 curr = curr->next; | |
392 } | |
393 | |
394 return -1; | |
395 } | |
396 | |
397 | |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
398 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
|
399 { |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
400 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
|
401 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
402 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
|
403 { |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
404 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
|
405 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
|
406 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
407 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
408 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
409 |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
410 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
|
411 { |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
412 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
|
413 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
414 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
|
415 { |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
416 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
|
417 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
|
418 { |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
419 *ret = curr; |
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
420 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
|
421 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
422 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
|
423 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
424 |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
425 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
|
426 } |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
427 |
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
428 |
16 | 429 qlist_t * th_llist_find(qlist_t *list, const void *data) |
430 { | |
431 qlist_t *curr = list; | |
432 | |
40 | 433 while (curr != NULL) |
434 { | |
16 | 435 if (curr->data == data) |
436 return curr; | |
437 curr = curr->next; | |
438 } | |
439 | |
440 return NULL; | |
441 } | |
442 | |
443 | |
444 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *)) | |
445 { | |
446 qlist_t *curr = list; | |
447 | |
40 | 448 while (curr != NULL) |
449 { | |
16 | 450 if (compare(curr->data, userdata) == 0) |
451 return curr; | |
452 curr = curr->next; | |
453 } | |
454 | |
455 return NULL; | |
456 } | |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
457 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
458 |
26 | 459 /* |
460 * Ringbuffers | |
461 */ | |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
462 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
|
463 { |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
464 qringbuf_t *res = th_calloc(1, sizeof(qringbuf_t)); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
465 |
33 | 466 res->data = (char **) th_calloc(size, sizeof(char *)); |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
467 res->size = size; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
468 res->n = 0; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
469 res->deallocator = mdeallocator; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
470 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
471 return res; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
472 } |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
473 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
474 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
475 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n) |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
476 { |
33 | 477 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *)); |
40 | 478 if (buf->data != NULL) |
479 { | |
480 th_memset(buf->data + buf->size, 0, sizeof(char *) * n); | |
33 | 481 buf->size += n; |
482 return TRUE; | |
483 } else | |
484 return FALSE; | |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
485 } |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
486 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
487 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
488 void th_ringbuf_free(qringbuf_t *buf) |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
489 { |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
490 int i; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
491 |
33 | 492 for (i = 0; i < buf->size; i++) |
25
2f7270a9d593
Make ringbuffer deallocator function call only happen if data pointer is != NULL
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
493 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
|
494 buf->deallocator(buf->data[i]); |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
495 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
496 th_free(buf->data); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
497 th_free(buf); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
498 } |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
499 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
500 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
501 void th_ringbuf_add(qringbuf_t *buf, void *ptr) |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
502 { |
28
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
503 if (buf->n < buf->size) |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
504 buf->n++; |
28
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
505 |
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
506 th_free(buf->data[0]); |
c47dcb8a4ffe
Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
507 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
|
508 buf->data[buf->size - 1] = ptr; |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
509 } |