home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / m_emacs / spellsrc / cdict.c next >
Encoding:
C/C++ Source or Header  |  1987-07-22  |  5.1 KB  |  243 lines

  1. /*    CDICT:    Compress Dictionary utility program for
  2.         MicroSPELL 1.01
  3.  
  4.         (C)opyright May 1987 by Daniel Lawrence
  5.         All Rights Reserved
  6. */
  7.  
  8. #include    <stdio.h>
  9. #include    "dopt.h"
  10. #include    "dsfx.h"
  11.  
  12. /* globals */
  13.  
  14. char mdfile[NSTRING];        /* main dictionary text file name */
  15. char mcfile[NSTRING];        /* compressed dictionary file name */
  16. FILE *mdptr = NULL;        /* main dictionary file pointer */
  17. FILE *mcptr = NULL;        /* compressed dictionary file pointer */
  18. int sflen[NSUFFIX];        /* length of suffixes */
  19.  
  20. main(argc, argv)
  21.  
  22. int argc;    /* # of command line arguments */
  23. char **argv;    /* text of command line arguments */
  24.  
  25. {
  26.     register char *word;        /* current word */
  27.     register int suffix;        /* suffix index */
  28.     char lastword[NSTRING];        /* previous word in dictionary */
  29.     char tempword[NSTRING];        /* temporary word in dictionary */
  30.     char *nxtmword();
  31.  
  32.     printf("CDICT Dictionary Compression Utility for MicroSPELL v1.00\n");
  33.  
  34.     if (argc != 3) {
  35.         help();
  36.         exit(EXBADOPT);
  37.     }
  38.  
  39.     strcpy(mdfile, argv[1]);
  40.     strcpy(mcfile, argv[2]);
  41.  
  42.     if (mopen() != TRUE) {
  43.         printf("%%Can not open text dictionary file\n");
  44.         exit(EXMDICT);
  45.     }
  46.  
  47.     /* open the output compressed dictionary file */
  48.     mcptr = fopen(mcfile, "w");
  49.     if (mcptr == NULL) {
  50.         printf("%%Can not open compressed dictionary output file\n");
  51.         exit(EXMDICT);
  52.     }
  53.  
  54.     /* prepare the suffix length table */
  55.     for (suffix = 0; suffix < NSUFFIX; suffix++)
  56.         sflen[suffix] = strlen(sfx[suffix]);
  57.  
  58.     printf("[Compressing %s => %s]\n", mdfile, mcfile);
  59.     lastword[0] = 0;    /* null last word */
  60.  
  61.     /* scan the dictionary, compressing */
  62.     word = nxtmword();
  63.     while (word) {
  64.         strcpy(tempword, word);
  65.         cmpsword(lastword, word);
  66.         strcpy(lastword, tempword);
  67.         word = nxtmword();
  68.     }
  69.  
  70.     /* close things up */
  71.     mclose();
  72.     fclose(mcptr);
  73.     printf("[Dictionary Compressed]\n");
  74. }
  75.  
  76. help()        /* tell us about cdict... */
  77.  
  78. {
  79.     printf("\nUsage:\n\n");
  80.     printf("    CDICT <text dictionary> <compressed dictionary>\n");
  81. }
  82.  
  83. mopen()        /* open the main dicionary */
  84.  
  85. {
  86.     /* if it is already open, close it down */
  87.     if (mdptr != NULL)
  88.         fclose(mdptr);
  89.  
  90.     /* open up the text dictionary... */
  91.     if ((mdptr = fopen(mdfile, "r")) == NULL)
  92.         return(FALSE);
  93.  
  94.     return(TRUE);
  95. }
  96.  
  97. mclose()    /* close the dictionary down */
  98.  
  99. {
  100.     /* if it is already open, close it down */
  101.     if (mdptr != NULL)
  102.         fclose(mdptr);
  103.     mdptr = NULL;
  104. }
  105.  
  106. char *nxtmword()    /* get the next word from the main dictionary */
  107.  
  108. {
  109.     static char word[NSTRING];    /* word to return */
  110.     char *fgets();
  111.  
  112.     /* is it already closed? */
  113.     if (mdptr == NULL)
  114.         return(NULL);
  115.  
  116.     /* get the next word */
  117.     if (fgets(word, NSTRING - 1, mdptr) == NULL) {
  118.         /* no more left!!!! close out */
  119.         fclose(mdptr);
  120.         mdptr = NULL;
  121.         return(NULL);
  122.     }
  123.  
  124.     /* all's well, dump the return, any trailing spaces and
  125.        return the word */
  126.     do
  127.         word[strlen(word) - 1] = 0;
  128.     while (word[strlen(word) - 1] == ' ');
  129.     return(word);
  130. }
  131.  
  132. cmpsword(lastword, word)    /* compress the given word */
  133.  
  134. char *lastword;        /* previous dictionary word */
  135. char *word;        /* current dictionary word */
  136.  
  137. {
  138.     register int index;    /* index into current word */
  139.     register int same;    /* # of same characters */
  140.     register int suffix;    /* suffix code */
  141.     register int wlen;    /* length of current word */
  142.  
  143.     /* scan for common suffixes */
  144.     wlen = strlen(word);
  145.     for (suffix = 0; suffix < NSUFFIX; suffix++) {
  146.         if (wlen < sflen[suffix])
  147.             continue;
  148.         if (strcmp(&word[wlen - sflen[suffix]], sfx[suffix]) == 0) {
  149.             word[wlen - sflen[suffix]] = 0;    /* trunc it */
  150.             break;
  151.         }
  152.     }
  153.  
  154.     /* If there is no suffix...suffix ends up as NSUFFIX */
  155.  
  156.     /* scan for like beginning characters */
  157.     index = 0;
  158.     while (lastword[index] && lastword[index] == word[index])
  159.         index++;
  160.  
  161.     same = index;
  162. #if    ASCII
  163.     suffix |= 128;
  164. #endif
  165.     fprintf(mcptr, "%c%s%c", 'A'+same, &word[index], suffix);
  166. }
  167.  
  168. #if    AZTEC & MSDOS
  169. #undef    fgetc
  170. #undef    fgets
  171. /*    a1gets:        Get an ascii string from a file using a1getc    */
  172.  
  173. char *a1gets(buffer, length, fp)
  174.  
  175. char *buffer;    /* buffer to leave string in */
  176. int length;    /* maximum length of string */
  177. FILE *fp;    /* file to get string from */
  178.  
  179. {
  180.     register int c;        /* current character read */
  181.     register char *bp;    /* pointer into buffer */
  182.  
  183.     bp = buffer;
  184.  
  185.     while ((c = a1getc(fp))    != EOF) {
  186.         *bp++ = (char)c;
  187.         if (c == '\n')
  188.             break;
  189.     }
  190.  
  191.     *bp = 0;
  192.     if (c == EOF)
  193.         return(NULL);
  194.     else
  195.         return(buffer);
  196. }
  197.  
  198. /*    a1getc:        Get an ascii char from the file input stream
  199.             but DO NOT strip the high bit
  200. */
  201.  
  202. int a1getc(fp)
  203.  
  204. FILE *fp;
  205.  
  206. {
  207.     int c;        /* translated character */
  208.  
  209.     c = getc(fp);    /* get the character */
  210.  
  211.     /* if its a <LF> char, throw it out  */
  212.     while (c == 10)
  213.         c = getc(fp);
  214.  
  215.     /* if its a <RETURN> char, change it to a LF */
  216.     if (c == '\r')
  217.         c = '\n';
  218.  
  219.     /* if its a ^Z, its an EOF */
  220.     if (c == 26)
  221.         c = EOF;
  222.  
  223.     return(c);
  224. }
  225. #endif
  226.  
  227. #if    CMS
  228. #undef    fopen
  229. /*    The IBM 30xx likes to tell us when file opens
  230.     fail...it's too chatty....I like to handle these myself    */
  231.  
  232. FILE *cmsopen(file, mode)
  233.  
  234. char *file;    /* name of file to open */
  235. char *mode;    /* mode to open it in */
  236.  
  237. {
  238.     quiet(1);
  239.     return(fopen(file,mode));
  240. }
  241. #endif
  242.  
  243.