Implicitly include config.h in all files
[profile/ivi/libxkbcommon.git] / src / atom.c
1 /***********************************************************
2 Copyright 1987, 1998  The Open Group
3
4 Permission to use, copy, modify, distribute, and sell this software and its
5 documentation for any purpose is hereby granted without fee, provided that
6 the above copyright notice appear in all copies and that both that
7 copyright notice and this permission notice appear in supporting
8 documentation.
9
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
16 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20 Except as contained in this notice, the name of The Open Group shall not be
21 used in advertising or otherwise to promote the sale, use or other dealings
22 in this Software without prior written authorization from The Open Group.
23
24
25 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
26
27                         All Rights Reserved
28
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Digital not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
36
37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43 SOFTWARE.
44
45 ******************************************************************/
46
47 /************************************************************
48  Copyright 1994 by Silicon Graphics Computer Systems, Inc.
49
50  Permission to use, copy, modify, and distribute this
51  software and its documentation for any purpose and without
52  fee is hereby granted, provided that the above copyright
53  notice appear in all copies and that both that copyright
54  notice and this permission notice appear in supporting
55  documentation, and that the name of Silicon Graphics not be
56  used in advertising or publicity pertaining to distribution
57  of the software without specific prior written permission.
58  Silicon Graphics makes no representation about the suitability
59  of this software for any purpose. It is provided "as is"
60  without any express or implied warranty.
61
62  SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
63  SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64  AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
65  GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
66  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
67  DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
68  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
69  THE USE OR PERFORMANCE OF THIS SOFTWARE.
70
71  ********************************************************/
72
73 #include "xkbmisc.h"
74 #include "xkbcommon/xkbcommon.h"
75 #include "XKBcommonint.h"
76
77 #define InitialTableSize 100
78
79 typedef struct _Node {
80     struct _Node    *left, *right;
81     uint32_t          a;
82     unsigned int    fingerPrint;
83     char            *string;
84 } NodeRec, *NodePtr;
85
86 #define BAD_RESOURCE 0xe0000000
87
88 static xkb_atom_t lastAtom = XKB_ATOM_NONE;
89 static NodePtr atomRoot;
90 static unsigned long tableLength;
91 static NodePtr *nodeTable;
92
93 const char *
94 XkbcAtomText(xkb_atom_t atom)
95 {
96     NodePtr node;
97
98     if ((atom == XKB_ATOM_NONE) || (atom > lastAtom))
99         return NULL;
100     if (!(node = nodeTable[atom]))
101         return NULL;
102     return node->string;
103 }
104
105 char *
106 XkbcAtomGetString(xkb_atom_t atom)
107 {
108     const char *ret = XkbcAtomText(atom);
109     return ret ? strdup(ret) : NULL;
110 }
111
112 xkb_atom_t
113 xkb_intern_atom(const char *string)
114 {
115     NodePtr *np;
116     NodePtr nd;
117     unsigned i;
118     int comp;
119     unsigned int fp = 0;
120     size_t len;
121
122     if (!string)
123         return XKB_ATOM_NONE;
124
125     len = strlen(string);
126     np = &atomRoot;
127     for (i = 0; i < (len + 1) / 2; i++) {
128         fp = fp * 27 + string[i];
129         fp = fp * 27 + string[len - 1 - i];
130     }
131
132     while (*np) {
133         if (fp < (*np)->fingerPrint)
134             np = &((*np)->left);
135         else if (fp > (*np)->fingerPrint)
136             np = &((*np)->right);
137         else {
138             /* now start testing the strings */
139             comp = strncmp(string, (*np)->string, len);
140             if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
141                 np = &((*np)->left);
142             else if (comp > 0)
143                 np = &((*np)->right);
144             else
145                 return(*np)->a;
146             }
147     }
148
149     nd = malloc(sizeof(NodeRec));
150     if (!nd)
151         return BAD_RESOURCE;
152
153     nd->string = malloc(len + 1);
154     if (!nd->string) {
155         free(nd);
156         return BAD_RESOURCE;
157     }
158     strncpy(nd->string, string, len);
159     nd->string[len] = 0;
160
161     if ((lastAtom + 1) >= tableLength) {
162         NodePtr *table;
163         int newLength;
164
165         if (tableLength == 0)
166             newLength = InitialTableSize;
167         else
168             newLength = tableLength * 2;
169
170         table = realloc(nodeTable, newLength * sizeof(NodePtr));
171         if (!table) {
172             if (nd->string != string)
173                 free(nd->string);
174             free(nd);
175             return BAD_RESOURCE;
176         }
177         tableLength = newLength;
178         table[XKB_ATOM_NONE] = NULL;
179
180         nodeTable = table;
181     }
182
183     *np = nd;
184     nd->left = nd->right = NULL;
185     nd->fingerPrint = fp;
186     nd->a = (++lastAtom);
187     *(nodeTable + lastAtom) = nd;
188
189     return nd->a;
190 }
191
192 static void
193 FreeAtom(NodePtr patom)
194 {
195     if (patom->left)
196         FreeAtom(patom->left);
197     if (patom->right)
198         FreeAtom(patom->right);
199     free(patom->string);
200     free(patom);
201 }
202
203 void
204 XkbcFreeAllAtoms(void)
205 {
206     if (atomRoot == NULL)
207         return;
208     FreeAtom(atomRoot);
209     atomRoot = NULL;
210     free(nodeTable);
211     nodeTable = NULL;
212     lastAtom = XKB_ATOM_NONE;
213     tableLength = 0;
214 }