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 / ASH / COMPRESS.C < prev    next >
C/C++ Source or Header  |  1992-01-15  |  3KB  |  155 lines

  1. /*
  2.         Test statistical compression using fixed dictionary
  3. */
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10.  
  11. #include "comp.h"
  12. #include "files.h"
  13.  
  14. #define COMP_STAT
  15.  
  16. static char *input_file_name;
  17. static char *output_file_name = "compfile";
  18. static unsigned long expand_size = 0;
  19.  
  20.  
  21. static void compress_file (int);
  22. static void print_time (clock_t);
  23. static void print_compression (FILE *);
  24.  
  25.  
  26. int main (int argc, char *argv [])
  27.  
  28. {
  29.         clock_t start_time;
  30.  
  31.         int max_order;
  32.         char *p;
  33.  
  34.         if (argc < 3 || argc > 4)
  35.         {
  36.                 fprintf (stderr, "Usage: cmp <order> <input file1> [<optional output file2>]\n");
  37.                 fprintf (stderr, "Output file name defaults to 'compfile'\n");
  38.                 return EXIT_FAILURE;
  39.         }
  40.  
  41.         max_order = 0;
  42.         p = argv [1];
  43.         while (*p)
  44.         {
  45.                 if (!isdigit (*p))
  46.                 {
  47.                         fprintf (stderr, "Invalid order\n");
  48.                         exit (EXIT_FAILURE);
  49.                 }
  50.  
  51.                 max_order *= 10;
  52.                 max_order += *p - '0';
  53.  
  54.                 p ++;
  55.         }
  56.  
  57.         if (max_order > MAX_ORDER) max_order = MAX_ORDER;
  58.  
  59.         input_file_name = argv [2];
  60.         if (argc == 4) output_file_name = argv [3];
  61.  
  62.         OpenInputFile (input_file_name);
  63.         OpenOutputFile (output_file_name);
  64.  
  65.         fprintf (stdout, "Input file: %s\n", input_file_name);
  66.         fprintf (stdout, "Order: %d\n", max_order);
  67.         fprintf (stdout, "Dictionary size: %u\n", NDICT);
  68.  
  69.         start_time = clock ();
  70.         compress_file (max_order);
  71.  
  72.         print_time (clock () - start_time);
  73.         CloseInputFile ();
  74.  
  75.         return EXIT_SUCCESS;
  76. }
  77.  
  78.  
  79.  
  80. static void compress_file (int max_order)
  81.  
  82. {
  83.         int ch;
  84.  
  85.         InitModel (max_order);
  86.  
  87.         expand_size = 0;
  88.  
  89.         ch = ReadInputFile ();
  90.         while (ch != EOF)
  91.         {
  92.                 CompressSymbol (ch);
  93.                 expand_size ++;
  94.  
  95. #ifdef COMP_STAT
  96.                 if ((expand_size & 0xFF) == 0) print_compression (stderr);
  97. #endif
  98.  
  99.                 ch = ReadInputFile ();
  100.         }
  101.  
  102.         CompressSymbol (END_OF_FILE);
  103.  
  104.         CloseModel ();
  105.  
  106. #ifdef COMP_STAT
  107.         print_compression (stderr);
  108. #endif
  109.  
  110.         print_compression (stdout);
  111.         fprintf (stdout, "\n");
  112. }
  113.  
  114.  
  115. static void print_compression (FILE *fptr)
  116.  
  117. {
  118.         unsigned long comp_size;
  119.         unsigned long t, t2;
  120.         unsigned x, y;
  121.  
  122.         comp_size = GetOutputLength ();
  123.         t = 1000 * comp_size;
  124.         t2 = expand_size >> 1;
  125.         x = (unsigned) ((t + t2) / expand_size);
  126.         y = (unsigned) ((8 * t + t2) / expand_size);
  127.  
  128.         fprintf (fptr, "%ld/%ld    %u.%01u    (%u.%03u) \r", expand_size, comp_size,
  129.                 x / 10, x % 10, y / 1000, y % 1000);
  130. }
  131.  
  132.  
  133.  
  134. static void print_time (clock_t t)
  135.  
  136. {
  137.         long t2;
  138.         unsigned t3;
  139.         unsigned hh, mm, ss;
  140.  
  141.         t2 = t * 10 / CLK_TCK;
  142.         hh = (unsigned) (t2 / 36000L);
  143.         t3 = (unsigned) (t2 % 36000L);
  144.         mm = t3 / 600;
  145.         t3 %= 600;
  146.         ss = t3 / 10;
  147.         t3 %= 10;
  148.  
  149.         fprintf (stdout, "Time: ");
  150.         if (hh)
  151.                 fprintf (stdout, "%d:%02d:%02d.%01d\n", hh, mm, ss, t3);
  152.         else
  153.                 fprintf (stdout, "%d:%02d.%01d\n", mm, ss, t3);
  154. }
  155.