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