home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / COMPRESS / RLE8_SC.ZIP / UNRLE8.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-14  |  2.6 KB  |  100 lines

  1. /* unrle8.c                            */
  2.  
  3. /* RLE compression, uses 8 bit headers */
  4.  
  5. /* by Shaun Case 1991 Borland C++ 2.0  */
  6.  
  7. /* Public Domain                       */
  8.  
  9.  
  10.  
  11. #include <stdio.h>
  12.  
  13. #include <string.h>
  14.  
  15.  
  16.  
  17. #include "rle8.h"
  18.  
  19.  
  20.  
  21. int main(int argc, char **argv)
  22.  
  23. {
  24.  
  25.     register int byte;
  26.  
  27.     register unsigned short i;
  28.  
  29.     register unsigned short length;
  30.  
  31.     int packet_hdr;
  32.  
  33.     char orig_filename[14]; /* original filename */
  34.  
  35.     char *infile_name;
  36.  
  37.     char scratch_space[134];
  38.  
  39.  
  40.  
  41.  
  42.  
  43.     FILE *infile, *outfile;
  44.  
  45.  
  46.  
  47.     if (argc != 2)
  48.  
  49.     {
  50.  
  51.         puts("Usege: unrle8 filename");
  52.  
  53.         return 1;
  54.  
  55.     }
  56.  
  57.     puts("unlre8   by Shaun Case 1991  public domain");
  58.  
  59.  
  60.  
  61.     infile_name = argv[1];
  62.  
  63.  
  64.  
  65.     if ((infile=fopen(infile_name, "rb")) == NULL)
  66.  
  67.     {
  68.  
  69.         strcpy(scratch_space, "Unable to open ");
  70.  
  71.         strcat(scratch_space, infile_name);
  72.  
  73.         puts(scratch_space);
  74.  
  75.         return 1;
  76.  
  77.     }
  78.  
  79.  
  80.  
  81.     for (i = 0; i < 13; i++)   /* get original filename */
  82.  
  83.         if ((orig_filename[i] = fgetc(infile)) == EOF)
  84.  
  85.         {
  86.  
  87.             puts("Error reading original filename from input file.");
  88.  
  89.             return 1;
  90.  
  91.         }
  92.  
  93.  
  94.  
  95.     if ((outfile=fopen(orig_filename, "wb")) == NULL)
  96.  
  97.     {
  98.  
  99.         strcpy(scratch_space, "Unable to open ");
  100.  
  101.         strcat(scratch_space, orig_filename);
  102.  
  103.         puts(scratch_space);
  104.  
  105.         return 1;
  106.  
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.     while (!feof(infile))
  114.  
  115.     {
  116.  
  117.         packet_hdr = fgetc(infile);
  118.  
  119.  
  120.  
  121.         if (feof(infile))
  122.  
  123.             continue;
  124.  
  125.  
  126.  
  127.         length = MAX_LEN & packet_hdr;
  128.  
  129.  
  130.  
  131.         if (packet_hdr & RUN)  /* if it's a run... */
  132.  
  133.         {
  134.  
  135.             byte = fgetc(infile);
  136.  
  137.  
  138.  
  139.             for (i = 0; i < length; i++)
  140.  
  141.                 if (fputc(byte, outfile)== EOF)
  142.  
  143.                 {
  144.  
  145.                     strcpy(scratch_space, "Error writing to ");
  146.  
  147.                     strcat(scratch_space, orig_filename);
  148.  
  149.                     puts(scratch_space);
  150.  
  151.                     fclose(infile);
  152.  
  153.                     fclose(outfile);
  154.  
  155.                     return 1;
  156.  
  157.                 }
  158.  
  159.         }
  160.  
  161.  
  162.  
  163.         else /* it's a sequence */
  164.  
  165.  
  166.  
  167.             for (i = 0; i < length; i++)
  168.  
  169.                 if (fputc(fgetc(infile), outfile)==EOF)
  170.  
  171.                 {
  172.  
  173.                     strcpy(scratch_space, "Error writing to ");
  174.  
  175.                     strcat(scratch_space, orig_filename);
  176.  
  177.                     puts(scratch_space);
  178.  
  179.                     fclose(infile);
  180.  
  181.                     fclose(outfile);
  182.  
  183.                     return 1;
  184.  
  185.                 }
  186.  
  187.     }
  188.  
  189.     fclose(infile);
  190.  
  191.     fclose(outfile);
  192.  
  193.     return 0;
  194.  
  195. }
  196.  
  197.  
  198.  
  199.