// file: $isip/class/sp/Component/cmp_03.cc
// version: $Id: cmp_03.cc 8236 2002-06-26 19:06:05Z gao $
//

// isip include files
//
#include "Component.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 Component::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 Component::readData(Sof& sof_a, const String& pname_a,
                         long size_a, boolean param_a,
                         boolean nested_a) {

  // we need a parser
  //
  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"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }       
   
  // the input and output names have to be specified
  //
  if (!(parser.isPresent(sof_a, PARAM_INPUT_NAMES) &&
	parser.isPresent(sof_a, PARAM_OUTPUT_NAME))) {

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

  // for text mode which parameter is specified means what mode to be
  // in. for binary the parameter is meaningless, so read the first
  // one and then the mode directly.
  //
  String str;
  if (!str.readData(sof_a, PARAM_INPUT_NAMES,
		    parser.getEntry(sof_a, PARAM_INPUT_NAMES),
		    false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }         
  if (!setInputName(str)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }         
    
  // read the output name
  //
  if (!output_name_d.readData(sof_a, PARAM_OUTPUT_NAME,
			    parser.getEntry(sof_a, PARAM_OUTPUT_NAME),
			    false, false)) {
    return Error::handle(name(), L"readData", Error::READ,
			 __FILE__, __LINE__, Error::WARNING);
  }       

  // the algorithm is optional, if not specified the Component will just
  // pass data straight through
  //
  if (parser.isPresent(sof_a, PARAM_COMPONENT)) {
    if (!algo_d.readData(sof_a, PARAM_COMPONENT,
			 parser.getEntry(sof_a, PARAM_COMPONENT), false,
			 true)) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    algo_d.clear();
  }

  // check that all parameters are accounted for
  //
  if (!parser.checkParams(sof_a)) {

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

  // exit gracefully
  //
  return true;
}
