/* HTML-DECODE.C - MAIN PROGRAM FOR DECOMPRESSING HTML FILES. */

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

main 
( int argc,
  char **argv
)
{   
  html_options options;		/* Options specifying how compression is done */
  html_context context;		/* Information on the current context */
  frequencies *freq;		/* Frequencies for current context */

  int ch; 			/* Next character to encode */
  int index;			/* Index of character to encode */

  /* Process command line options, exiting if there's an error. */

  if (html_arguments(&argv,&options)!=0 || *argv!=0)
  { fprintf (stderr,
     "Usage: html-decode [ -h | -H ] [ -p ] [ stats-file ] <encoded-file >HTML-file\n");
    exit(1);
  }

  /* Initialize arithmetic encoding modules. */

  start_inputing_bits();
  start_decoding();

  /* Initialize the context. */

  html_initial_context(&options,&context);

  /* Decode each character in turn, switching contexts as necessary. */

  for (;;)
  { 
    /* Decode character using current frequency table; terminate loop on EOF. */

    freq = html_find_table(&options,&context);
    index = decode_symbol(freq->cum_freq);
    ch = freq->index_to_symbol[index];
    if (ch==EOF_symbol) break;

    /* Write out the decoded character. */

    putc(ch,stdout);

    /* Update frequencies for current context based on decoded character. */

    update_frequencies(freq,index);

    /* Move to the new context, based on the decoded character. */

    html_next_context(ch,&options,&context);
  }

  exit(0);
}

