home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_02 / cujhuff.c < prev    next >
Text File  |  1991-12-09  |  6KB  |  254 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.       read_input_file_and_create_histogram(input_file_name, item_array);
  61.       sort_item_array(item_array);
  62.       printf("\n\nHUFFMAN> This is the sorted item array:\n");
  63.       print_item_array(item_array);
  64.       create_huffman_code(item_array);
  65.       printf("\n\nHUFFMAN> This is the Huffman coding of the characters:\n");
  66.       print_item_array(item_array);
  67.       printf("\n> Coding the file");
  68.       convert_long_to_short(item_array, &file_header);
  69.       code_and_write_output_file(item_array, 
  70.                                  input_file_name, 
  71.                                  output_file_name,
  72.                                  &file_header);
  73.  
  74.    }  /* ends else compress input file to output file */
  75.  
  76.  
  77. }  /* ends main  */
  78.  
  79.  
  80.  
  81.  
  82. /*  
  83.          read_input_file_and_create_histogram(...
  84.  
  85.          Read the input file.  Count up the occurances of each
  86.          character and create a histogram.
  87. */
  88.  
  89.  
  90. read_input_file_and_create_histogram(input_file_name, item_array)
  91.    char  input_file_name[];
  92.    struct item_struct item_array[];
  93. {
  94.    char  buffer[1000];
  95.  
  96.    int   bytes_read,
  97.          i,
  98.          in_file_desc,
  99.          j;
  100.  
  101.    clear_item_array(item_array);
  102.    in_file_desc = my_open(input_file_name);
  103.    printf("\n> in file desc = %d", in_file_desc);
  104.  
  105.    bytes_read = 1000;
  106.  
  107.    while(bytes_read == 1000){
  108.       bytes_read = my_read(in_file_desc, buffer, 1000);
  109.       for(i=0; i<bytes_read; i++){
  110.          j = buffer[i];
  111.          item_array[j].count = item_array[j].count + 1;
  112.       }  /* ends loop over i  */
  113.    }     /* ends while bytes_read == 1000  */
  114.  
  115.    close(in_file_desc);
  116.  
  117.  
  118. }  /* ends read_input_file_and_create_histogram  */
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. /* 
  126.         clear_item_array(...
  127.  
  128.         This function initializes the item_array.
  129. */
  130.  
  131.  
  132. clear_item_array(item_array)
  133.    struct item_struct item_array[];
  134. {
  135.    int i,j, k;
  136.  
  137.    for(i=0; i<LENGTH; i++){
  138.       item_array[i].indicator = 'E';
  139.       item_array[i].character = i;
  140.       item_array[i].count     = 0;
  141.       for(k=0; k<LLENGTH; k++)
  142.          item_array[i].includes[k] = 256;
  143.       for(j=0; j<CODE_LENGTH; j++)
  144.          item_array[i].coded[j] = OTHER;
  145.    }   /*  ends loop over i  */
  146. }      /*  ends clear_item_array  */
  147.  
  148.  
  149.  
  150.  
  151.  
  152. /* 
  153.         print_item_array(item_array)
  154.  
  155.         This function is for debugging.  It prints
  156.         to the screen the item_array.
  157. */
  158.  
  159. print_item_array(item_array)
  160.    struct item_struct item_array[];
  161. {
  162.    char  response[5];
  163.    int   i,
  164.          j,
  165.          k,
  166.          max_i,
  167.          printed;
  168.    float ratio;
  169.    long  max;
  170.    printed = 0;
  171.    max           = 0;
  172.  
  173.    printf("\n>");
  174.    printed++;
  175.  
  176.    for(i=0; i<LENGTH; i++){
  177.       if(item_array[i].count > max){
  178.          max = item_array[i].count;
  179.          max_i = i;
  180.       }  /* ends if count > max  */
  181.    }         /* ends loop over i         */
  182.  
  183.    ratio = 30.0/(float)(item_array[max_i].count);
  184.  
  185.    for(i=0; i<LENGTH; i++){
  186.       if(item_array[i].count != 0){
  187.          printed++;
  188.          if( (printed%22) == 0){
  189.             printf("\n> Hit return to continue--");
  190.             read_string(response);
  191.          }  /* ends if printed 20 lines */
  192.          printf("\n> [%3d]=%3d=%c=%4d=", i, item_array[i].character,
  193.                                       item_array[i].indicator,
  194.                                       item_array[i].count);
  195.          for(k=0; k<CODE_LENGTH; k++)
  196.             printf("%c", item_array[i].coded[k]);
  197.          for(j=0; j<(ratio*item_array[i].count); j++){
  198.             printf("*");
  199.          }  /* ends loop over j       */
  200.       }     /* ends if count != 0     */
  201.    }            /* ends loop over i       */
  202. }            /* ends print_item_array  */
  203.  
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.       convert_long_to_short(...
  210.  
  211.       This function converts the long item_array into
  212.       a shorter file header.
  213.  
  214. */
  215.  
  216. convert_long_to_short(item_array, file_header)
  217.    struct item_struct item_array[];
  218.    struct header_struct *file_header;
  219. {
  220.    int i, j, k;
  221.  
  222.    for(i=0; i<LENGTH; i++){
  223.       file_header->items[i].character = item_array[i].character;
  224.       for(j=0; j<CODE_LENGTH; j++)
  225.          file_header->items[i].coded[j] = item_array[i].coded[j];
  226.    }   /*  ends loop over i  */
  227.  
  228. }  /* ends convert_long_to_short */
  229.  
  230.  
  231.  
  232.  
  233. /*
  234.       convert_short_to_long(...
  235.  
  236.       This function converts the short file header into
  237.       a the longer item_array for use in the program.
  238.  
  239. */
  240.  
  241. convert_short_to_long(item_array, file_header)
  242.    struct item_struct item_array[];
  243.    struct header_struct *file_header;
  244. {
  245.    int i, j, k;
  246.  
  247.    for(i=0; i<LENGTH; i++){
  248.       item_array[i].character =  file_header->items[i].character; 
  249.       for(j=0; j<CODE_LENGTH; j++)
  250.          item_array[i].coded[j] =  file_header->items[i].coded[j]; 
  251.    }   /*  ends loop over i  */
  252.  
  253. }  /* ends convert_short_to_long */
  254.