PLearn 0.1
|
pl_fdstreambuf: stream buffer that acts on a POSIX file descriptor More...
#include <pl_fdstream.h>
Public Member Functions | |
pl_fdstreambuf (int fd_, int inbufsize) | |
virtual | ~pl_fdstreambuf () |
Protected Member Functions | |
void | reserveInputBuffer (int buflen) |
inline for efficiency | |
virtual int | overflow (int c=EOF) |
virtual int | underflow () |
virtual streamsize | showmanyc () |
not implemented (yet) | |
virtual streamsize | xsputn (const char *s, streamsize n) |
virtual streamsize | xsgetn (char *s, streamsize n) |
virtual streambuf * | setbuf (char *p, int len) |
virtual int | sync () |
Protected Attributes | |
int | fd |
the file descriptor | |
char * | inbuf |
input buffer | |
int | inbuf_capacity |
length of inbuf |
pl_fdstreambuf: stream buffer that acts on a POSIX file descriptor
Definition at line 56 of file pl_fdstream.h.
Definition at line 54 of file pl_fdstream.cc.
References reserveInputBuffer().
:fd(fd_), inbuf(0), inbuf_capacity(0) { if(inbufsize > 0) reserveInputBuffer(inbufsize); }
PLearn::pl_fdstreambuf::~pl_fdstreambuf | ( | ) | [virtual] |
Definition at line 90 of file pl_fdstream.cc.
References c, fd, n, and PLearn::write().
{ if(c!=EOF) { if(pbase()) // buffered mode { streamsize n = streamsize(pptr() - pbase()); *pptr()= char(c); // put it in extra space if(write(fd, pbase(), n) < 0) return EOF; pbump(-n); } else // unbuffered mode { char tinybuf[1]; tinybuf[0] = c; //cout << "writing " << (char)c << endl; if(write(fd, tinybuf, 1) < 0) return EOF; } } else // extra char is EOF, we ignore it { if(pbase()) // buffered mode { streamsize n = streamsize(pptr() - pbase()); if(write(fd, pbase(), n) < 0) return EOF; pbump(-n); } } return c; }
void PLearn::pl_fdstreambuf::reserveInputBuffer | ( | int | buflen | ) | [inline, protected] |
inline for efficiency
Definition at line 66 of file pl_fdstream.h.
Referenced by pl_fdstreambuf().
{ if(buflen>inbuf_capacity) { if(inbuf) delete[] inbuf; inbuf = new char[buflen]; inbuf_capacity = buflen; } }
Definition at line 68 of file pl_fdstream.cc.
{ if(p && len>0) setp(p, p+len-1); // -1 because we want space to put the extra character passed to overflow() else setp(0,0); return this; }
streamsize PLearn::pl_fdstreambuf::showmanyc | ( | ) | [protected, virtual] |
int PLearn::pl_fdstreambuf::sync | ( | ) | [protected, virtual] |
Definition at line 124 of file pl_fdstream.cc.
References fd, n, and PLearn::write().
Referenced by xsputn().
{ streamsize n = streamsize(pptr() - pbase()); if(n>0) { if(write(fd, pbase(), n) < 0) return EOF; pbump(-n); } return 0; }
int PLearn::pl_fdstreambuf::underflow | ( | ) | [protected, virtual] |
Definition at line 81 of file pl_fdstream.cc.
References fd, inbuf, inbuf_capacity, and PLearn::read().
{ int msglength= int(read(fd, inbuf, inbuf_capacity)); if(msglength < 1) return EOF; setg(inbuf, inbuf, inbuf+msglength); return *inbuf; }
streamsize PLearn::pl_fdstreambuf::xsgetn | ( | char * | s, |
streamsize | n | ||
) | [protected, virtual] |
Definition at line 156 of file pl_fdstream.cc.
{ return streambuf::xsgetn(s, n); }
streamsize PLearn::pl_fdstreambuf::xsputn | ( | const char * | s, |
streamsize | n | ||
) | [protected, virtual] |
Definition at line 140 of file pl_fdstream.cc.
References fd, n, sync(), and PLearn::write().
{ if(n>epptr()-pptr()) // n greater than buffer size! { // Let's not waste time copying stuff into the buffer, send it directly sync(); // first make sure we send what's left in the buffer if(write(fd, (char *)s, n) < 0) return 0; } else // call the default method streambuf::xsputn(s,n); return n; }
int PLearn::pl_fdstreambuf::fd [protected] |
the file descriptor
Definition at line 59 of file pl_fdstream.h.
Referenced by overflow(), sync(), underflow(), and xsputn().
char* PLearn::pl_fdstreambuf::inbuf [protected] |
input buffer
Definition at line 60 of file pl_fdstream.h.
Referenced by underflow(), and ~pl_fdstreambuf().
int PLearn::pl_fdstreambuf::inbuf_capacity [protected] |