|
1 /* |
|
2 * libtu/symlist.c |
|
3 * |
|
4 * Copyright (c) Tuomo Valkonen 1999-2004. |
|
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" |
|
11 #include "symlist.h" |
|
12 #include "types.h" |
|
13 #include "dlist.h" |
|
14 #include "misc.h" |
|
15 |
|
16 |
|
17 static void free_node(Symlist **symlist, Symlist *node) |
|
18 { |
|
19 UNLINK_ITEM(*symlist, node, next, prev); |
|
20 free(node); |
|
21 } |
|
22 |
|
23 |
|
24 bool symlist_insert(Symlist **symlist, void *symbol) |
|
25 { |
|
26 Symlist *node; |
|
27 |
|
28 if(symbol==NULL) |
|
29 return FALSE; |
|
30 |
|
31 node=ALLOC(Symlist); |
|
32 |
|
33 if(node==NULL) |
|
34 return FALSE; |
|
35 |
|
36 node->symbol=symbol; |
|
37 |
|
38 LINK_ITEM_FIRST(*symlist, node, next, prev); |
|
39 |
|
40 return TRUE; |
|
41 } |
|
42 |
|
43 |
|
44 void symlist_remove(Symlist **symlist, void *symbol) |
|
45 { |
|
46 Symlist *node=*symlist; |
|
47 |
|
48 while(node!=NULL){ |
|
49 if(node->symbol==symbol){ |
|
50 free_node(symlist, node); |
|
51 return; |
|
52 } |
|
53 node=node->next; |
|
54 } |
|
55 } |
|
56 |
|
57 |
|
58 void symlist_clear(Symlist **symlist) |
|
59 { |
|
60 while(*symlist!=NULL) |
|
61 free_node(symlist, *symlist); |
|
62 } |
|
63 |
|
64 |
|
65 /* Warning: not thread-safe */ |
|
66 |
|
67 |
|
68 static Symlist *iter_next=NULL; |
|
69 |
|
70 |
|
71 void *symlist_init_iter(Symlist *symlist) |
|
72 { |
|
73 if(symlist==NULL){ |
|
74 iter_next=NULL; |
|
75 return NULL; |
|
76 } |
|
77 |
|
78 iter_next=symlist->next; |
|
79 return symlist->symbol; |
|
80 } |
|
81 |
|
82 |
|
83 void *symlist_iter() |
|
84 { |
|
85 Symlist *ret; |
|
86 |
|
87 if(iter_next==NULL) |
|
88 return NULL; |
|
89 |
|
90 ret=iter_next; |
|
91 iter_next=iter_next->next; |
|
92 |
|
93 return ret->symbol; |
|
94 } |
|
95 |