/* DECODE.C - MAIN PROGRAM FOR DECODING. */

#include <stdio.h>
#include "code.h"
#include "freq.h"

main (void)
{
    frequencies f;		/* Structure holding character frequencies  */
    int ch; 			/* Character to encode                      */
    int index;			/* Index of character to encode             */

    start_inputing_bits();
    start_decoding();

    initialize_frequencies(&f);			/* Set all frequencies to 1 */

    for (;;) {					/* Loop through characters. */

        index = decode_symbol(f.cum_freq);	/* Decode next symbol.      */
        ch = f.index_to_symbol[index];		/* Translate to a character.*/
        if (ch==EOF_symbol) break;		/* Exit loop if EOF symbol. */
        putc(ch,stdout);			/* Write that character.    */
        update_frequencies(&f,index);		/* Update symbol frequencies*/
    }

    exit(0);
}

