home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / aufstols.zoo / aufstools.1 / binhex / run.c < prev   
C/C++ Source or Header  |  1991-02-26  |  1KB  |  60 lines

  1. /*
  2.  * do run length compression for unxbin
  3.  *
  4.  * David Gentzel, Lexeme Corporation
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #define RUNCHAR    0x90
  10. #define MAXREP    255
  11.  
  12. extern void putchar_6();
  13.  
  14. /*
  15.  * Output a character to the current output file generating run length
  16.  * compression on the fly.  All output goes through putchar_6 to do conversion
  17.  * from 8 bit to 6 bit format.  When c == EOF, call putchar_6 with EOF to flush
  18.  * pending output.
  19.  */
  20. void putchar_run(c)
  21. register int c;
  22. {
  23.     static unsigned int rep = 1;    /* # of repititions of lastc seen */
  24.     static int lastc = EOF;        /* last character passed to us */
  25.  
  26.     if (c == lastc)    /* increment rep */
  27.     {
  28.     /* repetition count limited to MAXREP */
  29.     if (++rep == MAXREP)
  30.     {
  31.         putchar_6(RUNCHAR);
  32.         putchar_6(MAXREP);
  33.         rep = 1;
  34.         lastc = EOF;
  35.     }
  36.     }
  37.     else
  38.     {
  39.     switch (rep)
  40.     {
  41.         case 2:    /* not worth running for only 2 reps... */
  42.         putchar_6(lastc);
  43.         if (lastc == RUNCHAR)
  44.             putchar_6(0);
  45.         break;
  46.         case 1:
  47.         break;
  48.         default:
  49.         putchar_6(RUNCHAR);
  50.         putchar_6(rep);
  51.         break;
  52.     }
  53.     putchar_6(c);    /* output character (EOF flushes) */
  54.     rep = 1;
  55.     if (c == RUNCHAR)
  56.         putchar_6(0);
  57.     lastc = c;
  58.     }
  59. }
  60.