SequentialTableReaderArchiveImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

Inheritance diagram for SequentialTableReaderArchiveImpl< Holder >:
Collaboration diagram for SequentialTableReaderArchiveImpl< Holder >:

Public Types

typedef Holder::T T
 
- Public Types inherited from SequentialTableReaderImplBase< Holder >
typedef Holder::T T
 

Public Member Functions

 SequentialTableReaderArchiveImpl ()
 
virtual bool Open (const std::string &rspecifier)
 
virtual void Next ()
 
virtual bool IsOpen () const
 
virtual bool Done () const
 
virtual std::string Key ()
 
TValue ()
 
virtual void FreeCurrent ()
 
void SwapHolder (Holder *other_holder)
 
virtual bool Close ()
 
virtual ~SequentialTableReaderArchiveImpl ()
 
- Public Member Functions inherited from SequentialTableReaderImplBase< Holder >
 SequentialTableReaderImplBase ()
 
virtual ~SequentialTableReaderImplBase ()
 

Private Types

enum  StateType {
  kUninitialized, kFileStart, kEof, kError,
  kHaveObject, kFreedObject
}
 

Private Attributes

Input input_
 
Holder holder_
 
std::string key_
 
std::string rspecifier_
 
std::string archive_rxfilename_
 
RspecifierOptions opts_
 
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_
 

Detailed Description

template<class Holder>
class kaldi::SequentialTableReaderArchiveImpl< Holder >

Definition at line 483 of file kaldi-table-inl.h.

Member Typedef Documentation

◆ T

typedef Holder::T T

Definition at line 486 of file kaldi-table-inl.h.

Member Enumeration Documentation

◆ StateType

enum StateType
private
Enumerator
kUninitialized 
kFileStart 
kEof 
kError 
kHaveObject 
kFreedObject 

Definition at line 688 of file kaldi-table-inl.h.

688  { // [The state of the reading process] [does holder_ [is input_
689  // have object] open]
690  kUninitialized, // Uninitialized or closed. no no
691  kFileStart, // [state we use internally: just opened.] no yes
692  kEof, // We did Next() and found eof in archive no no
693  kError, // Some other error no no
694  kHaveObject, // We read the key and the object after it. yes yes
695  kFreedObject, // The user called FreeCurrent(). no yes
696  } state_;
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

Constructor & Destructor Documentation

◆ SequentialTableReaderArchiveImpl()

Definition at line 488 of file kaldi-table-inl.h.

◆ ~SequentialTableReaderArchiveImpl()

virtual ~SequentialTableReaderArchiveImpl ( )
inlinevirtual

Definition at line 676 of file kaldi-table-inl.h.

References SequentialTableReaderImplBase< Holder >::Close(), SequentialTableReaderImplBase< Holder >::IsOpen(), KALDI_ERR, and kaldi::PrintableRxfilename().

676  {
677  if (this->IsOpen() && !Close())
678  KALDI_ERR << "TableReader: error detected closing archive "
680  }
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 648 of file kaldi-table-inl.h.

References SequentialTableReaderImplBase< Holder >::IsOpen(), KALDI_ERR, KALDI_WARN, and kaldi::PrintableRxfilename().

648  {
649  // To clean up, Close() also closes the Input object if
650  // it's open. It will succeed if the stream was not in an error state,
651  // and the Input object isn't in an error state we've found eof in the archive.
652  if (!this->IsOpen())
653  KALDI_ERR << "Close() called on TableReader twice or otherwise wrongly.";
654  int32 status = 0;
655  if (input_.IsOpen())
656  status = input_.Close();
657  if (state_ == kHaveObject)
658  holder_.Clear();
659  StateType old_state = state_;
661  if (old_state == kError || (old_state == kEof && status != 0)) {
662  if (opts_.permissive) {
663  KALDI_WARN << "Error detected closing TableReader for archive "
665  << " but ignoring "
666  << "it as permissive mode specified.";
667  return true;
668  } else {
669  return false;
670  }
671  } else {
672  return true;
673  }
674  }
bool IsOpen()
Definition: kaldi-io-inl.h:34
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 Close()
Definition: kaldi-io.cc:761
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ Done()

virtual bool Done ( ) const
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 591 of file kaldi-table-inl.h.

References KALDI_ERR.

591  {
592  switch (state_) {
593  case kHaveObject:
594  return false;
595  case kEof: case kError:
596  return true; // Error-state counts as Done(), but destructor
597  // will fail (unless you check the status with Close()).
598  default:
599  KALDI_ERR << "Done() called on TableReader object at the wrong time.";
600  return false;
601  }
602  }
#define KALDI_ERR
Definition: kaldi-error.h:147
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ FreeCurrent()

virtual void FreeCurrent ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 626 of file kaldi-table-inl.h.

References KALDI_WARN.

626  {
627  if (state_ == kHaveObject) {
628  holder_.Clear();
630  } else {
631  KALDI_WARN << "FreeCurrent called at the wrong time.";
632  }
633  }
#define KALDI_WARN
Definition: kaldi-error.h:150
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ IsOpen()

virtual bool IsOpen ( ) const
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 581 of file kaldi-table-inl.h.

References KALDI_ERR.

581  {
582  switch (state_) {
583  case kEof: case kError: case kHaveObject: case kFreedObject: return true;
584  case kUninitialized: return false;
585  default: KALDI_ERR << "IsOpen() called on invalid object."; // kFileStart
586  // is not valid state for user to call something on.
587  return false;
588  }
589  }
#define KALDI_ERR
Definition: kaldi-error.h:147
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ Key()

virtual std::string Key ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 604 of file kaldi-table-inl.h.

References KALDI_ERR.

