// file: $isip/class/mmedia/FeatureFile/ftrf_03.cc
// version: $Id: ftrf_03.cc 10597 2006-08-08 21:28:39Z ss754 $
//

// isip include files
//
#include "FeatureFile.h"

// method: read
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean indicating status
//
// this method has the object read itself from an Sof file.
//
boolean FeatureFile::read(Sof& sof_a, long tag_a, const String& name_a) {

  // get the instance of the object from the Sof file
  //
  if (!sof_a.find(name_a, tag_a)) {
    return false;
  }

  // read the actual data from the sof file
  //
  if (!readData(sof_a)) {
    return false;
  }

  // exit gracefully
  //
  return true;
}

// method: readData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or FULL_SIZE)
//  boolean param: (input) is the parameter specified?
//  boolean nested: (input) is this nested?
//
// return: a boolean indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean FeatureFile::readData(Sof& sof_a, const String& pname_a,
			      long size_a, boolean param_a,
			      boolean nested_a) {

  // declare local variables
  //
  String empty_str;

  // declare a SofParser object
  //
  SofParser parser;
  parser.setDebug(debug_level_d);

  // ignore the implicit parameter setting
  //
  
  // check if we are nested
  //
  if (nested_a) {
    parser.setNest();
  }
  
  // load the parse
  //
  if (!parser.load(sof_a, size_a)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // read the configuration parameters using the private method
  //
  if (!readConfig(sof_a, parser)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // query the data
  //
  no_data_d = true;
  if (parser.isPresent(sof_a, PARAM_DATA)) {

    // save the object size for later use
    //
    sof_length_pos_d = parser.getEntry(sof_a, PARAM_DATA);
    no_data_d = false;
  }
  
  // make sure all coefficients have been accessed
  //
  if (!parser.checkParams(sof_a)) {
    return Error::handle(name(), L"readData", Error::IO, __FILE__, __LINE__,
                         Error::WARNING);
  }
  
  if ((data_type_d != VECTOR_FLOAT) &&
      (data_type_d != ALGORITHM_DATA)) {
    return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__);
  }
  
  // exit gracefully
  //
  return true;
}

// method: readStart
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size of the object
//  boolean param: (input) is the parameter specified?
//  boolean nested: (input) is this nested?
//
// return: a boolean value indicating status
//
// this method allocates an SofParser object and loads the parse. it is
// used for partial read.
//
boolean FeatureFile::readStart(Sof& sof_a, 
			       const String& pname_a,
			       long size_a, boolean param_a,
			       boolean nested_a) {
  
  String empty_str;
  
  SofParser parser;
  parser.setDebug(debug_level_d);

  // ignore implicit parameter setting
  //
  
  // are we nested?
  //
  if (nested_a) {
    parser.setNest();
  }
  
  // load the parse
  //
  if (!parser.load(sof_a, size_a)) {
    return Error::handle(name(), L"readStart", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // read the configuration
  //
  if (!readConfig(sof_a, parser)) {
    return Error::handle(name(), L"readStart", Error::READ, __FILE__,
			 __LINE__, Error::WARNING);
  }

  // query the data
  //
  no_data_d = true;
  if (parser.isPresent(sof_a, PARAM_DATA)) {
    // save the object size for later use
    //
    sof_length_pos_d = parser.getEntry(sof_a, PARAM_DATA);
    no_data_d = false;
  }
  
  // call the base readStart method
  //
  return true;
}


// method: readConfig
//
// arguments:
//  Sof& sof: (input) sof file object
//  SofParser& parser: (output) parser object
//
// return: a boolean value indicating status
//
// this method reads the configuration parameters from the Sof object
// using the parser object. it assumes the parse has already been
// loaded before calling. this method exists since both the readData
// and readStart methods need to read in these configuration
// parameters so it prevents the duplication of code.
//
boolean FeatureFile::readConfig(Sof& sof_a, SofParser& parser_a) {

  // reset the number of frames
  //
  num_frames_d = DEF_NUM_FRAMES;
  
  // read the name of the feature
  //
  if (parser_a.isPresent(sof_a, PARAM_NAME)) {
    if (!name_d.readData(sof_a, PARAM_NAME,
			 parser_a.getEntry(sof_a, PARAM_NAME))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    name_d.assign(DEF_FEATURE_NAME);
  }

  // read the data
  //
  FILE_TYPE ftype;
  if (parser_a.isPresent(sof_a, PARAM_FILE_TYPE)) {
    if (!FILE_TYPE_MAP.readElementData((long&)ftype, sof_a,
				       PARAM_FILE_TYPE,
				       parser_a.getEntry(sof_a,
							 PARAM_FILE_TYPE))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  
  else {
    ftype = DEF_FILE_TYPE;
  }
  
  FILE_FORMAT fformat;
  if (parser_a.isPresent(sof_a, PARAM_FILE_FORMAT)) {
    if (!FILE_FORMAT_MAP.readElementData((long&)fformat, sof_a,
					 PARAM_FILE_FORMAT,
					 parser_a.getEntry(sof_a,
							 PARAM_FILE_FORMAT))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  
  else {
    fformat = DEF_FILE_FORMAT;
  }

  COMP_TYPE ctype;
  if (parser_a.isPresent(sof_a, PARAM_COMP_TYPE)) {
    if (!COMP_TYPE_MAP.readElementData((long&)ctype, sof_a,
				       PARAM_COMP_TYPE,
			       parser_a.getEntry(sof_a, PARAM_COMP_TYPE))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    ctype = DEF_COMP_TYPE;
  }
  setCompType(ctype);

  if (parser_a.isPresent(sof_a, PARAM_RANGE)) {
    if (!amplitude_range_d.readData(sof_a, PARAM_RANGE,
				    parser_a.getEntry(sof_a, PARAM_RANGE))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }					   
  }
  else {
    amplitude_range_d.assign(DEF_AMPLITUDE_RANGE);
  }

  // read number of channels
  //
  if (parser_a.isPresent(sof_a, PARAM_NUM_CHANNELS)) {
    if (!num_channels_d.readData(sof_a, PARAM_NUM_CHANNELS,
				 parser_a.getEntry(sof_a, PARAM_NUM_CHANNELS),
				 false, false)) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    num_channels_d.assign(DEF_NUM_CHANNELS);
  }  

  // reading id
  //
  if (parser_a.isPresent(sof_a, PARAM_ID)) {
    if (!id_d.readData(sof_a, PARAM_ID,
		       parser_a.getEntry(sof_a, PARAM_ID))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    id_d.clear();
  }
  
  // read number of features 
  //
  if (parser_a.isPresent(sof_a, PARAM_NUM_FEATURES)) {
    if (!num_features_d.readData(sof_a, PARAM_NUM_FEATURES,
				 parser_a.
				 getEntry(sof_a, PARAM_NUM_FEATURES))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    num_features_d.assign(DEF_NUM_FEATURES);
  }
  
  // now we can set the file type
  //
  setFileType(ftype);
  setFileFormat(fformat);

  // get data type
  //
  if (parser_a.isPresent(sof_a, PARAM_DATA_TYPE)) {
    if (!DATA_TYPE_MAP.readElementData((long&)data_type_d, sof_a,
				       PARAM_DATA_TYPE,
				       parser_a.getEntry(sof_a,
							 PARAM_DATA_TYPE))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readConfig", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    data_type_d = DEF_DATA_TYPE;
  }

  // get coef type
  //
  if (parser_a.isPresent(sof_a, PARAM_COEF_TYPE)) {
    if (!AlgorithmData::CTYPE_MAP.readElementData((long&)coef_type_d,
						sof_a, PARAM_COEF_TYPE,
						parser_a.getEntry(sof_a, PARAM_COEF_TYPE))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readConfig", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    coef_type_d = AlgorithmData::DEF_CTYPE;
  }
  
  if (parser_a.isPresent(sof_a, PARAM_TAG)) {
    if (!tag_d.readData(sof_a, PARAM_TAG, parser_a.getEntry(sof_a, PARAM_TAG))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    tag_d.assign(DEF_TAG);
  }

  if (parser_a.isPresent(sof_a, PARAM_BLOCK_SIZE)) {
    if (!block_size_d.readData(sof_a, PARAM_BLOCK_SIZE,
			       parser_a.getEntry(sof_a, PARAM_BLOCK_SIZE))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    block_size_d.assign(DEF_BLOCK_SIZE);
  }

  if (parser_a.isPresent(sof_a, PARAM_BUF_SIZE)) {
    if (!buf_size_d.readData(sof_a, PARAM_BUF_SIZE,
			     parser_a.getEntry(sof_a, PARAM_BUF_SIZE))) {
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    buf_size_d.assign(DEF_BUF_SIZE);
  }

  // read frame duration
  //
  if (parser_a.isPresent(sof_a, PARAM_FRAME_DURATION)) {
    if (!frame_duration_d.readData(sof_a,
				   PARAM_FRAME_DURATION,
				   parser_a.getEntry(sof_a,
						     PARAM_FRAME_DURATION))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  else {
    frame_duration_d.assign(DEF_FRAME_DURATION);
  }
  
  // read sample frequency
  //
  if (parser_a.isPresent(sof_a, PARAM_SAMPLE_FREQUENCY)) {
    if (!sample_frequency_d.readData(sof_a,
				     PARAM_SAMPLE_FREQUENCY,
				     parser_a.getEntry(sof_a,
						       PARAM_SAMPLE_FREQUENCY))) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readConfig", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  else {
    sample_frequency_d.assign(DEF_SAMPLE_FREQ);
  }  
  
  // exit gracefully
  //
  return true;
}






