// file: $isip/class/algo/Window/win_03.cc
// version: $Id: win_03.cc 8165 2002-05-31 21:57:33Z picone $
//

// isip include files
//
#include "Window.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 Window::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 Window::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 Window::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 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;
  }
  setAlgorithm(algorithm_d);

  // 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"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    implementation_d = DEF_IMPLEMENTATION;
  }
  
  // get the alignment
  //
  ALIGNMENT align;
  if (parser_d->isPresent(sof_a, PARAM_ALIGNMENT)) {
    if (!ALGN_MAP.readElementData((long&)align, sof_a,
				  PARAM_ALIGNMENT,
				  parser_d->getEntry(sof_a,
						     PARAM_ALIGNMENT))) {
      
      // 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 {
    align = DEF_ALIGNMENT;
  }
  setAlignment(align);

  // get the normalization flag
  //
  if (parser_d->isPresent(sof_a, PARAM_NORMALIZATION)) {
    if (!NORM_MAP.readElementData((long&)normalization_d, sof_a,
				  PARAM_NORMALIZATION,
				  parser_d->getEntry(sof_a,
						     PARAM_NORMALIZATION))) {

      // 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 {
    normalization_d = DEF_NORMALIZATION;
  }

  // get the duration
  //
  if (parser_d->isPresent(sof_a, PARAM_DURATION)) {
    if (!duration_d.readData(sof_a, PARAM_DURATION,
			     parser_d->getEntry(sof_a, PARAM_DURATION),
			     false, false)) {

      // 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 {
    duration_d.assign(DEF_DURATION);
  }

  // get the mode
  //
  if (parser_d->isPresent(sof_a, PARAM_CMODE)) {
    if (!CMODE_MAP.readElementData((long&)cmode_d, sof_a, PARAM_CMODE,
				   parser_d->getEntry(sof_a, PARAM_CMODE))) {
      
      // 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 {
    cmode_d = DEF_CMODE;
  }  
  setComputeMode(cmode_d);
  
  
  // 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"readData", Error::IO,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    debug_level_d = Integral::DEF_DEBUG;
  }

  // check known algorithms: invoke the proper lower-level method
  //
  if ((algorithm_d == RECTANGULAR) ||
      (algorithm_d == BARTLETT) ||
      (algorithm_d == BLACKMAN) ||
      (algorithm_d == GAUSSIAN) ||
      (algorithm_d == HAMMING) ||
      (algorithm_d == HANNING) ||
      (algorithm_d == KAISER) ||
      (algorithm_d == LIFTER) ||
      (algorithm_d == CUSTOM)) {
    status = readDataCommon(sof_a, pname_a, size_a, param_a, nested_a);
  }
    
  // check unsupported algorithms
  //
  else {
    return Error::handle(name(), L"readData",
			 ERR_UNKALG, __FILE__, __LINE__);
  }

  // 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"readData", Error::IO,
			 __FILE__, __LINE__, Error::WARNING);
  }

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

  // set the status to invalid
  //
  is_valid_d = false;

  // exit gracefully
  //
  return status;
}

// 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_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 assumes the algorithm has been read, and proceeds to read
// the rest of the object based on the value of the algorithm
//
boolean Window::readDataCommon(Sof& sof_a, const String& pname_a,
			       long size_a, boolean param_a,
			       boolean nested_a) {

  // case (1): not a custom window - read the constants
  //
  if (algorithm_d != CUSTOM) {
    if (parser_d->isPresent(sof_a, PARAM_CONSTANTS)) {
      if (!constants_d.readData(sof_a, PARAM_CONSTANTS,
				parser_d->getEntry(sof_a, PARAM_CONSTANTS) ,
				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);
	
      }
    }
  }
  
  // case (2): a custom window - read the data
  //
  else {
    if (parser_d->isPresent(sof_a, PARAM_DATA)) {
      if (!data_d.readData(sof_a, PARAM_DATA,
			   parser_d->getEntry(sof_a, PARAM_DATA),
			   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 {
      
      data_d.clear();
      
      if (algorithm_d == CUSTOM) {
      
	// 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);
      }
    }
  }
    
  // exit gracefully
  //
  return true;
}
