Mercurial > hg > th-libs
annotate th_datastruct.c @ 761:2263dd13cf2d
Cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 05 Feb 2023 19:40:14 +0200 |
parents | 618c7fa3a4f8 |
children |
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 |
726 | 4 * (C) Copyright 2002-2022 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 */ |
689
9c61834c191b
Remove th_llist_delete() and th_llist_delete_node() and rename
Matti Hamalainen <ccr@tnsp.org>
parents:
670
diff
changeset
|
158 void th_llist_delete_node(th_llist_t **list, th_llist_t *node) |
269
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 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
|
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 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
|
193 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 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
|
195 |
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
|
196 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
|
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 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 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
|
201 { |
348 | 202 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
|
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 |
757
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
206 size_t th_llist_length_slow(const th_llist_t *list) |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
207 { |
758 | 208 const th_llist_t *curr = list; |
757
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
209 size_t i; |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
210 |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
211 for (i = 0; curr != NULL; curr = curr->next, i++); |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
212 |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
213 return i; |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
214 } |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
215 |
2ab2fece83ea
Add th_llist_length_slow().
Matti Hamalainen <ccr@tnsp.org>
parents:
756
diff
changeset
|
216 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
217 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
|
218 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
219 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
|
220 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
|
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 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
|
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 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
|
225 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
|
226 else |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 i++; |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 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
|
230 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
231 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 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
|
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 |
754
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
236 void th_llist_reverse(th_llist_t **list) |
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
237 { |
755 | 238 th_llist_t |
239 *f_curr = *list, *f_prev = NULL, | |
240 *b_curr = *list, *b_prev = NULL; | |
756
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
241 size_t count = 0; |
754
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
242 |
756
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
243 // NULL list? |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
244 if (f_curr == NULL) |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
245 return; |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
246 |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
247 // Set initial previous backward pointer |
755 | 248 if (b_curr != NULL) |
249 b_prev = b_curr->prev; | |
250 | |
756
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
251 // Reverse the list |
755 | 252 while (f_curr != NULL) |
754
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
253 { |
755 | 254 th_llist_t |
756
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
255 *f_next = f_curr->next, // next = current->next |
755 | 256 *b_next = b_curr->prev; |
257 | |
756
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
258 count++; |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
259 f_curr->num = 0; |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
260 |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
261 f_curr->next = f_prev; // current->next = previous |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
262 f_prev = f_curr; // previous = current |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
263 f_curr = f_next; // current = next |
755 | 264 |
265 b_curr->prev = b_prev; | |
266 b_prev = b_curr; | |
267 b_curr = b_next; | |
754
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
268 } |
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
269 |
756
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
270 // Update count |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
271 f_prev->num = count; |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
272 |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
273 // Set list pointer |
755 | 274 *list = f_prev; |
756
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
275 |
4bd82aca5e98
Fix list length when reversing.
Matti Hamalainen <ccr@tnsp.org>
parents:
755
diff
changeset
|
276 // Fix last previous pointer |
755 | 277 if (b_curr != NULL) |
278 b_curr->prev = b_prev; | |
754
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
279 } |
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
280 |
d91d1174cfd8
Add th_llist_reverse() for reversing a linked list.
Matti Hamalainen <ccr@tnsp.org>
parents:
753
diff
changeset
|
281 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
282 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
|
283 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 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
|
285 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
286 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
|
287 { |
761 | 288 th_llist_t *next = curr->next; |
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 func(curr, data); |
761 | 290 curr = next; |
269
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 } |
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 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 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
|
296 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 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
|
298 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
299 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
|
300 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 int res = func(curr, data); |
670 | 302 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
|
303 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
304 *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
|
305 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
|
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 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
|
308 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
309 |
670 | 310 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
|
311 } |
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 |
753
64cb2b1777a9
Rename function th_llist_find() to th_llist_find_data().
Matti Hamalainen <ccr@tnsp.org>
parents:
752
diff
changeset
|
314 th_llist_t * th_llist_find_data(th_llist_t *list, const 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
|
315 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 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
|
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 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
|
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 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
|
321 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
|
322 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
|
323 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
325 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
|
326 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
328 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
329 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
|
330 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 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
|
332 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
333 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
|
334 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 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
|
336 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
|
337 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
|
338 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
339 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
340 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
|
341 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
342 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
343 |
759
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
344 static th_llist_t *th_llist_mergesort_merge(th_llist_t *left, th_llist_t *right, |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
345 int (compare)(const th_llist_t *lnode, const th_llist_t *rnode, void *userdata), |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
346 void *userdata) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
347 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
348 th_llist_t *result; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
349 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
350 if (left == NULL) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
351 return right; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
352 else |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
353 if (right == NULL) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
354 return left; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
355 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
356 if (compare(left, right, userdata)) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
357 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
358 result = left; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
359 result->next = th_llist_mergesort_merge(left->next, right, compare, userdata); |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
360 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
361 else |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
362 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
363 result = right; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
364 result->next = th_llist_mergesort_merge(left, right->next, compare, userdata); |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
365 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
366 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
367 if (result->next != NULL) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
368 result->prev = result->prev; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
369 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
370 return result; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
371 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
372 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
373 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
374 static void th_llist_mergesort_do(th_llist_t **list, |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
375 int (compare)(const th_llist_t *lnode, const th_llist_t *rnode, void *userdata), |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
376 void *userdata) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
377 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
378 th_llist_t *head = *list, *left, *right, *slow, *fast; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
379 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
380 if (head == NULL || head->next == NULL) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
381 return; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
382 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
383 slow = head; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
384 fast = head->next; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
385 while (fast != NULL) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
386 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
387 fast = fast->next; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
388 if (fast != NULL) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
389 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
390 slow = slow->next; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
391 fast = fast->next; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
392 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
393 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
394 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
395 left = head; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
396 right = slow->next; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
397 slow->next = NULL; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
398 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
399 th_llist_mergesort_do(&left, compare, userdata); |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
400 th_llist_mergesort_do(&right, compare, userdata); |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
401 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
402 *list = th_llist_mergesort_merge(left, right, compare, userdata); |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
403 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
404 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
405 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
406 void th_llist_mergesort(th_llist_t **list, |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
407 int (compare)(const th_llist_t *lnode, const th_llist_t *rnode, void *userdata), |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
408 void *userdata) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
409 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
410 if (list != NULL && *list != NULL) |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
411 { |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
412 size_t num = (*list)->num; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
413 th_llist_mergesort_do(list, compare, userdata); |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
414 (*list)->num = num; |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
415 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
416 } |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
417 |
618c7fa3a4f8
Add th_llist_mergesort() for sorting linked lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
758
diff
changeset
|
418 |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
419 /* |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
420 * Ringbuffers |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
421 */ |
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
|
422 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
|
423 { |
731
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
424 memset(buf, 0, sizeof(*buf)); |
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
425 |
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
|
426 // 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
|
427 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
|
428 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
|
429 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
430 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
|
431 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
|
432 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
433 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
|
434 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
|
435 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
|
436 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
437 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
|
438 } |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
439 |
d5221299656a
Add new function th_ringbuf_init() and add "allocated" field to th_ringbuf_t
Matti Hamalainen <ccr@tnsp.org>
parents:
629
diff
changeset
|
440 |
731
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
441 int th_ringbuf_new(th_ringbuf_t **buf, const size_t size, void (*mdeallocator)(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
|
442 { |
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
|
443 int res; |
628
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
444 |
731
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
445 if ((*buf = th_malloc0(sizeof(th_ringbuf_t))) == NULL) |
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
446 return THERR_MALLOC; |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
447 |
731
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
448 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
|
449 { |
732 | 450 th_free(*buf); |
731
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
451 return res; |
628
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
452 } |
140e2272471c
Add some error checking in th_ringbuf_new().
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
453 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
454 (*buf)->is_allocated = true; |
731
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
455 |
98d12f33da7b
Change th_ringbuf_new() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
456 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
|
457 } |
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 |
632
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
460 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
|
461 { |
632
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
462 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
|
463 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
|
464 |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
465 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
|
466 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
|
467 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
|
468 |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
469 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
|
470 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
|
471 |
553db886533e
Return a THERR_* error value from th_ringbuf_grow() instead of BOOL. This
Matti Hamalainen <ccr@tnsp.org>
parents:
631
diff
changeset
|
472 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
|
473 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
474 |
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 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
|
477 { |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
478 if (buf != 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
|
479 { |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
480 for (size_t i = 0; i < buf->size; i++) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
481 { |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
482 buf->deallocator(buf->data[i]); |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
483 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
484 |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
485 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
|
486 |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
487 if (buf->is_allocated) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
488 th_free(buf); |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
489 } |
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 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
491 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
492 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
493 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
|
494 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
495 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
|
496 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
|
497 |
730
1a3ea8f7bb35
Use specified deallocator for ringbuffers when rotating.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
498 buf->deallocator(buf->data[0]); |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
499 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
|
500 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
|
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 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
504 /* |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
505 * 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
|
506 */ |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
507 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
|
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 // 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
|
510 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
|
511 } |
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 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
|
515 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
516 // 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
|
517 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
|
518 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
|
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 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
521 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
522 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
|
523 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
524 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
|
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 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
|
527 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
|
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 th_growbuf_init(buf, mingrow); |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
530 buf->is_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
|
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 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
|
533 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
534 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
535 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
536 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
|
537 { |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
538 if (buf != NULL) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
539 { |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
540 th_free(buf->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
|
541 |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
542 if (buf->is_allocated) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
543 th_free(buf); |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
544 } |
269
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
545 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
546 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
547 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
548 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
|
549 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
550 if (buf == NULL) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
551 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
|
552 |
464 | 553 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
|
554 { |
464 | 555 buf->size += amount + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW); |
556 if ((buf->data = th_realloc(buf->data, buf->size)) == NULL) | |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
557 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
|
558 } |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
559 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
|
560 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
561 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
562 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
563 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
|
564 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
565 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
|
566 if (str == NULL) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
567 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
|
568 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
569 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
|
570 if (!th_growbuf_grow(buf, slen + 1)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
571 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
|
572 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
573 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
|
574 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
|
575 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
576 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
|
577 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
578 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
579 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
580 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
|
581 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
582 if (!th_growbuf_grow(buf, sizeof(char))) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
583 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
|
584 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
585 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
|
586 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
587 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
|
588 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
589 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
590 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
591 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
|
592 { |
464 | 593 if (str == NULL) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
594 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
|
595 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
596 if (!th_growbuf_grow(buf, len + 1)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
597 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
|
598 |
464 | 599 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
|
600 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
|
601 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
602 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
|
603 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
604 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
605 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
606 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
|
607 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
608 if (!th_growbuf_grow(buf, sizeof(uint8_t))) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
609 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
|
610 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
611 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
|
612 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
613 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
|
614 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
615 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
616 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
617 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
|
618 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
619 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
620 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
|
621 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
622 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
|
623 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
|
624 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
625 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
|
626 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
627 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
628 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
629 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
|
630 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
631 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
632 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
|
633 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
634 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
|
635 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
|
636 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
637 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
|
638 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
639 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
640 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
641 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
|
642 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
643 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
644 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
|
645 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
646 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
|
647 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
|
648 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
|
649 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
|
650 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
651 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
|
652 } |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
653 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
654 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
655 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
|
656 { |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
657 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
658 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
|
659 |
fcbdc12f5866
Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
660 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
|
661 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
|
662 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
|
663 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
|
664 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
665 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
|
666 } |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
667 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
668 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
669 /* |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
670 * 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
|
671 */ |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
672 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
|
673 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
674 if (*buf == NULL) |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
675 *bufsize = *len = 0; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
676 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
677 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
|
678 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
679 *bufsize += grow + TH_BUFGROW; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
680 *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
|
681 if (*buf == NULL) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
682 return false; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
683 } |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
684 return true; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
685 } |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
686 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
687 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
688 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
|
689 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
690 if (!th_strbuf_grow(buf, bufsize, len, 1)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
691 return false; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
692 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
693 (*buf)[*len] = ch; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
694 (*len)++; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
695 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
696 return true; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
697 } |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
698 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
699 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
700 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
|
701 { |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
702 if (str == NULL) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
703 return false; |
445
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
704 |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
705 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
706 return false; |
445
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
707 |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
708 memcpy(*buf + *len, str, slen); |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
709 (*len) += slen; |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
710 *(buf + *len + slen) = 0; |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
711 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
712 return true; |
445
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
713 } |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
714 |
6d29aaeab290
Add new function th_strbuf_putsn().
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
715 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
716 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
|
717 { |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
718 size_t slen; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
719 if (str == NULL) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
720 return false; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
721 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
722 slen = strlen(str); |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
723 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
724 return false; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
725 |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
726 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
|
727 (*len) += slen; |
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
728 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
733
diff
changeset
|
729 return true; |
271
2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
730 } |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
731 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
732 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
733 int th_ptrlist_init(th_ptrlist_t *list, const size_t ninitial, const size_t ngrow) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
734 { |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
735 if (list == NULL) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
736 return THERR_NULLPTR; |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
737 |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
738 memset(list, 0, sizeof(*list)); |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
739 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
740 if (ngrow == 0) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
741 return THERR_INVALID_ARGS; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
742 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
743 list->nitems = 0; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
744 list->nallocated = ninitial; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
745 list->ngrow = ngrow; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
746 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
747 if ((list->items = th_malloc(list->nallocated * sizeof(void *))) == NULL) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
748 return THERR_MALLOC; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
749 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
750 return THERR_OK; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
751 } |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
752 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
753 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
754 int th_ptrlist_new(th_ptrlist_t **list, const size_t ninitial, const size_t ngrow) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
755 { |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
756 int res; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
757 |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
758 if (list == NULL) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
759 return THERR_NULLPTR; |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
760 |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
761 if ((*list = th_malloc0(sizeof(th_ptrlist_t))) == NULL) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
762 return THERR_MALLOC; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
763 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
764 if ((res = th_ptrlist_init(*list, ninitial, ngrow)) != THERR_OK) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
765 { |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
766 th_free(*list); |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
767 return res; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
768 } |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
769 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
770 (*list)->is_allocated = true; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
771 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
772 return THERR_OK; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
773 } |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
774 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
775 |
751 | 776 int th_ptrlist_append(th_ptrlist_t *list, void *data) |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
777 { |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
778 if (list == NULL) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
779 return THERR_NULLPTR; |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
780 |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
781 if (list->nitems + 1 >= list->nallocated) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
782 { |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
783 list->nallocated += list->ngrow; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
784 if ((list->items = th_realloc(list->items, list->nallocated * sizeof(void *))) == NULL) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
785 return THERR_MALLOC; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
786 } |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
787 |
751 | 788 list->items[list->nitems++] = data; |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
789 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
790 return THERR_OK; |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
791 } |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
792 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
793 |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
794 void th_ptrlist_free(th_ptrlist_t *list, void (*mdeallocator)(void *data)) |
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
795 { |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
796 if (list != NULL) |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
797 { |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
798 if (mdeallocator != NULL) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
799 { |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
800 for (size_t i = 0; i < list->nitems; i++) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
801 mdeallocator(list->items[i]); |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
802 } |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
803 |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
804 th_free(list->items); |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
805 |
752
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
806 if (list->is_allocated) |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
807 th_free(list); |
3710c117b7c7
Add some NULL pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
751
diff
changeset
|
808 } |
736
ca837a4417f5
Add simple growing pointerlist data stucture implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
809 } |