/src/fontconfig/src/fcptrlist.c
Line | Count | Source |
1 | | /* |
2 | | * fontconfig/src/fcptrlist.c |
3 | | * |
4 | | * Copyright © 2000 Keith Packard |
5 | | * |
6 | | * Permission to use, copy, modify, distribute, and sell this software and its |
7 | | * documentation for any purpose is hereby granted without fee, provided that |
8 | | * the above copyright notice appear in all copies and that both that |
9 | | * copyright notice and this permission notice appear in supporting |
10 | | * documentation, and that the name of the author(s) not be used in |
11 | | * advertising or publicity pertaining to distribution of the software without |
12 | | * specific, written prior permission. The authors make no |
13 | | * representations about the suitability of this software for any purpose. It |
14 | | * is provided "as is" without express or implied warranty. |
15 | | * |
16 | | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
17 | | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
18 | | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
19 | | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
20 | | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
21 | | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
22 | | * PERFORMANCE OF THIS SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "fcint.h" |
26 | | |
27 | | typedef struct _FcPtrListEntry { |
28 | | struct _FcPtrListEntry *next; |
29 | | void *data; |
30 | | } FcPtrListEntry; |
31 | | struct _FcPtrList { |
32 | | FcDestroyFunc destroy_func; |
33 | | FcPtrListEntry *list; |
34 | | FcPtrListEntry *last; |
35 | | }; |
36 | | typedef struct _FcPtrListIterPrivate { |
37 | | const FcPtrList *list; |
38 | | FcPtrListEntry *entry; |
39 | | FcPtrListEntry *prev; |
40 | | } FcPtrListIterPrivate; |
41 | | |
42 | | FcPtrList * |
43 | | FcPtrListCreate (FcDestroyFunc func) |
44 | 0 | { |
45 | 0 | FcPtrList *ret = (FcPtrList *)malloc (sizeof (FcPtrList)); |
46 | |
|
47 | 0 | if (ret) { |
48 | 0 | ret->destroy_func = func; |
49 | 0 | ret->list = NULL; |
50 | 0 | ret->last = NULL; |
51 | 0 | } |
52 | |
|
53 | 0 | return ret; |
54 | 0 | } |
55 | | |
56 | | void |
57 | | FcPtrListDestroy (FcPtrList *list) |
58 | 0 | { |
59 | 0 | FcPtrListIter iter; |
60 | |
|
61 | 0 | if (list) { |
62 | 0 | FcPtrListIterInit (list, &iter); |
63 | 0 | do { |
64 | 0 | if (FcPtrListIterGetValue (list, &iter)) |
65 | 0 | if (list->destroy_func) |
66 | 0 | list->destroy_func (FcPtrListIterGetValue (list, &iter)); |
67 | 0 | FcPtrListIterRemove (list, &iter); |
68 | 0 | } while (FcPtrListIterIsValid (list, &iter)); |
69 | |
|
70 | 0 | free (list); |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | void |
75 | | FcPtrListIterInit (const FcPtrList *list, |
76 | | FcPtrListIter *iter) |
77 | 0 | { |
78 | 0 | FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter; |
79 | |
|
80 | 0 | priv->list = list; |
81 | 0 | priv->entry = list->list; |
82 | 0 | priv->prev = NULL; |
83 | 0 | } |
84 | | |
85 | | void |
86 | | FcPtrListIterInitAtLast (FcPtrList *list, |
87 | | FcPtrListIter *iter) |
88 | 0 | { |
89 | 0 | FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter; |
90 | |
|
91 | 0 | priv->list = list; |
92 | 0 | priv->entry = NULL; |
93 | 0 | priv->prev = list->last; |
94 | 0 | } |
95 | | |
96 | | FcBool |
97 | | FcPtrListIterNext (const FcPtrList *list, |
98 | | FcPtrListIter *iter) |
99 | 0 | { |
100 | 0 | FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter; |
101 | |
|
102 | 0 | if (list != priv->list) |
103 | 0 | return FcFalse; |
104 | 0 | priv->prev = priv->entry; |
105 | 0 | priv->entry = priv->entry->next; |
106 | |
|
107 | 0 | return priv->entry != NULL; |
108 | 0 | } |
109 | | |
110 | | FcBool |
111 | | FcPtrListIterIsValid (const FcPtrList *list, |
112 | | const FcPtrListIter *iter) |
113 | 0 | { |
114 | 0 | FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter; |
115 | |
|
116 | 0 | return list == priv->list && priv->entry; |
117 | 0 | } |
118 | | |
119 | | void * |
120 | | FcPtrListIterGetValue (const FcPtrList *list, |
121 | | const FcPtrListIter *iter) |
122 | 0 | { |
123 | 0 | FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter; |
124 | |
|
125 | 0 | if (list != priv->list || |
126 | 0 | !priv->entry) |
127 | 0 | return NULL; |
128 | | |
129 | 0 | return priv->entry->data; |
130 | 0 | } |
131 | | |
132 | | FcBool |
133 | | FcPtrListIterAdd (FcPtrList *list, |
134 | | FcPtrListIter *iter, |
135 | | void *data) |
136 | 0 | { |
137 | 0 | FcPtrListEntry *e; |
138 | 0 | FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter; |
139 | |
|
140 | 0 | if (list != priv->list) |
141 | 0 | return FcFalse; |
142 | | |
143 | 0 | e = (FcPtrListEntry *)malloc (sizeof (FcPtrListEntry)); |
144 | 0 | if (!e) |
145 | 0 | return FcFalse; |
146 | 0 | e->data = data; |
147 | |
|
148 | 0 | if (priv->entry) { |
149 | 0 | e->next = priv->entry->next; |
150 | 0 | priv->entry->next = e; |
151 | 0 | } else { |
152 | 0 | e->next = NULL; |
153 | 0 | list->last = e; |
154 | 0 | if (priv->prev) { |
155 | 0 | priv->prev->next = e; |
156 | 0 | priv->entry = priv->prev; |
157 | 0 | } else { |
158 | 0 | list->list = e; |
159 | 0 | priv->entry = e; |
160 | |
|
161 | 0 | return FcTrue; |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | 0 | return FcPtrListIterNext (list, iter); |
166 | 0 | } |
167 | | |
168 | | FcBool |
169 | | FcPtrListIterRemove (FcPtrList *list, |
170 | | FcPtrListIter *iter) |
171 | 0 | { |
172 | 0 | FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter; |
173 | 0 | FcPtrListEntry *e; |
174 | |
|
175 | 0 | if (list != priv->list) |
176 | 0 | return FcFalse; |
177 | 0 | if (!priv->entry) |
178 | 0 | return FcTrue; |
179 | | |
180 | 0 | if (list->list == priv->entry) |
181 | 0 | list->list = list->list->next; |
182 | 0 | e = priv->entry; |
183 | 0 | if (priv->prev) |
184 | 0 | priv->prev->next = priv->entry->next; |
185 | 0 | priv->entry = priv->entry->next; |
186 | 0 | free (e); |
187 | 0 | if (!priv->entry) |
188 | 0 | list->last = priv->prev; |
189 | |
|
190 | 0 | return FcTrue; |
191 | 0 | } |
192 | | |
193 | | #define __fcplist__ |
194 | | #include "fcaliastail.h" |
195 | | #undef __fcplist__ |