/* ENCODE.C - MAIN PROGRAM FOR ENCODING. */

#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_outputing_bits();
    start_encoding();

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

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

        ch = getc(stdin);			/* Read the next character. */
        if (ch==EOF) break;			/* Exit loop on end-of-file.*/
        index = f.symbol_to_index[ch];		/* Translate to an index.   */
        encode_symbol(index,f.cum_freq);	/* Encode that symbol.      */
        update_frequencies(&f,index);		/* Update symbol frequencies*/
    }

    index = f.symbol_to_index[EOF_symbol];	/* Encode the EOF symbol.   */
    encode_symbol(index,f.cum_freq);

    done_encoding();				/* Send the last few bits.  */
    done_outputing_bits();

    exit(0);
}
