home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / SSPELL11.ZIP / CHECK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-26  |  5.4 KB  |  197 lines

  1. /* **************************************************************** */
  2. /*             sspell - similar to Unix spell                       */
  3. /*                        version 1.1                               */
  4. /*                                                                  */
  5. /* Author: Maurice Castro                                           */
  6. /* Release Date: 26 Jan 1992                                        */
  7. /* Bug Reports: maurice@bruce.cs.monash.edu.au                      */
  8. /*                                                                  */
  9. /* This code has been placed by the Author into the Public Domain.  */
  10. /* The code is NOT covered by any warranty, the user of the code is */
  11. /* solely responsible for determining the fitness of the program    */
  12. /* for their purpose. No liability is accepted by the author for    */
  13. /* the direct or indirect losses incurred through the use of this   */
  14. /* program.                                                         */
  15. /*                                                                  */
  16. /* Segments of this code may be used for any purpose that the user  */
  17. /* deems appropriate. It would be polite to acknowledge the source  */
  18. /* of the code. If you modify the code and redistribute it please   */
  19. /* include a message indicating your changes and how users may      */
  20. /* contact you for support.                                         */
  21. /*                                                                  */
  22. /* The author reserves the right to issue the official version of   */
  23. /* this program. If you have useful suggestions or changes for the  */
  24. /* code, please forward them to the author so that they might be    */
  25. /* incorporated into the official version                           */
  26. /*                                                                  */
  27. /* Please forward bug reports to the author via Internet.           */
  28. /*                                                                  */
  29. /* **************************************************************** */
  30.  
  31. #include "check.h"
  32. #include "cache.h"
  33. #include "file.h"
  34. #include <stdio.h>
  35. #include "config.h"
  36. #include "strfn.h"
  37. #include "root.h"
  38. #include "utility.h"
  39.  
  40. extern FILE *fout;
  41. extern int prtderive;
  42. extern int prtroot;
  43.  
  44. int isupper(a)
  45. char a;
  46. {
  47.    if (('A' <= a) && (a <= 'Z')) return(1);
  48.    return(0);
  49.    }
  50.  
  51. int isnumber(a)
  52. char *a;
  53. {
  54.     while (*a != NULL)
  55.     {
  56.         if (!(('0' <= *a) && (*a <= '9'))) return (0);
  57.         a++;
  58.         }
  59.     return(1);
  60.     }
  61.  
  62. int chkcaps(dict, test)
  63. char *dict;
  64. char *test;
  65. {
  66.    /* check that any capitalized letter in the dictionary is matched */
  67.    /* by a capitalized letter in the word */
  68.    while (*dict != NULL)
  69.    {
  70.       if ((isupper(*dict)) && (*dict != *test)) 
  71.          return(0);
  72.       dict++;
  73.       test++;
  74.       }
  75.    return(1);
  76.    }
  77.  
  78. int checkword(word)
  79. char *word;
  80. {
  81.    char indict[MAXSTR];
  82.  
  83.    if (word == NULL)
  84.       return(0); /* fail */
  85.    if (searchcache(word,indict))
  86.    {
  87.        /* if it is not an exact match - check a bit more closely */
  88.        if (strcmp(indict,word))
  89.           if (!chkcaps(indict,word)) 
  90.              return(0);
  91.        }
  92.    else
  93.    {
  94.        if (searchfile(word,indict))
  95.        {
  96.            /* something close is in the file put it in the cache */
  97.            addtocache(indict);
  98.            if (strcmp(indict,word))
  99.               if (!chkcaps(indict,word)) 
  100.                  return(0);
  101.            }
  102.        else
  103.            return(0);
  104.        }
  105.    return(1);
  106.    }
  107.  
  108. void dump(a)
  109. WORDLST *a;
  110. {
  111.    while (a != NULL)
  112.    {
  113.       fprintf(fout,"=%s\n",a->word); 
  114.       a = a->next;
  115.       }
  116.    }
  117.  
  118. int checkgroup(word)
  119. char *word;
  120. {
  121.    WORDLST *a;
  122.    WORDLST *b;
  123.  
  124.    /* earliest opportunity to check if it is a number after looking for */
  125.    /* hyphens */
  126.  
  127.    if (isnumber(word)) return(1);
  128.    if (checkword(word)) return(1);  /* try the dictionary first */
  129.    /* the word is not in the dictionary try to find root */
  130.  
  131.    root(word, &a); 
  132.    b = a;
  133.    while (a != NULL)
  134.    {
  135.       if (checkword(a->word)) 
  136.       {
  137.          if (prtderive)
  138.          {
  139.             if ((a->prefix != NULL) && (a->suffix != NULL)) fprintf(fout,"%s%s%s\n",
  140.                 a->prefix, a->word, a->suffix);
  141.             else if (a->prefix != NULL) fprintf(fout,"%s%s\n",
  142.                 a->prefix, a->word);
  143.             else if (a->suffix != NULL) fprintf(fout,"%s%s\n",
  144.                 a->word, a->suffix);
  145.             }
  146.          if (prtroot) 
  147.             dump(b);
  148.          destroy(b);
  149.          return(1);  /* found it! */
  150.          }
  151.       a = a->next;
  152.       }
  153.    destroy(b);
  154.    return(0);
  155.    }
  156.  
  157. int checkhyphen(a)
  158. char *a;
  159. {
  160.    char *hold;
  161.    char *token;
  162.    char cpstr[MAXSTR];
  163.  
  164.    if (checkgroup(a)) return(1);
  165.    strcpy(cpstr, a);
  166.    token = strltok(cpstr,"-",&hold);
  167.    while (token != NULL)
  168.    {
  169.        if (!checkgroup(token)) return(0);
  170.        token = strltok(NULL,"-",&hold);
  171.        } 
  172.    return(1);
  173.    }
  174.  
  175. void checkspell(f)
  176. FILE *f;
  177. {
  178.    char instr[MAXSTR];
  179.    char *token;
  180.    char *hold;
  181.  
  182.    while (!feof(f))
  183.    {
  184.        if (fgets(instr,MAXSTR-1,f)==NULL)
  185.           break;
  186.        token = strltok(instr,SEPSTR,&hold); 
  187.        while (token != NULL)
  188.        {
  189.            if (!checkhyphen(token))
  190.            {
  191.                fprintf(fout,"%s\n",token);
  192.                }
  193.            token = strltok(NULL,SEPSTR,&hold); 
  194.            }
  195.        }
  196.    }
  197.