604  {
605  // Valid to call this whenever Done() returns false
606  switch (state_) {
607  case kHaveObject: break; // only valid case.
608  default:
609  // coding error.
610  KALDI_ERR << "Key() called on TableReader object at the wrong time.";
611  }
612  return key_;
613  }
#define KALDI_ERR
Definition: kaldi-error.h:147
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ Next()

virtual void Next ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 531 of file kaldi-table-inl.h.

References kaldi::CharToString(), KALDI_ERR, KALDI_WARN, and kaldi::PrintableRxfilename().

531  {
532  switch (state_) {
533  case kHaveObject:
534  holder_.Clear();
535  break;
536  case kFileStart: case kFreedObject:
537  break;
538  default:
539  KALDI_ERR << "Next() called wrongly.";
540  }
541  std::istream &is = input_.Stream();
542  is.clear(); // Clear any fail bits that may have been set... just in case
543  // this happened in the Read function.
544  is >> key_; // This eats up any leading whitespace and gets the string.
545  if (is.eof()) {
546  state_ = kEof;
547  return;
548  }
549  if (is.fail()) { // This shouldn't really happen, barring file-system
550  // errors.
551  KALDI_WARN << "Error reading archive "
553  state_ = kError;
554  return;
555  }
556  int c;
557  if ((c = is.peek()) != ' ' && c != '\t' && c != '\n') { // We expect a
558  // space ' ' after the key.
559  // We also allow tab [which is consumed] and newline [which is not], just
560  // so we can read archives generated by scripts that may not be fully
561  // aware of how this format works.
562  KALDI_WARN << "Invalid archive file format: expected space after key "
563  << key_ << ", got character "
564  << CharToString(static_cast<char>(is.peek())) << ", reading "
566  state_ = kError;
567  return;
568  }
569  if (c != '\n') is.get(); // Consume the space or tab.
570  if (holder_.Read(is)) {
572  return;
573  } else {
574  KALDI_WARN << "Object read failed, reading archive "
576  state_ = kError;
577  return;
578  }
579  }
std::istream & Stream()
Definition: kaldi-io.cc:826
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string CharToString(const char &c)
Definition: kaldi-utils.cc:36
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ Open()

virtual bool Open ( const std::string &  rspecifier)
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 490 of file kaldi-table-inl.h.

References kaldi::ClassifyRspecifier(), SequentialTableReaderImplBase< Holder >::Close(), KALDI_ASSERT, KALDI_ERR, KALDI_WARN, kaldi::kArchiveRspecifier, SequentialTableReaderImplBase< Holder >::Next(), and kaldi::PrintableRxfilename().

490  {
491  if (state_ != kUninitialized) {
492  if (!Close()) { // call Close() yourself to suppress this exception.
493  if (opts_.permissive)
494  KALDI_WARN << "Error closing previous input "
495  "(only warning, since permissive mode).";
496  else
497  KALDI_ERR << "Error closing previous input.";
498  }
499  }
500  rspecifier_ = rspecifier;
501  RspecifierType rs = ClassifyRspecifier(rspecifier,
503  &opts_);
505 
506  bool ans;
507  // NULL means don't expect binary-mode header
508  if (Holder::IsReadInBinary())
509  ans = input_.Open(archive_rxfilename_, NULL);
510  else
512  if (!ans) { // header.
513  KALDI_WARN << "Failed to open stream "
515  state_ = kUninitialized; // Failure on Open
516  return false; // User should print the error message.
517  }
518  state_ = kFileStart;
519  Next();
520  if (state_ == kError) {
521  KALDI_WARN << "Error beginning to read archive file (wrong filename?): "
523  input_.Close();
525  return false;
526  }
528  return true;
529  }
bool Open(const std::string &rxfilename, bool *contents_binary=NULL)
Definition: kaldi-io-inl.h:26
bool OpenTextMode(const std::string &rxfilename)
Definition: kaldi-io-inl.h:30
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
RspecifierType
Definition: kaldi-table.h:219
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 Close()
Definition: kaldi-io.cc:761
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ SwapHolder()

void SwapHolder ( Holder *  other_holder)
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 635 of file kaldi-table-inl.h.

References KALDI_ERR, and SequentialTableReaderImplBase< Holder >::Value().

635  {
636  // call Value() to ensure we have a value, and ignore its return value while
637  // suppressing compiler warnings by casting to void.
638  (void) Value();
639  if (state_ == kHaveObject) {
640  holder_.Swap(other_holder);
642  } else {
643  KALDI_ERR << "SwapHolder called at the wrong time "
644  "(error related to ',bg' modifier).";
645  }
646  }
#define KALDI_ERR
Definition: kaldi-error.h:147
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

◆ Value()

T& Value ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 615 of file kaldi-table-inl.h.

References KALDI_ERR.

615  {
616  switch (state_) {
617  case kHaveObject:
618  break; // only valid case.
619  default:
620  // coding error.
621  KALDI_ERR << "Value() called on TableReader object at the wrong time.";
622  }
623  return holder_.Value();
624  }
#define KALDI_ERR
Definition: kaldi-error.h:147
enum kaldi::SequentialTableReaderArchiveImpl::StateType state_

Member Data Documentation

◆ archive_rxfilename_

std::string archive_rxfilename_
private

Definition at line 686 of file kaldi-table-inl.h.

◆ holder_

Holder holder_
private

Definition at line 683 of file kaldi-table-inl.h.

◆ input_

Input input_
private

Definition at line 682 of file kaldi-table-inl.h.

◆ key_

std::string key_
private

Definition at line 684 of file kaldi-table-inl.h.

◆ opts_

RspecifierOptions opts_
private

Definition at line 687 of file kaldi-table-inl.h.

◆ rspecifier_

std::string rspecifier_
private

Definition at line 685 of file kaldi-table-inl.h.

◆ state_


The documentation for this class was generated from the following file: