// file: itri_trans_0.cc
//
// isip include files
//
#include "init_triphones.h"
#include "init_triphones_constants.h"
 
// method: read_trans_cc
//
// arguments:
//  FILE* file : (input) data file handle
//  int_4& num_trans : (output) number of transitions
//  int_4*& num_states : (output) number of states in model
//  
// return: a float_4*** pointing to a list of transition matrices
//
float_4*** read_trans_cc(FILE* file_a, int_4& num_trans_a,
			 int_4*& num_states_a) {
  
  // variables to read data
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  int_4 num_states = (int_4)0;
  float_4*** trans_mat = (float_4***)NULL;
  logical_1 number_flag = ISIP_FALSE;
  
  // read data from file
  //
  while (fscanf(file_a, "%s", tmp) != EOF) {

    // ignore comment lines
    //
    if (tmp[0] == (char_1)'#') {

      // do nothing
      //
      fscanf(file_a, "%[^\n]", tmp);
      fscanf(file_a, "%[\n]", tmp);
    }

    // read the transition matrix data
    //
    else if (strcmp((char*)tmp, (char*)ITRI_STR_NUM_TRANSITIONS) == 0) {

      // change the flag
      //
      number_flag = ISIP_TRUE;
      
      // read the number of transitions
      //
      fscanf(file_a, "%s", tmp);
      fscanf(file_a, "%ld", &num_trans_a);
      
      // allocate space for the tran list
      //
      trans_mat = new float_4**[num_trans_a];

      // allocate space for num_states_a
      //
      num_states_a = new int_4[num_trans_a];
      
      // now read the transitions data
      //
      for (int_4 i = 0; i < num_trans_a; i++) {
	
	// read the transition index and number of states
	//
	fscanf(file_a, "%s%ld", tmp, &num_states);
	num_states_a[i] = num_states;
	
	// allocate memory for the data
	//
	trans_mat[i] = new float_4*[num_states];
	for (int_4 j = 0; j < num_states; j++) {
	  trans_mat[i][j] = new float_4[num_states];

	  // read the transitions
	  //
	  for (int_4 k = 0; k < num_states; k++) {
	    fscanf(file_a, "%f", &trans_mat[i][j][k]);
	  }
	}
      } // end data loop
    } // end else if
  } // end while

  // check the flag
  //
  if (number_flag == ISIP_FALSE) {
    fprintf(stdout, "Error : no number of transitions specified.\n");
    exit(ISIP_PROTO_ERROR);
  }

  // free memory
  //
  delete [] tmp;
  
  // exit gracefully
  //
  return trans_mat;
}
