atom.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include <X11/X.h>
  40. #include <X11/Xatom.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "misc.h"
  44. #include "resource.h"
  45. #include "dix.h"
  46. #define InitialTableSize 100
  47. typedef struct _Node {
  48. struct _Node *left, *right;
  49. Atom a;
  50. unsigned int fingerPrint;
  51. char *string;
  52. } NodeRec, *NodePtr;
  53. static Atom lastAtom = None;
  54. static NodePtr atomRoot = (NodePtr) NULL;
  55. static unsigned long tableLength;
  56. static NodePtr *nodeTable;
  57. void FreeAtom(NodePtr patom);
  58. _X_EXPORT Atom
  59. MakeAtom(char *string, unsigned len, Bool makeit)
  60. {
  61. NodePtr *np;
  62. unsigned i;
  63. int comp;
  64. unsigned int fp = 0;
  65. np = &atomRoot;
  66. for (i = 0; i < (len + 1) / 2; i++) {
  67. fp = fp * 27 + string[i];
  68. fp = fp * 27 + string[len - 1 - i];
  69. }
  70. while (*np != (NodePtr) NULL) {
  71. if (fp < (*np)->fingerPrint)
  72. np = &((*np)->left);
  73. else if (fp > (*np)->fingerPrint)
  74. np = &((*np)->right);
  75. else { /* now start testing the strings */
  76. comp = strncmp(string, (*np)->string, (int) len);
  77. if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
  78. np = &((*np)->left);
  79. else if (comp > 0)
  80. np = &((*np)->right);
  81. else
  82. return (*np)->a;
  83. }
  84. }
  85. if (makeit) {
  86. NodePtr nd;
  87. nd = malloc(sizeof(NodeRec));
  88. if (!nd)
  89. return BAD_RESOURCE;
  90. if (lastAtom < XA_LAST_PREDEFINED) {
  91. nd->string = string;
  92. }
  93. else {
  94. nd->string = malloc(len + 1);
  95. if (!nd->string) {
  96. free(nd);
  97. return BAD_RESOURCE;
  98. }
  99. strncpy(nd->string, string, (int) len);
  100. nd->string[len] = 0;
  101. }
  102. if ((lastAtom + 1) >= tableLength) {
  103. NodePtr *table;
  104. table = (NodePtr *) realloc(nodeTable,
  105. tableLength * (2 * sizeof(NodePtr)));
  106. if (!table) {
  107. if (nd->string != string)
  108. free(nd->string);
  109. free(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. _X_EXPORT Bool
  126. ValidAtom(Atom atom)
  127. {
  128. return (atom != None) && (atom <= lastAtom);
  129. }
  130. _X_EXPORT char *
  131. NameForAtom(Atom atom)
  132. {
  133. NodePtr node;
  134. if (atom > lastAtom)
  135. return 0;
  136. if ((node = nodeTable[atom]) == (NodePtr) NULL)
  137. return 0;
  138. return node->string;
  139. }
  140. void
  141. AtomError()
  142. {
  143. FatalError("initializing atoms");
  144. }
  145. void
  146. FreeAtom(NodePtr patom)
  147. {
  148. if (patom->left)
  149. FreeAtom(patom->left);
  150. if (patom->right)
  151. FreeAtom(patom->right);
  152. if (patom->a > XA_LAST_PREDEFINED)
  153. free(patom->string);
  154. free(patom);
  155. }
  156. void
  157. FreeAllAtoms()
  158. {
  159. if (atomRoot == (NodePtr) NULL)
  160. return;
  161. FreeAtom(atomRoot);
  162. atomRoot = (NodePtr) NULL;
  163. free(nodeTable);
  164. nodeTable = (NodePtr *) NULL;
  165. lastAtom = None;
  166. }
  167. void
  168. InitAtoms()
  169. {
  170. FreeAllAtoms();
  171. tableLength = InitialTableSize;
  172. nodeTable = malloc(InitialTableSize * sizeof(NodePtr));
  173. if (!nodeTable)
  174. AtomError();
  175. nodeTable[None] = (NodePtr) NULL;
  176. MakePredeclaredAtoms();
  177. if (lastAtom != XA_LAST_PREDEFINED)
  178. AtomError();
  179. }