// file: bw_varf_0.cc
//

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

// isip include files
//
#include "bw_train.h"
#include "bw_train_constants.h"

// method: read_varf_cc
//
// arguments:
//  float_4* var_floor : (output) variance floor
//  int_4 num_features : (input) number of features
//  char_1* var_floor_file : (input) name of variance floor file 
//
// return: a logical_1 flag indicating success
//
// this method reads the variance floor 
//
logical_1 read_varf_cc(float_4* var_floor_a, int_4 num_features_a,
		       char_1* var_floor_file) {

  // open the file for reading
  //
  FILE* f_vf = fopen((char*)var_floor_file, "r");
  if (f_vf == (FILE*)NULL) {
    fprintf(stdout, "Cannot open file %s\n", var_floor_file);
    exit(ISIP_PROTO_ERROR);
  }

  // declare temporary variables
  //
  char_1* tmp = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* tmp1 = (char_1*)NULL;
  if (fgets((char*)tmp, ISIP_MAX_STRING_LENGTH, f_vf) != (char*)NULL) {

    if (tmp[strlen((char*)tmp)-1] == ISIP_NEWLINE) {
      tmp[strlen((char*)tmp)-1] = (char_1)ISIP_STRING_SPACE[0];
    }
    tmp1 = (char_1*)strtok((char*)tmp, ISIP_STRING_SPACE);

    if (tmp1 == (char_1*)NULL) {
      fprintf(stdout, "Error, cannot read enough data from %s\n",
	      var_floor_file);
      exit(ISIP_PROTO_ERROR);
    }

    // read the first variance floor value from the file
    //
    var_floor_a[0] = atof((char*)tmp1);

    // read the subsequent values
    //
    for (int_4 i = 1; i < num_features_a; i++) {
      tmp1 = (char_1*)strtok(NULL, ISIP_STRING_SPACE);
      if (tmp1 == (char_1*)NULL) {
	fprintf(stdout, "Error, cannot read enough data from %s\n",
		var_floor_file);
	exit(ISIP_PROTO_ERROR);
      }
      var_floor_a[i] = atof((char*)tmp1);
    }
  }

  // error message
  //
  else {
    fprintf(stdout, "Error: cannot read from file %s\n", var_floor_file);
    exit(ISIP_PROTO_ERROR);
  }

  // clean up
  //
  delete [] tmp;
  fclose(f_vf);
    
  // exit gracefully
  //
  return ISIP_TRUE;
}


