All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 ( 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 }
std::string PrintableWxfilename(std::string wxfilename)
PrintableWxfilename turns the filename into a more human-readable form for error reporting, i.e.
Definition: kaldi-io.cc:73
#define KALDI_ERR
Definition: kaldi-error.h:127
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
OutputImplBase * impl_
Definition: kaldi-io.h:160
Output ( )
inline

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

131 : impl_(NULL) {}
OutputImplBase * impl_
Definition: kaldi-io.h:160
~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().

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 }
std::string PrintableWxfilename(std::string wxfilename)
PrintableWxfilename turns the filename into a more human-readable form for error reporting, i.e.
Definition: kaldi-io.cc:73
#define KALDI_ERR
Definition: kaldi-error.h:127
virtual bool Close()=0
OutputType ClassifyWxfilename(const std::string &filename)
ClassifyWxfilename interprets filenames as follows:
Definition: kaldi-io.cc:85
OutputImplBase * impl_
Definition: kaldi-io.h:160
std::string filename_
Definition: kaldi-io.h:161

Member Function Documentation

bool IsOpen ( )
inline

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

References Output::impl_.

Referenced by TableWriterArchiveImpl< Holder >::Close(), TableWriterBothImpl< Holder >::Close(), and Output::Open().

38  {
39  return impl_ != NULL;
40 }
OutputImplBase * impl_
Definition: kaldi-io.h:160
KALDI_DISALLOW_COPY_AND_ASSIGN ( Output  )
private
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 TableWriterArchiveImpl< Holder >::Open(), TableWriterBothImpl< Holder >::Open(), 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
std::string PrintableWxfilename(std::string wxfilename)
PrintableWxfilename turns the filename into a more human-readable form for error reporting, i.e.
Definition: kaldi-io.cc:73
OutputType
Definition: kaldi-io.h:89
virtual bool Open(const std::string &filename, bool binary)=0
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
OutputType ClassifyWxfilename(const std::string &filename)
ClassifyWxfilename interprets filenames as follows:
Definition: kaldi-io.cc:85
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

Member Data Documentation

std::string filename_
private

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

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


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