// file: ht_phone_5.cc
//

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

// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"
 
// method: read_sph_cc
//
// arguments:
//  int_4& num_sph : (output) number of special phones
//  char_1** monophones : (input) monophones list
//  int_4 num_mono : (input) number of monophones
//  char_1* file : (input) data file name
//  
// return: an int_4* pointing to a list of special phone indices
//
int_4* read_sph_cc(int_4& num_sph_a, char_1** monophones_a, int_4 num_mono_a,
		   char_1* file_a) {

  // open special phone list file
  //
  FILE* fp = fopen((char*)file_a, "r");
  if (fp == (FILE*)NULL) {
    fprintf(stdout, "Cannot open file %s\n", file_a);
    exit(ISIP_PROTO_ERROR);
  }
  
  // variables to read data
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  int_4* sph_phn = new int_4[HT_MAX_NUM_SPH];
  
  // read data from file
  //
  while (fscanf(fp, "%s", tmp) != EOF) {
    
    // ignore comment lines
    //
    if (tmp[0] == (char_1)'#') {
      
      // do nothing
      //
      fscanf(fp, "%[^\n]", tmp);
      fscanf(fp, "%[\n]", tmp);
    }
    
    // read the monophone data
    //
    else {

      // read the monophone string and copy it
      //
      fscanf(fp, "%s", tmp);
      
      // get the phone index
      //
      for(int_4 j = 0; j < num_mono_a; j++) {

	if(strcmp((char*)tmp, (char*)monophones_a[j]) == 0) {

	  if (num_sph_a == HT_MAX_NUM_SPH - 1) {
	    fprintf(stdout, "%s:\texceeded max number of special phones\n",
		    file_a);
	    exit(ISIP_PROTO_ERROR);
	  }
	  
	  sph_phn[num_sph_a] = j;
	  num_sph_a++;
	  break;
	}
      }
    }
    
    // reset the temporary buffer
    //
    tmp = (char_1*)memset(tmp, (int_4)0, ISIP_MAX_STRING_LENGTH);        
  }
  
  // close file
  //
  fclose(fp);
    
  // free memory
  //
  delete [] tmp;
  
  // exit gracefully
  //
  return sph_phn;
}
