PLearn 0.1
Public Member Functions | Protected Member Functions | Protected Attributes
PLearn::pl_fdstreambuf Class Reference

pl_fdstreambuf: stream buffer that acts on a POSIX file descriptor More...

#include <pl_fdstream.h>

Inheritance diagram for PLearn::pl_fdstreambuf:
Inheritance graph
[legend]
Collaboration diagram for PLearn::pl_fdstreambuf:
Collaboration graph
[legend]

List of all members.

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 streambufsetbuf (char *p, int len)
virtual int sync ()

Protected Attributes

int fd
 the file descriptor
char * inbuf
 input buffer
int inbuf_capacity
 length of inbuf

Detailed Description

pl_fdstreambuf: stream buffer that acts on a POSIX file descriptor

Definition at line 56 of file pl_fdstream.h.


Constructor & Destructor Documentation

PLearn::pl_fdstreambuf::pl_fdstreambuf ( int  fd_,
int  inbufsize 
)

Definition at line 54 of file pl_fdstream.cc.

References reserveInputBuffer().

    :fd(fd_), inbuf(0), inbuf_capacity(0)
{
    if(inbufsize > 0)
        reserveInputBuffer(inbufsize);
}

Here is the call graph for this function:

PLearn::pl_fdstreambuf::~pl_fdstreambuf ( ) [virtual]

Definition at line 62 of file pl_fdstream.cc.

References inbuf.

{
    if(inbuf)
        delete[] inbuf;
}

Member Function Documentation

int PLearn::pl_fdstreambuf::overflow ( int  c = EOF) [protected, 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;
}

Here is the call graph for this function:

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;
        }
    }

Here is the caller graph for this function:

streambuf * PLearn::pl_fdstreambuf::setbuf ( char *  p,
int  len 
) [protected, virtual]

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]

not implemented (yet)

Definition at line 78 of file pl_fdstream.cc.

{ return -1; }
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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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;
}

Here is the call graph for this function:

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;
}

Here is the call graph for this function:


Member Data Documentation

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().

length of inbuf

Definition at line 61 of file pl_fdstream.h.

Referenced by underflow().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines