Mon, 07 May 2012 15:25:18 +0200
-DCF_NO_GETTEXT option to system.mk
| 60 | 1 | /* |
| 90 | 2 | * libtu/ptrlist.c |
| 60 | 3 | * |
| 90 | 4 | * Copyright (c) Tuomo Valkonen 1999-2005. |
| 60 | 5 | * |
| 6 | * You may distribute and modify this library under the terms of either | |
| 7 | * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. | |
| 8 | */ | |
| 9 | ||
| 10 | #include "obj.h" | |
| 90 | 11 | #include "ptrlist.h" |
| 60 | 12 | #include "types.h" |
| 13 | #include "dlist.h" | |
| 14 | #include "misc.h" | |
| 15 | ||
| 16 | ||
| 90 | 17 | static void free_node(PtrList **ptrlist, PtrList *node) |
| 60 | 18 | { |
| 90 | 19 | UNLINK_ITEM(*ptrlist, node, next, prev); |
| 62 | 20 | free(node); |
| 60 | 21 | } |
| 22 | ||
| 23 | ||
| 90 | 24 | static PtrList *mknode(void *ptr) |
| 60 | 25 | { |
| 90 | 26 | PtrList *node; |
| 62 | 27 | |
| 90 | 28 | if(ptr==NULL) |
| 88 | 29 | return NULL; |
| 62 | 30 | |
| 90 | 31 | node=ALLOC(PtrList); |
| 62 | 32 | |
| 33 | if(node==NULL) | |
| 34 | return FALSE; | |
| 35 | ||
| 90 | 36 | node->ptr=ptr; |
| 62 | 37 | |
| 88 | 38 | return node; |
| 39 | } | |
| 40 | ||
| 41 | ||
| 90 | 42 | static PtrList *ptrlist_find_node(PtrList *ptrlist, void *ptr) |
| 88 | 43 | { |
| 90 | 44 | PtrList *node=ptrlist; |
| 88 | 45 | |
| 46 | while(node!=NULL){ | |
| 90 | 47 | if(node->ptr==ptr) |
| 88 | 48 | break; |
| 49 | node=node->next; | |
| 50 | } | |
| 51 | ||
| 52 | return node; | |
| 53 | } | |
| 54 | ||
| 55 | ||
| 111 | 56 | bool ptrlist_contains(PtrList *ptrlist, void *ptr) |
| 57 | { | |
| 58 | return (ptrlist_find_node(ptrlist, ptr)!=NULL); | |
| 59 | } | |
| 60 | ||
| 61 | ||
| 90 | 62 | bool ptrlist_insert_last(PtrList **ptrlist, void *ptr) |
| 88 | 63 | { |
| 90 | 64 | PtrList *node=mknode(ptr); |
| 88 | 65 | |
| 66 | if(node==NULL) | |
| 67 | return FALSE; | |
| 68 | ||
| 90 | 69 | LINK_ITEM_LAST(*ptrlist, node, next, prev); |
| 88 | 70 | |
| 71 | return TRUE; | |
| 72 | } | |
| 73 | ||
| 74 | ||
| 90 | 75 | bool ptrlist_insert_first(PtrList **ptrlist, void *ptr) |
| 88 | 76 | { |
| 90 | 77 | PtrList *node=mknode(ptr); |
| 88 | 78 | |
| 79 | if(node==NULL) | |
| 80 | return FALSE; | |
| 81 | ||
| 90 | 82 | LINK_ITEM_FIRST(*ptrlist, node, next, prev); |
| 88 | 83 | |
| 84 | return TRUE; | |
| 85 | } | |
| 86 | ||
| 87 | ||
| 90 | 88 | bool ptrlist_reinsert_last(PtrList **ptrlist, void *ptr) |
| 88 | 89 | { |
| 90 | 90 | PtrList *node=ptrlist_find_node(*ptrlist, ptr); |
| 88 | 91 | |
| 92 | if(node==NULL) | |
| 111 | 93 | return ptrlist_insert_last(ptrlist, ptr); |
| 88 | 94 | |
| 90 | 95 | UNLINK_ITEM(*ptrlist, node, next, prev); |
| 96 | LINK_ITEM_LAST(*ptrlist, node, next, prev); | |
| 88 | 97 | |
| 98 | return TRUE; | |
| 99 | } | |
| 100 | ||
| 101 | ||
| 90 | 102 | bool ptrlist_reinsert_first(PtrList **ptrlist, void *ptr) |
| 88 | 103 | { |
| 90 | 104 | PtrList *node=ptrlist_find_node(*ptrlist, ptr); |
| 88 | 105 | |
| 106 | if(node==NULL) | |
| 111 | 107 | return ptrlist_insert_first(ptrlist, ptr); |
| 88 | 108 | |
| 90 | 109 | UNLINK_ITEM(*ptrlist, node, next, prev); |
| 110 | LINK_ITEM_FIRST(*ptrlist, node, next, prev); | |
| 62 | 111 | |
| 112 | return TRUE; | |
| 60 | 113 | } |
| 114 | ||
| 115 | ||
|
101
50525dab6c8e
*list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents:
92
diff
changeset
|
116 | bool ptrlist_remove(PtrList **ptrlist, void *ptr) |
| 60 | 117 | { |
| 90 | 118 | PtrList *node=ptrlist_find_node(*ptrlist, ptr); |
| 62 | 119 | |
| 88 | 120 | if(node!=NULL) |
| 90 | 121 | free_node(ptrlist, node); |
|
101
50525dab6c8e
*list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents:
92
diff
changeset
|
122 | |
|
50525dab6c8e
*list_remove return true if the item was found (and removed).
Tuomo Valkonen <tuomov@iki.fi>
parents:
92
diff
changeset
|
123 | return (node!=NULL); |
| 60 | 124 | } |
| 125 | ||
| 126 | ||
| 90 | 127 | void ptrlist_clear(PtrList **ptrlist) |
| 60 | 128 | { |
| 90 | 129 | while(*ptrlist!=NULL) |
| 130 | free_node(ptrlist, *ptrlist); | |
| 60 | 131 | } |
| 132 | ||
| 133 | ||
| 90 | 134 | PtrListIterTmp ptrlist_iter_tmp=NULL; |
| 60 | 135 | |
| 136 | ||
| 90 | 137 | void ptrlist_iter_init(PtrListIterTmp *state, PtrList *ptrlist) |
| 60 | 138 | { |
| 90 | 139 | *state=ptrlist; |
| 60 | 140 | } |
| 141 | ||
| 142 | ||
| 90 | 143 | void *ptrlist_iter(PtrListIterTmp *state) |
| 60 | 144 | { |
| 90 | 145 | void *ptr=NULL; |
| 62 | 146 | |
| 88 | 147 | if(*state!=NULL){ |
| 90 | 148 | ptr=(*state)->ptr; |
| 88 | 149 | (*state)=(*state)->next; |
| 150 | } | |
| 62 | 151 | |
| 90 | 152 | return ptr; |
| 60 | 153 | } |
| 154 | ||
| 88 | 155 | |
| 90 | 156 | void ptrlist_iter_rev_init(PtrListIterTmp *state, PtrList *ptrlist) |
| 88 | 157 | { |
| 90 | 158 | *state=(ptrlist==NULL ? NULL : ptrlist->prev); |
| 88 | 159 | } |
| 160 | ||
| 161 | ||
| 90 | 162 | void *ptrlist_iter_rev(PtrListIterTmp *state) |
| 88 | 163 | { |
| 90 | 164 | void *ptr=NULL; |
| 88 | 165 | |
| 166 | if(*state!=NULL){ | |
| 90 | 167 | ptr=(*state)->ptr; |
| 88 | 168 | *state=(*state)->prev; |
| 169 | if((*state)->next==NULL) | |
| 170 | *state=NULL; | |
| 171 | } | |
| 172 | ||
| 90 | 173 | return ptr; |
| 88 | 174 | } |
| 175 | ||
|
92
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
176 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
177 | void *ptrlist_take_first(PtrList **ptrlist) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
178 | { |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
179 | PtrList *node=*ptrlist; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
180 | void *ptr; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
181 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
182 | if(node==NULL) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
183 | return NULL; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
184 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
185 | ptr=node->ptr; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
186 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
187 | free_node(ptrlist, node); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
188 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
189 | return ptr; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
190 | } |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
191 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
192 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
193 | void *ptrlist_take_last(PtrList **ptrlist) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
194 | { |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
195 | PtrList *node=*ptrlist; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
196 | void *ptr; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
197 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
198 | if(node==NULL) |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
199 | return NULL; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
200 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
201 | node=node->prev; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
202 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
203 | ptr=node->ptr; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
204 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
205 | free_node(ptrlist, node); |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
206 | |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
207 | return ptr; |
|
55fcdff5bcea
Added routines to take first/last elements of objlist and ptrlist.
Tuomo Valkonen <tuomov@iki.fi>
parents:
90
diff
changeset
|
208 | } |