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 651 of file kaldi-io.cc.

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

652  :impl_(NULL) {
653  if (!Open(wxfilename, binary, write_header)) {
654  if (impl_) {
655  delete impl_;
656  impl_ = NULL;
657  }
658  KALDI_ERR << "Error opening output stream " <<
659  PrintableWxfilename(wxfilename);
660  }
661 }
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:70
#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:693
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 674 of file kaldi-io.cc.

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

674  {
675  if (impl_) {
676  bool ok = impl_->Close();
677  delete impl_;
678  impl_ = NULL;
679  if (!ok)
680  KALDI_ERR << "Error closing output file "
683  " (disk full?)" : "");
684  }
685 }
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:70
#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:82
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 693 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().

693  {
694  if (IsOpen()) {
695  if (!Close()) { // Throw here rather than return status, as it's an error
696  // about something else: if the user wanted to avoid the exception he/she
697  // could have called Close().
698  KALDI_ERR << "Output::Open(), failed to close output stream: "
700  }
701  }
702 
703  filename_ = wxfn;
704 
705  OutputType type = ClassifyWxfilename(wxfn);
706  KALDI_ASSERT(impl_ == NULL);
707 
708  if (type == kFileOutput) {
709  impl_ = new FileOutputImpl();
710  } else if (type == kStandardOutput) {
711  impl_ = new StandardOutputImpl();
712  } else if (type == kPipeOutput) {
713  impl_ = new PipeOutputImpl();
714  } else { // type == kNoOutput
715  KALDI_WARN << "Invalid output filename format "<<
716  PrintableWxfilename(wxfn);
717  return false;
718  }
719  if (!impl_->Open(wxfn, binary)) {
720  delete impl_;
721  impl_ = NULL;
722  return false; // failed to open.
723  } else { // successfully opened it.
724  if (header) {
725  InitKaldiOutputStream(impl_->Stream(), binary);
726  bool ok = impl_->Stream().good(); // still OK?
727  if (!ok) {
728  delete impl_;
729  impl_ = NULL;
730  return false;
731  }
732  return true;
733  } else {
734  return true;
735  }
736  }
737 }
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:70
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:82
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:663

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: