home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / ddjcompr / ashford / expand.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  3KB  |  158 lines

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