// file: $isip/class/algo/Math/math_03.cc
// version: $Id: math_03.cc 6945 2001-05-08 05:43:27Z roberts $
//

// isip include files
//
#include "Math.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 Math::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
//
boolean Math::read(Sof& sof_a, long tag_a, const String& name_a) {

  // read 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 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 Math::readData(Sof& sof_a, const String& pname_a,
				  long size_a, boolean param_a,
				  boolean nested_a) {

  // 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 algorithm
  //
  if (parser_d->isPresent(sof_a, PARAM_ALGORITHM)) {
    if (!ALGO_MAP.readElementData((long&)algorithm_d, sof_a, PARAM_ALGORITHM,
                                  parser_d->getEntry(sof_a, PARAM_ALGORITHM))){

      // 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 {
    algorithm_d = DEF_ALGORITHM;
  }

  // check known algorithms: invoke the proper lower-level method
  //
  if (algorithm_d == FUNCTION_CALCULATOR) {
    return readDataCommon(sof_a, pname_a, param_a, nested_a);
  }

  // exit ungracefully
  //
  return Error::handle(name(), L"readData",
                       Error::ARG, __FILE__, __LINE__);
}

// method: readDataCommon
//
// 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 value indicating status
//
// this method has been implemented for FUNCTION_CALCULATOR
// algorithm. this method has the object read itself from an Sof
// file. it assumes that the Sof file is already positioned correctly.
//
boolean Math::readDataCommon(Sof& sof_a, const String& pname_a,
			     long size_a, boolean param_a,
			     boolean nested_a) {

  // get the implementation
  //
  if (parser_d->isPresent(sof_a, PARAM_IMPLEMENTATION)) {
    if (!IMPL_MAP.readElementData((long&)implementation_d, sof_a,
				  PARAM_IMPLEMENTATION,
				  parser_d->getEntry(sof_a,
						     PARAM_IMPLEMENTATION))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readDataCommon", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    implementation_d = DEF_IMPLEMENTATION;
  }
  
  // read the input type
  //
  if (parser_d->isPresent(sof_a, PARAM_NUMBER)) {
    if (!num_operands_d.readData(sof_a, PARAM_NUMBER,
                        parser_d->getEntry(sof_a, PARAM_NUMBER))) {
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readDataCommon",
			   Error::READ, __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    num_operands_d = DEF_NUM_OPERANDS;
  }

  // get the constant
  //
  if (parser_d->isPresent(sof_a, PARAM_CONST)) {
    if (!const_d.readData(sof_a, PARAM_CONST,
                        parser_d->getEntry(sof_a, PARAM_CONST))) {

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

      // return a warning message
      //
      return Error::handle(name(), L"readDataCommon",
			   Error::READ, __FILE__,
                           __LINE__, Error::WARNING);
    }
  }
  else {
    const_d = DEF_CONST;
  }

  // get the function, operation and weight sequence
  //
  if (parser_d->isPresent(sof_a, PARAM_FUNCTION)) {

    boolean res = true;

    // for text mode, read it as a string and parse it into the function
    //
    if (sof_a.isText()) {
      String str;

      res = str.readData(sof_a, PARAM_FUNCTION,
			 parser_d->getEntry(sof_a, PARAM_FUNCTION));
      if (res) {
	setFunction(str);
      }
    }

    // for binary mode, just read the VectorLong
    //
    else {
      res = function_d.readData(sof_a, PARAM_FUNCTION,
				parser_d->getEntry(sof_a, PARAM_FUNCTION),
				false, false);
    }

    // check for error
    //
    if (!res) {

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

      // return a warning message
      //
      return Error::handle(name(), L"readDataCommon",
			   Error::READ, __FILE__,
                           __LINE__, Error::WARNING);
    }
  }
  else {
    function_d.clear();
  }
  
  if (parser_d->isPresent(sof_a, PARAM_OPERATION)) {

    boolean res = true;

    // for text mode, read it as a string and parse it into the operation
    //
    if (sof_a.isText()) {
      String str;

      res = str.readData(sof_a, PARAM_OPERATION,
			 parser_d->getEntry(sof_a, PARAM_OPERATION));
      if (res) {
	setOperation(str);
      }
    }

    // for binary mode, just read the VectorLong
    //
    else {
      res = operation_d.readData(sof_a, PARAM_OPERATION,
				parser_d->getEntry(sof_a, PARAM_OPERATION),
				false, false);
    }

    // check for error
    //
    if (!res) {

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

      // return a warning message
      //
      return Error::handle(name(), L"readDataCommon",
			   Error::READ, __FILE__,
                           __LINE__, Error::WARNING);
    }
  }
  else {
    operation_d.clear();
  }

  if (parser_d->isPresent(sof_a, PARAM_WEIGHT)) {
    if (!weight_d.readData(sof_a, PARAM_WEIGHT,
			   parser_d->getEntry(sof_a, PARAM_WEIGHT),
			   false, false)) {
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;

      // return a warning message
      //
      return Error::handle(name(), L"readDataCommon",
			   Error::READ, __FILE__,
                           __LINE__, Error::WARNING);
    }
  }
  else {
    weight_d.clear();
  }

  // get the debug level
  //
  if (parser_d->isPresent(sof_a, PARAM_DBGL)) {
    if (!debug_level_d.readData(sof_a, PARAM_DBGL,
                                parser_d->getEntry(sof_a, PARAM_DBGL))) {

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

      // return a warning message
      //
      return Error::handle(name(), L"readDataCommon",
			   Error::IO, __FILE__, __LINE__,
                           Error::WARNING);
    }
  }
  else {
    debug_level_d = Integral::DEF_DEBUG;
  }

  // check that all parameters are accounted for
  //
  if (!parser_d->checkParams(sof_a)) {

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

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

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

  // exit gracefully
  //
  return true;
}    
