home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / hash.c < prev    next >
C/C++ Source or Header  |  1995-04-29  |  5KB  |  186 lines

  1. /*******************WARNING*********************
  2.  
  3. This is a *MODIFIED* version of Geoff Coller's proof-of-concept NOV
  4. implementation.
  5.  
  6. It has been modified to support threading directly from a file handle
  7. to a NNTP server without a temporary file.
  8.  
  9. This is not a complete distribution.  We have only distributed enough
  10. to support NN's needs.
  11.  
  12. The original version came from world.std.com:/src/news/nov.dist.tar.Z
  13. and was dated 11 Aug 1993.
  14.  
  15. In any case, bugs you find here are probably my fault, as I've trimmed
  16. a fair bit of unused code.
  17.  
  18. -Peter Wemm  <peter@DIALix.oz.au>
  19. */
  20.  
  21. /*
  22.  * Copyright (c) Geoffrey Collyer 1992, 1993.
  23.  * All rights reserved.
  24.  * Written by Geoffrey Collyer.
  25.  * Thanks to UUNET Communications Services Inc for financial support.
  26.  *
  27.  * This software is not subject to any license of the American Telephone
  28.  * and Telegraph Company, the Regents of the University of California, or
  29.  * the Free Software Foundation.
  30.  *
  31.  * Permission is granted to anyone to use this software for any purpose on
  32.  * any computer system, and to alter it and redistribute it freely, subject
  33.  * to the following restrictions:
  34.  *
  35.  * 1. The authors are not responsible for the consequences of use of this
  36.  *    software, no matter how awful, even if they arise from flaws in it.
  37.  *
  38.  * 2. The origin of this software must not be misrepresented, either by
  39.  *    explicit claim or by omission.  Since few users ever read sources,
  40.  *    credits must appear in the documentation.
  41.  *
  42.  * 3. Altered versions must be plainly marked as such, and must not be
  43.  *    misrepresented as being the original software.  Since few users
  44.  *    ever read sources, credits must appear in the documentation.
  45.  *
  46.  * 4. This notice may not be removed or altered.
  47.  */
  48.  
  49.  
  50. /*
  51.  * general-purpose in-core hashing, normal interface (wrapper)
  52.  */
  53.  
  54. #include "config.h"
  55. #include "hdbm.h"
  56. #include "hash.h"
  57.  
  58. #ifdef notdef
  59. static unsigned                /* not yet taken modulus table size */
  60. hashdef(key)
  61. register HASHDATUM key;
  62. {
  63.     register unsigned hash = 0;
  64.     register char c;
  65.  
  66.     while ((c = *key++) != '\0')
  67.         hash += c;
  68.     return hash;
  69. }
  70. #endif
  71.  
  72. HASHTABLE *
  73. hashcreate(size, hashfunc)
  74. unsigned size;                /* a crude guide to size */
  75. unsigned (*hashfunc)();
  76. {
  77.     return hdbmcreate(size, hashfunc);
  78. }
  79.  
  80. hashdestroy(tbl)
  81. register HASHTABLE *tbl;
  82. {
  83.     hdbmdestroy(tbl);
  84. }
  85.  
  86. int
  87. hashstore(tbl, key, data)
  88. HASHTABLE *tbl;
  89. register HASHDATUM key, data;
  90. {
  91.     register HDBMDATUM hdbmkey, hdbmdata;
  92.  
  93.     hdbmkey.dat_ptr = key;            /* actually a string */
  94.     hdbmkey.dat_len = strlen(key);
  95.     hdbmdata.dat_ptr = data;        /* just an address */
  96.     hdbmdata.dat_len = 0;            /* no promises */
  97.     return hdbmstore(tbl, hdbmkey, hdbmdata);
  98. }
  99.  
  100. static HASHDATUM (*hashalloc)();
  101.  
  102. static HDBMDATUM
  103. hdbmalloc(key)            /* hdbm->hash->hdbm allocator translator */
  104. HDBMDATUM key;
  105. {
  106.     register HDBMDATUM hdbmdata;
  107.  
  108.     hdbmdata.dat_ptr = (*hashalloc)(key.dat_ptr);
  109.     hdbmdata.dat_len = 0;            /* just a string */
  110.     return hdbmdata;
  111. }
  112.  
  113. /* return any existing entry for key; otherwise call allocator to make one */
  114. HASHDATUM
  115. hashentry(tbl, key, allocator)
  116. register HASHTABLE *tbl;
  117. HASHDATUM key;
  118. HASHDATUM (*allocator)();
  119. {
  120.     register HDBMDATUM hdbmkey, hdbmdata;
  121.  
  122.     hdbmkey.dat_ptr = key;            /* just a string */
  123.     hdbmkey.dat_len = strlen(key);
  124.     hashalloc = allocator;
  125.     hdbmdata = hdbmentry(tbl, hdbmkey, hdbmalloc);
  126.     return hdbmdata.dat_ptr;
  127. }
  128.  
  129. HASHDATUM                    /* data corresponding to key */
  130. hashfetch(tbl, key)
  131. HASHTABLE *tbl;
  132. register HASHDATUM key;
  133. {
  134.     register HDBMDATUM hdbmkey, hdbmdata;
  135.  
  136.     hdbmkey.dat_ptr = key;            /* actually a string */
  137.     hdbmkey.dat_len = strlen(key);
  138.     hdbmdata = hdbmfetch(tbl, hdbmkey);
  139.     return hdbmdata.dat_ptr;        /* just an address */
  140. }
  141.  
  142. int
  143. hashdelete(tbl, key)
  144. HASHTABLE *tbl;
  145. register HASHDATUM key;
  146. {
  147.     register HDBMDATUM hdbmkey;
  148.  
  149.     hdbmkey.dat_ptr = key;            /* actually a string */
  150.     hdbmkey.dat_len = strlen(key);
  151.     return hdbmdelete(tbl, hdbmkey);
  152. }
  153.  
  154. struct translate {
  155.     char *realhook;
  156.     int (*func)();
  157. };
  158.  
  159. static
  160. hdbmtohash(key, data, hook)
  161. HDBMDATUM key, data;
  162. char *hook;
  163. {
  164.     register struct translate *thp = (struct translate *)hook;
  165.  
  166.     (*thp->func)(key.dat_ptr, data.dat_ptr, thp->realhook);
  167. }
  168.  
  169. /*
  170.  * arrange that at each node, hdbmtohash gets called to map the
  171.  * HDBMDATUM arguments to HASHDATUM arguments.  this also demonstrates
  172.  * how to use the hook argument.
  173.  */
  174. hashwalk(tbl, nodefunc, hook)
  175. HASHTABLE *tbl;
  176. int (*nodefunc)();
  177. char *hook;                    /* (void *) really */
  178. {
  179.     struct translate transhook;
  180.     register struct translate *tp = &transhook;
  181.  
  182.     tp->realhook = hook;
  183.     tp->func = nodefunc;
  184.     hdbmwalk(tbl, hdbmtohash, (char *)tp);
  185. }
  186.