PLearn 0.1
|
#include <FdPStreamBuf.h>
Public Member Functions | |
FdPStreamBuf (int in_fd=-1, int out_fd=-1, bool own_in_=false, bool own_out_=false) | |
virtual | ~FdPStreamBuf () |
Protected Member Functions | |
virtual streamsize | read_ (char *p, streamsize n) |
reads up to n characters into p You should override this call in subclasses. | |
virtual void | write_ (const char *p, streamsize n) |
writes exactly n characters from p (unbuffered, must flush) | |
Protected Attributes | |
int | in |
input unix file descriptor (-1 if no input) | |
int | out |
output unix file descriptor (-1 if no output) | |
bool | own_in |
bool | own_out |
true if {pin|pout} should be closed by this object upon destruction. | |
Private Types | |
typedef PStreamBuf | inherited |
Definition at line 54 of file FdPStreamBuf.h.
typedef PStreamBuf PLearn::FdPStreamBuf::inherited [private] |
Reimplemented from PLearn::PStreamBuf.
Definition at line 59 of file FdPStreamBuf.h.
PLearn::FdPStreamBuf::FdPStreamBuf | ( | int | in_fd = -1 , |
int | out_fd = -1 , |
||
bool | own_in_ = false , |
||
bool | own_out_ = false |
||
) |
Definition at line 76 of file FdPStreamBuf.cc.
:PStreamBuf(in_fd>=0, out_fd>=0, 4096, 4096, default_ungetsize), in(in_fd), out(out_fd), own_in(own_in_), own_out(own_out_) {}
PLearn::FdPStreamBuf::~FdPStreamBuf | ( | ) | [virtual] |
Definition at line 82 of file FdPStreamBuf.cc.
References PLearn::PStreamBuf::flush(), in, out, own_in, and own_out.
{ const bool in_and_out_equal = (in == out); flush(); if(in>=0 && own_in) { ::close(in); in = -1; } if(out>=0 && own_out) { if (!in_and_out_equal) ::close(out); out = -1; } }
FdPStreamBuf::streamsize PLearn::FdPStreamBuf::read_ | ( | char * | p, |
streamsize | n | ||
) | [protected, virtual] |
reads up to n characters into p You should override this call in subclasses.
Default version issues a PLERROR
On success, the number of bytes read is returned. Zero indicates end of file. If we are not at end of file, at least one character should be returned (the call must block until at least one char is available). It is not an error if the number returned is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal). If an error occurs, an exception should be thrown.
Reimplemented from PLearn::PStreamBuf.
Definition at line 100 of file FdPStreamBuf.cc.
References in, and PLearn::PStreamBuf::read().
{ return ::read(in, p, n); }
void PLearn::FdPStreamBuf::write_ | ( | const char * | p, |
streamsize | n | ||
) | [protected, virtual] |
writes exactly n characters from p (unbuffered, must flush)
Reimplemented from PLearn::PStreamBuf.
Definition at line 106 of file FdPStreamBuf.cc.
References n, out, PLERROR, and PLearn::PStreamBuf::write().
{ streamsize nwritten = ::write(out, p, n); if(nwritten!=n) PLERROR("In FdPStreamBuf::write_ failed to write the requested number of bytes"); #ifdef _MINGW_ PLERROR("In FdPStreamBuf::write_ - The 'fsync' function is not defined " "under MinGW (see code)"); #else fsync(out); #endif }
int PLearn::FdPStreamBuf::in [protected] |
input unix file descriptor (-1 if no input)
Definition at line 66 of file FdPStreamBuf.h.
Referenced by read_(), and ~FdPStreamBuf().
int PLearn::FdPStreamBuf::out [protected] |
output unix file descriptor (-1 if no output)
Definition at line 67 of file FdPStreamBuf.h.
Referenced by write_(), and ~FdPStreamBuf().
bool PLearn::FdPStreamBuf::own_in [protected] |
Definition at line 68 of file FdPStreamBuf.h.
Referenced by ~FdPStreamBuf().
bool PLearn::FdPStreamBuf::own_out [protected] |
true if {pin|pout} should be closed by this object upon destruction.
Definition at line 68 of file FdPStreamBuf.h.
Referenced by ~FdPStreamBuf().