OffsetFileInputImpl Class Reference
Inheritance diagram for OffsetFileInputImpl:
Collaboration diagram for OffsetFileInputImpl:

Public Member Functions

bool Seek (size_t offset)
 
virtual bool Open (const std::string &rxfilename, bool binary)
 
virtual std::istream & Stream ()
 
virtual int32 Close ()
 
virtual InputType MyType ()
 
virtual ~OffsetFileInputImpl ()
 
- Public Member Functions inherited from InputImplBase
virtual ~InputImplBase ()
 

Static Public Member Functions

static void SplitFilename (const std::string &rxfilename, std::string *filename, size_t *offset)
 

Private Attributes

std::string filename_
 
bool binary_
 
std::ifstream is_
 

Detailed Description

Definition at line 556 of file kaldi-io.cc.

Constructor & Destructor Documentation

◆ ~OffsetFileInputImpl()

virtual ~OffsetFileInputImpl ( )
inlinevirtual

Definition at line 654 of file kaldi-io.cc.

654  {
655  // Stream will automatically be closed, and we don't care about
656  // whether it fails.
657  }

Member Function Documentation

◆ Close()

virtual int32 Close ( )
inlinevirtual

Implements InputImplBase.

Definition at line 643 of file kaldi-io.cc.

References KALDI_ERR.

643  {
644  if (!is_.is_open())
645  KALDI_ERR << "FileInputImpl::Close(), file is not open.";
646  // I believe this error can only arise from coding error.
647  is_.close();
648  // Don't check status.
649  return 0;
650  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ MyType()

virtual InputType MyType ( )
inlinevirtual

Implements InputImplBase.

Definition at line 652 of file kaldi-io.cc.

References kaldi::kOffsetFileInput.

◆ Open()

virtual bool Open ( const std::string &  rxfilename,
bool  binary 
)
inlinevirtual

Implements InputImplBase.

Definition at line 602 of file kaldi-io.cc.

References MapOsPath.

602  {
603  if (is_.is_open()) {
604  // We are opening when we have an already-open file.
605  // We may have to seek within this file, or else close it and
606  // open a different one.
607  std::string tmp_filename;
608  size_t offset;
609  SplitFilename(rxfilename, &tmp_filename, &offset);
610  if (tmp_filename == filename_ && binary == binary_) { // Just seek
611  is_.clear(); // clear fail bit, etc.
612  return Seek(offset);
613  } else {
614  is_.close(); // don't bother checking error status of is_.
615  filename_ = tmp_filename;
616  is_.open(MapOsPath(filename_).c_str(),
617  binary ? std::ios_base::in | std::ios_base::binary
618  : std::ios_base::in);
619  if (!is_.is_open()) return false;
620  else
621  return Seek(offset);
622  }
623  } else {
624  size_t offset;
625  SplitFilename(rxfilename, &filename_, &offset);
626  binary_ = binary;
627  is_.open(MapOsPath(filename_).c_str(),
628  binary ? std::ios_base::in | std::ios_base::binary
629  : std::ios_base::in);
630  if (!is_.is_open()) return false;
631  else
632  return Seek(offset);
633  }
634  }
#define MapOsPath(x)
Definition: kaldi-io.cc:36
static void SplitFilename(const std::string &rxfilename, std::string *filename, size_t *offset)
Definition: kaldi-io.cc:562
bool Seek(size_t offset)
Definition: kaldi-io.cc:578

◆ Seek()

bool Seek ( size_t  offset)
inline

Definition at line 578 of file kaldi-io.cc.

References rnnlm::i.

578  {
579  size_t cur_pos = is_.tellg();
580  if (cur_pos == offset) return true;
581  else if (cur_pos<offset && cur_pos+100 > offset) {
582  // We're close enough that it may be faster to just
583  // read that data, rather than seek.
584  for (size_t i = cur_pos; i < offset; i++)
585  is_.get();
586  return (is_.tellg() == std::streampos(offset));
587  }
588  // Try to actually seek.
589  is_.seekg(offset, std::ios_base::beg);
590  if (is_.fail()) { // failbit or badbit is set [error happened]
591  is_.close();
592  return false; // failure.
593  } else {
594  is_.clear(); // Clear any failure bits (e.g. eof).
595  return true; // success.
596  }
597  }

◆ SplitFilename()

static void SplitFilename ( const std::string &  rxfilename,
std::string *  filename,
size_t offset 
)
inlinestatic

Definition at line 562 of file kaldi-io.cc.

References kaldi::ConvertStringToInteger(), KALDI_ASSERT, and KALDI_ERR.

564  {
565  size_t pos = rxfilename.find_last_of(':');
566  KALDI_ASSERT(pos != std::string::npos); // would indicate error in calling
567  // code, as the filename is supposed to be of the correct form at this
568  // point.
569  *filename = std::string(rxfilename, 0, pos);
570  std::string number(rxfilename, pos+1);
571  bool ans = ConvertStringToInteger(number, offset);
572  if (!ans)
573  KALDI_ERR << "Cannot get offset from filename " << rxfilename
574  << " (possibly you compiled in 32-bit and have a >32-bit"
575  << " byte offset into a file; you'll have to compile 64-bit.";
576  }
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
Definition: text-utils.h:118
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Stream()

virtual std::istream& Stream ( )
inlinevirtual

Implements InputImplBase.

Definition at line 636 of file kaldi-io.cc.

References KALDI_ERR.

636  {
637  if (!is_.is_open())
638  KALDI_ERR << "FileInputImpl::Stream(), file is not open.";
639  // I believe this error can only arise from coding error.
640  return is_;
641  }
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Data Documentation

◆ binary_

bool binary_
private

Definition at line 660 of file kaldi-io.cc.

◆ filename_

std::string filename_
private

Definition at line 659 of file kaldi-io.cc.

◆ is_

std::ifstream is_
private

Definition at line 661 of file kaldi-io.cc.


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