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