// file: tp_param_0.cc
//

// isip include files
//
#include "trace_projector.h"
#include "trace_projector_constants.h"

// method: read_params_cc
//
// arguments:
//  FILE* fp : (input) file pointer to params file
//
// return: a logical flag to indicate success
//
logical_1 Trace_projector::read_params_cc(FILE*& fp_a) {
  
  // string to hold temporary data
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* tag_str = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* tmp1 = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* tmp2 = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* tmp3 = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* tmp4 = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* tmp5 = new char_1[ISIP_MAX_STRING_LENGTH];

  // read data from file
  //
  while (fgets((char*)tmp, ISIP_MAX_STRING_LENGTH - 1, fp_a) != (char*)NULL) {
    
    // ignore comment lines 
    //
    if (tmp[0] == (char_1)'#') {
      
      // do nothing
      //
      continue;
    }

    // if the line is not empty, get the first string which should be the 
    // tag string
    //
    if (strlen((char*)tmp) != 1) {
      sscanf((char*)tmp, "%s", tag_str);
      
      // read name of monophones file
      //
      if (strcmp((char*)tag_str, TP_OP_MONOPHONE_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  monophones_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)monophones_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(monophones_file_d);
	}
      }

      // read name of transitions data file
      //
      else if (strcmp((char*)tag_str, TP_OP_TRANSITION_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  transitions_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)transitions_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(transitions_file_d);
	}
      }
      
      // read name of states data file
      //
      else if (strcmp((char*)tag_str, TP_OP_STATE_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  states_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)states_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(states_file_d);
	}
      }
      
      // read name of HMM models data file
      //
      else if (strcmp((char*)tag_str, TP_OP_MODEL_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  models_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)models_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(models_file_d);
	}
      }
      
      // read name of triphones data file
      //
      else if (strcmp((char*)tag_str, TP_OP_PHONE_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  phones_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)phones_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(phones_file_d);
	}
      }
      
      // read name of lexicon data file
      //
      else if (strcmp((char*)tag_str, TP_OP_LEXICON_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  lexicon_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)lexicon_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(lexicon_file_d);
	}
      }
      
      // read name of language model data file
      //
      else if (strcmp((char*)tag_str, TP_OP_LM_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  lm_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)lm_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(lm_file_d);
	}
      }  
      
      // read name of file containing the list of grammars to be used
      // in a hierarchical grammar case
      //
      else if (strcmp((char*)tag_str, TP_OP_GRAM_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  gram_list_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)gram_list_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(gram_list_file_d);
	}
      }  
      
      // read the name of the forced alignment transcription file
      //
      else if (strcmp((char*)tag_str, TP_OP_TRANSCRIPTION_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  forcealign_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)forcealign_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(forcealign_file_d);
	}
      }
      
      // read the name of the input file list
      //
      else if (strcmp((char*)tag_str, TP_OP_MFCC_LIST) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  input_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)input_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(input_file_d);
	}
      }
      
      // read the name of the output file list
      //
      else if (strcmp((char*)tag_str, TP_OP_OUTPUT_LIST) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  output_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)output_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(output_file_d);
	}
      }
      
      // read the name of the input lattice file list
      //
      else if (strcmp((char*)tag_str, TP_OP_LATTICE_LIST) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  lattice_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)lattice_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(lattice_file_d);
	}
      }
      
      // read the name of the output lattice file list
      //
      else if (strcmp((char*)tag_str, TP_OP_OUTLAT_LIST) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  outlat_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)outlat_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(outlat_file_d);
	}
      }

      // read the name of the output nbest file list
      //
      else if (strcmp((char*)tag_str, TP_OP_OUTNBEST_LIST) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  outnbest_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)outnbest_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(outnbest_file_d);
	}
      }
      
      // read the name of the output lattice wer file
      //
      else if (strcmp((char*)tag_str, TP_OP_LATWER_FILE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  latwer_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)latwer_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(latwer_file_d);
	}
      }
      
      // read the value for ngram size
      //
      else if (strcmp((char*)tag_str, TP_OP_NGRAM) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  ngram_size_d = (int_4)atoi((char*)tmp3);
	}
      }
      
      // read the value for number of history paths
      //
      else if (strcmp((char*)tag_str, TP_OP_HISTORY) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  num_hist_d = (int_4)atoi((char*)tmp3);
	}
      }
      
      // read the value for number of nbest paths to output
      //
      else if (strcmp((char*)tag_str, TP_OP_NBEST) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  num_nbest_d = (int_4)atoi((char*)tmp3);
	}
      }
      
      // set the beam widths if beam pruning is allowed
      //
      else if (strcmp((char*)tag_str, TP_OP_PRUNING) == 0) {
	if (sscanf((char*)tmp, "%s%s%s%s%s", tmp1, tmp2, tmp3,
		   tmp4, tmp5) == 5) {
	  
	  beam_width_d[0] = -(float_4)fabs((float_4)atof((char*)tmp3));
	  beam_width_d[1] = -(float_4)fabs((float_4)atof((char*)tmp4));
	  beam_width_d[2] = -(float_4)fabs((float_4)atof((char*)tmp5));
	}
      }
      
      // set the limit on maximum active phone model instances if
      // specified
      //
      else if (strcmp((char*)tag_str, TP_OP_MAPMI) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  mapmi_limit_d = (int_4)atoi((char*)tmp3);
	  if (mapmi_limit_d < (int_4)0) {
	    mapmi_limit_d = (int_4)0;
	  }
	}
      }
      
      // set the limit on maximum number of words allowed to grow in
      // each frame if specified
      //
      else if (strcmp((char*)tag_str, TP_OP_NUMWORDS) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  num_wdcut_d = (int_4)atoi((char*)tmp3);
	  if (num_wdcut_d < (int_4)0) {
	    num_wdcut_d = (int_4)0;
	  }
	}
      }
      
      // read the value for lm scale factor
      //
      else if (strcmp((char*)tag_str, TP_OP_LMSCALE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  lm_scale_d = (int_4)atof((char*)tmp3);
	}
      }
      
      // read the value for word insertion penalty
      //
      else if (strcmp((char*)tag_str, TP_OP_WORDPENALTY) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  word_penalty_d = (int_4)atof((char*)tmp3);
	}
      }
      
      // read the function mode
      //
      else if (strcmp((char*)tag_str, TP_OP_FUNCTION_MODE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  
	  if (strcmp((char*)tmp3, (char*)TP_OP_FORCE_ALIGN) == 0){
	    function_mode_d = TP_FORCE_ALIGN_FUNCTION;
	  }
	  else if (strcmp((char*)tmp3, (char*)TP_OP_LATTICE_RESCORE) == 0){
	    function_mode_d = TP_LATTICE_RESCORE_FUNCTION;
	  }
	  else if (strcmp((char*)tmp3, (char*)TP_OP_NGRAM_DECODE) == 0){
	    function_mode_d = TP_NGRAM_DECODE_FUNCTION;
	  }
	  else if (strcmp((char*)tmp3, (char*)TP_OP_LATTICE_GENERATE) == 0){
	    function_mode_d = TP_LATTICE_GENERATE_FUNCTION;
	  }
	  else if (strcmp((char*)tmp3, (char*)TP_OP_LATTICE_VERIFY) == 0){
	    function_mode_d = TP_LATTICE_VERIFY_FUNCTION;
	  }
	  else if (strcmp((char*)tmp3, (char*)TP_OP_LATTICE_LATTICE) == 0){
	    function_mode_d = TP_LATTICE_LATTICE_FUNCTION;
	  }
	  else if (strcmp((char*)tmp3, (char*)TP_OP_LATTICE_WER) == 0){
	    function_mode_d = TP_LATTICE_WER_FUNCTION;
	  }
	  else {
	    fprintf(stdout, "\nError : tp_param_0.cc : function mode error\n\n");
	    exit(ISIP_PROTO_ERROR);
	  }
	}
      }
      
      // read the context mode
      //
      else if (strcmp((char*)tag_str, TP_OP_CONTEXT_MODE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  if (strcmp((char*)TP_OP_WORD_INTERNAL, (char*)tmp3) == 0){
	    context_mode_d = TP_WORD_INTERNAL_MODE;
	  }
	  else if (strcmp((char*)TP_OP_MONO_PHONE, (char*)tmp3) == 0){
	    context_mode_d = TP_MONO_PHONE_MODE;
	  }
	  else if (strcmp((char*)TP_OP_CROSS_WORD, (char*)tmp3) == 0){
	    context_mode_d = TP_CROSS_WORD_MODE;
	  }
	  else {
	    fprintf(stdout, "\nError : tp_param_0.cc : context mode error\n\n");
	    exit(ISIP_PROTO_ERROR);
	  }
	}
      }
      
      // read the alignment level mode
      //
      else if (strcmp((char*)tag_str, TP_OP_ALIGN_MODE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  if (strcmp((char*)TP_OP_STATE_ALIGN, (char*)tmp3) == 0){
	    align_mode_d = TP_STATE_ALIGN_MODE;
	  }
	  else if (strcmp((char*)TP_OP_MODEL_ALIGN, (char*)tmp3) == 0){
	    align_mode_d = TP_MODEL_ALIGN_MODE;
	  }
	  else if (strcmp((char*)TP_OP_WORD_ALIGN, (char*)tmp3) == 0){
	    align_mode_d = TP_WORD_ALIGN_MODE;
	  }
	  else {
	    fprintf(stdout, "\nError : tp_param_0.cc : align mode error\n\n");
	    exit(ISIP_PROTO_ERROR);
	  }
	}
      }
      
      // read the input format
      //
      else if (strcmp((char*)tag_str, TP_OP_INPUT_FORMAT) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  if (strcmp((char*)TP_OP_BINARY, (char*)tmp3) == 0) {
	    input_format_d = TP_BINARY_FORMAT;
	  }
	  else if (strcmp((char*)TP_OP_ASCII, (char*)tmp3) == 0) {
	    input_format_d = TP_ASCII_FORMAT;
	  }
	  else {
	    fprintf(stdout, "\nError : tp_param_0.cc : input format error\n\n");
	    exit(ISIP_PROTO_ERROR);
	  }
	}
      }

      // read the input feature file format
      //
      else if (strcmp((char*)tag_str, TP_OP_INPUT_FEATURE_FORMAT) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  if (strcmp((char*)TP_OP_HTK, (char*)tmp3) == 0) {
	    input_feature_format_d = TP_HTK_FORMAT;
	  }
	  else {
	    input_feature_format_d = TP_ISIP_PROTO_FORMAT;
	  }
	}
      }
      
      // read the delta window
      //
      else if (strcmp((char*)tag_str, TP_OP_DELTA_WIN) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  delta_win_d = (int_4)atoi((char*)tmp);
	}
	else {
	  input_feature_format_d = TP_DF_DELTA_WIN;
	}
      }
      
      // read if delta computation required
      //
      else if (strcmp((char*)tag_str, TP_OP_COMPUTE_DELTA) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  if (strcmp((char*)tmp3, TP_OP_YES) == 0) {
	    delta_d = ISIP_TRUE;
	  }
	  else {
	    delta_d = ISIP_FALSE;
	  }
	}
      }
      
      // read if acceleration computation required
      //
      else if (strcmp((char*)tag_str, TP_OP_COMPUTE_ACC) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  if (strcmp((char*)tmp3, TP_OP_YES) == 0) {
	    acc_d = ISIP_TRUE;
	  }
	  else {
	    acc_d = ISIP_FALSE;
	  }
	}
      }

      // read frontend parameter file
      //
      else if (strcmp((char*)tag_str, TP_OP_FRONTEND_PARAMS) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  frontend_params_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)frontend_params_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(frontend_params_d);
	}
      }

      // read compact lattice mode
      //
      else if (strcmp((char*)tag_str, TP_OP_COMPACT_LATTICE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  frontend_params_d = new char_1[strlen((char*)tmp3) + 1];
	  if (strcmp((char*)tmp3, TP_OP_YES) == 0) {
	    compact_lattice_d = ISIP_TRUE;
	  }
	  else {
	    compact_lattice_d = ISIP_FALSE;
	  }
	}
      }      

      // read nbest pruning parameter: maximum paths
      //
      else if (strcmp((char*)tag_str, TP_OP_NBEST_MAX_PATH) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  nbest_max_paths_d = (int_4)atof((char*)tmp3);
	}
      }      

      // read nbest pruning parameter: beam
      //
      else if (strcmp((char*)tag_str, TP_OP_NBEST_BEAM) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  nbest_beam_d = (int_4)atof((char*)tmp3);
	}
      }
    
      // read the segment level mode
      //
      else if (strcmp((char*)tag_str, TP_OP_SEGMENT_MODE) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  if (strcmp((char*)TP_OP_NONE_SEGMENT, (char*)tmp3) == 0){
	    segment_mode_d = TP_NONE_SEGMENT_MODE;
	  }
	  if (strcmp((char*)TP_OP_MODEL_SEGMENT, (char*)tmp3) == 0){
	    segment_mode_d = TP_MODEL_SEGMENT_MODE;	    
	  }
	  else {
	    segment_mode_d = TP_NONE_SEGMENT_MODE;
	  }
	}
      }
      
      // read name of model level segment list
      //
      else if (strcmp((char*)tag_str, TP_OP_MODEL_SEGMENT_LIST) == 0) {
	if (sscanf((char*)tmp, "%s%s%s", tmp1, tmp2, tmp3) == 3) {
	  model_segment_file_d = new char_1[strlen((char*)tmp3) + 1];
	  strcpy((char*)model_segment_file_d, (char*)tmp3); 
	  Decoder::expand_filename_cc(model_segment_file_d);
	}
      }
    }
    
    // otherwise ignore the line
    //
    else {			  
    }
  }
  
  // delete memory
  //
  delete [] tmp;
  delete [] tag_str;
  delete [] tmp1;
  delete [] tmp2;
  delete [] tmp3;
  delete [] tmp4;
  delete [] tmp5;
  
  // return gracefully
  //
  return ISIP_TRUE;
}
