// file: dec_read_0.cc
//
 
// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"
 
// method: read_transitions_cc
//
// arguments:
//  FILE* fp: (input) file pointer
//  
// return: a logical_1 indicating status
//
// this method reads in the transition matrices from file into an
// array
//
logical_1 Decoder::read_transitions_cc(FILE* fp_a) {

  // check file
  //
  if (fp_a == (FILE*)NULL) {
    error_handler_cc((char_1*)"read_transitions_cc",
		     (char_1*)"NULL input file pointer");
  }
  
  // variables to read data
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  int_4 len = (int_4)0;
  int_4 ind = (int_4)0;
  float_4 val = (float_4)0;
  
  // read data from file
  //
  while (fscanf(fp_a, "%s", tmp) != EOF) {

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

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

    // read the transition matrix data
    //
    else if (strcmp((char*)tmp, "num_transitions") == 0) {
      
      // read the number of transitions
      //
      fscanf(fp_a, "%s", tmp);
      fscanf(fp_a, "%ld", &num_trans_d);
      
      // allocate space for the tran list
      //
      transitions_d = new float_4*[num_trans_d];
      
      // now read the transitions data
      //
      for (int_4 i = 0; i < num_trans_d; i++) {
	
	// read the transition index and number of states
	//
	fscanf(fp_a, "%s%ld", tmp, &len);
	
	// allocate memory for the data
	//
	transitions_d[i] = new float_4[len*len];
	
	// read the transitions
	//
	for (int_4 j = 0; j < len; j++) {
	  for (int_4 k = 0; k < len; k++) {

	    // read the score value
	    //
	    ind = j*len + k;
	    fscanf(fp_a, "%f", &val);

	    // convert this to log score
            //
            if (val == (float_4)0) {
              transitions_d[i][ind] = (float_4)DEC_DEFAULT_SCORE;
            }
            else {
              transitions_d[i][ind] = (float_4)log(val);
            }
	  } // end for k
	} // end for j
      } // end for i
    } // end else if
  } // end while

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