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

  1. /* rle8comp.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. #include <dir.h>    /* for fnsplit() */
  16.  
  17.  
  18.  
  19.  
  20.  
  21. #include "rle8.h"
  22.  
  23.  
  24.  
  25. int main(int argc, char **argv)
  26.  
  27. {
  28.  
  29.     register int cur_char;                /* a character                    */
  30.  
  31.     register unsigned int i;              /* generic index variable         */
  32.  
  33.     register unsigned short run_len = 0;  /* length of character run so far */
  34.  
  35.     int run_char;                         /* which char run is of           */
  36.  
  37.     unsigned int j;                       /* another index variable         */
  38.  
  39.     unsigned short seq_len=0;             /* length of non-run sequence     */
  40.  
  41.  
  42.  
  43.     char scratch_space[256];              /* string scratch space           */
  44.  
  45.     char seq[MAX_LEN];                    /* buffer for uncompressible data */
  46.  
  47.  
  48.  
  49.     char orig_name[MAXFILE+MAXEXT];       /* original filename      */
  50.  
  51.     char drive[MAXDRIVE],                 /* needed for fnsplit()   */
  52.  
  53.          dir[MAXDIR],                     /* needed for fnsplit()   */
  54.  
  55.          filename[MAXFILE],               /* 8 chars, orig name     */
  56.  
  57.          ext[MAXEXT];                     /* 3 chars, orig ext      */
  58.  
  59.     char outfile_name[MAXFILE+MAXEXT];    /* output filename        */
  60.  
  61.  
  62.  
  63.     unsigned long  orig_total;              /* total number of unencoded bytes          */
  64.  
  65.     unsigned long  rle_total;               /* total number of encoded bytes            */
  66.  
  67.     FILE *infile;                           /* file ptr to input file (uncompressed)    */
  68.  
  69.     FILE *outfile;                          /* file ptr to output file (compressed)     */
  70.  
  71.     char *infile_name;
  72.  
  73.  
  74.  
  75.     fnsplit(argv[1], drive, dir, filename, ext);    /* get filename    */
  76.  
  77.     strcpy(orig_name, filename);
  78.  
  79.     strcat(orig_name, ext);
  80.  
  81.     strcpy(outfile_name, filename);       /* build output filename  */
  82.  
  83.     strcat(outfile_name, ".r8");
  84.  
  85.  
  86.  
  87.     if (argc != 2)
  88.  
  89.     {
  90.  
  91.         puts("Usage: RLE8 filename.ext compresses file to filename.r8");
  92.  
  93.         return 1;
  94.  
  95.     }
  96.  
  97.  
  98.  
  99.     puts("rle8  by Shaun Case 1991  public domain.");
  100.  
  101.  
  102.  
  103.     infile_name=argv[1];
  104.  
  105.  
  106.  
  107.     if ((infile=fopen(infile_name, "rb")) == NULL)
  108.  
  109.     {
  110.  
  111.         strcpy(scratch_space, "Uable to open ");
  112.  
  113.         strcat(scratch_space, infile_name);
  114.  
  115.         puts(scratch_space);
  116.  
  117.         return 1;
  118.  
  119.     }
  120.  
  121.     
  122.  
  123.     if ((outfile=fopen(outfile_name, "wb")) == NULL)
  124.  
  125.     {
  126.  
  127.         strcpy(scratch_space, "Uable to open ");
  128.  
  129.         strcat(scratch_space, outfile_name);
  130.  
  131.         puts(scratch_space);
  132.  
  133.         return 1;
  134.  
  135.     }
  136.  
  137.  
  138.  
  139.     for (i = 0; i < 13; i++)            /* write original filename */
  140.  
  141.         fputc(orig_name[i], outfile);
  142.  
  143.  
  144.  
  145.     while (!feof(infile))
  146.  
  147.     {
  148.  
  149.         cur_char = fgetc(infile);
  150.  
  151.  
  152.  
  153.         if (feof(infile))
  154.  
  155.             continue;
  156.  
  157.  
  158.  
  159.  
  160.  
  161.         if (seq_len ==0)                /* haven't got a sequence yet   */
  162.  
  163.         {
  164.  
  165.             if (run_len == 0)           /* start a new run              */
  166.  
  167.             {
  168.  
  169.                 run_char = cur_char;
  170.  
  171.                 ++run_len;
  172.  
  173.                 continue;
  174.  
  175.             }
  176.  
  177.  
  178.  
  179.             if (run_char == cur_char)   /* got another char in the run  */
  180.  
  181.                 if (++run_len == MAX_LEN)
  182.  
  183.                 {
  184.  
  185.                     fputc((int)MAX_RUN_HEADER, outfile);
  186.  
  187.                     fputc((int) run_char, outfile);
  188.  
  189.  
  190.  
  191.                     run_len = 0;
  192.  
  193.                     continue;
  194.  
  195.                 }
  196.  
  197.  
  198.  
  199.                                    /* got a different character     */
  200.  
  201.                                    /* than the run we were building */
  202.  
  203.             if (run_len > 2)       /* so write out the run and      */
  204.  
  205.                                    /* start a new one of the new    */
  206.  
  207.                                    /* character.                    */
  208.  
  209.             {
  210.  
  211.                 fputc((int)(RUN | run_len), outfile);
  212.  
  213.                 fputc((int)run_char, outfile);
  214.  
  215.  
  216.  
  217.                 run_len = 1;
  218.  
  219.                 run_char   = cur_char;
  220.  
  221.                 continue;
  222.  
  223.             }
  224.  
  225.  
  226.  
  227.             /* run was only one or two chars, make a seq out of it instead       */
  228.  
  229.  
  230.  
  231.             for (j = 0; j < run_len; j++);    /* copy 1 or 2 char run to seq[]   */
  232.  
  233.             {
  234.  
  235.                 seq[seq_len] = run_char;
  236.  
  237.                 ++seq_len;
  238.  
  239.                 if (seq_len == MAX_LEN)       /* if seq[] is full, write to disk */
  240.  
  241.                 {
  242.  
  243.                     fputc((int)MAX_SEQ_HEADER, outfile);
  244.  
  245.  
  246.  
  247.                     for (i = 0; i < seq_len; i++)
  248.  
  249.                         fputc((int)seq[i], outfile);
  250.  
  251.  
  252.  
  253.                     seq_len = 0;
  254.  
  255.                 }
  256.  
  257.             }
  258.  
  259.  
  260.  
  261.             run_len = 0;
  262.  
  263.  
  264.  
  265.             seq[seq_len++] = cur_char;
  266.  
  267.             if (seq_len == MAX_LEN)        /* if seq[] is full, write to disk */
  268.  
  269.             {
  270.  
  271.                 fputc((int)MAX_SEQ_HEADER, outfile);
  272.  
  273.  
  274.  
  275.                 for (i = 0; i < seq_len; i++)
  276.  
  277.                     fputc((int)seq[i], outfile);
  278.  
  279.  
  280.  
  281.                 seq_len = 0;
  282.  
  283.             }
  284.  
  285.         }
  286.  
  287.         else    /* a sequence exists */
  288.  
  289.         {
  290.  
  291.             if (run_len != 0)           /* if a run exists */
  292.  
  293.             {
  294.  
  295.                 if (cur_char == run_char )  /* add to run!  Yay.  */
  296.  
  297.                 {
  298.  
  299.                     ++run_len;
  300.  
  301.                     if (run_len == MAX_LEN)  /* if run is full */
  302.  
  303.                     {
  304.  
  305.                         /* write sequence that precedes run */
  306.  
  307.  
  308.  
  309.                         fputc((int)(SEQ | seq_len), outfile);
  310.  
  311.  
  312.  
  313.                         for (i = 0; i < seq_len; i++)
  314.  
  315.                             fputc((int)seq[i], outfile);
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                         /* write run                        */
  322.  
  323.  
  324.  
  325.                         fputc((int)(RUN | run_len), outfile);
  326.  
  327.                         fputc((int)run_char, outfile);
  328.  
  329.  
  330.  
  331.                         /* and start out fresh              */
  332.  
  333.                         seq_len = run_len = 0;
  334.  
  335.  
  336.  
  337.                     }  /* end write full run with existing sequence */
  338.  
  339.  
  340.  
  341.                     continue;
  342.  
  343.  
  344.  
  345.                 }  /* end add to run for sequence exists */
  346.  
  347.  
  348.  
  349.                 /* we couldn't add to the run, and a preceding sequence */
  350.  
  351.                 /* exists, so write the sequence and the run, and       */
  352.  
  353.                 /* try starting a new run with the current character.   */
  354.  
  355.  
  356.  
  357.                 /* write sequence that precedes run */
  358.  
  359.  
  360.  
  361.                 fputc((int)(SEQ | seq_len), outfile);
  362.  
  363.  
  364.  
  365.                 for (i = 0; i < seq_len; i++)
  366.  
  367.                     fputc((int)seq[i], outfile);
  368.  
  369.  
  370.  
  371.  
  372.  
  373.                 /* write run                        */
  374.  
  375.  
  376.  
  377.                 fputc((int)(RUN | run_len), outfile);
  378.  
  379.                 fputc((int)run_char, outfile);
  380.  
  381.  
  382.  
  383.                 /* and start a new run w/ cur_char  */
  384.  
  385.                 seq_len = 0;
  386.  
  387.                 run_len = 1;
  388.  
  389.                 run_char = cur_char;
  390.  
  391.  
  392.  
  393.                 continue;
  394.  
  395.  
  396.  
  397.             }    /* end can't add to existing run, and preceding seq exists */
  398.  
  399.  
  400.  
  401.             /* no run exists, but a sequences does.  Try to create a run    */
  402.  
  403.             /* by looking at cur_char and the last char of the sequence.    */
  404.  
  405.             /* if that fails, add the char to the sequence.                 */
  406.  
  407.             /* if the sequence is full, write it to disk.  (Slightly non    */
  408.  
  409.             /* optimal; we could wait one more char.  A small thing to fix  */
  410.  
  411.             /* if someone gets the urge...                                  */
  412.  
  413.  
  414.  
  415.             if (seq[seq_len - 1] == cur_char)       /* if we can make a run */
  416.  
  417.             {
  418.  
  419.                 run_char = cur_char;
  420.  
  421.                 run_len = 2;
  422.  
  423.                 --seq_len;
  424.  
  425.                 continue;
  426.  
  427.             }
  428.  
  429.  
  430.  
  431.             /* couldn't make a run, add char to seq.  Maybe next time       */
  432.  
  433.             /* around...                                                    */
  434.  
  435.  
  436.  
  437.             seq[seq_len++] = cur_char;
  438.  
  439.  
  440.  
  441.             if (seq_len == MAX_LEN) /* if the sequence is full, write out   */
  442.  
  443.             {
  444.  
  445.                 fputc((int)MAX_SEQ_HEADER, outfile);
  446.  
  447.  
  448.  
  449.                 for (i = 0; i < MAX_LEN; i++)
  450.  
  451.                     fputc((int)seq[i], outfile);
  452.  
  453.  
  454.  
  455.                 seq_len = 0;
  456.  
  457.             }
  458.  
  459.  
  460.  
  461.         }  /* end branch on sequence exists */
  462.  
  463.  
  464.  
  465.     } /* done with whole file */
  466.  
  467.  
  468.  
  469.     /* there may be stuff left that hasn't been written yet; if so, write it */
  470.  
  471.  
  472.  
  473.     if (seq_len != 0)  /* write sequence that precedes run */
  474.  
  475.     {
  476.  
  477.         fputc((int)(SEQ | seq_len), outfile);
  478.  
  479.  
  480.  
  481.         for (i = 0; i < seq_len; i++)
  482.  
  483.             fputc((int)seq[i], outfile);
  484.  
  485.     }
  486.  
  487.  
  488.  
  489.     if (run_len != 0)  /* write run */
  490.  
  491.     {
  492.  
  493.         fputc((int)(RUN | run_len), outfile);
  494.  
  495.         fputc((int)run_char, outfile);
  496.  
  497.     }
  498.  
  499.  
  500.  
  501.     fclose(infile);
  502.  
  503.     fclose (outfile);
  504.  
  505.  
  506.  
  507.     return 0;
  508.  
  509.  
  510.  
  511. }  /* end main() */
  512.  
  513.  
  514.  
  515.