home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_02 / 1002058a < prev    next >
Text File  |  1991-12-12  |  6KB  |  259 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.       
  8.        /*****************************************************
  9.        *
  10.        *       file d:\huffman.c
  11.        *
  12.        *
  13.        ******************************************************/
  14.  
  15.  
  16. #include "d:\lsu\cujhuff.h"
  17.  
  18.  
  19.  
  20. main(argc, argv)
  21.    int  argc;
  22.    char *argv[];
  23. {
  24.    char  input_file_name[80],
  25.          output_file_name[80],
  26.          r[80];
  27.  
  28.    int i, j;
  29.    struct item_struct item_array[LENGTH];
  30.    struct header_struct file_header;
  31.  
  32.  
  33.    if(argc < 3){
  34.       printf("\nHUFFMAN> You did not enter enough file names.");
  35.       printf("\nHUFFMAN> Try again: huffman in_file_name out_file_name");
  36.       printf("\nHUFFMAN>                     or");
  37.       printf("\nHUFFMAN>            huffman destination_file packed_file d");
  38.       exit(1);
  39.    }
  40.  
  41.        /*
  42.           If there are four arguments then you are decompressing
  43.           the compressed input file to a full size output file.
  44.        */
  45.  
  46.    if(argc >= 4){
  47.       strcpy(output_file_name, argv[1]);
  48.       strcpy(input_file_name, argv[2]);
  49.       decode_compressed_file(input_file_name,
  50.                              output_file_name,
  51.                              item_array,
  52.                              &file_header);
  53.    }
  54.  
  55.    else{   /* else you compress the full size input file and write
  56.               out a compressed output file */
  57.  
  58.       strcpy(input_file_name, argv[1]);
  59.       strcpy(output_file_name, argv[2]);
  60.  
  61.       read_input_file_and_create_histogram(input_file_name, item_array);
  62.       sort_item_array(item_array);
  63.       printf("\n\nHUFFMAN> This is the sorted item array:\n");
  64.       print_item_array(item_array);
  65.       create_huffman_code(item_array);
  66.       printf("\n\nHUFFMAN> This is the Huffman coding of the characters:\n");
  67.       print_item_array(item_array);
  68.       printf("\n> Coding the file");
  69.       convert_long_to_short(item_array, &file_header);
  70.       code_and_write_output_file(item_array, 
  71.                                  input_file_name, 
  72.                                  output_file_name,
  73.                                  &file_header);
  74.  
  75.    }  /* ends else compress input file to output file */
  76.  
  77.  
  78. }  /* ends main  */
  79.  
  80.  
  81.  
  82.  
  83. /*  
  84.          read_input_file_and_create_histogram(...
  85.  
  86.          Read the input file.  Count up the occurances of each
  87.          character and create a histogram.
  88. */
  89.  
  90.  
  91. read_input_file_and_create_histogram(input_file_name, item_array)
  92.    char  input_file_name[];
  93.    struct item_struct item_array[];
  94. {
  95.    char  buffer[1000];
  96.  
  97.    int   bytes_read,
  98.          i,
  99.          in_file_desc,
  100.          j;
  101.  
  102.    clear_item_array(item_array);
  103.    in_file_desc = my_open(input_file_name);
  104.    printf("\n> in file desc = %d", in_file_desc);
  105.  
  106.    bytes_read = 1000;
  107.  
  108.    while(bytes_read == 1000){
  109.       bytes_read = my_read(in_file_desc, buffer, 1000);
  110.       for(i=0; i<bytes_read; i++){
  111.          j = buffer[i];
  112.          item_array[j].count = item_array[j].count + 1;
  113.       }  /* ends loop over i  */
  114.    }     /* ends while bytes_read == 1000  */
  115.  
  116.    close(in_file_desc);
  117.  
  118.  
  119. }  /* ends read_input_file_and_create_histogram  */
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. /* 
  128.         clear_item_array(...
  129.  
  130.         This function initializes the item_array.
  131. */
  132.  
  133.  
  134. clear_item_array(item_array)
  135.    struct item_struct item_array[];
  136. {
  137.    int i,j, k;
  138.  
  139.    for(i=0; i<LENGTH; i++){
  140.       item_array[i].indicator = 'E';
  141.       item_array[i].character = i;
  142.       item_array[i].count     = 0;
  143.       for(k=0; k<LLENGTH; k++)
  144.          item_array[i].includes[k] = 256;
  145.       for(j=0; j<CODE_LENGTH; j++)
  146.          item_array[i].coded[j] = OTHER;
  147.    }   /*  ends loop over i  */
  148. }      /*  ends clear_item_array  */
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /* 
  155.         print_item_array(item_array)
  156.  
  157.         This function is for debugging.  It prints
  158.         to the screen the item_array.
  159. */
  160.  
  161. print_item_array(item_array)
  162.    struct item_struct item_array[];
  163. {
  164.    char  response[5];
  165.    int   i,
  166.          j,
  167.          k,
  168.          max_i,
  169.          printed;
  170.    float ratio;
  171.    long  max;
  172.    printed = 0;
  173.    max           = 0;
  174.  
  175.    printf("\n>");
  176.    printed++;
  177.  
  178.    for(i=0; i<LENGTH; i++){
  179.       if(item_array[i].count > max){
  180.          max = item_array[i].count;
  181.          max_i = i;
  182.  
  183.       }  /* ends if count > max  */
  184.    }         /* ends loop over i         */
  185.  
  186.    ratio = 30.0/(float)(item_array[max_i].count);
  187.  
  188.    for(i=0; i<LENGTH; i++){
  189.       if(item_array[i].count != 0){
  190.          printed++;
  191.          if( (printed%22) == 0){
  192.             printf("\n> Hit return to continue--");
  193.             read_string(response);
  194.          }  /* ends if printed 20 lines */
  195.          printf("\n> [%3d]=%3d=%c=%4d=", i, item_array[i].character,
  196.                                       item_array[i].indicator,
  197.                                       item_array[i].count);
  198.          for(k=0; k<CODE_LENGTH; k++)
  199.             printf("%c", item_array[i].coded[k]);
  200.          for(j=0; j<(ratio*item_array[i].count); j++){
  201.             printf("*");
  202.          }  /* ends loop over j       */
  203.       }     /* ends if count != 0     */
  204.    }            /* ends loop over i       */
  205. }            /* ends print_item_array  */
  206.  
  207.  
  208.  
  209.  
  210.  
  211. /*
  212.       convert_long_to_short(...
  213.  
  214.       This function converts the long item_array into
  215.       a shorter file header.
  216.  
  217. */
  218.  
  219. convert_long_to_short(item_array, file_header)
  220.    struct item_struct item_array[];
  221.    struct header_struct *file_header;
  222. {
  223.    int i, j, k;
  224.  
  225.    for(i=0; i<LENGTH; i++){
  226.       file_header->items[i].character = item_array[i].character;
  227.       for(j=0; j<CODE_LENGTH; j++)
  228.          file_header->items[i].coded[j] = item_array[i].coded[j];
  229.    }   /*  ends loop over i  */
  230.  
  231. }  /* ends convert_long_to_short */
  232.  
  233.  
  234.  
  235.  
  236. /*
  237.       convert_short_to_long(...
  238.  
  239.       This function converts the short file header into
  240.       a the longer item_array for use in the program.
  241.  
  242. */
  243.  
  244.  
  245. convert_short_to_long(item_array, file_header)
  246.    struct item_struct item_array[];
  247.    struct header_struct *file_header;
  248. {
  249.    int i, j, k;
  250.  
  251.    for(i=0; i<LENGTH; i++){
  252.       item_array[i].character =  file_header->items[i].character; 
  253.       for(j=0; j<CODE_LENGTH; j++)
  254.          item_array[i].coded[j] =  file_header->items[i].coded[j]; 
  255.    }   /*  ends loop over i  */
  256.  
  257. }  /* ends convert_short_to_long */
  258. /* End of File */
  259.