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

// isip include files
//
#include "keyword_search.h"
#include "keyword_search_constants.h"

// method: read_params_cc
//
// arguments:
//  FILE* fp : (input) file pointer to params file
//  char_1*& monophones_file : (output) name of monophones file
//  char_1*& word_lex_file : (output) name of lexicon file
//  char_1*& lex_file : (output) name of lexicon file
//  char_1*& lm_file : (output) name of language model file
//  char_1*& input_file : (output) name of the input file
//  char_1*& output_file : (output) name of the output file
//  int_4& ngram : (output) n-gram order
//  int_4& nbst : (output) n-best word hyps
//  int_4& max_paths : (output) max number of partial paths per frame
//  float_8& beam : (output) beam widths for pruning
//  float_8& lm_scale : (output) lm scale factor
//  float_8& wd_penalty : (output) word insertion penalty
//
// return: a logical flag to indicate success
// 
logical_1 read_params_cc(FILE* fp_a, char_1*& monophones_file_a, 
			 char_1*& word_lex_file_a, 
			 char_1*& phone_lex_file_a, char_1*& lm_file_a,
			 char_1*& search_words_file_a, char_1*& input_file_a, 
			 char_1*& output_file_a, int_4& ngram_a, int_4& nbst_a,
			 int_4& max_paths_a, float_8& beam_a, 
			 float_8& lm_scale_a, float_8& wd_penalty_a) {
 
  // string to hold temporary data
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  
  // read data from file
  //
  while (fscanf(fp_a, "%s", tmp) != EOF) {
    
    // ignore comment lines
    //
    if (tmp[0] == (char_1)'#') {
      fscanf(fp_a, "%[^\n]", tmp);
      fscanf(fp_a, "%[\n]", tmp);
    }

    // read name of the monophones data file
    //
    else if (strcmp((char*)tmp, KSRCH_OP_MONOPHONES_FILE) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      monophones_file_a = new char_1[strlen((char*)tmp) + 1];
      strcpy((char*)monophones_file_a, (char*)tmp); 
      Decoder::expand_filename_cc(monophones_file_a);
    }

    // read name of word lexicon data file
    //
    else if (strcmp((char*)tmp, KSRCH_OP_WORD_LEXICON_FILE) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      word_lex_file_a = new char_1[strlen((char*)tmp) + 1];
      strcpy((char*)word_lex_file_a, (char*)tmp); 
      Decoder::expand_filename_cc(word_lex_file_a);
    }

    // read name of lexicon data file
    //
    else if (strcmp((char*)tmp, KSRCH_OP_PHONE_LEXICON_FILE) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      phone_lex_file_a = new char_1[strlen((char*)tmp) + 1];
      strcpy((char*)phone_lex_file_a, (char*)tmp); 
      Decoder::expand_filename_cc(phone_lex_file_a);
    }
    
    // read name of language model data file
    //
    else if (strcmp((char*)tmp, KSRCH_OP_LM_FILE) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      lm_file_a = new char_1[strlen((char*)tmp) + 1];
      strcpy((char*)lm_file_a, (char*)tmp); 
      Decoder::expand_filename_cc(lm_file_a);
    }  

    // read name of search words file
    //
    else if (strcmp((char*)tmp, KSRCH_OP_SEARCH_WORDS_FILE) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      search_words_file_a = new char_1[strlen((char*)tmp) + 1];
      strcpy((char*)search_words_file_a, (char*)tmp); 
      Decoder::expand_filename_cc(search_words_file_a);
    }  

    // read the name of the output file list
    //
    else if (strcmp((char*)tmp, KSRCH_OP_INPUT_LIST) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      input_file_a = new char_1[strlen((char*)tmp) + 1];
      strcpy((char*)input_file_a, (char*)tmp); 
      Decoder::expand_filename_cc(input_file_a);
    }

    // read the name of the output file list
    //
    else if (strcmp((char*)tmp, KSRCH_OP_OUTPUT_LIST) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      output_file_a = new char_1[strlen((char*)tmp) + 1];
      strcpy((char*)output_file_a, (char*)tmp); 
      Decoder::expand_filename_cc(output_file_a);
    }

    // read the value for number of nbest paths to output
    //
    else if (strcmp((char*)tmp, KSRCH_OP_NBEST) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      nbst_a = (int_4)atoi((char*)tmp);
    }

    // set the beam widths if beam pruning is allowed
    //
    else if (strcmp((char*)tmp, KSRCH_OP_MAX_PATHS) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      max_paths_a = (int_4)atoi((char*)tmp);
    }

    // set the beam widths if beam pruning is allowed
    //
    else if (strcmp((char*)tmp, KSRCH_OP_NBEST_BEAM) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      beam_a = (float_8)atof((char*)tmp);
    }

    // read the value for lm scale factor
    //
    else if (strcmp((char*)tmp, KSRCH_OP_LMSCALE) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      lm_scale_a = (float_8)atof((char*)tmp);
    }

    // read the value for word insertion penalty
    //
    else if (strcmp((char*)tmp, KSRCH_OP_WORDPENALTY) == 0) {
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%s", tmp);
      wd_penalty_a = (float_8)atof((char*)tmp);
    }

    // otherwise ignore the line
    //
    else {
      fscanf(fp_a, "%[^\n]", tmp);
      fscanf(fp_a, "\n");
    }
  }
  
  // delete memory
  //
  delete [] tmp;
  
  // return gracefully
  //
  return ISIP_TRUE;
}
