// file: $isip/class/algo/Algorithm/algo_03.cc
// version: $Id: algo_03.cc 8370 2002-07-14 14:56:24Z picone $
//

// isip include files
//
#include "Algorithm.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 value indicating status
//
// this method has the object read itself from an Sof file according
// to the specified name and tag
//
boolean Algorithm::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
  //
  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 Algorithm::readData(Sof& sof_a, const String& pname_a, long size_a,
			    boolean param_a, boolean nested_a) {

  // we need a parser
  //
  SofParser* parser = new SofParser;
  parser->setDebug(debug_level_d.getIndex());
  
  // 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"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }       
    
  // read the type
  //
  TYPES type;
  if (!TYPES_MAP.readElementData((long&)type, sof_a, PARAM_TYPES,
				 parser->getEntry(sof_a, PARAM_TYPES))) {
    clear();

    if (parser != (SofParser*)NULL) {
      delete parser;
      parser = (SofParser*)NULL;
    }
    
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }      

  // set the type
  //
  clear();
  setType(type);

  // pass the parser to the sub-object for reading. the sub-object
  // will delete the object.
  //
  virtual_algo_d->setParser(parser);
  
  // read the data
  //
  if (!virtual_algo_d->readData(sof_a, pname_a, size_a, param_a, nested_a)) {

    clear();
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }       

  // exit gracefully
  //
  return true;
}
