Fix error creation and warning
[claws.git] / src / plugins / mailmbox / clist.c
blob5c8151abafb914adbeec23866c26dca297630189
2 /*
3 * libEtPan! -- a mail stuff library
5 * clist - Implements simple generic double-linked pointer lists
7 * Copyright (c) 1999-2000, Gaƫl Roualland <gael.roualland@iname.com>
8 * interface changes - 2002 - DINH Viet Hoa
9 * All rights reserved.
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.
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.
37 * $Id$
40 #include "config.h"
42 #include <stdlib.h>
43 #include "clist.h"
45 clist * clist_new() {
46 clist * lst;
48 lst = (clist *) malloc(sizeof(clist));
49 if (!lst) return NULL;
51 lst->first = lst->last = NULL;
52 lst->count = 0;
54 return lst;
57 void clist_free(clist * lst) {
58 clistcell * l1, * l2;
60 l1 = lst->first;
61 while (l1) {
62 l2 = l1->next;
63 free(l1);
64 l1 = l2;
67 free(lst);
70 #ifdef NO_MACROS
71 int clist_isempty(clist * lst) {
72 return ((lst->first==lst->last) && (lst->last==NULL));
75 clistiter * clist_begin(clist * lst) {
76 return lst->first;
79 clistiter * clist_end(clist * lst) {
80 return lst->last;
83 clistiter * clist_next(clistiter * iter) {
84 if (iter)
85 return iter->next;
86 else
87 return NULL;
90 clistiter * clist_previous(clistiter * iter) {
91 if (iter)
92 return iter->previous;
93 else
94 return NULL;
97 void * clist_content(clistiter * iter) {
98 if (iter)
99 return iter->data;
100 else
101 return NULL;
104 int clist_count(clist * lst) {
105 return lst->count;
108 int clist_prepend(clist * lst, void * data) {
109 return clist_insert_before(lst, lst->first, data);
112 int clist_append(clist * lst, void * data) {
113 return clist_insert_after(lst, lst->last, data);
115 #endif
117 int clist_insert_before(clist * lst, clistiter * iter, void * data) {
118 clistcell * c;
120 c = (clistcell *) malloc(sizeof(clistcell));
121 if (!c) return -1;
123 c->data = data;
124 lst->count++;
126 if (clist_isempty(lst)) {
127 c->previous = c->next = NULL;
128 lst->first = lst->last = c;
129 return 0;
132 if (!iter) {
133 c->previous = lst->last;
134 c->previous->next = c;
135 c->next = NULL;
136 lst->last = c;
137 return 0;
140 c->previous = iter->previous;
141 c->next = iter;
142 c->next->previous = c;
143 if (c->previous)
144 c->previous->next = c;
145 else
146 lst->first = c;
148 return 0;
151 int clist_insert_after(clist * lst, clistiter * iter, void * data) {
152 clistcell * c;
154 c = (clistcell *) malloc(sizeof(clistcell));
155 if (!c) return -1;
157 c->data = data;
158 lst->count++;
160 if (clist_isempty(lst)) {
161 c->previous = c->next = NULL;
162 lst->first = lst->last = c;
163 return 0;
166 if (!iter) {
167 c->previous = lst->last;
168 c->previous->next = c;
169 c->next = NULL;
170 lst->last = c;
171 return 0;
174 c->previous = iter;
175 c->next = iter->next;
176 if (c->next)
177 c->next->previous = c;
178 else
179 lst->last = c;
180 c->previous->next = c;
182 return 0;
185 clistiter * clist_delete(clist * lst, clistiter * iter) {
186 clistiter * ret;
188 if (!iter) return NULL;
190 if (iter->previous)
191 iter->previous->next = iter->next;
192 else
193 lst->first = iter->next;
195 if (iter->next) {
196 iter->next->previous = iter->previous;
197 ret = iter->next;
198 } else {
199 lst->last = iter->previous;
200 ret = NULL;
203 free(iter);
204 lst->count--;
206 return ret;
211 void clist_foreach(clist * lst, clist_func func, void * data)
213 clistiter * cur;
215 for(cur = clist_begin(lst) ; cur != NULL ; cur = cur->next)
216 func(cur->data, data);
219 void clist_concat(clist * dest, clist * src)
221 if (src->first == NULL) {
222 /* do nothing */
224 else if (dest->last == NULL) {
225 dest->first = src->first;
226 dest->last = src->last;
228 else {
229 dest->last->next = src->first;
230 src->first->previous = dest->last;
231 dest->last = src->last;
234 dest->count += src->count;
235 src->last = src->first = NULL;
238 static inline clistiter * internal_clist_nth(clist * lst, int index)
240 clistiter * cur;
242 cur = clist_begin(lst);
243 while ((index > 0) && (cur != NULL)) {
244 cur = cur->next;
245 index --;
248 if (cur == NULL)
249 return NULL;
251 return cur;
254 void * clist_nth_data(clist * lst, int index)
256 clistiter * cur;
258 cur = internal_clist_nth(lst, index);
259 if (cur == NULL)
260 return NULL;
262 return cur->data;
265 clistiter * clist_nth(clist * lst, int index)
267 return internal_clist_nth(lst, index);