home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part03 / l_name.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  3.8 KB  |  186 lines

  1. /*
  2.     l_name.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include "l_proto.h"
  26.  
  27.  
  28. #define HASHTABSIZE 401
  29.  
  30. static eindex hash_tab[HASHTABSIZE];
  31. static eindex one_char_name['~' - ' '];
  32.  
  33. ushort hash(byteptr s, uint len)
  34. {
  35.     ushort h = 0;
  36.  
  37.     while (len>0) {
  38.         if (len--%2)
  39.             h ^= *s++ << 8;
  40.         else
  41.             h ^= *s++;
  42.     }
  43.     return h % HASHTABSIZE;
  44. }
  45.  
  46.  
  47. /*
  48.     always increments the ref count, even if the name already existed
  49. */
  50. eindex
  51. name_add(byteptr s, uint len, byte attr)
  52. {
  53.     eindex ei;
  54.     eptr ep;
  55.     ushort h;
  56.  
  57.     if (len == 1) {
  58.         ei = one_char_name[*s - ' ' - 1];
  59.         if (!ei) {
  60.             ei = new_element(0, T_NAME);
  61.             ep = gaddr(ei);
  62.  
  63.             eplen(ep) = 1;
  64.             epattr(ep) = attr;
  65.             ep->V.nam.u.n[0] = *s;
  66.             one_char_name[*s - ' ' - 1] = ei;
  67.         } else
  68.             increfp(gaddr(ei));
  69.         return ei;
  70.     }
  71.     h = hash(s, len);
  72.     ei = hash_tab[h];
  73.     while (ei) {
  74.         ep = gaddr(ei);
  75.         if (eplen(ep) == len && eptype(ep) == T_NAME) {
  76.             byteptr cp = (len<=SHORTNAMELEN) ? ep->V.nam.u.n : ep->V.nam.u.s;
  77.             if (memcmp((char*)cp,(char*)s,len) == 0) {
  78.                 increfp(ep);
  79.                 return ei;
  80.             }
  81.         }
  82.         ei = ep->V.nam.next;
  83.     }
  84.     ei = new_element(0, T_NAME);
  85.     ep = gaddr(ei);
  86.     eplen(ep) = len;
  87.     epattr(ep) = attr;
  88.     ep->V.nam.next = hash_tab[h];
  89.     hash_tab[h] = ei;
  90.     if (len <= SHORTNAMELEN)
  91.         memcpy((char*)(ep->V.nam.u.n), (char*)s, len);
  92.     else {
  93.         ep->V.nam.u.s = (byteptr) malloc(len);
  94.         memcpy((char*)(ep->V.nam.u.s), (char*)s, len);
  95.     }
  96.     TRACE(4,printf("adding name %d\n", ei))
  97.  
  98.     return ei;
  99. }
  100.  
  101.  
  102. int
  103. name_eq(eindex n1, eindex n2)
  104. {
  105.  
  106.     return desub(0, n1) == desub(0, n2) ? 1 : 0;
  107. }
  108.  
  109.  
  110. eindex
  111. key_add(byteptr k)
  112. {
  113.     eindex ei;
  114.     eptr ep;
  115.     ushort h;
  116.  
  117.     h = hash(k, 8);
  118.     ei = hash_tab[h];
  119.     while (ei) {
  120.         ep = gaddr(ei);
  121.         if (eptype(ep) == T_KEY) {
  122.             if (memcmp((char*)(ep->V.nam.u.s), (char*)k, 8) == 0) {
  123.                 increfp(ep);
  124.                 return ei;
  125.             }
  126.         }
  127.         ei = ep->V.nam.next;
  128.     }
  129.     ei = new_element(0, T_KEY);
  130.     ep = gaddr(ei);
  131.     eplen(ep) = 8;
  132.     ep->V.nam.next = hash_tab[h];
  133.     hash_tab[h] = ei;
  134.     ep->V.nam.u.s = (byteptr) malloc(8);
  135.     memcpy((char*)(ep->V.nam.u.s), (char*)k, 8);
  136.  
  137.     TRACE(4, printf("adding key %d\n", ei))
  138.  
  139.     return ei;
  140. }
  141.  
  142.  
  143. int
  144. key_eq(eindex k1, eindex k2)
  145. {
  146.  
  147.     return desub(0, k1) == desub(0, k2) ? 1 : 0;
  148. }
  149.  
  150.  
  151. void
  152. free_name(eindex ei)
  153. {
  154.     eptr ep = gaddr(ei);
  155.     ushort h;
  156.  
  157.     TRACE(4, printf("freeing name %d (%d)\n", ei, eplen(ep)))
  158.  
  159.     if (eplen(ep) == 1) {
  160.         one_char_name[ep->V.nam.u.n[0] - ' ' - 1] = 0;
  161.         eptype(ep) = T_EMPTY;
  162.         return;
  163.     }
  164.  
  165.     if (eplen(ep) > SHORTNAMELEN) {
  166.         h = hash(ep->V.nam.u.s, eplen(ep));
  167.         free((char*)(ep->V.nam.u.s));
  168.     } else
  169.         h = hash(ep->V.nam.u.n, eplen(ep));
  170.     if (hash_tab[h] == ei)
  171.         hash_tab[h] = ep->V.nam.next;
  172.     else {
  173.         eindex e = hash_tab[h];
  174.         while (e && gaddr(e)->V.nam.next!=ei)
  175.             e = gaddr(e)->V.nam.next;
  176.         if (!e)
  177.             fprintf(stderr,
  178. "  ## *** internal error: empty hash chain while freeing name %d (%d)\n",
  179.                             ei, eplen(ep));
  180.         else
  181.             gaddr(e)->V.nam.next = ep->V.nam.next;
  182.     }
  183.     eptype(ep) = T_EMPTY;
  184.     return;
  185. }
  186.