// file: $isip_ifc/class/mmedia/AlgorithmData/algd_03.cc
// version: $Id: algd_03.cc,v 1.6 2003/02/13 20:39:32 srinivas Exp $
//

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

// ------------------------------------------------------------------
// these methods have to be in the same file so they can use the same
// static parser pointer
// ------------------------------------------------------------------

// declare a static sof parser pointer
//
static SofParser* parser_d = (SofParser*)NULL;

// method: setParser
//
// arguments:
//  SofParser* parser: (input) sof file object
//
// return: a boolean value indicating status
//
// this method sets the parser from the parent object to be used in
// the next readData method
//
boolean AlgorithmData::setParser(SofParser* parser_a) {

  // set the parser
  //
  parser_d = parser_a;

  // exit gracefully
  //
  return true;
}

// 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 value indicating status
//
// this method has the object read itself from an Sof file according
// to the specified name and tag
//
boolean AlgorithmData::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
  //
  return readData(sof_a);
}

// 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_OBJECT)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly
//
boolean AlgorithmData::readData(Sof& sof_a, const String& pname_a,
			 long size_a, boolean param_a, boolean nested_a) {

  // declare local variables
  //
  boolean status = true;

  // we need a parser
  // 
  if (parser_d == (SofParser*)NULL) {
    
    parser_d = new SofParser;
    
    if (nested_a) {
      parser_d->setNest();
    }
    
    // load the parser
    //
    if (!parser_d->load(sof_a, size_a)) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
                           __FILE__, __LINE__, Error::WARNING);
    }           
  }

  // set the debug level of the parser
  //
  parser_d->setDebug(debug_level_d.getIndex());
    
  // get the data type
  //
  DATA_TYPE dtype;
  if (parser_d->isPresent(sof_a, PARAM_DATA_TYPE)) {
    if (!DTYPE_MAP.readElementData((long&)dtype, sof_a, PARAM_DATA_TYPE,
				  parser_d->getEntry(sof_a, PARAM_DATA_TYPE))) {

      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    dtype = DEF_DTYPE;
  }
  //  data_type_d = dtype;
  setDataType(dtype);

  // get the coef type
  //
  if (parser_d->isPresent(sof_a, PARAM_COEF_TYPE)) {
    if (!CTYPE_MAP.readElementData((long&)coef_type_d, sof_a,
				   PARAM_COEF_TYPE,
				   parser_d->getEntry(sof_a,
						      PARAM_COEF_TYPE))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    coef_type_d = DEF_CTYPE;
  }
  
  if (parser_d->isPresent(sof_a, PARAM_DATA)) {

    // cast data variable according to data type and then read data
    //
    switch (data_type_d) {
    case VECTOR_FLOAT:
      status = getVectorFloat().readData(sof_a, PARAM_DATA,
					 parser_d->getEntry(sof_a, PARAM_DATA),
					 false, false);
      break;
    case MATRIX_FLOAT:
      status = getMatrixFloat().readData(sof_a, PARAM_DATA,
					 parser_d->getEntry(sof_a, PARAM_DATA),
					 false, true);
      
      break;
    case VECTOR_DOUBLE:
      status = getVectorDouble().readData(sof_a, PARAM_DATA,
					  parser_d->getEntry(sof_a, PARAM_DATA),
					  false, false);
      break;
    case MATRIX_DOUBLE:
      status = getMatrixDouble().readData(sof_a, PARAM_DATA,
					  parser_d->getEntry(sof_a, PARAM_DATA),
					  false, true);
      break;
    case VECTOR_COMPLEX_FLOAT:
      status = getVectorComplexFloat().readData(sof_a, PARAM_DATA,
						parser_d->getEntry(sof_a, PARAM_DATA),
						false, false);
      break;
    case MATRIX_COMPLEX_FLOAT:
      status = getMatrixComplexFloat().readData(sof_a, PARAM_DATA,
						parser_d->getEntry(sof_a, PARAM_DATA),
						false, true);
      break;
    case VECTOR_COMPLEX_DOUBLE:
      status = getVectorComplexDouble().readData(sof_a, PARAM_DATA,
						 parser_d->getEntry(sof_a, PARAM_DATA),
						 false, false);
      break;
    case MATRIX_COMPLEX_DOUBLE:
      status = getMatrixComplexDouble().readData(sof_a, PARAM_DATA,
						 parser_d->getEntry(sof_a, PARAM_DATA),
						 false, true);
      break;
    case COMBINATION:
      return Error::handle(name(), L"read", Error::NOT_IMPLEM,
      			   __FILE__, __LINE__);

    default:
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  
  // check that all parameters are accounted for and that
  // we didn't error while reading data
  //
  if (!parser_d->checkParams(sof_a) || !status) {
    
    // delete the parser
    //
    delete parser_d;
    parser_d = (SofParser*)NULL;

    // return a warning message
    //
    return Error::handle(name(), L"readData", Error::IO,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // delete the parser
  //
  delete parser_d;
  parser_d = (SofParser*)NULL;

  // exit gracefully
  //
  return status;
}
