Mercurial > pidgin
annotate plugins/icq/list.c @ 2009:ac9dd6b00b34
[gaim-migrate @ 2019]
different way of specifying offline messages. Jeremy, I'm just about to look at your patch
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Wed, 13 Jun 2001 21:28:03 +0000 |
parents | 7b3f1eb1ef7d |
children |
rev | line source |
---|---|
1152 | 1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
3 /* |
1977
7b3f1eb1ef7d
[gaim-migrate @ 1987]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1912
diff
changeset
|
4 * $Id: list.c 1987 2001-06-09 14:46:51Z warmenhoven $ |
7b3f1eb1ef7d
[gaim-migrate @ 1987]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1912
diff
changeset
|
5 * |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
7 * Bill Soudan <soudan@kde.org> |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
8 * |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
9 * This program is free software; you can redistribute it and/or modify |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
10 * it under the terms of the GNU General Public License as published by |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
11 * the Free Software Foundation; either version 2 of the License, or |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
12 * (at your option) any later version. |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
13 * |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
14 * This program is distributed in the hope that it will be useful, |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
17 * GNU General Public License for more details. |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
18 * |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
19 * You should have received a copy of the GNU General Public License |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
20 * along with this program; if not, write to the Free Software |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
22 * |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
23 */ |
1152 | 24 |
25 /* | |
26 * linked list functions | |
27 */ | |
28 | |
29 #include <stdlib.h> | |
30 #include <stdio.h> | |
31 | |
32 #include "list.h" | |
33 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
34 icq_List *icq_ListNew() |
1152 | 35 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
36 icq_List *plist=(icq_List *)malloc(sizeof(icq_List)); |
1152 | 37 |
38 plist->head=0; | |
39 plist->tail=0; | |
40 plist->count=0; | |
41 | |
42 return plist; | |
43 } | |
44 | |
45 /* Frees all list nodes and list itself */ | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
46 void icq_ListDelete(icq_List *plist, void (*item_free_f)(void *)) |
1152 | 47 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
48 if (item_free_f) |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
49 icq_ListFree(plist, item_free_f); |
1152 | 50 free(plist); |
51 } | |
52 | |
53 /* Only frees the list nodes */ | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
54 void icq_ListFree(icq_List *plist, void (*item_free_f)(void *)) |
1152 | 55 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
56 icq_ListNode *p=plist->head; |
1152 | 57 |
58 #ifdef LIST_TRACE | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
59 printf("icq_ListFree(%p)\n", plist); |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
60 icq_ListDump(plist); |
1152 | 61 #endif |
62 | |
63 while(p) | |
64 { | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
65 icq_ListNode *ptemp=p; |
1152 | 66 |
67 p=p->next; | |
68 (*item_free_f)((void *)ptemp->item); | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
69 icq_ListRemoveNode(plist, ptemp); |
1152 | 70 } |
71 } | |
72 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
73 void icq_ListInsertSorted(icq_List *plist, void *pitem) |
1498
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
74 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
75 icq_ListNode *i=plist->head; |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
76 int done = 0; |
1498
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
77 |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
78 while (i && !done) |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
79 { |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
80 if ((*plist->compare_function)(pitem, i->item)<0) |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
81 done = 1; |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
82 else |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
83 i=i->next; |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
84 } |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
85 |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
86 icq_ListInsert(plist, i, pitem); |
1498
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
87 } |
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
88 |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
89 void icq_ListInsert(icq_List *plist, icq_ListNode *pnode, void *pitem) |
1152 | 90 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
91 icq_ListNode *pnew=(icq_ListNode *)malloc(sizeof(icq_ListNode)); |
1152 | 92 pnew->item=pitem; |
93 | |
94 #ifdef LIST_TRACE | |
95 printf("inserting %x (node=%x) into list %x\n", pitem, pnew, plist); | |
96 #endif | |
97 | |
98 plist->count++; | |
99 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
100 /* null source node signifies insert at end of icq_List */ |
1152 | 101 if(!pnode) |
102 { | |
103 pnew->previous=plist->tail; | |
104 pnew->next=0; | |
105 if(plist->tail) | |
106 plist->tail->next=pnew; | |
107 plist->tail=pnew; | |
108 | |
109 if(!plist->head) | |
110 plist->head=pnew; | |
111 } | |
112 else | |
113 { | |
114 pnew->previous=pnode->previous; | |
115 pnew->next=pnode; | |
116 | |
117 if(pnew->previous) | |
118 pnew->previous->next=pnew; | |
119 | |
120 if(pnew->next) | |
121 pnode->previous=pnew; | |
122 | |
123 if(plist->head==pnode) | |
124 plist->head=pnew; | |
125 } | |
126 | |
127 #ifdef LIST_TRACE | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
128 icq_ListDump(plist); |
1152 | 129 #endif |
130 } | |
131 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
132 void *icq_ListRemoveNode(icq_List *plist, icq_ListNode *p) |
1152 | 133 { |
134 void *pitem; | |
135 | |
136 if(!p) | |
137 return 0; | |
138 | |
139 #ifdef LIST_TRACE | |
140 printf("removing %x (node=%x) from list %x\n", p->item, p, plist); | |
141 #endif | |
142 | |
143 plist->count--; | |
144 | |
145 if(p->next) | |
146 p->next->previous=p->previous; | |
147 | |
148 if(p->previous) | |
149 p->previous->next=p->next; | |
150 | |
151 if(plist->head==p) | |
152 plist->head=p->next; | |
153 | |
154 if(plist->tail==p) | |
155 plist->tail=p->previous; | |
156 | |
157 p->next=0; | |
158 p->previous=0; | |
159 | |
160 #ifdef LIST_TRACE | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
161 icq_ListDump(plist); |
1152 | 162 #endif |
163 | |
164 pitem=p->item; | |
165 | |
166 free(p); | |
167 | |
168 return pitem; | |
169 } | |
170 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
171 void *icq_ListTraverse(icq_List *plist, int (*item_f)(void *, va_list), ...) |
1152 | 172 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
173 icq_ListNode *i=plist->head; |
1152 | 174 int f=0; |
175 va_list ap; | |
176 | |
177 #ifdef LIST_TRACE | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
178 printf("icq_ListTraverse(%p)\n", plist); |
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
179 icq_ListDump(plist); |
1152 | 180 #endif |
181 va_start(ap, item_f); | |
182 | |
183 /* call item_f for each item in list until end of list or item | |
184 * function returns 0 */ | |
185 while(i && !f) | |
186 { | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
187 icq_ListNode *pnext=i->next; |
1152 | 188 |
189 if(!(f=(*item_f)(i->item, ap))) | |
190 i=pnext; | |
191 } | |
1498
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
192 |
1152 | 193 va_end(ap); |
1498
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
194 |
1152 | 195 if (i) |
196 return i->item; | |
197 else | |
198 return 0; | |
199 } | |
200 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
201 int icq_ListDump(icq_List *plist) |
1152 | 202 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
203 icq_ListNode *p=plist->head; |
1152 | 204 |
1977
7b3f1eb1ef7d
[gaim-migrate @ 1987]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1912
diff
changeset
|
205 printf("list %lx { head=%lx, tail=%lx, count=%d }\ncontents: ", |
7b3f1eb1ef7d
[gaim-migrate @ 1987]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1912
diff
changeset
|
206 (long)plist, (long)plist->head, (long)plist->tail, plist->count); |
1152 | 207 |
208 while(p) | |
209 { | |
1977
7b3f1eb1ef7d
[gaim-migrate @ 1987]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1912
diff
changeset
|
210 printf("%lx, ", (long)p->item); |
1152 | 211 p=p->next; |
212 } | |
213 printf("end\n"); | |
214 | |
215 return 0; | |
216 } | |
217 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
218 void *icq_ListFirst(icq_List *plist) |
1152 | 219 { |
220 if(plist->head) | |
221 return plist->head->item; | |
222 else | |
223 return 0; | |
224 } | |
225 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
226 void *icq_ListLast(icq_List *plist) |
1152 | 227 { |
228 if(plist->tail) | |
229 return plist->tail->item; | |
230 else | |
231 return 0; | |
232 } | |
233 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
234 void *icq_ListAt(icq_List *plist, int num) |
1152 | 235 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
236 icq_ListNode *ptr = plist->head; |
1152 | 237 while(ptr && num) |
238 { | |
239 num--; | |
240 ptr = ptr->next; | |
241 } | |
242 if(!num) | |
243 return ptr->item; | |
244 else | |
245 return 0L; | |
246 } | |
247 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
248 icq_ListNode *icq_ListFind(icq_List *plist, void *pitem) |
1152 | 249 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
250 icq_ListNode *p=plist->head; |
1152 | 251 |
252 while(p) | |
253 { | |
254 if(p->item==pitem) | |
255 return p; | |
256 p=p->next; | |
257 } | |
258 return 0; | |
259 } | |
260 | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
261 void *icq_ListRemove(icq_List *plist, void *pitem) |
1152 | 262 { |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
263 icq_ListNode *p=icq_ListFind(plist, pitem); |
1152 | 264 |
265 if(p) | |
1912
8ed70631ed15
[gaim-migrate @ 1922]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
266 return icq_ListRemoveNode(plist, p); |
1152 | 267 else |
268 return 0; | |
269 } |