// file: ts_answers_0.cc
//

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

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

// method: read_answers_cc
//
// arguments:
//    FILE* fpq: (input)  file pointer to membership file
//    Hash_table*& answers: (output) Pointer to answers
//    char_1** mono           : (input)  monophone list
//    int_4 num_mono          : (input)  number of monophones
//
// return: a logical_1 showing status
//
// this is the method to set state related informaiton to all decision trees
//
logical_1 read_answers_cc(FILE* fpq_a, Hash_table*& answers_a,
			  char_1** mono_a, int_4 num_mono_a) { 
	  
  // variables to read data
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* attr = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* dire = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* key_string = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* phn = (char_1*)NULL;
  int_4 phn_num = (int_4)0;
  Hash_cell* hash_cell;
  void_p item = (void_p)NULL;

  answers_a = new Hash_table(HASH_TABLE_SIZE);

  fseek(fpq_a,0,SEEK_SET);
  
  // read data from file
  //
  while (fscanf(fpq_a, "%s", tmp) != EOF) {
    
    // ignore comment lines
    //
    if (tmp[0] == (char_1)'#') {
      
      // do nothing
      //
      fscanf(fpq_a, "%[^\n]", tmp);
      fscanf(fpq_a, "%[\n]", tmp);
    }

    else {
      strcpy((char*)dire, (char*)tmp);
      fscanf(fpq_a, "%s", attr);    
      fscanf(fpq_a, "%s", tmp);     // ":"
      fscanf(fpq_a, "%[^\n]", tmp); // all the phones in the answer lists
      
    }
    phn = (char_1*)strtok((char*)tmp, ISIP_STRING_SPACE);
    
    for (int_4 k = 0; k < num_mono_a; k++) {
      if (strcmp((char*)phn, (char*)mono_a[k]) == 0) {
	phn_num = k;
	break;
      }
    }

    sprintf((char*)key_string, "%ld", phn_num);
    strcat((char*)key_string, (char*)ISIP_STRING_SPACE);
    strcat((char*)key_string, (char*)dire);
    strcat((char*)key_string, (char*)ISIP_STRING_SPACE);
    strcat((char*)key_string, (char*)attr);
    
    hash_cell = new Hash_cell(item, key_string);
    answers_a->hash_insert_cc(hash_cell);
    
    while ((phn = (char_1*)strtok(NULL, ISIP_STRING_SPACE))) {
      for (int_4 k = 0; k < num_mono_a; k++) {
	if (strcmp((char*)phn, (char*)mono_a[k]) == 0) {
	  phn_num = k;
	  break;
	}
      }
      
      sprintf((char*)key_string, "%ld", phn_num);
      strcat((char*)key_string, (char*)ISIP_STRING_SPACE);
      strcat((char*)key_string, (char*)dire);
      strcat((char*)key_string, (char*)ISIP_STRING_SPACE);
      strcat((char*)key_string, (char*)attr);
      
      hash_cell = new Hash_cell(item, key_string);
      answers_a->hash_insert_cc(hash_cell);	
    }
    
    fscanf(fpq_a, "%[\n]", tmp);
  }

  // release memory
  //
  delete [] tmp ; 
  delete [] attr ; 
  delete [] dire ; 
  delete [] key_string ;
  delete [] phn ;

  // exit gracefully
  //
  return ISIP_TRUE;
}
