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