add packaging
[platform/upstream/fontconfig.git] / src / fcobjs.c
1 /*
2  * fontconfig/src/fclist.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 static unsigned int
28 FcObjectTypeHash (register const char *str, register unsigned int len);
29
30 static const struct FcObjectTypeInfo *
31 FcObjectTypeLookup (register const char *str, register unsigned int len);
32
33 #include "fcobjshash.h"
34
35 #include <string.h>
36
37 /* The 1000 is to leave some room for future added internal objects, such
38  * that caches from newer fontconfig can still be used with older fontconfig
39  * without getting confused. */
40 static fc_atomic_int_t next_id = FC_MAX_BASE_OBJECT + FC_EXT_OBJ_INDEX;
41 struct FcObjectOtherTypeInfo {
42     struct FcObjectOtherTypeInfo *next;
43     FcObjectType object;
44     FcObject id;
45 } *other_types;
46
47 static FcObjectType *
48 _FcObjectLookupOtherTypeByName (const char *str, FcObject *id)
49 {
50     struct FcObjectOtherTypeInfo *ots, *ot;
51
52 retry:
53     ots = fc_atomic_ptr_get (&other_types);
54
55     for (ot = ots; ot; ot = ot->next)
56         if (0 == strcmp (ot->object.object, str))
57             break;
58
59     if (!ot)
60     {
61         ot = malloc (sizeof (*ot));
62         if (!ot)
63             return NULL;
64
65         ot->object.object = (const char *) FcStrdup (str);
66         ot->object.type = FcTypeUnknown;
67         ot->id = fc_atomic_int_add (next_id, +1);
68         ot->next = ots;
69
70         if (!fc_atomic_ptr_cmpexch (&other_types, ots, ot)) {
71             free (ot);
72             goto retry;
73         }
74     }
75
76     if (id)
77       *id = ot->id;
78
79     return &ot->object;
80 }
81
82 FcObject
83 FcObjectLookupBuiltinIdByName (const char *str)
84 {
85     const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
86
87     if (o)
88         return o->id;
89
90     return 0;
91 }
92
93 FcObject
94 FcObjectLookupIdByName (const char *str)
95 {
96     const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
97     FcObject id;
98     if (o)
99         return o->id;
100
101     if (_FcObjectLookupOtherTypeByName (str, &id))
102         return id;
103
104     return 0;
105 }
106
107 const char *
108 FcObjectLookupOtherNameById (FcObject id)
109 {
110     struct FcObjectOtherTypeInfo *ot;
111
112     for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
113         if (ot->id == id)
114             return ot->object.object;
115
116     return NULL;
117 }
118
119 const FcObjectType *
120 FcObjectLookupOtherTypeByName (const char *str)
121 {
122     return _FcObjectLookupOtherTypeByName (str, NULL);
123 }
124
125 FcPrivate const FcObjectType *
126 FcObjectLookupOtherTypeById (FcObject id)
127 {
128     struct FcObjectOtherTypeInfo *ot;
129
130     for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
131         if (ot->id == id)
132             return &ot->object;
133
134     return NULL;
135 }
136
137
138 #include "fcaliastail.h"
139 #undef __fcobjs__