// file: $isip/class/asr/AudioDatabase/audb_03.cc
// version: $Id: audb_03.cc 9420 2004-03-10 20:09:46Z parihar $
//

// isip include files
//
#include "AudioDatabase.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
//
boolean AudioDatabase::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
  //
  if (!readData(sof_a)) {
    return false;
  }

  // exit gracefully
  //
  return true;
}

// 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_SIZE)
//  boolean param: (input) is the parameter specified?
//  boolean nested: (input) is this 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 AudioDatabase::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);
  }    

  // read the database name
  //  
  if (parser.isPresent(sof_a, PARAM_NAME)) {
      if (!name_d.readData(sof_a, PARAM_NAME,
			   parser.getEntry(sof_a, PARAM_NAME))) {
	
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ,
			     __FILE__, __LINE__, Error::WARNING);
	
      }
  }
  
  // read the keys associated with the hash table
  //
  Vector<String> keys;
  if (parser.isPresent(sof_a, PARAM_KEYS)) {
    if (!keys.readData(sof_a, PARAM_KEYS,
			   parser.getEntry(sof_a, PARAM_KEYS),
			   false, false)) {
      
	// return a warning message
	//
	return Error::handle(name(), L"readData", Error::READ,
			     __FILE__, __LINE__, Error::WARNING);
      }
  }

  // read the values associated with the hash table
  //
  Vector<Long> values;
  if (parser.isPresent(sof_a, PARAM_VALUES)) {
    if (!values.readData(sof_a, PARAM_VALUES,
			 parser.getEntry(sof_a, PARAM_VALUES),
			 false, false)) {
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
      }
  }

  // reset the hash table to begin with
  //
  hash_d.clear(Integral::RESET);
  
  for (long i=0; i < keys.length(); i++) {
    hash_d.insert(keys(i), &values(i));
  }

  // exit gracefully
  //
  return true;
}

// method: close
//
// arguments: none
//
// return: a boolean value indicating status
//
// this method closes the database file
//
boolean AudioDatabase::close() {

  // close the database file
  //
  return database_sof_d.close();
}

// method: open
//
// arguments:
//  const Filename& filename: (input) database file
//
// return: a boolean value indicating status
//
// this method opens the database file
//
boolean AudioDatabase::open(const Filename& filename_a) {

  // error checking
  //
  if (filename_a.length() < 1) {
    filename_a.debug(L"filename_a");
    return Error::handle(name(), L"readData - invalid filename", Error::ARG, __FILE__, __LINE__);
  }

  // open the audio database
  //
  if (!database_sof_d.open(filename_a)) {
    filename_a.debug(L"filename_a");
    return Error::handle(name(), L"error opening audio database file", Error::ARG, __FILE__, __LINE__);
  }

  // read the audio database
  //
  if (!read(database_sof_d, (long)0)) {
    return Error::handle(name(), L"error loading transcription database", Error::ARG, __FILE__, __LINE__);
  }
  
  // exit gracefully
  //
  return true;
}


