home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / UTILS / SQUEEZE / TR1.C < prev    next >
Text File  |  2000-06-30  |  1KB  |  64 lines

  1. #include <stdio.h>
  2. #include "sqcom.h"
  3. #include "sq.h"
  4. #define ERROR -1
  5. #define TRUE 1
  6. #define FALSE 0
  7.  
  8. /* First translation - encoding of repeated characters
  9.  * The code is byte for byte pass through except that
  10.  * DLE is encoded as DLE, zero and repeated byte values
  11.  * are encoded as value, DLE, count for count >= 3.
  12.  */
  13.  
  14. init_ncr()    /*initialize getcnr() */
  15. {
  16.     state = NOHIST;
  17. }
  18.  
  19. int
  20. getcnr(iob)
  21. FILE *iob;
  22. {
  23.     switch(state) {
  24.     case NOHIST:
  25.         /* No relevant history */
  26.         state = SENTCHAR;
  27.         return lastchar = getc_crc(iob);   
  28.     case SENTCHAR:
  29.         /* Lastchar is set, need lookahead */
  30.         switch(lastchar) {
  31.         case DLE:
  32.             state = NOHIST;
  33.             return 0;    /* indicates DLE was the data */
  34.         case EOF:
  35.             return EOF;
  36.         default:
  37.             for(likect = 1; (newchar = getc_crc(iob)) == lastchar && likect < 255; ++likect)
  38.                 ;
  39.             switch(likect) {
  40.             case 1:
  41.                 return lastchar = newchar;
  42.             case 2:
  43.                 /* just pass through */
  44.                 state = SENDNEWC;
  45.                 return lastchar;
  46.             default:
  47.                 state = SENDCNT;
  48.                 return DLE;
  49.             }
  50.         }
  51.     case SENDNEWC:
  52.         /* Previous sequence complete, newchar set */
  53.         state = SENTCHAR;
  54.         return lastchar = newchar;
  55.     case SENDCNT:
  56.         /* Sent DLE for repeat sequence, send count */
  57.         state = SENDNEWC;
  58.         return likect;
  59.     default:
  60.         puts("Bug - bad state\n");
  61.         exit(1);
  62.     }
  63. }
  64.