xkbcomp: Lazy keysym parsing (avoid XStringToKeysym)
[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 #ifdef HAVE_CONFIG_H
74 #include <config.h>
75 #endif
76 #include "xkbmisc.h"
77 #include "X11/extensions/XKBcommon.h"
78 #include "XKBcommonint.h"
79
80 #define InitialTableSize 100
81
82 typedef struct _Node {
83     struct _Node    *left, *right;
84     Atom            a;
85     unsigned int    fingerPrint;
86     char            *string;
87 } NodeRec, *NodePtr;
88
89 #define BAD_RESOURCE 0xe0000000
90
91 static Atom lastAtom = None;
92 static NodePtr atomRoot = NULL;
93 static unsigned long tableLength;
94 static NodePtr *nodeTable = NULL;
95
96 void
97 XkbcInitAtoms(void)
98 {
99     if (nodeTable)
100         return;
101
102     tableLength = InitialTableSize;
103     nodeTable = (NodePtr *)_XkbAlloc(InitialTableSize * sizeof(NodePtr));
104     nodeTable[None] = NULL;
105 }
106
107 static char *
108 _XkbcAtomGetString(Atom atom)
109 {
110     NodePtr node;
111
112     if ((atom == None) || (atom > lastAtom))
113         return NULL;
114     if (!(node = nodeTable[atom]))
115         return NULL;
116     return node->string;
117 }
118
119 char *
120 XkbcAtomGetString(Atom atom)
121 {
122     char *ret = _XkbcAtomGetString(atom);
123     return ret ? strdup(ret) : NULL;
124 }
125
126 char *
127 XkbcAtomText(Atom atom)
128 {
129     char *tmp, *ret;
130
131     tmp = _XkbcAtomGetString(atom);
132     if (!tmp)
133         return "";
134
135     ret = tbGetBuffer(strlen(tmp) + 1);
136     if (!ret)
137         return "";
138
139     strcpy(ret, tmp);
140     return ret;
141 }
142
143 static Atom
144 _XkbcMakeAtom(char *string, unsigned len, Bool makeit)
145 {
146     NodePtr *np;
147     unsigned i;
148     int comp;
149     unsigned int fp = 0;
150
151     np = &atomRoot;
152     for (i = 0; i < (len + 1) / 2; i++) {
153         fp = fp * 27 + string[i];
154         fp = fp * 27 + string[len - 1 - i];
155     }
156
157     while (*np) {
158         if (fp < (*np)->fingerPrint)
159             np = &((*np)->left);
160         else if (fp > (*np)->fingerPrint)
161             np = &((*np)->right);
162         else {
163             /* now start testing the strings */
164             comp = strncmp(string, (*np)->string, (int)len);
165             if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
166                 np = &((*np)->left);
167             else if (comp > 0)
168                 np = &((*np)->right);
169             else
170                 return(*np)->a;
171             }
172     }
173
174     if (makeit) {
175         NodePtr nd;
176
177         nd = (NodePtr)_XkbAlloc(sizeof(NodeRec));
178         if (!nd)
179             return BAD_RESOURCE;
180
181         nd->string = (char *)_XkbAlloc(len + 1);
182         if (!nd->string) {
183             _XkbFree(nd);
184             return BAD_RESOURCE;
185         }
186         strncpy(nd->string, string, (int)len);
187         nd->string[len] = 0;
188
189         if ((lastAtom + 1) >= tableLength) {
190             NodePtr *table;
191
192             table = (NodePtr *)_XkbRealloc(nodeTable,
193                                            tableLength * 2 * sizeof(NodePtr));
194             if (!table) {
195                 if (nd->string != string)
196                     _XkbFree(nd->string);
197                 _XkbFree(nd);
198                 return BAD_RESOURCE;
199             }
200             tableLength <<= 1;
201
202             nodeTable = table;
203         }
204
205         *np = nd;
206         nd->left = nd->right = NULL;
207         nd->fingerPrint = fp;
208         nd->a = (++lastAtom);
209         *(nodeTable + lastAtom) = nd;
210
211         return nd->a;
212     }
213     else
214         return None;
215 }
216
217 Atom
218 XkbcInternAtom(char *name, Bool onlyIfExists)
219 {
220     if (!name)
221         return None;
222     return _XkbcMakeAtom(name, strlen(name), !onlyIfExists);
223 }