home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / US20SRC.ZIP / MDICT.C < prev    next >
C/C++ Source or Header  |  1992-06-26  |  3KB  |  154 lines

  1. /*    MDICT:    Main Dictionary processing for MicroSPELL 2.0
  2.         Spell Checker and Corrector
  3.  
  4.         (C)opyright May 1987,1992 by Daniel Lawrence
  5.         All Rights Reserved
  6. */
  7.  
  8. #include    <stdio.h>
  9. #include    "dopt.h"
  10. #include    "dstruct.h"
  11. #include    "ddef.h"
  12. #include    "dsfx.h"
  13.  
  14. /* compression function globals */
  15. char lastword[NSTRING];        /* last word from dictionary */
  16.  
  17. mopen()        /* open the main dicionary */
  18.  
  19. {
  20.  
  21.     /* if it is already open, close it down */
  22.     if (mdptr != NULL)
  23.         fclose(mdptr);
  24.  
  25.     /* open up the main dictionary... */
  26.     if ((mdptr = popen(mdfile, "rb")) == NULL) {
  27.         printf("%%Can not find main dictionary\n");
  28.         return(FALSE);
  29.     }
  30.  
  31.     /* and read the letter offset table */
  32.     fread((char *)&(letter_offset[0]), sizeof(long), ALPHASIZE, mdptr);
  33.  
  34.     return(TRUE);
  35. }
  36.  
  37. mclose()    /* close the dictionary down */
  38.  
  39. {
  40.     /* if it is already open, close it down */
  41.     if (mdptr != NULL)
  42.         fclose(mdptr);
  43.     mdptr = NULL;
  44. }
  45.  
  46. char *lastmword()    /* get the last word gotten again! */
  47.  
  48. {
  49.     return(lastword);
  50. }
  51.  
  52. char *nxtmword()    /* get the next word from the main dictionary */
  53.  
  54. {
  55.     static char word[NSTRING];    /* word to return */
  56.     char *gcword();
  57.  
  58.     /* is it already closed? */
  59.     if (mdptr == NULL)
  60.         return(hivalue);
  61.  
  62.     /* get the next word */
  63.     if (gcword(word) == NULL) {
  64.  
  65.         /* no more left!!!! close out */
  66.         fclose(mdptr);
  67.         mdptr = NULL;
  68.         return(hivalue);
  69.     }
  70.  
  71.     /* all's well, dump the return and return the word */
  72.     return(word);
  73. }
  74.  
  75. char *skipdict(first_char)    /* position us at the first word with this letter */
  76.  
  77. char first_char;
  78.  
  79. {
  80.     register int offset;    /* index into letter offset table */
  81.  
  82.     /* is it already closed? */
  83.     if (mdptr == NULL)
  84.         return(hivalue);
  85.  
  86.     /* lookup the correct offset */
  87.     offset = lcase[first_char] - 'a';
  88.  
  89.     /* position us at the right offset in the file */
  90.     fseek(mdptr, letter_offset[offset], 0);
  91.  
  92.     strcpy(lastword, "");
  93. }
  94.  
  95. char *gcword(word)    /* get the next compressed word */
  96.  
  97. char *word;    /* buffer to place word in */
  98.  
  99. {
  100.     register int c;        /* current character from file */
  101.     register char *wptr;    /* ptr into word to return */
  102.     register char *lptr;    /* pointer int last word */
  103.     int same;        /* # of common characters from last word */
  104.     int suffix;        /* index of common suffix */
  105.     int fgetc();
  106.     int upper_flag;        /* is this word upper case? */
  107.  
  108.     /* first grab the #same count */
  109.     same = fgetc(mdptr);
  110.     if (same == EOF)    /* at EOF... return NULL */
  111.         return(NULL);
  112.     upper_flag = FALSE;
  113.     if (same > 127) {
  114.         upper_flag = TRUE;
  115.         same |= 127;
  116.     }
  117.     same -= 'A';
  118.  
  119.     /* and copy things across */
  120.     wptr = word;
  121.     lastword[0] = lcase[lastword[0]];
  122.     lptr = lastword;
  123.     while (same-- > 0)
  124.         *wptr++ = *lptr++;
  125.  
  126.     c = fgetc(mdptr);
  127. #if     EBCDIC
  128.     /* opposite high bit flags */
  129.         while (c > 128) {
  130. #else
  131.         while (c < 128) {
  132. #endif
  133.         *wptr++ = c;
  134.         c = fgetc(mdptr);
  135.     }
  136.  
  137.     *wptr = 0;
  138.  
  139.     /* calculate the suffix to add... */
  140.     suffix = c & 127;
  141.     if (suffix != NSUFFIX)
  142.         strcpy(wptr, sfx[suffix]);
  143.  
  144.     /* upper cased? */
  145.     if (upper_flag)
  146.         word[0] += 'A' - 'a';
  147.  
  148.     /* save the current uncompressed word */
  149.     strcpy(lastword, word);
  150.  
  151.     /* and return the word */
  152.     return(word);
  153. }
  154.