comparison th_datastruct.h @ 269:fcbdc12f5866

Split data structures (linked lists, growbufs, ringbufs, etc.) code into th_datastruct.[ch] module.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 17 Feb 2016 14:20:36 +0200
parents
children 19d4b3a1a37c
comparison
equal deleted inserted replaced
268:5cbf24411c02 269:fcbdc12f5866
1 /*
2 * Various data structure functions
3 * Programmed and designed by Matti 'ccr' Hamalainen
4 * (C) Copyright 2002-2016 Tecnic Software productions (TNSP)
5 *
6 * Please read file 'COPYING' for information on license and distribution.
7 */
8 #ifndef TH_DATASTRUCT_H
9 #define TH_DATASTRUCT_H
10
11 #include "th_util.h"
12
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18
19 /* Doubly linked list handling
20 */
21 typedef struct _th_llist_t
22 {
23 void *data;
24 size_t num;
25 struct _th_llist_t *prev, *next;
26 } th_llist_t;
27
28
29 th_llist_t * th_llist_new(void *data);
30 void th_llist_free(th_llist_t *list);
31 void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data));
32 void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *node));
33
34 void th_llist_append_node(th_llist_t **list, th_llist_t *node);
35 th_llist_t * th_llist_append(th_llist_t **list, void *data);
36 void th_llist_prepend_node(th_llist_t **list, th_llist_t *node);
37 th_llist_t * th_llist_prepend(th_llist_t **list, void *data);
38 void th_llist_delete(th_llist_t **list, const void *data);
39 void th_llist_delete_node(th_llist_t **list, th_llist_t *node);
40 void th_llist_delete_node_fast(th_llist_t **list, th_llist_t *node);
41
42 th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n);
43 size_t th_llist_length(const th_llist_t *list);
44 ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node);
45
46 void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data);
47 int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_t **res);
48
49 th_llist_t * th_llist_find(th_llist_t *list, const void *data);
50 th_llist_t * th_llist_find_func(th_llist_t *list, const void *userdata, int (compare)(const void *, const void *));
51
52
53 /* Ringbuffer implementation
54 */
55 typedef struct
56 {
57 char **data;
58 int n, size;
59 void (*deallocator)(void *);
60 } th_ringbuf_t;
61
62 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *));
63 BOOL th_ringbuf_grow(th_ringbuf_t *buf, const size_t n);
64 void th_ringbuf_free(th_ringbuf_t *buf);
65 void th_ringbuf_add(th_ringbuf_t *buf, void *ptr);
66
67
68 /* Growing buffers
69 */
70 #define TH_BUFGROW (32)
71
72
73 typedef struct
74 {
75 BOOL allocated;
76 uint8_t *data;
77 size_t size, len, mingrow;
78 } th_growbuf_t;
79
80
81 /* Simple growing string buffer
82 */
83 BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, const size_t grow);
84 BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch);
85 BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str);
86
87
88 /* Growing byte buffer
89 */
90 void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow);
91 void th_growbuf_clear(th_growbuf_t *buf);
92 th_growbuf_t *th_growbuf_new(const size_t mingrow);
93 void th_growbuf_free(th_growbuf_t *buf);
94
95
96 BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow);
97 BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos);
98 BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch);
99 BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len);
100 BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val);
101 BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val);
102 BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val);
103 BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val);
104 BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val);
105
106
107 #ifdef __cplusplus
108 }
109 #endif
110 #endif // TH_DATASTRUCT_H