Output Class Reference

#include <kaldi-io.h>

Collaboration diagram for Output:

Public Member Functions

 Output (const std::string &filename, bool binary, bool write_header=true)
 
 Output ()
 
bool Open (const std::string &wxfilename, bool binary, bool write_header)
 This opens the stream, with the given mode (binary or text). More...
 
bool IsOpen ()
 
std::ostream & Stream ()
 
bool Close ()
 
 ~Output ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (Output)
 

Private Attributes

OutputImplBaseimpl_
 
std::string filename_
 

Detailed Description

Definition at line 124 of file kaldi-io.h.

Constructor & Destructor Documentation

◆ Output() [1/2]

Output ( const std::string &  filename,
bool  binary,
bool  write_header = true 
)

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

References Output::impl_, KALDI_ERR, Output::Open(), and kaldi::PrintableWxfilename().

666  :impl_(NULL) {
667  if (!Open(wxfilename, binary, write_header)) {
668  if (impl_) {
669  delete impl_;
670  impl_ = NULL;
671  }
672  KALDI_ERR << "Error opening output stream " <<
673  PrintableWxfilename(wxfilename);
674  }
675 }
#define KALDI_ERR
Definition: kaldi-error.h:147
bool Open(const std::string &wxfilename, bool binary, bool write_header)
This opens the stream, with the given mode (binary or text).
Definition: kaldi-io.cc:707
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
OutputImplBase * impl_
Definition: kaldi-io.h:160

◆ Output() [2/2]

Output ( )
inline

Definition at line 131 of file kaldi-io.h.

References Output::Close(), Output::IsOpen(), Output::Open(), Output::Stream(), and Output::~Output().

131 : impl_(NULL) {}
OutputImplBase * impl_
Definition: kaldi-io.h:160

◆ ~Output()

~Output ( )

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

References kaldi::ClassifyWxfilename(), OutputImplBase::Close(), Output::filename_, Output::impl_, KALDI_ERR, kaldi::kFileOutput, and kaldi::PrintableWxfilename().

Referenced by Output::Output().

688  {
689  if (impl_) {
690  bool ok = impl_->Close();
691  delete impl_;
692  impl_ = NULL;
693  if (!ok)
694  KALDI_ERR << "Error closing output file "
697  " (disk full?)" : "");
698  }
699 }
#define KALDI_ERR
Definition: kaldi-error.h:147
virtual bool Close()=0
OutputType ClassifyWxfilename(const std::string &filename)
ClassifyWxfilename interprets filenames as follows:
Definition: kaldi-io.cc:85
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
OutputImplBase * impl_
Definition: kaldi-io.h:160
std::string filename_
Definition: kaldi-io.h:161

Member Function Documentation

◆ Close()

bool Close ( )

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

References OutputImplBase::Close(), and Output::impl_.

Referenced by Input::Input(), main(), Output::Open(), Output::Output(), kaldi::TestEventTypeIo(), kaldi::TestGenRandContextDependency(), kaldi::UnitTestIoNew(), kaldi::UnitTestIoPipe(), kaldi::UnitTestReadScriptFile(), Nnet::Write(), TableWriterScriptImpl< Holder >::Write(), kaldi::WriteIntegerVectorSimple(), and kaldi::WriteIntegerVectorVectorSimple().

677  {
678  if (!impl_) {
679  return false; // error to call Close if not open.
680  } else {
681  bool ans = impl_->Close();
682  delete impl_;
683  impl_ = NULL;
684  return ans;
685  }
686 }
virtual bool Close()=0
OutputImplBase * impl_
Definition: kaldi-io.h:160

◆ IsOpen()

bool IsOpen ( )
inline

Definition at line 38 of file kaldi-io-inl.h.

References Input::impl_.

Referenced by Input::Input(), Output::Open(), and Output::Output().

38  {
39  return impl_ != NULL;
40 }
OutputImplBase * impl_
Definition: kaldi-io.h:160

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( Output  )
private

◆ Open()

bool Open ( const std::string &  wxfilename,
bool  binary,
bool  write_header 
)

This opens the stream, with the given mode (binary or text).

It returns true on success and false on failure. However, it will throw if something was already open and could not be closed (to avoid this, call Close() first. if write_header == true and binary == true, it writes the Kaldi binary-mode header ('\0' then 'B'). You may call Open even if it is already open; it will close the existing stream and reopen (however if closing the old stream failed it will throw).

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

References kaldi::ClassifyWxfilename(), Output::Close(), Output::filename_, Output::impl_, kaldi::InitKaldiOutputStream(), Output::IsOpen(), KALDI_ASSERT, KALDI_ERR, KALDI_WARN, kaldi::kFileOutput, kaldi::kPipeOutput, kaldi::kStandardOutput, OutputImplBase::Open(), kaldi::PrintableWxfilename(), and OutputImplBase::Stream().

Referenced by Input::Input(), Output::Output(), TableWriterScriptImpl< Holder >::Write(), kaldi::WriteIntegerVectorSimple(), kaldi::WriteIntegerVectorVectorSimple(), and kaldi::WriteScriptFile().

707  {
708  if (IsOpen()) {
709  if (!Close()) { // Throw here rather than return status, as it's an error
710  // about something else: if the user wanted to avoid the exception he/she
711  // could have called Close().
712  KALDI_ERR << "Output::Open(), failed to close output stream: "
714  }
715  }
716 
717  filename_ = wxfn;
718 
719  OutputType type = ClassifyWxfilename(wxfn);
720  KALDI_ASSERT(impl_ == NULL);
721 
722  if (type == kFileOutput) {
723  impl_ = new FileOutputImpl();
724  } else if (type == kStandardOutput) {
725  impl_ = new StandardOutputImpl();
726  } else if (type == kPipeOutput) {
727  impl_ = new PipeOutputImpl();
728  } else { // type == kNoOutput
729  KALDI_WARN << "Invalid output filename format "<<
730  PrintableWxfilename(wxfn);
731  return false;
732  }
733  if (!impl_->Open(wxfn, binary)) {
734  delete impl_;
735  impl_ = NULL;
736  return false; // failed to open.
737  } else { // successfully opened it.
738  if (header) {
739  InitKaldiOutputStream(impl_->Stream(), binary);
740  bool ok = impl_->Stream().good(); // still OK?
741  if (!ok) {
742  delete impl_;
743  impl_ = NULL;
744  return false;
745  }
746  return true;
747  } else {
748  return true;
749  }
750  }
751 }
virtual std::ostream & Stream()=0
OutputType
Definition: kaldi-io.h:89
virtual bool Open(const std::string &filename, bool binary)=0
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
OutputType ClassifyWxfilename(const std::string &filename)
ClassifyWxfilename interprets filenames as follows:
Definition: kaldi-io.cc:85
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
Definition: io-funcs-inl.h:291
OutputImplBase * impl_
Definition: kaldi-io.h:160
std::string filename_
Definition: kaldi-io.h:161
bool Close()
Definition: kaldi-io.cc:677

◆ Stream()

Member Data Documentation

◆ filename_

std::string filename_
private

Definition at line 161 of file kaldi-io.h.

Referenced by Output::Open(), and Output::~Output().

◆ impl_

OutputImplBase* impl_
private

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