1c9710b42Smrg/*
2c9710b42Smrg * fontconfig/src/fclist.c
3c9710b42Smrg *
4c9710b42Smrg * Copyright © 2000 Keith Packard
5c9710b42Smrg *
6c9710b42Smrg * Permission to use, copy, modify, distribute, and sell this software and its
7c9710b42Smrg * documentation for any purpose is hereby granted without fee, provided that
8c9710b42Smrg * the above copyright notice appear in all copies and that both that
9c9710b42Smrg * copyright notice and this permission notice appear in supporting
10c9710b42Smrg * documentation, and that the name of the author(s) not be used in
11c9710b42Smrg * advertising or publicity pertaining to distribution of the software without
12c9710b42Smrg * specific, written prior permission.  The authors make no
13c9710b42Smrg * representations about the suitability of this software for any purpose.  It
14c9710b42Smrg * is provided "as is" without express or implied warranty.
15c9710b42Smrg *
16c9710b42Smrg * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17c9710b42Smrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18c9710b42Smrg * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19c9710b42Smrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20c9710b42Smrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21c9710b42Smrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22c9710b42Smrg * PERFORMANCE OF THIS SOFTWARE.
23c9710b42Smrg */
24c9710b42Smrg
25c9710b42Smrg#include "fcint.h"
26c9710b42Smrg
27c9710b42Smrg#include "fcobjshash.h"
28c9710b42Smrg
29c9710b42Smrg#include <string.h>
30c9710b42Smrg
31c9710b42Smrg/* The 1000 is to leave some room for future added internal objects, such
32c9710b42Smrg * that caches from newer fontconfig can still be used with older fontconfig
33c9710b42Smrg * without getting confused. */
346fc018e4Smrgstatic fc_atomic_int_t next_id = FC_MAX_BASE_OBJECT + FC_EXT_OBJ_INDEX;
35c9710b42Smrgstruct FcObjectOtherTypeInfo {
36c9710b42Smrg    struct FcObjectOtherTypeInfo *next;
37c9710b42Smrg    FcObjectType object;
38c9710b42Smrg    FcObject id;
39c9710b42Smrg} *other_types;
40c9710b42Smrg
41953daebaSmrgvoid
42953daebaSmrgFcObjectFini (void)
43953daebaSmrg{
44953daebaSmrg    struct FcObjectOtherTypeInfo *ots, *ot;
45953daebaSmrg
46953daebaSmrgretry:
47953daebaSmrg    ots = fc_atomic_ptr_get (&other_types);
48953daebaSmrg    if (!ots)
49953daebaSmrg	return;
50953daebaSmrg    if (!fc_atomic_ptr_cmpexch (&other_types, ots, NULL))
51953daebaSmrg	goto retry;
52953daebaSmrg
53953daebaSmrg    while (ots)
54953daebaSmrg    {
55953daebaSmrg	ot = ots->next;
56953daebaSmrg	if (ots->object.object)
57953daebaSmrg	    free (ots->object.object);
58953daebaSmrg	free (ots);
59953daebaSmrg	ots = ot;
60953daebaSmrg    }
61953daebaSmrg}
62953daebaSmrg
63c9710b42Smrgstatic FcObjectType *
64c9710b42Smrg_FcObjectLookupOtherTypeByName (const char *str, FcObject *id)
65c9710b42Smrg{
66c9710b42Smrg    struct FcObjectOtherTypeInfo *ots, *ot;
67c9710b42Smrg
68c9710b42Smrgretry:
69c9710b42Smrg    ots = fc_atomic_ptr_get (&other_types);
70c9710b42Smrg
71c9710b42Smrg    for (ot = ots; ot; ot = ot->next)
72c9710b42Smrg	if (0 == strcmp (ot->object.object, str))
73c9710b42Smrg	    break;
74c9710b42Smrg
75c9710b42Smrg    if (!ot)
76c9710b42Smrg    {
77c9710b42Smrg	ot = malloc (sizeof (*ot));
78c9710b42Smrg	if (!ot)
79c9710b42Smrg	    return NULL;
80c9710b42Smrg
81953daebaSmrg	ot->object.object = (char *) FcStrdup (str);
826fc018e4Smrg	ot->object.type = FcTypeUnknown;
83c9710b42Smrg	ot->id = fc_atomic_int_add (next_id, +1);
84953daebaSmrg	if (ot->id < (FC_MAX_BASE_OBJECT + FC_EXT_OBJ_INDEX))
85953daebaSmrg	{
86953daebaSmrg	    fprintf (stderr, "Fontconfig error: No object ID to assign\n");
87953daebaSmrg	    abort ();
88953daebaSmrg	}
89c9710b42Smrg	ot->next = ots;
90c9710b42Smrg
91c9710b42Smrg	if (!fc_atomic_ptr_cmpexch (&other_types, ots, ot)) {
92953daebaSmrg	    if (ot->object.object)
93953daebaSmrg		free (ot->object.object);
94c9710b42Smrg	    free (ot);
95c9710b42Smrg	    goto retry;
96c9710b42Smrg	}
97c9710b42Smrg    }
98c9710b42Smrg
99c9710b42Smrg    if (id)
100c9710b42Smrg      *id = ot->id;
101c9710b42Smrg
102c9710b42Smrg    return &ot->object;
103c9710b42Smrg}
104c9710b42Smrg
105c9710b42SmrgFcObject
106c9710b42SmrgFcObjectLookupBuiltinIdByName (const char *str)
107c9710b42Smrg{
108c9710b42Smrg    const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
109c9710b42Smrg
110c9710b42Smrg    if (o)
111c9710b42Smrg	return o->id;
112c9710b42Smrg
113c9710b42Smrg    return 0;
114c9710b42Smrg}
115c9710b42Smrg
116c9710b42SmrgFcObject
117c9710b42SmrgFcObjectLookupIdByName (const char *str)
118c9710b42Smrg{
119c9710b42Smrg    const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
120c9710b42Smrg    FcObject id;
121c9710b42Smrg    if (o)
122c9710b42Smrg	return o->id;
123c9710b42Smrg
124c9710b42Smrg    if (_FcObjectLookupOtherTypeByName (str, &id))
125c9710b42Smrg	return id;
126c9710b42Smrg
127c9710b42Smrg    return 0;
128c9710b42Smrg}
129c9710b42Smrg
130c9710b42Smrgconst char *
131c9710b42SmrgFcObjectLookupOtherNameById (FcObject id)
132c9710b42Smrg{
133c9710b42Smrg    struct FcObjectOtherTypeInfo *ot;
134c9710b42Smrg
135c9710b42Smrg    for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
136c9710b42Smrg	if (ot->id == id)
137c9710b42Smrg	    return ot->object.object;
138c9710b42Smrg
139c9710b42Smrg    return NULL;
140c9710b42Smrg}
141c9710b42Smrg
142c9710b42Smrgconst FcObjectType *
143c9710b42SmrgFcObjectLookupOtherTypeByName (const char *str)
144c9710b42Smrg{
145c9710b42Smrg    return _FcObjectLookupOtherTypeByName (str, NULL);
146c9710b42Smrg}
147c9710b42Smrg
148c9710b42SmrgFcPrivate const FcObjectType *
149c9710b42SmrgFcObjectLookupOtherTypeById (FcObject id)
150c9710b42Smrg{
151c9710b42Smrg    struct FcObjectOtherTypeInfo *ot;
152c9710b42Smrg
153c9710b42Smrg    for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
154c9710b42Smrg	if (ot->id == id)
155c9710b42Smrg	    return &ot->object;
156c9710b42Smrg
157c9710b42Smrg    return NULL;
158c9710b42Smrg}
159c9710b42Smrg
160c9710b42Smrg
161c9710b42Smrg#include "fcaliastail.h"
162c9710b42Smrg#undef __fcobjs__
163