// file: $isip/class/search/StackSearch/ssrch_03.cc
// version: $Id: ssrch_03.cc 9170 2003-05-27 22:16:13Z jelinek $
//

// isip include files
//
#include "StackSearch.h"
#include <Sof.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 indicating status
//
// this method has the object read itself from an Sof file
//
boolean StackSearch::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) number of bytes in file
//  boolean param: (input) is the parameter specified?
//  boolean nested: (input) is this nested?
//
// return: logical error status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean StackSearch::readData(Sof& sof_a, const String& pname_a,
				    long size_a,
				    boolean param_a, boolean nested_a) {

  // allocate a parser
  //
  SofParser parser;
  parser.setDebug(debug_level_d);

  // ignore implicit parameter setting
  //
  
  // are we nested?
  //
  if (nested_a) {
    parser.setNest();
  }
    
  // load the parser
  //
  if (!parser.load(sof_a, size_a)) {

      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
  }
  
  // read the decoding mode
  //
  if (parser.isPresent(sof_a, PARAM_DECODING_MODE)) {
    if (!DECODING_MODE_MAP.readElementData(
				 (long&)decoding_mode_d, sof_a,
				 PARAM_DECODING_MODE,
				 parser.getEntry(sof_a, PARAM_DECODING_MODE))
	) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }    
  }
  else {
    decoding_mode_d = DEF_DECODING_MODE;
  }

  // read the score normalization
  //
  if (parser.isPresent(sof_a, PARAM_SCORE_NORMALIZATION)) {
    if (!SCORE_NORMALIZATION_MAP.readElementData(
				 (long&)score_normalization_d, sof_a,
				 PARAM_SCORE_NORMALIZATION,
				 parser.getEntry(sof_a,
						 PARAM_SCORE_NORMALIZATION))
	) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
    if (score_normalization_d == NONE) {
      user_requested_no_score_normalization_d = true;
    }
  }
  else {
    score_normalization_d = DEF_SCORE_NORMALIZATION;
  }

  // read the stack level
  //
  if (parser.isPresent(sof_a, PARAM_STACK_LEVEL)) {
    if (!stack_level_d.readData(sof_a, PARAM_STACK_LEVEL,
				parser.getEntry(sof_a,
						PARAM_STACK_LEVEL))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__);
    }
  }

  if ((decoding_mode_d == VITERBI) and (stack_level_d != long(-1))) {
    return Error::handle(name(),
			 L"readData - stack level must be -1 in VITERBI mode",
			 Error::ARG, __FILE__, __LINE__);
  }
  
  // read the max-min filename
  //
  if (parser.isPresent(sof_a, PARAM_MAX_MIN_FILENAME)) {
    if (!max_min_filename_d.readData(sof_a, PARAM_MAX_MIN_FILENAME,
				     parser.getEntry(sof_a,
					     PARAM_MAX_MIN_FILENAME))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__);
    }
  }

  // read the silence duration penalty
  //
  if (parser.isPresent(sof_a, PARAM_SIL_DUR_PENALTY)) {
    if (!sil_dur_penalty_d.readData(sof_a, PARAM_SIL_DUR_PENALTY,
				    parser.getEntry(sof_a,
						    PARAM_SIL_DUR_PENALTY))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__);
    }
  }
  else {
    sil_dur_penalty_d = DEF_SIL_DUR_PENALTY;
  }

  // exit gracefully
  //
  return true;
}
