home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume4 / bog / word.c < prev   
C/C++ Source or Header  |  1988-03-09  |  3KB  |  192 lines

  1. /* vi: set tabstop=4 : */
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "bog.h"
  6.  
  7. #ifdef ATARI
  8. #include <stat.h>
  9. #include <osbind.h>
  10. #define malloc(x)       Malloc(x)
  11. #else
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #endif
  15.  
  16. static char *dictspace, *dictend;
  17. static char *sp;
  18.  
  19. static int first = 1, lastch = 0;
  20.  
  21. /*
  22.  * Return the next word in the compressed dictionary in 'buffer' or
  23.  * NULL on end-of-file
  24.  */
  25. char *
  26. nextword(fp)
  27. FILE *fp;
  28. {
  29.     register int ch, pcount;
  30.     register char *p;
  31.     static char buf[MAXWORDLEN + 1];
  32.     extern int wordlen;
  33.  
  34.     if (fp == (FILE *) NULL) {
  35.         if (sp == dictend)
  36.             return((char *) NULL);
  37.  
  38.         p = buf + (int) *sp++;
  39.  
  40.         /*
  41.          * The dictionary ends with a null byte
  42.          */
  43.         while (*sp >= 'a') {
  44.             if ((*p++ = *sp++) == 'q')
  45.                 *p++ = 'u';
  46.         }
  47.     }
  48.     else {
  49.         if (first) {
  50.             if ((pcount = getc(fp)) == EOF)
  51.                 return((char *) NULL);
  52.             first = 0;
  53.         }
  54.         else if ((pcount = lastch) == EOF)
  55.             return((char *) NULL);
  56.  
  57.         p = buf + pcount;
  58.  
  59.         while ((ch = getc(fp)) != EOF && ch >= 'a') {
  60.             if ((*p++ = ch) == 'q')
  61.                 *p++ = 'u';
  62.         }
  63.         lastch = ch;
  64.     }
  65.     wordlen = (int) (p - buf);
  66.     *p = '\0';
  67.     return(buf);
  68. }
  69.  
  70. /*
  71.  * Reset the state of nextword() and do the fseek()
  72.  */
  73. dictseek(fp, offset, ptrname)
  74. FILE *fp;
  75. long offset;
  76. int ptrname;
  77. {
  78.  
  79.     if (fp == (FILE *) NULL) {
  80.         if ((sp = dictspace + offset) >= dictend)
  81.             return(-1);
  82.         return(0);
  83.     }
  84.  
  85.     first = 1;
  86.     return(fseek(fp, offset, ptrname));
  87. }
  88.  
  89. FILE *
  90. opendict(dict)
  91. char *dict;
  92. {
  93.     FILE *fp;
  94.  
  95. #ifdef ATARI
  96.     if ((fp = fopen(dict, "rb")) == (FILE *) NULL)
  97.         return((FILE *) NULL);
  98. #else
  99.     if ((fp = fopen(dict, "r")) == (FILE *) NULL)
  100.         return((FILE *) NULL);
  101. #endif
  102.     return(fp);
  103. }
  104.  
  105. /*
  106.  * Load the given dictionary and initialize the pointers
  107.  */
  108. loaddict(fp)
  109. FILE *fp;
  110. {
  111.     int st;
  112.     char *p;
  113.     long n;
  114.     struct stat statb;
  115.  
  116. #ifdef ATARI
  117.     if (stat(DICT, &statb) < 0) {
  118.         (void) fclose(fp);
  119.         return(-1);
  120.     }
  121. #else
  122.     char *malloc();
  123.  
  124.     if (fstat(fileno(fp), &statb) < 0) {
  125.         (void) fclose(fp);
  126.         return(-1);
  127.     }
  128. #endif
  129.  
  130.     /*
  131.      * An extra character (a sentinel) is allocated and set to null to improve
  132.      * the expansion loop in nextword()
  133.      */
  134.     if ((dictspace = (char *) malloc(statb.st_size + 1)) == (char *) NULL) {
  135.         (void) fclose(fp);
  136.         return(-1);
  137.     }
  138.     n = (long) statb.st_size;
  139.     sp = dictspace;
  140.     dictend = dictspace + n;
  141.  
  142.     p = dictspace;
  143.     while (n > 0 && (st = fread(p, 1, BUFSIZ, fp)) > 0) {
  144.         p += st;
  145.         n -= st;
  146.     }
  147.     if (st < 0) {
  148.         (void) fclose(fp);
  149.         (void) fprintf(stderr, "Error reading dictionary\n");
  150.         return(-1);
  151.     }
  152.     *p = '\0';
  153.     return(0);
  154. }
  155.  
  156. /*
  157.  * Dependent on the exact format of the index file:
  158.  * Starting offset field begins in column 1 and length field in column 9
  159.  */
  160. loadindex(indexfile)
  161. char *indexfile;
  162. {
  163.     register int i, j;
  164.     char buf[MAXWORDLEN + 1];
  165.     FILE *fp;
  166.     long atol();
  167.     extern struct dictindex dictindex[];
  168.  
  169.     if ((fp = fopen(indexfile, "r")) == (FILE *) NULL) {
  170.         (void) fprintf(stderr, "Can't open '%s'\n", indexfile);
  171.         return(-1);
  172.     }
  173.     i = 0;
  174.     while (fgets(buf, sizeof(buf), fp) != (char *) NULL) {
  175.         j = *buf - 'a';
  176.         if (i != j) {
  177.             (void) fprintf(stderr, "Bad index order\n");
  178.             return(-1);
  179.         }
  180.         dictindex[j].start = atol(buf + 1);
  181.         dictindex[j].length = atol(buf + 9) - dictindex[j].start;
  182.         i++;
  183.     }
  184.     if (i != 26) {
  185.         (void) fprintf(stderr, "Bad index length\n");
  186.         return(-1);
  187.     }
  188.     (void) fclose(fp);
  189.     return(0);
  190.  
  191.