// file: bw_phone_0.cc
//
// isip include files
//
#include "bw_train.h"
#include "bw_train_constants.h"
 
// method: read_monophones_cc
//
// arguments:
//  int_4& num_mono : (output) number of monophones
//  char_1* file : (input) data file name
//  
// return: a char_1** pointing to a list of monophone labels
//

// this method reads the monophones file
//
char_1** read_monophones_cc(int_4& num_mono_a, char_1* file_a) {

  // open 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];
  char_1** mphones = (char_1**)NULL;
  logical_1 number_flag = ISIP_FALSE;
  
  // 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 if (strcmp((char*)tmp, (char*)BW_STR_NUM_MONOPHONES) == 0) {

      // change the flag
      //
      number_flag = ISIP_TRUE;
      
      // read the number of monophones
      //
      fscanf(fp, "%s", tmp);
      fscanf(fp, "%ld", &num_mono_a);
      
      // allocate space for the monophone list
      //
      mphones = new char_1*[num_mono_a];
      
      // now read the monophone data
      //
      for (int_4 i = 0; i < num_mono_a; i++) {
	
	// read the monophone string and copy it
	//
	fscanf(fp, "%s", tmp);
	mphones[i] = new char_1[strlen((char*)tmp) + 1];
	strcpy((char*)mphones[i], (char*)tmp);
      }
    }
  }

  // check the flag
  //
  if (number_flag == ISIP_FALSE) {
    fprintf(stdout, "Error : no number of monophones specified in %s\n",
	    (char*)file_a);
    exit(ISIP_PROTO_ERROR);
  }

  // close file
  //
  fclose(fp);
    
  // free memory
  //
  delete [] tmp; tmp = (char_1*)NULL;
  
  // exit gracefully
  //
  return mphones;
}
