// file: $isip/util/speech/isip_model_converter/imc_param_0.cc
// version: $Id: imc_param_0.cc 8977 2003-01-12 06:37:52Z jelinek $
//
// isip include files
//
#include "isip_model_converter.h"

// method: read_params_cc
//
// arguments:
//  String& param_filename : (input) parameter file
//  File& param_file  : (output) parameter file
//  String& mono_filename : (output) monophone file
//  String& lexicon_filename : (output) lexicon file
//  String& exclude_filename : (output) exclude file
//  String& lat_filename : (output) exclude file
//  String& state_filename : (output) stat file
//  String& transitions_filename : (output) transition file
//  String& models_filename : (output) model file
//  String& phones_filename : (output) phones file
//  long& context_mode : (output) context mode
//  String& output_lm_filename : (output) lm file
//  String& output_am_filename : (output) am file
//  String& output_config_filename : (output) config file
//  long& output_mode : (output) output mode
//
// return: a logical flag to indicate success
//
// this method reads the input parameter file line by line and stores
// the relevant information
//
boolean read_params_cc(String& param_filename_a, File& param_file_a,
                       String& mono_filename_a,
		       String& lexicon_filename_a,
		       String& exclude_filename_a, String& lat_filename_a,
		       String& state_filename_a,
		       String& transitions_filename_a,
		       String& models_filename_a,
		       String& phones_filename_a, long& context_mode_a,
		       String& output_lm_filename_a,
		       String& output_am_filename_a,
		       String& output_config_filename_a,
		       long& output_mode_a) {                           

  // declare local variables
  //
  //  printf("HAI\n");

  // check if the file exists
  //
  if (!param_file_a.open(param_filename_a)) {
    String out(L"error opening ");
    out.concat(param_filename_a);
    Console::put(out);
    Integral::exit();
  }

  String tmp_str(L"");
  String tmp;

  // read the contents of the parameter file
  // store the necessary information
  //
  while(!param_file_a.eof()) {

    // get the next line
    //
    if(!param_file_a.get(tmp_str)) {
      break;
    }

    // trim the line and check for empty lines and comment lines
    //
    tmp_str.trim();
    if (tmp_str.length() <= 0) {
      continue;
    }
    if (tmp_str(0) == IMC_COMMENT_CHAR) {
      continue;
    }
  
    // long num_tokens = tmp_str.countTokens();
    //    printf("num_toks = %ld\n", num_tokens);

    String token;

    // tokenize the line
    //
    long pos = 0;
    if (tmp_str.tokenize(token, pos, IMC_EQ_DELIMITER_CHAR)) {

      // trim off the whitespace
      //
      token.trim();
    
      // get the monophone file
      //
      if (token.eq(IMC_MONOPHONE_FILE, false)) {
	tmp_str.tokenize(mono_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	mono_filename_a.trim();
	//	Console::put(mono_filename_a);
      }

      // get the lexicon file
      //
      if (token.eq(IMC_LEXICON_FILE, false)) {
	tmp_str.tokenize(lexicon_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	lexicon_filename_a.trim();
	//	Console::put(lexicon_filename_a);
      }

      // get the exclude file
      //
      if (token.eq(IMC_EXCLUDE_FILE, false)) {
	tmp_str.tokenize(exclude_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	exclude_filename_a.trim();
	//	Console::put(exclude_filename_a);
      }

      // get the lattice file
      //
      if (token.eq(IMC_LATTICE_FILE, false)) {
	tmp_str.tokenize(lat_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	lat_filename_a.trim();
	//	Console::put(lat_filename_a);
      }

      // get the states filename
      //
      if (token.eq(IMC_STATES_FILE, false)) {
	tmp_str.tokenize(state_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	state_filename_a.trim();
	//	Console::put(state_filename_a);
      }

      // read name of transitions data file
      //
      if (token.eq(IMC_TRANSITIONS_FILE, false)) {
	tmp_str.tokenize(transitions_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	transitions_filename_a.trim();
	//	Console::put(transitions_filename_a);
      }

      // read name of HMM models data file
      //
      if (token.eq(IMC_MODELS_FILE, false)) {
	tmp_str.tokenize(models_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	models_filename_a.trim();
	//	Console::put(models_filename_a);
      }

      // read name of triphones data file
      //
      if (token.eq(IMC_PHONES_FILE, false)) {
	tmp_str.tokenize(phones_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	phones_filename_a.trim();
	//	Console::put(phones_filename_a);
      }  

      // read context mode
      //
      if (token.eq(IMC_CONTEXT_MODE, false)) {
	String context_mode;
	tmp_str.tokenize(context_mode, pos, IMC_EQ_DELIMITER_CHAR);
	context_mode.trim();
	
	if (context_mode.eq(IMC_MONOPHONE_MODE)) {
	  context_mode_a = IMC_MONOPHONE;
	}
	else if (context_mode.eq(IMC_CROSS_WORD_MODE)) {
	  context_mode_a = IMC_CROSS_WORD;
	}
	else if (context_mode.eq(IMC_WRD_INTERNAL_MODE)) {
	  context_mode_a = IMC_WRD_INTERNAL;
	}
	else {
	  String out(L"error opening ");
	  out.concat(param_filename_a);
	  Console::put(out);
	  Integral::exit();
	}
	Console::put(context_mode);
      }  

      // read output file and mode
      //
      if (token.eq(IMC_OUTPUT_LM_FILE, false)) {
	tmp_str.tokenize(output_lm_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	output_lm_filename_a.trim();
	//	Console::put(output_filename_a);
      }

      if (token.eq(IMC_OUTPUT_AM_FILE, false)) {
	tmp_str.tokenize(output_am_filename_a, pos, IMC_EQ_DELIMITER_CHAR);
	output_am_filename_a.trim();
	//	Console::put(output_filename_a);
      }

      if (token.eq(IMC_OUTPUT_CONFIG_FILE, false)) {
	tmp_str.tokenize(output_config_filename_a, pos,
			 IMC_EQ_DELIMITER_CHAR);
	output_config_filename_a.trim();
	//	Console::put(output_filename_a);
      }

      if (token.eq(IMC_OUTPUT_MODE, false)) {
	String output_mode;
	tmp_str.tokenize(output_mode, pos, IMC_EQ_DELIMITER_CHAR);
	output_mode.trim();
	//	Console::put(mode);
	
	if (output_mode.eq(IMC_ASCII, false)) {
	  output_mode_a = (long) IMC_ASCII_MODE;
	}
	else if (output_mode.eq(IMC_BINARY, false)) {
	  output_mode_a = (long) IMC_BINARY_MODE;
	}
	else {
	  String output(L"Unknown output mode: \"");
	  output.concat(output_mode);
	  output.concat(L"\", using default (ascii)");
	  Console::put(output);
	}
      }
    }
  }
  param_file_a.close();
  return true;
}


