"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.4/src/hashstr.c" (20 Nov 2019, 4002 Bytes) of package /linux/misc/tin-2.4.4.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hashstr.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.3_vs_2.4.4.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : hashstr.c
    4  *  Author    : I. Lea & R. Skrenta
    5  *  Created   : 1991-04-01
    6  *  Updated   : 2003-09-19
    7  *  Notes     :
    8  *
    9  * Copyright (c) 1991-2020 Iain Lea <iain@bricbrac.de>, Rich Skrenta <skrenta@pbm.com>
   10 
   11  * All rights reserved.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  *
   17  * 1. Redistributions of source code must retain the above copyright notice,
   18  *    this list of conditions and the following disclaimer.
   19  *
   20  * 2. Redistributions in binary form must reproduce the above copyright
   21  *    notice, this list of conditions and the following disclaimer in the
   22  *    documentation and/or other materials provided with the distribution.
   23  *
   24  * 3. Neither the name of the copyright holder nor the names of its
   25  *    contributors may be used to endorse or promote products derived from
   26  *    this software without specific prior written permission.
   27  *
   28  * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   38  * POSSIBILITY OF SUCH DAMAGE.
   39  */
   40 
   41 
   42 #ifndef TIN_H
   43 #   include "tin.h"
   44 #endif /* !TIN_H */
   45 
   46 /*
   47  * Maintain a table of all strings we have seen.
   48  * If a new string comes in, add it to the table and return a pointer
   49  * to it. If we've seen it before, just return the pointer to it.
   50  *
   51  *  Usage: hash_str("some string") returns char *
   52  *
   53  * Spillovers are chained on the end
   54  */
   55 
   56 /*
   57  * Arbitrary table size, but make sure it's prime!
   58  */
   59 #define HASHNODE_TABLE_SIZE 2411
   60 
   61 static struct t_hashnode *table[HASHNODE_TABLE_SIZE];
   62 static struct t_hashnode *add_string(const char *s);
   63 
   64 char *
   65 hash_str(
   66     const char *s)
   67 {
   68     const unsigned char *t = (const unsigned char *) s;
   69     int len = 0;
   70     long h;             /* result of hash: index into hash table */
   71     struct t_hashnode **p;  /* used to descend the spillover structs */
   72 
   73     if (s == NULL)
   74         return NULL;
   75 
   76     h = 0;
   77     while (*t) {
   78         h = (h << 1) ^ *t++;
   79         if (++len & 7)
   80             continue;
   81         h %= (long) HASHNODE_TABLE_SIZE;
   82     }
   83     h %= (long) HASHNODE_TABLE_SIZE;
   84 
   85     p = &table[h];
   86 
   87     while (*p) {
   88         if (STRCMPEQ(s, (*p)->txt))
   89             return (*p)->txt;
   90         p = &(*p)->next;
   91     }
   92 
   93     *p = add_string(s);
   94     return (*p)->txt;           /* Return ptr to text, _not_ the struct */
   95 }
   96 
   97 
   98 /*
   99  * Add a string to the hash table
  100  * Each entry will have the following structure:
  101  *
  102  * t_hashnode *next     Pointer to the next hashnode in chain
  103  * int aptr                 'magic' ptr used to speed subj threading
  104  * T                            The text itself. The ptr that hash_str()
  105  * E                            returns points here - the earlier fields
  106  * X                            are 'hidden'.
  107  * T
  108  * \0                           String terminator
  109  */
  110 static struct t_hashnode *
  111 add_string(
  112     const char *s)
  113 {
  114     struct t_hashnode *p;
  115 
  116     p = my_malloc(sizeof(struct t_hashnode) + strlen(s));
  117 
  118     p->next = (struct t_hashnode *) 0;
  119     p->aptr = -1;                   /* -1 is the default value */
  120 
  121     strcpy(p->txt, s);          /* Copy in the text */
  122 
  123     return p;
  124 }
  125 
  126 
  127 void
  128 hash_init(
  129     void)
  130 {
  131     int i;
  132 
  133     for (i = 0; i < HASHNODE_TABLE_SIZE; i++)
  134         table[i] = (struct t_hashnode *) 0;
  135 }
  136 
  137 
  138 void
  139 hash_reclaim(
  140     void)
  141 {
  142     int i;
  143     struct t_hashnode *p, *next;
  144 
  145     for (i = 0; i < HASHNODE_TABLE_SIZE; i++) {
  146         if (table[i] != NULL) {
  147             p = table[i];
  148             while (p != NULL) {
  149                 next = p->next;
  150                 free(p);
  151                 p = next;
  152             }
  153             table[i] = (struct t_hashnode *) 0;
  154         }
  155     }
  156 }