PipeOutputImpl Class Reference
Inheritance diagram for PipeOutputImpl:
Collaboration diagram for PipeOutputImpl:

Public Member Functions

 PipeOutputImpl ()
 
virtual bool Open (const std::string &wxfilename, bool binary)
 
virtual std::ostream & Stream ()
 
virtual bool Close ()
 
virtual ~PipeOutputImpl ()
 
- Public Member Functions inherited from OutputImplBase
virtual ~OutputImplBase ()
 

Private Attributes

std::string filename_
 
FILE * f_
 
PipebufTypefb_
 
std::ostream * os_
 

Detailed Description

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

Constructor & Destructor Documentation

◆ PipeOutputImpl()

PipeOutputImpl ( )
inline

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

278 : f_(NULL), os_(NULL) { }
std::ostream * os_
Definition: kaldi-io.cc:354

◆ ~PipeOutputImpl()

virtual ~PipeOutputImpl ( )
inlinevirtual

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

References OutputImplBase::Close(), KALDI_ERR, and kaldi::PrintableWxfilename().

342  {
343  if (os_) {
344  if (!Close())
345  KALDI_ERR << "Error writing to pipe " << PrintableWxfilename(filename_);
346  }
347  }
virtual bool Close()
Definition: kaldi-io.cc:319
#define KALDI_ERR
Definition: kaldi-error.h:147
std::ostream * os_
Definition: kaldi-io.cc:354
std::string filename_
Definition: kaldi-io.cc:349
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

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements OutputImplBase.

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

References KALDI_ERR, and KALDI_WARN.

319  {
320  if (os_ == NULL) KALDI_ERR << "PipeOutputImpl::Close(), file is not open.";
321  bool ok = true;
322  os_->flush();
323  if (os_->fail()) ok = false;
324  delete os_;
325  os_ = NULL;
326  int status;
327 #ifdef _MSC_VER
328  status = _pclose(f_);
329 #else
330  status = pclose(f_);
331 #endif
332  if (status)
333  KALDI_WARN << "Pipe " << filename_ << " had nonzero return status "
334  << status;
335  f_ = NULL;
336 #ifndef _MSC_VER
337  delete fb_;
338  fb_ = NULL;
339 #endif
340  return ok;
341  }
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::ostream * os_
Definition: kaldi-io.cc:354
PipebufType * fb_
Definition: kaldi-io.cc:352
std::string filename_
Definition: kaldi-io.cc:349

◆ Open()

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

Implements OutputImplBase.

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

References KALDI_ASSERT, and KALDI_WARN.

280  {
281  filename_ = wxfilename;
282  KALDI_ASSERT(f_ == NULL); // Make sure closed.
283  KALDI_ASSERT(wxfilename.length() != 0 && wxfilename[0] == '|'); // should
284  // start with '|'
285  std::string cmd_name(wxfilename, 1);
286 #if defined(_MSC_VER) || defined(__CYGWIN__)
287  f_ = popen(cmd_name.c_str(), (binary ? "wb" : "w"));
288 #else
289  f_ = popen(cmd_name.c_str(), "w");
290 #endif
291  if (!f_) { // Failure.
292  KALDI_WARN << "Failed opening pipe for writing, command is: "
293  << cmd_name << ", errno is " << strerror(errno);
294  return false;
295  } else {
296 #ifndef _MSC_VER
297  fb_ = new PipebufType(f_, // Using this constructor won't make the
298  // destructor try to close the stream when
299  // we're done.
300  (binary ? std::ios_base::out|
301  std::ios_base::binary
302  :std::ios_base::out));
303  KALDI_ASSERT(fb_ != NULL); // or would be alloc error.
304  os_ = new std::ostream(fb_);
305 #else
306  os_ = new std::ofstream(f_);
307 #endif
308  return os_->good();
309  }
310  }
basic_pipebuf< char > PipebufType
Definition: kaldi-io.cc:55
#define KALDI_WARN
Definition: kaldi-error.h:150
std::ostream * os_
Definition: kaldi-io.cc:354
PipebufType * fb_
Definition: kaldi-io.cc:352
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string filename_
Definition: kaldi-io.cc:349

◆ Stream()

virtual std::ostream& Stream ( )
inlinevirtual

Implements OutputImplBase.

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

References KALDI_ERR.

312  {
313  if (os_ == NULL) KALDI_ERR << "PipeOutputImpl::Stream(),"
314  " object not initialized.";
315  // I believe this error can only arise from coding error.
316  return *os_;
317  }
#define KALDI_ERR
Definition: kaldi-error.h:147
std::ostream * os_
Definition: kaldi-io.cc:354

Member Data Documentation

◆ f_

FILE* f_
private

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

◆ fb_

PipebufType* fb_
private

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

◆ filename_

std::string filename_
private

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

◆ os_

std::ostream* os_
private

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


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