Diffstat (limited to 'libetpan/src/data-types/clist.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libetpan/src/data-types/clist.h | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/libetpan/src/data-types/clist.h b/libetpan/src/data-types/clist.h new file mode 100644 index 0000000..0daf1ed --- a/dev/null +++ b/libetpan/src/data-types/clist.h | |||
@@ -0,0 +1,133 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * clist - Implements simple generic double-linked pointer lists | ||
5 | * | ||
6 | * Copyright (c) 1999-2005, Gaël Roualland <gael.roualland@iname.com> | ||
7 | * interface changes - 2005 - DINH Viet Hoa | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
19 | * contributors may be used to endorse or promote products derived | ||
20 | * from this software without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | /* | ||
36 | * $Id$ | ||
37 | */ | ||
38 | |||
39 | #ifndef CLIST_H | ||
40 | #define CLIST_H | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | typedef struct clistcell_s { | ||
47 | void * data; | ||
48 | struct clistcell_s * previous; | ||
49 | struct clistcell_s * next; | ||
50 | } clistcell; | ||
51 | |||
52 | struct clist_s { | ||
53 | clistcell * first; | ||
54 | clistcell * last; | ||
55 | int count; | ||
56 | }; | ||
57 | |||
58 | typedef struct clist_s clist; | ||
59 | typedef clistcell clistiter; | ||
60 | |||
61 | /* Allocate a new pointer list */ | ||
62 | clist * clist_new(); | ||
63 | |||
64 | /* Destroys a list. Data pointed by data pointers is NOT freed. */ | ||
65 | void clist_free(clist *); | ||
66 | |||
67 | /* Some of the following routines can be implemented as macros to | ||
68 | be faster. If you don't want it, define NO_MACROS */ | ||
69 | #ifdef NO_MACROS | ||
70 | |||
71 | /* Returns TRUE if list is empty */ | ||
72 | int clist_isempty(clist *); | ||
73 | |||
74 | /* Returns the number of elements in the list */ | ||
75 | int clist_count(clist *); | ||
76 | |||
77 | /* Returns an iterator to the first element of the list */ | ||
78 | clistiter * clist_begin(clist *); | ||
79 | |||
80 | /* Returns an iterator to the last element of the list */ | ||
81 | clistiter * clist_end(clist *); | ||
82 | |||
83 | /* Returns an iterator to the next element of the list */ | ||
84 | clistiter * clist_next(clistiter *); | ||
85 | |||
86 | /* Returns an iterator to the previous element of the list */ | ||
87 | clistiter * clist_previous(clistiter *); | ||
88 | |||
89 | /* Returns the data pointer of this element of the list */ | ||
90 | void* clist_content(clistiter *); | ||
91 | |||
92 | /* Inserts this data pointer at the beginning of the list */ | ||
93 | int clist_prepend(clist *, void *); | ||
94 | |||
95 | /* Inserts this data pointer at the end of the list */ | ||
96 | int clist_append(clist *, void *); | ||
97 | #else | ||
98 | #define clist_isempty(lst) ((lst->first==lst->last) && (lst->last==NULL)) | ||
99 | #define clist_count(lst) (lst->count) | ||
100 | #define clist_begin(lst) (lst->first) | ||
101 | #define clist_end(lst) (lst->last) | ||
102 | #define clist_next(iter) (iter ? iter->next : NULL) | ||
103 | #define clist_previous(iter) (iter ? iter->previous : NULL) | ||
104 | #define clist_content(iter) (iter ? iter->data : NULL) | ||
105 | #define clist_prepend(lst, data) (clist_insert_before(lst, lst->first, data)) | ||
106 | #define clist_append(lst, data) (clist_insert_after(lst, lst->last, data)) | ||
107 | #endif | ||
108 | |||
109 | /* Inserts this data pointer before the element pointed by the iterator */ | ||
110 | int clist_insert_before(clist *, clistiter *, void *); | ||
111 | |||
112 | /* Inserts this data pointer after the element pointed by the iterator */ | ||
113 | int clist_insert_after(clist *, clistiter *, void *); | ||
114 | |||
115 | /* Deletes the element pointed by the iterator. | ||
116 | Returns an iterator to the next element. */ | ||
117 | clistiter * clist_delete(clist *, clistiter *); | ||
118 | |||
119 | typedef void (* clist_func)(void *, void *); | ||
120 | |||
121 | void clist_foreach(clist * lst, clist_func func, void * data); | ||
122 | |||
123 | void clist_concat(clist * dest, clist * src); | ||
124 | |||
125 | void * clist_nth_data(clist * lst, int index); | ||
126 | |||
127 | clistiter * clist_nth(clist * lst, int index); | ||
128 | |||
129 | #ifdef __cplusplus | ||
130 | } | ||
131 | #endif | ||
132 | |||
133 | #endif | ||