xkbatom.c revision 70728a38
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(const 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 fp = fp * 27 + string[i]; 116 fp = fp * 27 + string[len - 1 - i]; 117 } 118 while (*np != (NodePtr) NULL) { 119 if (fp < (*np)->fingerPrint) 120 np = &((*np)->left); 121 else if (fp > (*np)->fingerPrint) 122 np = &((*np)->right); 123 else { /* now start testing the strings */ 124 comp = strncmp(string, (*np)->string, (int) len); 125 if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string)))) 126 np = &((*np)->left); 127 else if (comp > 0) 128 np = &((*np)->right); 129 else 130 return (*np)->a; 131 } 132 } 133 if (makeit) { 134 register NodePtr nd; 135 136 nd = (NodePtr) _XkbAlloc(sizeof(NodeRec)); 137 if (!nd) 138 return BAD_RESOURCE; 139 nd->string = (char *) _XkbAlloc(len + 1); 140 if (!nd->string) { 141 _XkbFree(nd); 142 return BAD_RESOURCE; 143 } 144 strncpy(nd->string, string, (int) len); 145 nd->string[len] = 0; 146 if ((lastAtom + 1) >= tableLength) { 147 NodePtr *table; 148 149 table = (NodePtr *) _XkbRealloc(nodeTable, 150 tableLength * (2 * 151 sizeof(NodePtr))); 152 if (!table) { 153 if (nd->string != string) 154 _XkbFree(nd->string); 155 _XkbFree(nd); 156 return BAD_RESOURCE; 157 } 158 tableLength <<= 1; 159 nodeTable = table; 160 } 161 *np = nd; 162 nd->left = nd->right = (NodePtr) NULL; 163 nd->fingerPrint = fp; 164 nd->a = (++lastAtom); 165 *(nodeTable + lastAtom) = nd; 166 return nd->a; 167 } 168 else 169 return None; 170} 171 172static char * 173_XkbNameForAtom(Atom atom) 174{ 175 NodePtr node; 176 177 if (atom > lastAtom) 178 return NULL; 179 if ((node = nodeTable[atom]) == (NodePtr) NULL) 180 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, const 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{ 222 char *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{ 237 static int been_here = 0; 238 239 if ((dpy == NULL) && (!been_here)) { 240 _XkbInitAtoms(); 241 been_here = 1; 242 } 243 return; 244} 245