Debian Codegraph @

src/fcptrlist.c

raw
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 {
45 FcPtrList *ret = (FcPtrList *)malloc (sizeof (FcPtrList));
46
47 if (ret) {
48 ret->destroy_func = func;
49 ret->list = NULL;
50 ret->last = NULL;
51 }
52
53 return ret;
54 }
55
56 void
57 FcPtrListDestroy (FcPtrList *list)
58 {
59 FcPtrListIter iter;
60
61 if (list) {
62 FcPtrListIterInit (list, &iter);
63 do {
64 if (FcPtrListIterGetValue (list, &iter))
65 if (list->destroy_func)
66 list->destroy_func (FcPtrListIterGetValue (list, &iter));
67 FcPtrListIterRemove (list, &iter);
68 } while (FcPtrListIterIsValid (list, &iter));
69
70 free (list);
71 }
72 }
73
74 void
75 FcPtrListIterInit (const FcPtrList *list,
76 FcPtrListIter *iter)
77 {
78 FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter;
79
80 priv->list = list;
81 priv->entry = list->list;
82 priv->prev = NULL;
83 }
84
85 void
86 FcPtrListIterInitAtLast (FcPtrList *list,
87 FcPtrListIter *iter)
88 {
89 FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter;
90
91 priv->list = list;
92 priv->entry = NULL;
93 priv->prev = list->last;
94 }
95
96 FcBool
97 FcPtrListIterNext (const FcPtrList *list,
98 FcPtrListIter *iter)
99 {
100 FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter;
101
102 if (list != priv->list)
103 return FcFalse;
104 priv->prev = priv->entry;
105 priv->entry = priv->entry->next;
106
107 return priv->entry != NULL;
108 }
109
110 FcBool
111 FcPtrListIterIsValid (const FcPtrList *list,
112 const FcPtrListIter *iter)
113 {
114 FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter;
115
116 return list == priv->list && priv->entry;
117 }
118
119 void *
120 FcPtrListIterGetValue (const FcPtrList *list,
121 const FcPtrListIter *iter)
122 {
123 FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter;
124
125 if (list != priv->list ||
126 !priv->entry)
127 return NULL;
128
129 return priv->entry->data;
130 }
131
132 FcBool
133 FcPtrListIterAdd (FcPtrList *list,
134 FcPtrListIter *iter,
135 void *data)
136 {
137 FcPtrListEntry *e;
138 FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter;
139
140 if (list != priv->list)
141 return FcFalse;
142
143 e = (FcPtrListEntry *)malloc (sizeof (FcPtrListEntry));
144 if (!e)
145 return FcFalse;
146 e->data = data;
147
148 if (priv->entry) {
149 e->next = priv->entry->next;
150 priv->entry->next = e;
151 } else {
152 e->next = NULL;
153 list->last = e;
154 if (priv->prev) {
155 priv->prev->next = e;
156 priv->entry = priv->prev;
157 } else {
158 list->list = e;
159 priv->entry = e;
160
161 return FcTrue;
162 }
163 }
164
165 return FcPtrListIterNext (list, iter);
166 }
167
168 FcBool
169 FcPtrListIterRemove (FcPtrList *list,
170 FcPtrListIter *iter)
171 {
172 FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *)iter;
173 FcPtrListEntry *e;
174
175 if (list != priv->list)
176 return FcFalse;
177 if (!priv->entry)
178 return FcTrue;
179
180 if (list->list == priv->entry)
181 list->list = list->list->next;
182 e = priv->entry;
183 if (priv->prev)
184 priv->prev->next = priv->entry->next;
185 priv->entry = priv->entry->next;
186 free (e);
187 if (!priv->entry)
188 list->last = priv->prev;
189
190 return FcTrue;
191 }
192
193 #define __fcplist__
194 #include "fcaliastail.h"
195 #undef __fcplist__