// file: $isip/class/sp/Component/cmp_04.cc
// version: $Id: cmp_04.cc 8351 2002-07-12 13:07:31Z picone $
//

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

// method: sofSize
//
// arguments: none 
//
// return: size of the object
//
// this method returns the size of the object in the Sof file and is
// used for binary write
//
long Component::sofSize() const {

  // start with the space required for the input names
  //
  String str;
  joinName(str);
  long bytes = str.sofSize();

  // add the space required for the output name
  //
  bytes += output_name_d.sofSize();

  // add the space required for the algorithm
  //
  bytes += algo_d.sofSize();

  // return the size
  //
  return bytes;
}

// method: write
//
// 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 write itself to an Sof file
//
boolean Component::write(Sof& sof_a, long tag_a, const String& name_a) const {

  // declare local variable
  //
  long obj_size;
  
  // write the instance of the object into the Sof file
  //
  if (sof_a.isText()) {

    // set the size to be dynamic
    //
    obj_size = Sof::ANY_SIZE;
  }
  else {

    // set the size to be the size of the object written to the Sof file
    //
    obj_size = sofSize();
  }

  // write the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }
  
  // write data and exit gracefully
  //
  return writeData(sof_a);
}

// method: writeData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method has the object writes itself to an Sof file. it assumes
// that the Sof file is already positioned correctly
//
boolean Component::writeData(Sof& sof_a, const String& pname_a) const {

  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write the input names
  //
  String in_name;
  joinName(in_name);
  if (!in_name.writeData(sof_a, PARAM_INPUT_NAMES)) {
    return Error::handle(name(), L"writeData",
			 Error::ARG, __FILE__, __LINE__, Error::WARNING);
  }

  if (!output_name_d.writeData(sof_a, PARAM_OUTPUT_NAME)) {
    return Error::handle(name(), L"writeData",
			 Error::ARG, __FILE__, __LINE__, Error::WARNING);
  }

  // write the algorithm
  //
  algo_d.writeData(sof_a, PARAM_COMPONENT);

  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return true;
}
