home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / VIEWERS / WAS062 / SPCPAK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-14  |  2.7 KB  |  96 lines

  1. /* This program is designed to run length encode spaces in a video memory image
  2.    file such as summary documentation files for WAS.COM.  Each series of
  3.    consecutive spaces will be represented by a byte value that can range from
  4.    128 to 255.  If the high bit is set in a code byte it represents a coded
  5.    series of spaces.  Attributes for coded spaces are not written to the code
  6.    file.  Spaces in a coded series will be given 7 as attribute when decoded.
  7.    The lower 7 bits of a spaces encoded byte indicate the number of spaces in
  8.    the series.  SPCPAK.C can be compiled with either Microsoft/Quick C or Turbo
  9.    C.
  10. */
  11.  
  12. #include <string.h>
  13. #include <io.h>
  14. #include <fcntl.h>
  15. #include <conio.h>
  16. #include <sys\types.h>
  17. #include <sys\stat.h>
  18.  
  19. #define OUTSIZ 1024
  20.  
  21. char outbuf[OUTSIZ];
  22.  
  23. void setcodebyte(int outhand, unsigned char outbyte, int setcode);
  24.  
  25. main(int argc, char **argv) {
  26.     unsigned char infile[64], outfile[64], ch, inpline[160], priorspaces = 0;
  27.     int inhand, outhand, i;
  28.  
  29.     if (argc < 2) {
  30.         printf("\n  Input file:   ");
  31.         gets(infile);
  32.     } else strcpy(infile, argv[1]);
  33.  
  34.     if (argc < 3) {
  35.         if (argc == 2) printf("\n");
  36.         printf("  Output file:  ");
  37.         gets(outfile);
  38.     } else strcpy(outfile, argv[2]);
  39.  
  40.     inhand = open(infile, O_RDONLY | O_BINARY);
  41.     if (inhand == -1) {
  42.         printf("  Can't open file %s\n", infile);
  43.         exit(1);
  44.     }
  45.     if (!access(outfile, 0)) {
  46.         printf("\nOutput file %s exists.  Overwrite (Y/N)? ", outfile);
  47.         while ((ch = 0xdf & getch()) && ch != 'N' && ch != 'Y');
  48.         printf("%c", ch);
  49.         if (ch == 'N') exit(0);
  50.     }
  51.     outhand = open(outfile, O_CREAT | O_TRUNC | O_RDWR | O_BINARY, S_IWRITE);
  52.     if (outhand == -1) {
  53.         printf("\n  Error creating file %s.\n", outfile);
  54.         exit(2);
  55.     }
  56.     while (read(inhand, inpline, 160)) {
  57.         for (i = 0; i < 80; i++) {
  58.             if (inpline[i*2] == ' ') {
  59.                 priorspaces++;
  60.                 if (priorspaces == 127) {
  61.                     priorspaces = 0;
  62.                     setcodebyte(outhand, 255, 1);
  63.                 }
  64.             }    else {
  65.                 if (priorspaces) {
  66.                     setcodebyte(outhand, 128+priorspaces, 1);
  67.                     priorspaces = 0;
  68.                 }
  69.                 setcodebyte(outhand, inpline[i*2], 1);
  70.                 setcodebyte(outhand, inpline[i*2+1], 1);
  71.             }
  72.         }
  73.     }
  74.     if (priorspaces) setcodebyte(outhand, 128+priorspaces, 1);
  75.     setcodebyte(outhand, 0, 0);
  76. }
  77.  
  78. /* setcodebyte() places a coded byte into the output file buffer if setcode is
  79.      1 or flushes the buffer and closes the output file if setcode = 0.
  80. */
  81.  
  82. void setcodebyte(int outhand, unsigned char outbyte, int setcode) {
  83.     static int bufpos = 0;
  84.  
  85.     if (setcode) {
  86.         outbuf[bufpos++] = outbyte;
  87.         if (bufpos == OUTSIZ) {
  88.             bufpos = 0;
  89.             write(outhand, outbuf, OUTSIZ);
  90.         }
  91.     } else {
  92.         if (bufpos) write(outhand, outbuf, bufpos);
  93.         close(outhand);
  94.     }
  95. }
  96.