PipeInputImpl Class Reference
Inheritance diagram for PipeInputImpl:
Collaboration diagram for PipeInputImpl:

Public Member Functions

 PipeInputImpl ()
 
virtual bool Open (const std::string &rxfilename, bool binary)
 
virtual std::istream & Stream ()
 
virtual int32 Close ()
 
virtual ~PipeInputImpl ()
 
virtual InputType MyType ()
 
- Public Member Functions inherited from InputImplBase
virtual ~InputImplBase ()
 

Private Attributes

std::string filename_
 
FILE * f_
 
PipebufTypefb_
 
std::istream * is_
 

Detailed Description

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

Constructor & Destructor Documentation

◆ PipeInputImpl()

PipeInputImpl ( )
inline

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

455 : f_(NULL), is_(NULL) { }
std::istream * is_
Definition: kaldi-io.cc:535

◆ ~PipeInputImpl()

virtual ~PipeInputImpl ( )
inlinevirtual

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

References OutputImplBase::Close().

524  {
525  if (is_)
526  Close();
527  }
virtual int32 Close()
Definition: kaldi-io.cc:503
std::istream * is_
Definition: kaldi-io.cc:535

Member Function Documentation

◆ Close()

virtual int32 Close ( )
inlinevirtual

Implements InputImplBase.

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

References KALDI_ERR, and KALDI_WARN.

503  {
504  if (is_ == NULL)
505  KALDI_ERR << "PipeInputImpl::Close(), file is not open.";
506  delete is_;
507  is_ = NULL;
508  int32 status;
509 #ifdef _MSC_VER
510  status = _pclose(f_);
511 #else
512  status = pclose(f_);
513 #endif
514  if (status)
515  KALDI_WARN << "Pipe " << filename_ << " had nonzero return status "
516  << status;
517  f_ = NULL;
518 #ifndef _MSC_VER
519  delete fb_;
520  fb_ = NULL;
521 #endif
522  return status;
523  }
kaldi::int32 int32
std::string filename_
Definition: kaldi-io.cc:530
PipebufType * fb_
Definition: kaldi-io.cc:533
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::istream * is_
Definition: kaldi-io.cc:535

◆ MyType()

virtual InputType MyType ( )
inlinevirtual

Implements InputImplBase.

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

References kaldi::kPipeInput.

528 { return kPipeInput; }

◆ Open()

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

Implements InputImplBase.

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

References KALDI_ASSERT, KALDI_WARN, and kaldi::PrintableRxfilename().

457  {
458  filename_ = rxfilename;
459  KALDI_ASSERT(f_ == NULL); // Make sure closed.
460  KALDI_ASSERT(rxfilename.length() != 0 &&
461  rxfilename[rxfilename.length()-1] == '|'); // should end with '|'
462  std::string cmd_name(rxfilename, 0, rxfilename.length()-1);
463 #if defined(_MSC_VER) || defined(__CYGWIN__)
464  f_ = popen(cmd_name.c_str(), (binary ? "rb" : "r"));
465 #else
466  f_ = popen(cmd_name.c_str(), "r");
467 #endif
468 
469  if (!f_) { // Failure.
470  KALDI_WARN << "Failed opening pipe for reading, command is: "
471  << cmd_name << ", errno is " << strerror(errno);
472  return false;
473  } else {
474 #ifndef _MSC_VER
475  fb_ = new PipebufType(f_, // Using this constructor won't lead the
476  // destructor to close the stream.
477  (binary ? std::ios_base::in|
478  std::ios_base::binary
479  :std::ios_base::in));
480  KALDI_ASSERT(fb_ != NULL); // or would be alloc error.
481  is_ = new std::istream(fb_);
482 #else
483  is_ = new std::ifstream(f_);
484 #endif
485  if (is_->fail() || is_->bad()) return false;
486  if (is_->eof()) {
487  KALDI_WARN << "Pipe opened with command "
488  << PrintableRxfilename(rxfilename)
489  << " is empty.";
490  // don't return false: empty may be valid.
491  }
492  return true;
493  }
494  }
std::string filename_
Definition: kaldi-io.cc:530
basic_pipebuf< char > PipebufType
Definition: kaldi-io.cc:55
PipebufType * fb_
Definition: kaldi-io.cc:533
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::istream * is_
Definition: kaldi-io.cc:535
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

◆ Stream()

virtual std::istream& Stream ( )
inlinevirtual

Implements InputImplBase.

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

References KALDI_ERR.

496  {
497  if (is_ == NULL)
498  KALDI_ERR << "PipeInputImpl::Stream(), object not initialized.";
499  // I believe this error can only arise from coding error.
500  return *is_;
501  }
#define KALDI_ERR
Definition: kaldi-error.h:147
std::istream * is_
Definition: kaldi-io.cc:535

Member Data Documentation

◆ f_

FILE* f_
private

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

◆ fb_

PipebufType* fb_
private

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

◆ filename_

std::string filename_
private

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

◆ is_

std::istream* is_
private

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


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