1 |
/* Creation date: 2005-07-02 22:25:10
|
2 |
* Authors: Don
|
3 |
* Change log:
|
4 |
*/
|
5 |
|
6 |
/* Copyright (c) 2005 Don Owens
|
7 |
All rights reserved.
|
8 |
|
9 |
This code is released under the BSD license:
|
10 |
|
11 |
Redistribution and use in source and binary forms, with or without
|
12 |
modification, are permitted provided that the following conditions
|
13 |
are met:
|
14 |
|
15 |
* Redistributions of source code must retain the above copyright
|
16 |
notice, this list of conditions and the following disclaimer.
|
17 |
|
18 |
* Redistributions in binary form must reproduce the above
|
19 |
copyright notice, this list of conditions and the following
|
20 |
disclaimer in the documentation and/or other materials provided
|
21 |
with the distribution.
|
22 |
|
23 |
* Neither the name of the author nor the names of its
|
24 |
contributors may be used to endorse or promote products derived
|
25 |
from this software without specific prior written permission.
|
26 |
|
27 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
28 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
29 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
30 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
31 |
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
32 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
33 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
34 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
35 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
36 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
37 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
38 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
39 |
*/
|
40 |
|
41 |
#ifndef _CFU_LIST_
|
42 |
#define _CFU_LIST_
|
43 |
|
44 |
#include <cfu.h>
|
45 |
|
46 |
#include <sys/types.h>
|
47 |
#include <stdio.h>
|
48 |
#include <string.h>
|
49 |
#include <pthread.h>
|
50 |
|
51 |
#ifdef __cplusplus
|
52 |
extern "C" {
|
53 |
#endif
|
54 |
|
55 |
/* The list itself. */
|
56 |
struct cfulist;
|
57 |
typedef struct cfulist cfulist_t;
|
58 |
|
59 |
/* Function called for each element in the list when passed to
|
60 |
* cfulist_foreach(). A non-zero return value means to stop
|
61 |
* iteration.
|
62 |
*/
|
63 |
typedef int (*cfulist_foreach_fn_t)(void *data, size_t data_size, void *arg);
|
64 |
|
65 |
/* Function called for each element in the list when passed to
|
66 |
* cfulist_map(). The return value is used to build a new
|
67 |
* list.
|
68 |
*/
|
69 |
typedef void * (*cfulist_map_fn_t)(void *data, size_t data_size, void *arg,
|
70 |
size_t *new_data_size);
|
71 |
|
72 |
/* Function called to free the data in an element. */
|
73 |
typedef void (*cfulist_free_fn_t)(void *data);
|
74 |
|
75 |
/* Returns a new list. */
|
76 |
extern cfulist_t * cfulist_new();
|
77 |
|
78 |
/* Same as cfulist_new(), but set a function to be called on each
|
79 |
* element when the list is destroyed.
|
80 |
*/
|
81 |
extern cfulist_t * cfulist_new_with_free_fn(cfulist_free_fn_t free_fn);
|
82 |
|
83 |
/* Returns the number of entries in the list. */
|
84 |
extern size_t cfulist_num_entries(cfulist_t *list);
|
85 |
|
86 |
/* Manipulating list entries */
|
87 |
|
88 |
/* Push a value onto the end of the list. */
|
89 |
extern int cfulist_push_data(cfulist_t *list, void *data, size_t data_size);
|
90 |
|
91 |
/* Pop a value from the end of the list. */
|
92 |
extern int cfulist_pop_data(cfulist_t *list, void **data, size_t *data_size);
|
93 |
|
94 |
/* Add a value at the beginning of the list. */
|
95 |
extern int cfulist_unshift_data(cfulist_t *list, void *data, size_t data_size);
|
96 |
|
97 |
/* Shift a value off the beginning of the list. */
|
98 |
extern int cfulist_shift_data(cfulist_t *list, void **data, size_t *data_size);
|
99 |
|
100 |
/* Add a value at the end of the queue (equivalent to push) */
|
101 |
extern int cfulist_enqueue_data(cfulist_t *list, void *data, size_t data_size);
|
102 |
|
103 |
/* Remove the value at the beginning of the list (equivalent to shift). */
|
104 |
extern int cfulist_dequeue_data(cfulist_t *list, void **data, size_t *data_size);
|
105 |
|
106 |
/* Return the last entry from the list (without removing it from
|
107 |
* the list).
|
108 |
*/
|
109 |
extern int cfulist_first_data(cfulist_t *list, void **data, size_t *data_size);
|
110 |
|
111 |
/* Return the last entry from the list (without removing it from
|
112 |
* the list).
|
113 |
*/
|
114 |
extern int cfulist_last_data(cfulist_t *list, void **data, size_t *data_size);
|
115 |
|
116 |
/* Return the nth entry from the list (without removing it from
|
117 |
* the list). n starts at zero.
|
118 |
*/
|
119 |
extern int cfulist_nth_data(cfulist_t *list, void **data, size_t *data_size, size_t n);
|
120 |
|
121 |
extern void cfulist_reset_each(cfulist_t *list);
|
122 |
extern int cfulist_each_data(cfulist_t *list, void **data, size_t *data_size);
|
123 |
extern int cfulist_next_data(cfulist_t *list, void **data, size_t *data_size);
|
124 |
|
125 |
/* Calls fe_fn() for each element in the list. Also passes arg on
|
126 |
* each call. If fe_fn() returns a non-zero value, the iteration
|
127 |
* over the elements stops.
|
128 |
*/
|
129 |
extern size_t cfulist_foreach(cfulist_t *list, cfulist_foreach_fn_t fe_fn, void *arg);
|
130 |
|
131 |
/* Creates a new list from the list passed in. Calls map_fn() on
|
132 |
* each element in the list. The return value is placed in the
|
133 |
* corresponding position in the new list.
|
134 |
*/
|
135 |
extern cfulist_t *cfulist_map(cfulist_t *list, cfulist_map_fn_t map_fn, void *arg);
|
136 |
|
137 |
/* Free all resources used by the list. */
|
138 |
extern void cfulist_destroy(cfulist_t *list);
|
139 |
extern void cfulist_destroy_with_free_fn(cfulist_t *list, cfulist_free_fn_t free_fn);
|
140 |
|
141 |
/* When you don't care about the size of the data */
|
142 |
|
143 |
extern int cfulist_push(cfulist_t *list, void *data);
|
144 |
extern void * cfulist_pop(cfulist_t *list);
|
145 |
extern int cfulist_unshift(cfulist_t *list, void *data);
|
146 |
extern void * cfulist_shift(cfulist_t *list);
|
147 |
extern int cfulist_enqueue(cfulist_t *list, void *data);
|
148 |
extern void *cfulist_dequeue(cfulist_t *list);
|
149 |
|
150 |
/* Strings -- assume data is a null-terminated string -- size is
|
151 |
* calculated by strlen(data) + 1
|
152 |
*/
|
153 |
|
154 |
extern int cfulist_push_string(cfulist_t *list, char *data);
|
155 |
extern char * cfulist_pop_string(cfulist_t *list);
|
156 |
extern int cfulist_unshift_string(cfulist_t *list, char *data);
|
157 |
extern char * cfulist_shift_string(cfulist_t *list);
|
158 |
extern int cfulist_enqueue_string(cfulist_t *list, char *data);
|
159 |
extern char *cfulist_dequeue_string(cfulist_t *list);
|
160 |
|
161 |
extern char *cfulist_join(cfulist_t *list, const char *delimiter);
|
162 |
|
163 |
#ifdef __cplusplus
|
164 |
}
|
165 |
#endif
|
166 |
|
167 |
#endif
|