home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 495a.lha / ISpell_v3.1ljr / src / lookup.c < prev    next >
C/C++ Source or Header  |  1991-04-06  |  4KB  |  163 lines

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