home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 215 / DDJ9206.ZIP / DFLT12.ZIP / DECOMP.C < prev    next >
Text File  |  1992-03-26  |  2KB  |  120 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 "dflat.h"
  10. #include "htree.h"
  11.  
  12. static int in8;
  13. static int ct8 = 8;
  14.  
  15. static FILE *fi;
  16. static BYTECOUNTER bytectr;
  17.  
  18. static int LoadingASCII;
  19.  
  20. struct htr *HelpTree;
  21. static int root;
  22.  
  23. FILE *OpenHelpFile(void)
  24. {
  25.     char *cp;
  26.     int treect, i;
  27.     char helpname[65];
  28.  
  29.     BuildFileName(helpname, ".hlp");
  30.  
  31.     if ((fi = fopen(helpname, "rb")) == NULL)    {
  32.         if ((cp = strrchr(helpname, '.')) != NULL)    {
  33.             strcpy(cp, ".TXT");
  34.             fi = fopen(helpname, "rt");
  35.         }
  36.         if (fi == NULL)
  37.             return NULL;
  38.         LoadingASCII = TRUE;
  39.     }
  40.  
  41.     if (!LoadingASCII && HelpTree == NULL)    {
  42.            /* ----- read the byte count ------ */
  43.            fread(&bytectr, sizeof bytectr, 1, fi);
  44.            /* ----- read the frequency count ------ */
  45.            fread(&treect, sizeof treect, 1, fi);
  46.            /* ----- read the root offset ------ */
  47.            fread(&root, sizeof root, 1, fi);
  48.         HelpTree = DFcalloc(treect-256, sizeof(struct htr));
  49.         /* ---- read in the tree --- */
  50.         for (i = 0; i < treect-256; i++)    {
  51.                fread(&HelpTree[i].left,  sizeof(int), 1, fi);
  52.             fread(&HelpTree[i].right, sizeof(int), 1, fi);
  53.         }
  54.     }
  55.     return fi;
  56. }
  57.  
  58. void *GetHelpLine(char *line)
  59. {
  60.     int h;
  61.     if (LoadingASCII)
  62.         return fgets(line, 160, fi);
  63.     *line = '\0';
  64.     while (TRUE)    {
  65.         /* ----- decompress a line from the file ------ */
  66.         h = root;
  67.         /* ----- first get a character ----- */
  68.         while (h > 255)    {
  69.             if (ct8 == 8)   {
  70.                 if ((in8 = fgetc(fi)) == EOF)    {
  71.                     *line = '\0';
  72.                     return NULL;
  73.                 }
  74.                 ct8 = 0;
  75.             }
  76.             if (in8 & 0x80)
  77.                 h = HelpTree[h-256].left;
  78.             else
  79.                 h = HelpTree[h-256].right;
  80.             in8 <<= 1;
  81.             ct8++;
  82.         }
  83.         if (h == '\r')
  84.             continue;
  85.         *line++ = h;
  86.         if (h == '\n')
  87.             break;
  88.     }
  89.     *line = '\0';
  90.     return line;
  91. }
  92.  
  93. void HelpFilePosition(long *offset, int *bit)
  94. {
  95.     *offset = ftell(fi);
  96.     if (LoadingASCII)
  97.         *bit = 0;
  98.     else    {
  99.         if (ct8 < 8)
  100.             --*offset;
  101.         *bit = ct8;
  102.     }
  103. }
  104.  
  105. void SeekHelpLine(long offset, int bit)
  106. {
  107.     int i;
  108.     fseek(fi, offset, 0);
  109.     if (!LoadingASCII)    {
  110.         ct8 = bit;
  111.         if (ct8 < 8)    {
  112.             in8 = fgetc(fi);
  113.             for (i = 0; i < bit; i++)
  114.                 in8 <<= 1;
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120.