home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / bwt / src / rle.cpp < prev    next >
C/C++ Source or Header  |  1996-06-17  |  3KB  |  103 lines

  1. //
  2. //  RLE.CPP
  3. //
  4. //  Mark Nelson
  5. //  March 8, 1996
  6. //  http://web2.airmail.net/markn
  7. //
  8. // DESCRIPTION
  9. // -----------
  10. //
  11. //  This program performs a Run Length Encoding function on an
  12. //  input file/stream, and sends the result to an output file
  13. //  or stream.  In the output stream, any two consecutive
  14. //  characters with the same value flag a run.  A byte following
  15. //  those two characters gives the count of *additional*
  16. //  repeat characters, which can be anything from 0 to 255.
  17. //
  18. //  Using the RLE program as a front end to BWT avoids
  19. //  pathologically slow sorts that occur when the input stream
  20. //  has long sequences of identical characters. (Which means
  21. //  comparison functions have to spend lots of time on a pair
  22. //  of strings before deciding who is larger.)
  23. //
  24. //  This program takes two arguments: an input file and an output
  25. //  file.  You can leave off one argument and send your output to
  26. //  stdout.  Leave off two arguments and read your input from stdin
  27. //  as well.
  28. //
  29. //  This program accompanies my article "Data Compression with the
  30. //  Burrows-Wheeler Transform."
  31. //
  32. // Build Instructions
  33. // ------------------
  34. //
  35. //  Define the constant unix for UNIX or UNIX-like systems.  The
  36. //  use of this constant turns off the code used to force the MS-DOS
  37. //  file system into binary mode. g++ already does this, your UNIX C++
  38. //  compiler might also.
  39. //
  40. //  Borland C++ 4.5 16 bit    : bcc -w rle.cpp
  41. //  Borland C++ 4.5 32 bit    : bcc32 -w rle.cpp
  42. //  Microsoft Visual C++ 1.52 : cl /W4 rle.cpp
  43. //  Microsoft Visual C++ 2.1  : cl /W4 rle.cpp
  44. //  g++                       : g++ -o rle rle.cpp
  45. //
  46. // Typical Use
  47. // -----------
  48. //
  49. //  rle < raw-file | bwt | mtf | rle | ari > compressed-file
  50. //
  51. //
  52.  
  53. #define unix
  54.  
  55. #include <stdio.h>
  56. #if !defined( unix )
  57. #include <io.h>
  58. #include <fcntl.h>
  59. #endif
  60.  
  61. main( int argc, char *argv[] )
  62. {
  63.     fprintf( stderr, "Run length encoding " );
  64.     if ( argc > 1 ) {
  65.         freopen( argv[ 1 ], "rb", stdin );
  66.         fprintf( stderr, "%s", argv[ 1 ] );
  67.     } else
  68.         fprintf( stderr, "stdin" );
  69.     fprintf( stderr, " to " );
  70.     if ( argc > 2 ) {
  71.         freopen( argv[ 2 ], "wb", stdout );
  72.         fprintf( stderr, "%s", argv[ 2 ] );
  73.     } else
  74.         fprintf( stderr, "stdout" );
  75.     fprintf( stderr, "\n" );
  76. #if !defined( unix )
  77.     setmode( fileno( stdin ), O_BINARY );
  78.     setmode( fileno( stdout ), O_BINARY );
  79. #endif
  80.  
  81.     int last = 0;
  82.     int c;
  83.     while ( ( c = getc( stdin ) ) >= 0 )  {
  84.         putc( (char) c, stdout );
  85.         if ( c == last ) {
  86.             int count = 0;
  87.             while ( count < 255 &&
  88.                     ( ( c = getc( stdin ) ) >= 0 ) ) {
  89.                 if ( c == last )
  90.                     count++;
  91.                 else
  92.                     break;
  93.             }
  94.             putc( (char) count, stdout );
  95.             if ( count != 255 && c >= 0 )
  96.                 putc( (char) c, stdout );
  97.         }
  98.         last = c;
  99.     }
  100.     return 1;
  101. }
  102.  
  103.