Minor
[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 #include "fcobjshash.h"
28
29 #include <string.h>
30
31 static int next_id = FC_MAX_BASE_OBJECT + 1;
32 struct FcObjectOtherTypeInfo {
33     struct FcObjectOtherTypeInfo *next;
34     FcObjectType object;
35     int id;
36 } *other_types;
37
38 static FcObjectType *
39 _FcObjectLookupOtherTypeByName (const char *str, FcObject *id)
40 {
41     struct FcObjectOtherTypeInfo *ots, *ot;
42
43 retry:
44     ots = fc_atomic_ptr_get (&other_types);
45
46     for (ot = ots; ot; ot = ot->next)
47         if (0 == strcmp (ot->object.object, str))
48             break;
49
50     if (!ot)
51     {
52         ot = malloc (sizeof (*ot));
53         if (!ot)
54             return NULL;
55
56         ot->object.object = (const char *) FcStrdup (str);
57         ot->object.type = -1;
58         ot->id = fc_atomic_int_add (next_id, +1);
59         ot->next = ots;
60
61         if (!fc_atomic_ptr_cmpexch (&other_types, ots, ot)) {
62             free (ot);
63             goto retry;
64         }
65     }
66
67     if (id)
68       *id = ot->id;
69
70     return &ot->object;
71 }
72
73 FcObject
74 FcObjectLookupBuiltinIdByName (const char *str)
75 {
76     const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
77
78     if (o)
79         return o->id;
80
81     return 0;
82 }
83
84 FcObject
85 FcObjectLookupIdByName (const char *str)
86 {
87     const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
88     FcObject id;
89     if (o)
90         return o->id;
91
92     if (_FcObjectLookupOtherTypeByName (str, &id))
93         return id;
94
95     return 0;
96 }
97
98 const char *
99 FcObjectLookupOtherNameById (FcObject id)
100 {
101     struct FcObjectOtherTypeInfo *ot;
102
103     for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
104         if (ot->id == id)
105             return ot->object.object;
106
107     return NULL;
108 }
109
110 const FcObjectType *
111 FcObjectLookupOtherTypeByName (const char *str)
112 {
113     return _FcObjectLookupOtherTypeByName (str, NULL);
114 }
115
116 FcPrivate const FcObjectType *
117 FcObjectLookupOtherTypeById (FcObject id)
118 {
119     struct FcObjectOtherTypeInfo *ot;
120
121     for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
122         if (ot->id == id)
123             return &ot->object;
124
125     return NULL;
126 }
127
128
129 #include "fcaliastail.h"
130 #undef __fcobjs__