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

#include "fcint.h"

typedef struct _FcPtrListEntry {
    struct _FcPtrListEntry	*next;
    void			*data;
} FcPtrListEntry;
struct _FcPtrList {
    FcDestroyFunc	destroy_func;
    FcPtrListEntry	*list;
};
typedef struct _FcPtrListIterPrivate {
    const FcPtrList	*list;
    FcPtrListEntry	*entry;
    FcPtrListEntry	*prev;
} FcPtrListIterPrivate;

FcPtrList *
FcPtrListCreate (FcDestroyFunc func)
{
    FcPtrList *ret = (FcPtrList *) malloc (sizeof (FcPtrList));

    if (ret)
    {
	ret->destroy_func = func;
	ret->list = NULL;
    }

    return ret;
}

void
FcPtrListDestroy (FcPtrList *list)
{
    FcPtrListIter iter;

    FcPtrListIterInit (list, &iter);
    do
    {
	if (FcPtrListIterGetValue (list, &iter))
	    list->destroy_func (FcPtrListIterGetValue (list, &iter));
	FcPtrListIterRemove (list, &iter);
    } while (FcPtrListIterIsValid (list, &iter));

    free (list);
}

void
FcPtrListIterInit (const FcPtrList	*list,
		 FcPtrListIter		*iter)
{
    FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *) iter;

    priv->list = list;
    priv->entry = list->list;
    priv->prev = NULL;
}

void
FcPtrListIterInitAtLast (FcPtrList	*list,
		       FcPtrListIter	*iter)
{
    FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *) iter;
    FcPtrListEntry **e, **p;

    e = &list->list;
    p = e;
    for (; *e; p = e, e = &(*e)->next);

    priv->list = list;
    priv->entry = *e;
    priv->prev = *p;
}

FcBool
FcPtrListIterNext (const FcPtrList	*list,
		 FcPtrListIter		*iter)
{
    FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *) iter;

    if (list != priv->list)
	return FcFalse;
    priv->prev = priv->entry;
    priv->entry = priv->entry->next;

    return priv->entry != NULL;
}

FcBool
FcPtrListIterIsValid (const FcPtrList	*list,
		    const FcPtrListIter	*iter)
{
    FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *) iter;

    return list == priv->list && priv->entry;
}

void *
FcPtrListIterGetValue (const FcPtrList		*list,
		     const FcPtrListIter	*iter)
{
    FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *) iter;

    if (list != priv->list ||
	!priv->entry)
	return NULL;

    return priv->entry->data;
}

FcBool
FcPtrListIterAdd (FcPtrList	*list,
		FcPtrListIter	*iter,
		void		*data)
{
    FcPtrListEntry *e;
    FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *) iter;

    if (list != priv->list)
	return FcFalse;

    e = (FcPtrListEntry *) malloc (sizeof (FcPtrListEntry));
    if (!e)
	return FcFalse;
    e->data = data;

    if (priv->entry)
    {
	e->next = priv->entry->next;
	priv->entry->next = e;
    }
    else
    {
	e->next = NULL;
	if (priv->prev)
	{
	    priv->prev->next = e;
	    priv->entry = priv->prev;
	}
	else
	{
	    list->list = e;
	    priv->entry = e;

	    return FcTrue;
	}
    }

    return FcPtrListIterNext (list, iter);
}

FcBool
FcPtrListIterRemove (FcPtrList		*list,
		   FcPtrListIter	*iter)
{
    FcPtrListIterPrivate *priv = (FcPtrListIterPrivate *) iter;
    FcPtrListEntry *e;

    if (list != priv->list)
	return FcFalse;
    if (!priv->entry)
	return FcTrue;

    if (list->list == priv->entry)
	list->list = list->list->next;
    e = priv->entry;
    if (priv->prev)
	priv->prev->next = priv->entry->next;
    priv->entry = priv->entry->next;
    free (e);

    return FcTrue;
}

#define __fcplist__
#include "fcaliastail.h"
#undef __fcplist__