home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d191 / ispell.lha / ISpell / src.zoo / lookup.c < prev    next >
C/C++ Source or Header  |  1989-02-22  |  4KB  |  161 lines

  1. /* -*- Mode:Text -*- */
  2.  
  3. /*
  4.  * lookup.c - see if a word appears in the dictionary
  5.  *
  6.  * Pace Willisson, 1983
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "config.h"
  11. #include "ispell.h"
  12.  
  13.  
  14. struct dent *treelookup();
  15. struct dent *hashtbl;
  16. int hashsize;
  17.  
  18. extern char hashname[];
  19.  
  20. static inited = 0;
  21.  
  22. linit ()
  23. {
  24.     int hashfd;
  25.     register int i;
  26.     register struct dent *dp;
  27.  
  28.     if (inited)
  29.         return;
  30.  
  31.     if ((hashfd = open (hashname, 0)) < 0) {
  32.         fprintf (stderr, "can't open %s\r\n", hashname);
  33.         return (-1);
  34.     }
  35.  
  36.     hashsize = read (hashfd, &hashheader, sizeof hashheader);
  37.     if (hashsize == 0) {
  38.         /*
  39.          * Empty file - create an empty dummy table.  We
  40.          * actually have to have one entry since the hash
  41.          * algorithm involves a divide by the table size
  42.          * (actually modulo, but zero is still unacceptable).
  43.          * So we create an entry with a word of all lowercase,
  44.          * which can't match because the comparison string has
  45.          * been converted to uppercase by then.
  46.          */
  47.         close (hashfd);
  48.         hashsize = 1;        /* This prevents divides by zero */
  49.         hashtbl = (struct dent *) calloc (1, sizeof (struct dent));
  50.         if (hashtbl == NULL) {
  51.             (void) fprintf (stderr,
  52.                 "Couldn't allocate space for hash table\n");
  53.             return (-1);
  54.         }
  55.         hashtbl[0].word = "xxxxxxxxxxx";
  56.         hashtbl[0].next = NULL;
  57.         hashtbl[0].keep = 0;
  58.         hashtbl[0].used = 1;
  59.         /* The flag bits don't matter, but calloc cleared them. */
  60.         inited = 1;
  61.         return 0;
  62.     }
  63.     else if (hashsize < 0  ||  hashheader.magic != MAGIC) {
  64.         fprintf (stderr, "Illegal format hash table\r\n");
  65.         return (-1);
  66.     }
  67.     hashstrings = (char *) malloc (hashheader.stringsize);
  68.     hashtbl = (struct dent *) malloc (hashheader.tblsize * sizeof (struct dent));
  69.     if (hashtbl == NULL  ||  hashstrings == NULL) {
  70.         (void) fprintf (stderr,
  71.             "Couldn't allocate space for hash table\n");
  72.         return (-1);
  73.     }
  74.     hashsize = hashheader.tblsize;
  75.  
  76.     read (hashfd, hashstrings, hashheader.stringsize);
  77.     read (hashfd, hashtbl, hashheader.tblsize * sizeof (struct dent));
  78.     close (hashfd);
  79.  
  80.     for (i = hashsize, dp = hashtbl;  --i >= 0;  dp++) {
  81.         dp->word = &hashstrings [ (int)(dp->word) ];
  82.         if (dp->next == (struct dent *) -1)
  83.             dp->next = NULL;
  84.         else
  85.             dp->next = &hashtbl [ (int)(dp->next) ];
  86.     }
  87.  
  88.     inited = 1;
  89.     return (0);
  90. }
  91.  
  92. /* n is length of s */
  93. struct dent *
  94. lookup (s, n, dotree)
  95. register char *s;
  96. {
  97.     int i;
  98.     register struct dent *dp;
  99.     register char *s1, *s2;
  100.  
  101.     dp = &hashtbl [ hash (s, n, hashsize) ];
  102.     for (  ; dp != NULL; dp = dp->next) {
  103.         /* quick strcmp, but only for equality */
  104.         s1 = dp->word;
  105.         s2 = s;
  106.         while (*s1 == *s2++)
  107.             if (*s1++=='\0') {
  108.                 lastdent = dp;
  109.                 return (lastdent);
  110.             }
  111.     }
  112.     if (dotree) {
  113.         i = s[n];
  114.         s[n] = '\0';
  115.         if ((dp = treelookup (s)) != NULL)
  116.             lastdent = dp;
  117.         s[n] = i;
  118.         return dp;
  119.     }
  120.     else
  121.         return NULL;
  122. }
  123.  
  124. #ifdef REGEX
  125. #include <ctype.h>
  126. /* check the hashed dictionary for words matching the regex. return the */
  127. /* a matching string if found else return NULL */
  128. char *do_regex_lookup(regex,whence)
  129. char *regex;    /* regular expression to use in the match   */
  130. int  whence;    /* 0 = start at the beg with new regx, else */
  131.             /* continue from cur point w/ old regex     */
  132. {
  133.     static curindex = 0;
  134.     static char *cmp_expr;
  135.     char *re_comp();
  136.  
  137.     if ( whence == 0 ) {
  138.         char *ptr = regex;
  139.     while ( *ptr ) {
  140.         if ( islower(*ptr) ) 
  141.              *ptr = toupper(*ptr);
  142.         ptr++;
  143.         }
  144.         cmp_expr = re_comp(regex);
  145.         curindex = 0;
  146.     }
  147.     
  148.     /* search the dictionary untill the word is found or the words run out */
  149.     for ( ; curindex < hashsize; curindex++ ) {
  150.         if ( re_exec(hashtbl[curindex].word) )
  151.         return( hashtbl[curindex++].word );
  152.     }
  153.     return( NULL );
  154. }
  155.  
  156. #endif
  157.  
  158.  
  159.  
  160.  
  161.