home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / DECOMP.C < prev    next >
C/C++ Source or Header  |  1994-05-02  |  3KB  |  112 lines

  1. /* ------------------- decomp.c -------------------- */
  2.  
  3. /*
  4.  * Decompress the application.HLP file
  5.  * or load the application.TXT file if the .HLP file
  6.  * does not exist
  7.  */
  8.  
  9. #include <assert.h>
  10. #include "dflat.h"
  11. #include "htree.h"
  12.  
  13. static int in8;
  14. static int ct8 = 8;
  15. static FILE *fi;
  16. static BYTECOUNTER bytectr;
  17. struct htr *HelpTree;
  18. static int root;
  19.  
  20. /* ------- open the help database file -------- */
  21. FILE *OpenHelpFile(const char *fn, const char *md)
  22. {
  23.     char *cp;
  24.     int treect, i;
  25.     char helpname[65];
  26.  
  27.     /* -------- get the name of the help file ---------- */
  28.     BuildFileName(helpname, fn, ".hlp");
  29.     if ((fi = fopen(helpname, md)) == NULL)
  30.         return NULL;
  31.     if (HelpTree == NULL)    {
  32.         /* ----- read the byte count ------ */
  33.         fread(&bytectr, sizeof bytectr, 1, fi);
  34.         /* ----- read the frequency count ------ */
  35.         fread(&treect, sizeof treect, 1, fi);
  36.         /* ----- read the root offset ------ */
  37.         fread(&root, sizeof root, 1, fi);
  38.         HelpTree = calloc(treect-256, sizeof(struct htr));
  39.         if (HelpTree != NULL)    {
  40.             /* ---- read in the tree --- */
  41.             for (i = 0; i < treect-256; i++)    {
  42.                 fread(&HelpTree[i].left,  sizeof(int), 1, fi);
  43.                 fread(&HelpTree[i].right, sizeof(int), 1, fi);
  44.             }
  45.         }
  46.     }
  47.     return fi;
  48. }
  49.  
  50. /* ----- read a line of text from the help database ----- */
  51. void *GetHelpLine(char *line)
  52. {
  53.     int h;
  54.     *line = '\0';
  55.     while (TRUE)    {
  56.         /* ----- decompress a line from the file ------ */
  57.         h = root;
  58.         /* ----- walk the Huffman tree ----- */
  59.         while (h > 255)    {
  60.             /* --- h is a node pointer --- */
  61.             if (ct8 == 8)   {
  62.                 /* --- read 8 bits of compressed data --- */
  63.                 if ((in8 = fgetc(fi)) == EOF)    {
  64.                     *line = '\0';
  65.                     return NULL;
  66.                 }
  67.                 ct8 = 0;
  68.             }
  69.             /* -- point to left or right node based on msb -- */
  70.             if (in8 & 0x80)
  71.                 h = HelpTree[h-256].left;
  72.             else
  73.                 h = HelpTree[h-256].right;
  74.             /* --- shift the next bit in --- */
  75.             in8 <<= 1;
  76.             ct8++;
  77.         }
  78.         /* --- h < 255 = decompressed character --- */
  79.         if (h == '\r')
  80.             continue;    /* skip the '\r' character */
  81.         /* --- put the character in the buffer --- */
  82.         *line++ = h;
  83.         /* --- if '\n', end of line --- */
  84.         if (h == '\n')
  85.             break;
  86.     }
  87.     *line = '\0';    /* null-terminate the line */
  88.     return line;
  89. }
  90.  
  91. /* --- compute the database file byte and bit position --- */
  92. void HelpFilePosition(long *offset, int *bit)
  93. {
  94.     *offset = ftell(fi);
  95.     if (ct8 < 8)
  96.         --*offset;
  97.     *bit = ct8;
  98. }
  99.  
  100. /* -- position the database to the specified byte and bit -- */
  101. void SeekHelpLine(long offset, int bit)
  102. {
  103.     int fs = fseek(fi, offset, 0);
  104.     assert(fs == 0);
  105.     ct8 = bit;
  106.     if (ct8 < 8)    {
  107.         in8 = fgetc(fi);
  108.         in8 <<= bit;
  109.     }
  110. }
  111.  
  112.