home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / dix / atom.c next >
Encoding:
C/C++ Source or Header  |  1989-08-01  |  4.2 KB  |  183 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* $XConsortium: atom.c,v 1.28 89/08/02 09:10:50 rws Exp $ */
  26.  
  27. #include "X.h"
  28. #include "Xatom.h"
  29. #include "misc.h"
  30. #include "resource.h"
  31.  
  32. #define InitialTableSize 100
  33.  
  34. typedef struct _Node {
  35.     struct _Node   *left,   *right;
  36.     Atom a;
  37.     unsigned int fingerPrint;
  38.     char   *string;
  39. } NodeRec, *NodePtr;
  40.  
  41. static Atom lastAtom = None;
  42. static NodePtr atomRoot = (NodePtr)NULL;
  43. static unsigned long tableLength;
  44. static NodePtr *nodeTable;
  45.  
  46. Atom 
  47. MakeAtom(string, len, makeit)
  48.     char *string;
  49.     unsigned len;
  50.     Bool makeit;
  51. {
  52.     register    NodePtr * np;
  53.     unsigned i;
  54.     int     comp;
  55.     register unsigned int   fp = 0;
  56.  
  57.     np = &atomRoot;
  58.     for (i = 0; i < (len+1)/2; i++)
  59.     {
  60.     fp = fp * 27 + string[i];
  61.     fp = fp * 27 + string[len - 1 - i];
  62.     }
  63.     while (*np != (NodePtr) NULL)
  64.     {
  65.     if (fp < (*np)->fingerPrint)
  66.         np = &((*np)->left);
  67.     else if (fp > (*np)->fingerPrint)
  68.         np = &((*np)->right);
  69.     else
  70.     {                   /* now start testing the strings */
  71.         comp = strncmp(string, (*np)->string, (int)len);
  72.         if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
  73.         np = &((*np)->left);
  74.         else if (comp > 0)
  75.         np = &((*np)->right);
  76.         else
  77.         return(*np)->a;
  78.         }
  79.     }
  80.     if (makeit)
  81.     {
  82.     register NodePtr nd;
  83.  
  84.     nd = (NodePtr) xalloc(sizeof(NodeRec));
  85.     if (!nd)
  86.         return BAD_RESOURCE;
  87.     if (lastAtom < XA_LAST_PREDEFINED)
  88.     {
  89.         nd->string = string;
  90.     }
  91.     else
  92.     {
  93.         nd->string = (char *) xalloc(len + 1);
  94.         if (!nd->string) {
  95.         xfree(nd);
  96.         return BAD_RESOURCE;
  97.         }
  98.         strncpy(nd->string, string, (int)len);
  99.         nd->string[len] = 0;
  100.     }
  101.     if ((lastAtom + 1) >= tableLength) {
  102.         NodePtr *table;
  103.  
  104.         table = (NodePtr *) xrealloc(nodeTable,
  105.                      tableLength * (2 * sizeof(NodePtr)));
  106.         if (!table) {
  107.         if (nd->string != string)
  108.             xfree(nd->string);
  109.         xfree(nd);
  110.         return BAD_RESOURCE;
  111.         }
  112.         tableLength <<= 1;
  113.         nodeTable = table;
  114.     }
  115.     *np = nd;
  116.     nd->left = nd->right = (NodePtr) NULL;
  117.     nd->fingerPrint = fp;
  118.     nd->a = (++lastAtom);
  119.     *(nodeTable+lastAtom) = nd;
  120.     return nd->a;
  121.     }
  122.     else
  123.     return None;
  124. }
  125.  
  126. ValidAtom(atom)
  127.     Atom atom;
  128. {
  129.     return (atom != None) && (atom <= lastAtom);
  130. }
  131.  
  132. char *
  133. NameForAtom(atom)
  134.     Atom atom;
  135. {
  136.     NodePtr node;
  137.     if (atom > lastAtom) return 0;
  138.     if ((node = nodeTable[atom]) == (NodePtr)NULL) return 0;
  139.     return node->string;
  140. }
  141.  
  142. AtomError()
  143. {
  144.     FatalError("initializing atoms");
  145. }
  146.  
  147. InitAtoms()
  148. {
  149.     FreeAllAtoms();
  150.     tableLength = InitialTableSize;
  151.     nodeTable = (NodePtr *)xalloc(InitialTableSize*sizeof(NodePtr));
  152.     if (!nodeTable)
  153.     AtomError();
  154.     nodeTable[None] = (NodePtr)NULL;
  155.     MakePredeclaredAtoms();
  156.     if (lastAtom != XA_LAST_PREDEFINED)
  157.     AtomError ();
  158. }
  159.  
  160. FreeAllAtoms()
  161. {
  162.     if(atomRoot == (NodePtr)NULL)
  163.     return;
  164.     FreeAtom(atomRoot);
  165.     atomRoot = (NodePtr)NULL;
  166.     xfree(nodeTable);
  167.     nodeTable = (NodePtr *)NULL;
  168.     lastAtom = None;
  169. }
  170.  
  171. FreeAtom(patom)
  172.     NodePtr patom;
  173. {
  174.     if(patom->left)
  175.     FreeAtom(patom->left);
  176.     if(patom->right)
  177.     FreeAtom(patom->right);
  178.     if (patom->a > XA_LAST_PREDEFINED)
  179.     xfree(patom->string);
  180.     xfree(patom);
  181. }
  182.     
  183.