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: clist.h,v 1.13 2011/05/09 21:49:46 hoa Exp $
37  */
38 
39 #ifndef CLIST_H
40 #define CLIST_H
41 
42 #ifndef LIBETPAN_CONFIG_H
43 #       include <libetpan/libetpan-config.h>
44 #endif
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 typedef struct clistcell_s {
51   void * data;
52   struct clistcell_s * previous;
53   struct clistcell_s * next;
54 } clistcell;
55 
56 struct clist_s {
57   clistcell * first;
58   clistcell * last;
59   int count;
60 };
61 
62 typedef struct clist_s clist;
63 typedef clistcell clistiter;
64 
65 /* Allocate a new pointer list */
66 LIBETPAN_EXPORT
67 clist *      clist_new(void);
68 
69 /* Destroys a list. Data pointed by data pointers is NOT freed. */
70 LIBETPAN_EXPORT
71 void        clist_free(clist *);
72 
73 /* Some of the following routines can be implemented as macros to
74    be faster. If you don't want it, define NO_MACROS */
75 #ifdef NO_MACROS
76 
77 /* Returns TRUE if list is empty */
78 int         clist_isempty(clist *);
79 
80 /* Returns the number of elements in the list */
81 int         clist_count(clist *);
82 
83 /* Returns an iterator to the first element of the list */
84 clistiter *   clist_begin(clist *);
85 
86 /* Returns an iterator to the last element of the list */
87 clistiter *   clist_end(clist *);
88 
89 /* Returns an iterator to the next element of the list */
90 clistiter *   clist_next(clistiter *);
91 
92 /* Returns an iterator to the previous element of the list */
93 clistiter *   clist_previous(clistiter *);
94 
95 /* Returns the data pointer of this element of the list */
96 void*       clist_content(clistiter *);
97 
98 /* Inserts this data pointer at the beginning of the list */
99 int         clist_prepend(clist *, void *);
100 
101 /* Inserts this data pointer at the end of the list */
102 int         clist_append(clist *, void *);
103 #else
104 #define     clist_isempty(lst)             (((lst)->first==(lst)->last) && ((lst)->last==NULL))
105 #define     clist_count(lst)               ((lst)->count)
106 #define     clist_begin(lst)               ((lst)->first)
107 #define     clist_end(lst)                 ((lst)->last)
108 #define     clist_next(iter)               (iter ? (iter)->next : NULL)
109 #define     clist_previous(iter)           (iter ? (iter)->previous : NULL)
110 #define     clist_content(iter)            (iter ? (iter)->data : NULL)
111 #define     clist_prepend(lst, data)  (clist_insert_before(lst, (lst)->first, data))
112 #define     clist_append(lst, data)   (clist_insert_after(lst, (lst)->last, data))
113 #endif
114 
115 /* Inserts this data pointer before the element pointed by the iterator */
116 LIBETPAN_EXPORT
117 int         clist_insert_before(clist *, clistiter *, void *);
118 
119 /* Inserts this data pointer after the element pointed by the iterator */
120 LIBETPAN_EXPORT
121 int         clist_insert_after(clist *, clistiter *, void *);
122 
123 /* Deletes the element pointed by the iterator.
124    Returns an iterator to the next element. */
125 LIBETPAN_EXPORT
126 clistiter *   clist_delete(clist *, clistiter *);
127 
128 typedef void (* clist_func)(void *, void *);
129 
130 LIBETPAN_EXPORT
131 void clist_foreach(clist * lst, clist_func func, void * data);
132 
133 LIBETPAN_EXPORT
134 void clist_concat(clist * dest, clist * src);
135 
136 LIBETPAN_EXPORT
137 void * clist_nth_data(clist * lst, int indx);
138 
139 LIBETPAN_EXPORT
140 clistiter * clist_nth(clist * lst, int indx);
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif
147