Mercurial > hg > th-libs
annotate th_datastruct.c @ 687:5a7254b78614
Add th_strchr().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 06 Mar 2020 14:26:45 +0200 |
parents | b383f4e6ab89 |
children | 9c61834c191b |
rev | line source |
---|---|
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Various data structure functions |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
553 | 4 * (C) Copyright 2002-2020 Tecnic Software productions (TNSP) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_datastruct.h" |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 /* |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 * Doubly linked list handling |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 * |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 * In this implementation first node's prev points to last node of the list, |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 * and last node's next is NULL. This way we can semi-efficiently traverse to |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 * beginning and end of the list, assuming user does not do weird things. |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 */ |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 th_llist_t * th_llist_new(void *data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 th_llist_t *res = th_malloc0(sizeof(th_llist_t)); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 res->data = data; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 return res; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *)) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 th_llist_t *next = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 freefunc(curr); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 curr = next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
560
414755d33796
Rename th_llist_free_func() to th_llist_free_func_data() to make it a bit more clear what this function does.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
38 void th_llist_free_func_data(th_llist_t *list, void (*freefunc)(void *data)) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 th_llist_t *next = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 if (curr->data != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 freefunc(curr->data); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 th_free(curr); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 curr = next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 void th_llist_free(th_llist_t *list) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 th_llist_t *next = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 th_free(curr); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 curr = next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 void th_llist_append_node(th_llist_t **list, th_llist_t *node) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 if (*list != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 node->prev = (*list)->prev; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 (*list)->prev->next = node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 (*list)->prev = node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 (*list)->num++; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 else |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 *list = node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 node->prev = node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 (*list)->num = 1; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 node->next = NULL; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 th_llist_t *th_llist_append(th_llist_t **list, void *data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 th_llist_t *node = th_llist_new(data); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 th_llist_append_node(list, node); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 return node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 void th_llist_prepend_node(th_llist_t **list, th_llist_t *node) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 if (*list != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 node->prev = (*list)->prev; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 node->next = *list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 (*list)->prev = node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 node->num = (*list)->num + 1; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 *list = node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 else |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 *list = node->prev = node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 node->next = NULL; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 (*list)->num = 1; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 th_llist_t *th_llist_prepend(th_llist_t **list, void *data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 th_llist_t *node = th_llist_new(data); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 th_llist_prepend_node(list, node); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 return node; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 /* |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 1) Remove a middle node |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 node0->prev->next = node->next (node1) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 node0->next->prev = node->prev (list) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 node2 <- list <=> node1 <=> node2 -> NULL |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 2) Remove first node when many items |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 node2 <- node0 <=> node1 <=> node2 -> NULL |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 *list = node0 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 3) Remove last node in list |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 if (node->next == NULL) { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 list->prev = node->prev; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 node->prev->next = NULL; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 node1 <- list <=> node0 <=> node1 -> NULL |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 4) Remove last |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 list <- list -> NULL |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
155 |
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
156 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 */ |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 void th_llist_delete_node_fast(th_llist_t **list, th_llist_t *node) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 if (node == *list) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 // First node in list |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 th_llist_t *tmp = (*list)->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 if (tmp != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 tmp->num = (*list)->num - 1; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 tmp->prev = (*list)->prev; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 *list = tmp; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 else |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 // Somewhere in middle or end |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 if (node->prev != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 node->prev->next = node->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 if (node->next != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 node->next->prev = node->prev; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 else |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 (*list)->prev = node; // Last node |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 (*list)->num--; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
184 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 node->next = node->prev = NULL; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
189 void th_llist_delete_node(th_llist_t **list, th_llist_t *node) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 th_llist_t *curr = *list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 th_llist_t *next = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 if (curr == node) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 th_llist_delete_node_fast(list, curr); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 th_free(node); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 break; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 curr = next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 void th_llist_delete(th_llist_t **list, const void *data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 th_llist_t *curr = *list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 th_llist_t *next = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 if (curr->data == data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 th_llist_delete_node_fast(list, curr); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
217 th_free(curr); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 break; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
219 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 curr = next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
226 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 size_t i; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 for (i = 0; curr != NULL && i < n; curr = curr->next, i++); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 |
349
5c5b3919263d
th_llist_get_nth() returned last value of list if N > number of elements.
Matti Hamalainen <ccr@tnsp.org>
parents:
348
diff
changeset
|
232 return (i == n) ? curr : NULL; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 size_t th_llist_length(const th_llist_t *list) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 { |
348 | 238 return (list == NULL) ? 0 : list->num; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 const th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 ssize_t i = 0; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
249 if (curr == node) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 return i; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 else |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 i++; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 curr = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
256 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 return -1; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
267 func(curr, data); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 curr = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
272 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
273 int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_t **ret) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
275 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
277 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
278 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
279 int res = func(curr, data); |
670 | 280 if (res != THERR_OK) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
281 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
282 *ret = curr; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
283 return res; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
285 curr = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
286 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
287 |
670 | 288 return THERR_OK; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
289 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
291 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 th_llist_t * th_llist_find(th_llist_t *list, const void *data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
293 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
294 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
298 if (curr->data == data) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
299 return curr; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
300 curr = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
302 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
303 return NULL; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
304 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 th_llist_t * th_llist_find_func(th_llist_t *list, const void *userdata, int (compare)(const void *, const void *)) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 th_llist_t *curr = list; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
311 while (curr != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
312 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
313 if (compare(curr->data, userdata) == 0) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
314 return curr; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
315 curr = curr->next; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
317 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
318 return NULL; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
319 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
320 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
321 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 /* |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 * Ringbuffers |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 */ |
631
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
325 int th_ringbuf_init(th_ringbuf_t *buf, const size_t size, void (*mdeallocator)(void *data)) |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
326 { |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
327 // Must have at least 2 elements |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
328 memset(buf, 0, sizeof(th_ringbuf_t)); |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
329 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
330 if (size < 2) |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
331 return THERR_BOUNDS; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
332 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
333 if ((buf->data = th_calloc(size, sizeof(void *))) == NULL) |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
334 return THERR_MALLOC; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
335 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
336 buf->size = size; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
337 buf->n = 0; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
338 buf->deallocator = mdeallocator; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
339 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
340 return THERR_OK; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
341 } |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
342 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
343 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
344 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data)) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
345 { |
631
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
346 th_ringbuf_t *buf; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
347 int res; |
628
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
348 |
631
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
349 if ((buf = th_malloc0(sizeof(th_ringbuf_t))) == NULL) |
628
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
350 return NULL; |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
351 |
631
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
352 if ((res = th_ringbuf_init(buf, size, mdeallocator)) != THERR_OK) |
628
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
353 { |
631
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
354 th_free(buf); |
628
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
355 return NULL; |
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
356 } |
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
357 |
631
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
358 buf->allocated = TRUE; |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
359 return buf; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
360 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
361 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
362 |
632
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
363 int th_ringbuf_grow(th_ringbuf_t *buf, const size_t nadd) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 { |
632
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
365 if (buf == NULL) |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
366 return THERR_NULLPTR; |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
367 |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
368 buf->data = (void **) th_realloc(buf->data, (buf->size + nadd) * sizeof(void *)); |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
369 if (buf->data == NULL) |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
370 return THERR_MALLOC; |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
371 |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
372 memset(buf->data + buf->size, 0, sizeof(void *) * nadd); |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
373 buf->size += nadd; |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
374 |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
375 return THERR_OK; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
376 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
377 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
378 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
379 void th_ringbuf_free(th_ringbuf_t *buf) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
380 { |
626
98c8bd80f633
Fix ringbuf size and n types.
Matti Hamalainen <ccr@tnsp.org>
parents:
581
diff
changeset
|
381 for (size_t i = 0; i < buf->size; i++) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
382 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
383 if (buf->data[i] != NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
384 buf->deallocator(buf->data[i]); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
386 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
387 th_free(buf->data); |
631
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
388 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
389 if (buf->allocated) |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
390 th_free(buf); |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
391 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
392 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
393 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
394 void th_ringbuf_add(th_ringbuf_t *buf, void *ptr) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
395 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
396 if (buf->n < buf->size) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 buf->n++; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
398 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
399 th_free(buf->data[0]); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
400 memmove(&(buf->data[0]), &(buf->data[1]), (buf->size - 1) * sizeof(void *)); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
401 buf->data[buf->size - 1] = ptr; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
402 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
403 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
404 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
405 /* |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
406 * Growing buffer |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
407 */ |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
408 void th_growbuf_clear(th_growbuf_t *buf) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
409 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
410 // Simply reset the current "length" |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
411 buf->len = 0; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
412 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
413 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
414 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
415 void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
416 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
417 // Initialize the buffer structure |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
418 memset(buf, 0, sizeof(th_growbuf_t)); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
419 buf->mingrow = mingrow; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
420 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
421 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
422 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 th_growbuf_t *th_growbuf_new(const size_t mingrow) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
424 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
425 th_growbuf_t *buf; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
426 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
427 if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
428 return NULL; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
429 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
430 th_growbuf_init(buf, mingrow); |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
431 buf->allocated = TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
432 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
433 return buf; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
434 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
435 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
436 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
437 void th_growbuf_free(th_growbuf_t *buf) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
438 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
439 th_free(buf->data); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
440 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
441 if (buf->allocated) |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
442 th_free(buf); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
443 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
444 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
445 |
464 | 446 BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t amount) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
447 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
448 if (buf == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
449 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
450 |
464 | 451 if (buf->data == NULL || buf->len + amount >= buf->size) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
452 { |
464 | 453 buf->size += amount + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW); |
454 if ((buf->data = th_realloc(buf->data, buf->size)) == NULL) | |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
455 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
456 } |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
457 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
458 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
459 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
460 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
461 BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
462 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
463 size_t slen; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
464 if (str == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
465 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
466 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
467 slen = strlen(str); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
468 if (!th_growbuf_grow(buf, slen + 1)) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
469 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
470 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
471 memcpy(buf->data + buf->len, str, slen + 1); |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
472 buf->len += eos ? (slen + 1) : slen; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
473 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
474 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
475 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
476 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
477 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
478 BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
479 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
480 if (!th_growbuf_grow(buf, sizeof(char))) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
481 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
482 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
483 buf->data[buf->len++] = (uint8_t) ch; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
484 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
485 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
486 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
487 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
488 |
464 | 489 BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *str, const size_t len) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
490 { |
464 | 491 if (str == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
492 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
493 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
494 if (!th_growbuf_grow(buf, len + 1)) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
495 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
496 |
464 | 497 memcpy(buf->data + buf->len, str, len + 1); |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
498 buf->len += len; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
499 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
500 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
501 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
502 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
503 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
504 BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
505 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
506 if (!th_growbuf_grow(buf, sizeof(uint8_t))) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
507 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
508 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
509 buf->data[buf->len++] = val; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
510 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
511 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
512 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
513 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
514 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
515 BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
516 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
517 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
518 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
519 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
520 buf->data[buf->len++] = (val >> 8) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
521 buf->data[buf->len++] = val & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
522 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
523 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
524 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
525 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
526 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
527 BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
528 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
529 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
530 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
531 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
532 buf->data[buf->len++] = val & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
533 buf->data[buf->len++] = (val >> 8) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
534 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
535 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
536 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
537 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
538 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
539 BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
540 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
541 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
542 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
543 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
544 buf->data[buf->len++] = (val >> 24) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
545 buf->data[buf->len++] = (val >> 16) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
546 buf->data[buf->len++] = (val >> 8) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
547 buf->data[buf->len++] = val & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
548 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
549 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
550 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
551 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
552 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
553 BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val) |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
554 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
555 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
556 return FALSE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
557 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
558 buf->data[buf->len++] = val & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
559 buf->data[buf->len++] = (val >> 8) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
560 buf->data[buf->len++] = (val >> 16) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
561 buf->data[buf->len++] = (val >> 24) & 0xff; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
562 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
563 return TRUE; |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
564 } |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
565 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
566 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
567 /* |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
568 * Simple legacy string growing buffer |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
569 */ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
570 BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
571 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
572 if (*buf == NULL) |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
573 *bufsize = *len = 0; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
574 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
575 if (*buf == NULL || *len + grow >= *bufsize) |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
576 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
577 *bufsize += grow + TH_BUFGROW; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
578 *buf = th_realloc(*buf, *bufsize); |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
579 if (*buf == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
580 return FALSE; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
581 } |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
582 return TRUE; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
583 } |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
584 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
585 |
581
935ee9a9f631
Change 'ch' argument of th_strbuf_putch from char to uint8_t.
Matti Hamalainen <ccr@tnsp.org>
parents:
560
diff
changeset
|
586 BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const uint8_t ch) |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
587 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
588 if (!th_strbuf_grow(buf, bufsize, len, 1)) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
589 return FALSE; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
590 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
591 (*buf)[*len] = ch; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
592 (*len)++; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
593 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
594 return TRUE; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
595 } |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
596 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
597 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
598 BOOL th_strbuf_putsn(char **buf, size_t *bufsize, size_t *len, const char *str, const size_t slen) |
445
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
599 { |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
600 if (str == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
601 return FALSE; |
445
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
602 |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
603 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
604 return FALSE; |
445
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
605 |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
606 memcpy(*buf + *len, str, slen); |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
607 (*len) += slen; |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
608 *(buf + *len + slen) = 0; |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
609 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
610 return TRUE; |
445
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
611 } |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
612 |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
613 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
614 BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str) |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
615 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
616 size_t slen; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
617 if (str == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
618 return FALSE; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
619 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
620 slen = strlen(str); |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
621 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
622 return FALSE; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
623 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
624 memcpy(*buf + *len, str, slen + 1); |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
625 (*len) += slen; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
626 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
627 return TRUE; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
628 } |