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

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

663  :impl_(NULL) {
664  if (!Open(wxfilename, binary, write_header)) {
665  if (impl_) {
666  delete impl_;
667  impl_ = NULL;
668  }
669  KALDI_ERR << "Error opening output stream " <<
670  PrintableWxfilename(wxfilename);
671  }
672 }
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:704
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 685 of file kaldi-io.cc.

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

685  {
686  if (impl_) {
687  bool ok = impl_->Close();
688  delete impl_;
689  impl_ = NULL;
690  if (!ok)
691  KALDI_ERR << "Error closing output file "
694  " (disk full?)" : "");
695  }
696 }
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 704 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().

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

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: