// file: ht_param_0.cc
//
// function to read data from parameter file
//

// system include files
//
#include <stdlib.h>

// isip include files
//
#include "tie_state.h"
#include "tie_state_constants.h"

// method: read_params_cc
//
// arguments:
//  char_1* params_file : (input) file pointer to params file
//  char_1*& in_models_file : (output) name of file containing input models
//  char_1*& out_models_file : (output) name of file containing output model
//  char_1*& in_states_file : (output) name of file containing input states
//  char_1*& out_states_file : (output) name of file containing output states
//  char_1*& monophone_file : (output) name of file containing monophones
//  char_1*& membership_file : (output) name of file with membership
//  char_1*& occupancy_file : (output) name of file with state occupancy
//  char_1*& out_tree_file : (output) name of file to output decision trees
//  char_1*& in_tree_file : (output) name of file to input decision trees
//  char_1*& in_phlist_file : (output) name of file with all phones
//  char_1*& in_clist_file : (output) name of clustered models list file
//  char_1*& special_models_file : (output) name of file with special models
//  float_4& split_threshld : (output) threshold for splitting node
//  float_4& merge_threshld : (output) threshold for merging node
//  float_4& num_occ_threshld : (output) threshold of the minimum num of
//                               state occupancies at each node
//  int_4& num_context : (output) number of context in models
//  logical_1& mode : (output) the operating mode - train or test
//  logical_1& output_mode : (output) format for the output states file
//  int_4& debug_level : (output) the debug level
//
// return: a logical flag to indicate success
//
logical_1 read_params_cc(char_1* params_file_a, char_1*& in_models_file_a,
			 char_1*& out_models_file_a, char_1*& in_states_file_a,
			 char_1*& out_states_file_a, char_1*& monophone_file_a,
			 char_1*& membership_file_a, char_1*& occupancy_file_a,
			 char_1*& out_tree_file_a, char_1*& in_tree_file_a,
			 char_1*& in_phlist_file_a, char_1*& out_clist_file_a,
			 char_1*& special_models_file_a,
			 float_4& split_threshd_a, float_4& merge_threshd_a,
			 float_4& num_occ_threshd_a, int_4& num_context_a,
			 logical_1& mode_a, logical_1& output_mode_a,
			 int_4& debug_level_a) {


  // open the parameter file
  //
  FILE* fp_param = fopen((char*)params_file_a, "r");
  
  if (fp_param == (FILE*)NULL) {
    fprintf(stdout, "Cannot open file %s\n", params_file_a);
    exit(ISIP_PROTO_ERROR);
  }
  
  // string to hold temporary data
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  
  // read data from file
  //
  while (fscanf(fp_param, "%s", tmp) != EOF) {
    
    // ignore comment lines 
    //
    if (tmp[0] == (char_1)'#') {
      
      // do nothing
      //
      fscanf(fp_param, "%[^\n]", tmp);
      fscanf(fp_param, "%[\n]", tmp);
    }

    // read name of special models list file
    //
    else if (strcmp((char*)tmp, (char*)TS_SPE_MODELS ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      special_models_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)special_models_file_a, (char*)tmp); 
      expand_filename_cc(special_models_file_a);
    }
   
    // read name of input and output models files
    //
    else if (strcmp((char*)tmp, (char*)TS_IN_MODELS ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      in_models_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)in_models_file_a, (char*)tmp);
      expand_filename_cc(in_models_file_a);
    }

    else if (strcmp((char*)tmp, (char*)TS_OUT_MODELS ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      out_models_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)out_models_file_a, (char*)tmp);
      expand_filename_cc(out_models_file_a);
    }

    // read the input  and output states files
    //
    else if (strcmp((char*)tmp, (char*)TS_IN_STATES ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      in_states_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)in_states_file_a, (char*)tmp);
      expand_filename_cc(in_states_file_a);
    }

    else if (strcmp((char*)tmp, (char*)TS_OUT_STATES ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      out_states_file_a = new char_1[ISIP_MAX_STRING_LENGTH];  
      strcpy((char*)out_states_file_a, (char*)tmp); 
      expand_filename_cc(out_states_file_a);
    }
    
    else if (strcmp((char*)tmp, (char*)TS_MONOPHONE ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      monophone_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)monophone_file_a, (char*)tmp); 
      expand_filename_cc(monophone_file_a);
    }
    
    else if (strcmp((char*)tmp, (char*)TS_MEMBERSHIP ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      membership_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)membership_file_a, (char*)tmp); 
      expand_filename_cc(membership_file_a);
    }

    else if (strcmp((char*)tmp, (char*)TS_OCCUPANCY ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      occupancy_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)occupancy_file_a, (char*)tmp); 
      expand_filename_cc(occupancy_file_a);
    }

    else if (strcmp((char*)tmp, (char*)TS_OUT_TREE ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      out_tree_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)out_tree_file_a, (char*)tmp); 
      expand_filename_cc(out_tree_file_a);
    }
    
    else if (strcmp((char*)tmp, (char*)TS_IN_TREE ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      in_tree_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)in_tree_file_a, (char*)tmp); 
      expand_filename_cc(in_tree_file_a);
    }

    else if (strcmp((char*)tmp, (char*)TS_OUT_CLIST ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      out_clist_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)out_clist_file_a, (char*)tmp); 
      expand_filename_cc(out_clist_file_a);
    }
    
    else if (strcmp((char*)tmp, (char*)TS_IN_PHONE ) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      in_phlist_file_a = new char_1[ISIP_MAX_STRING_LENGTH];
      strcpy((char*)in_phlist_file_a, (char*)tmp); 
      expand_filename_cc(in_phlist_file_a);
    }

    else if (strcmp((char*)TS_SPLIT_THRESHOLD, (char*)tmp) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      split_threshd_a = (float_4)atof((char*)tmp);
    }
    
    else if (strcmp((char*)TS_MERGE_THRESHOLD, (char*)tmp) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      merge_threshd_a = (float_4)atof((char*)tmp);
    }
    
    else if (strcmp((char*)TS_NUM_OCC_THRESHOLD, (char*)tmp) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      num_occ_threshd_a = (float_4)atof((char*)tmp);
    }
    
    else if (strcmp((char*)TS_NUM_CONTEXT, (char*)tmp) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      num_context_a = (int_4)atoi((char*)tmp);
    }

    else if (strcmp((char*)TS_DEBUG_LEVEL, (char*)tmp) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      debug_level_a = (int_4)atoi((char*)tmp);
    }
    
    // read the modes
    //
    else if (strcmp((char*)tmp, (char*)TS_MODE) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      if (strcmp((char*)tmp, (char*)TS_TRAIN) == 0) {
        mode_a = TS_TRAIN_MODE;
      }
      else if (strcmp((char*)tmp, (char*)TS_TEST) == 0) {
        mode_a = TS_TEST_MODE;
      }
    }

    else if (strcmp((char*)tmp, (char*)TS_OUTPUT_MODE) == 0) {
      fscanf(fp_param, "%s", tmp);
      fscanf(fp_param, "%s", tmp);
      if (strcmp((char*)tmp, (char*)TS_ASCII) == 0) {
        output_mode_a = TS_ASCII_MODE;
      }
      else if (strcmp((char*)tmp, (char*)TS_BINARY) == 0) {
        output_mode_a = TS_BINARY_MODE;
      }
    }
    
    // otherwise ignore the line
    //
    else {
      fscanf(fp_param, "%[^\n]", tmp);
      fscanf(fp_param, "\n");
    }
  }

  
  // delete memory
  //
  delete [] tmp;

  // close the parameter file
  //
  fclose(fp_param);
  
  // return gracefully
  //
  return(ISIP_TRUE);
}
  





