home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / l / lds_10.zip / COMP / COMP-1.C < prev    next >
C/C++ Source or Header  |  1990-06-02  |  4KB  |  152 lines

  1. /*
  2.  * Listing 6 -- comp-1.c
  3.  *
  4.  * This listing is the driver program for an order 0 fixed context
  5.  * compression program.  It follows the model shown in BILL.C
  6.  * for a compression program, by reading in symbols from a file,
  7.  * converting them to a high, low, range set, then encoding them.
  8.  * The order 0 modeling code for this routine is found Listing 9,
  9.  * model-1.c.
  10.  *
  11.  * To build this program:
  12.  *
  13.  * Turbo C:     tcc -w comp-1.c model-1.c bitio.c coder.c
  14.  * QuickC:      qcl /W3 comp-1.c model-1.c bitio.c coder.c
  15.  * Zortech:     ztc comp-1.c model-1.c bitio.c coder.c
  16.  * *NIX:        cc -o comp-1 comp-1.c model-1.c bitio.c coder.c
  17.  *
  18.  * Command line options:
  19.  *
  20.  *  -f text_file_name  [defaults to test.inp]
  21.  *  -c compressed_file_name [defaults to test.cmp]
  22.  */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "coder.h"
  27. #include "model.h"
  28. #include "bitio.h"
  29.  
  30. FILE *text_file;              /* These variables are used by several   */
  31. FILE *compressed_file;        /* of the modules in this routine.       */
  32.  
  33. /*
  34.  * Local declarations.
  35.  */
  36. void initialize_options( int argc, char **argv );
  37. void print_compression( void );
  38.  
  39. /*
  40.  * The main() procedure for this routine is very simple.  After
  41.  * initializing the command line options, model, I/O, and arithmetic
  42.  * coder, it sits in a loop reading in characters from the input
  43.  * file, converting them to type SYMBOL, then encoding them for
  44.  * output.  When done, everything is shut down properly.
  45.  *
  46.  */
  47. void main( int argc, char **argv )
  48. {
  49.     SYMBOL s;
  50.     int c;
  51.     long int text_count = 0;
  52.  
  53.     initialize_options( --argc, ++argv );
  54.     initialize_model();
  55.     initialize_output_bitstream();
  56.     initialize_arithmetic_encoder();
  57. /*
  58.  * This is the main compression loop.  It gets a character, converts
  59.  * it to SYMBOL, then encodes it.  Finally, the model is updated.
  60.  * One potential portability problem:  EOF is assumed to be -1.
  61.  */
  62.     for ( ; ; )
  63.     {
  64.         if (( ++text_count & 0x0ff ) == 0 )
  65.         print_compression();
  66.         c = getc( text_file );
  67.     convert_int_to_symbol( c, &s );
  68.     encode_symbol( compressed_file, &s );
  69.         if ( c == EOF )
  70.         break;
  71.     update_model( c );
  72.     }
  73. /*
  74.  * This is just some code that is needed to clean up loose ends before
  75.  * the program can exit.
  76.  */
  77.     flush_arithmetic_encoder( compressed_file );
  78.     flush_output_bitstream( compressed_file );
  79.     print_compression();
  80.     fputc( '\n', stderr );
  81.     exit( 0 );
  82. }
  83. /*
  84.  * This routine has the job of reading in all of the command line
  85.  * options.  For this program, the only options are the input file
  86.  * name and the output file name.  After the names are read in,
  87.  * the files are opened.
  88.  */
  89. void initialize_options( int argc, char **argv )
  90. {
  91.     char text_file_name[ 81 ];
  92.     char compressed_file_name[ 81 ];
  93.  
  94.     strcpy( compressed_file_name, "test.cmp" );
  95.     strcpy( text_file_name, "test.inp" );
  96.     while ( argc-- > 0 )
  97.     {
  98.         if ( strcmp( *argv, "-f" ) == 0 )
  99.     {
  100.         argc--;
  101.             strcpy( text_file_name, *++argv );
  102.     }
  103.         else if ( strcmp( *argv, "-c" ) == 0 )
  104.     {
  105.         argc--;
  106.         strcpy( compressed_file_name, *++argv );
  107.     }
  108.     else
  109.     {
  110.             fprintf( stderr,
  111.                 "\nUsage: COMP-1 [-f text file] [-c compressed file]\n" );
  112.         exit( -1 );
  113.     }
  114.     argc--;
  115.     argv++;
  116.     }
  117.  
  118.     text_file = fopen( text_file_name, "rb" );
  119.     compressed_file = fopen( compressed_file_name, "wb" );
  120.  
  121.     if ( text_file == NULL || compressed_file == NULL )
  122.     {
  123.         printf( "Had trouble opening one of the files!\n" );
  124.         exit( -1 );
  125.     }
  126.     setvbuf( text_file, NULL, _IOFBF, 4096 );
  127.     setbuf( stdout, NULL );
  128.     printf( "Compressing %s to %s\n", text_file_name, compressed_file_name );
  129. }
  130.  
  131. /*
  132.  * This routine is a "bell and whistle" type thing.  It just prints
  133.  * out the current input and output byte counts, and the current number
  134.  * of bits being used per byte.
  135.  */
  136. void print_compression()
  137. {
  138.     long input_bytes;
  139.     long output_bytes;
  140.  
  141.     input_bytes = ftell( text_file );
  142.     if ( input_bytes == 0 )
  143.         input_bytes = 1;
  144.     output_bytes = bit_ftell_output( compressed_file );
  145.     fprintf( stderr,
  146.              "\r%ld/%ld, %2.3f     ",
  147.          input_bytes,
  148.          output_bytes,
  149.              8.0 * output_bytes / input_bytes );
  150. }
  151.  
  152.