PLearn 0.1
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Types | Private Member Functions | Private Attributes
PLearn::PStreamBuf Class Reference

#include <PStreamBuf.h>

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

List of all members.

Public Types

typedef size_t streamsize
typedef off_t streampos

Public Member Functions

 PStreamBuf (bool is_readable_, bool is_writable_, streamsize inbuf_capacity=1, streamsize outbuf_capacity=0, streamsize unget_capacity=default_ungetsize)
 Default constructor.
void setBufferCapacities (streamsize inbuf_capacity, streamsize outbuf_capacity, streamsize unget_capacity)
virtual ~PStreamBuf ()
bool inbufEmpty () const
bool isReadable () const
bool isWritable () const
int get ()
void putback (char c)
 Character c will be returned by the next get().
void unget ()
 Call putback(c) where c is the last character read by get() or read(), stored in 'last_get'.
int peek ()
streamsize read (char *p, streamsize n)
 Reads n chars, unless eof is reached or an error occurs; blocks if needed.
void unread (const char *p, streamsize n)
 Puts the given characters back in the input buffer so that they're the next thing read.
virtual void flush ()
void put (char c)
void write (const char *p, streamsize n)
virtual bool good () const
 Checks if the streambuf is valid and can be written to or read from.
bool eof () const
 Checks if we reached the end of the file.

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) Default version issues a PLERROR

Protected Attributes

bool is_readable
bool is_writable
int last_get
 Remember the last character read by get() or read().

Static Protected Attributes

static const streamsize default_ungetsize = 100
 Default size for unget buffer for PStreamBuf and its subclasses.

Private Types

typedef PPointable inherited

Private Member Functions

streamsize refill_in_buf ()

Private Attributes

streamsize ungetsize
streamsize inbuf_chunksize
char * inbuf
 beginning of input buffer
char * inbuf_p
 position of next character to be read
char * inbuf_end
 one after last available character
streamsize outbuf_chunksize
char * outbuf
 beginning of output buffer
char * outbuf_p
 position of next character to be written
char * outbuf_end
 one after last reserved character in outbuf

Detailed Description

Definition at line 49 of file PStreamBuf.h.


Member Typedef Documentation

Definition at line 57 of file PStreamBuf.h.

Definition at line 56 of file PStreamBuf.h.


Constructor & Destructor Documentation

PLearn::PStreamBuf::PStreamBuf ( bool  is_readable_,
bool  is_writable_,
streamsize  inbuf_capacity = 1,
streamsize  outbuf_capacity = 0,
streamsize  unget_capacity = default_ungetsize 
)

Default constructor.

Definition at line 48 of file PStreamBuf.cc.

References setBufferCapacities().

    :is_readable(is_readable_),
     is_writable(is_writable_),
     last_get(PSTREAMBUF_NO_GET),
     ungetsize(0),
     inbuf_chunksize(0),
     inbuf(0), inbuf_p(0), inbuf_end(0),
     outbuf_chunksize(0),
     outbuf(0), outbuf_p(0), outbuf_end(0)
{
    setBufferCapacities(inbuf_capacity, outbuf_capacity, unget_capacity);
}

Here is the call graph for this function:

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

Definition at line 63 of file PStreamBuf.cc.

References inbuf, and outbuf.

{
    if (inbuf)
        delete[] inbuf;
    if (outbuf)
        delete[] outbuf;
}

Member Function Documentation

bool PLearn::PStreamBuf::eof ( ) const [inline]

Checks if we reached the end of the file.

Definition at line 198 of file PStreamBuf.h.

Referenced by good(), and PLearn::StdPStreamBuf::good().

    {
        return const_cast<PStreamBuf*>(this)->peek() == EOF;
    }

Here is the caller graph for this function:

void PLearn::PStreamBuf::flush ( ) [virtual]
int PLearn::PStreamBuf::get ( ) [inline]

Definition at line 139 of file PStreamBuf.h.

    {
        if(inbuf_p<inbuf_end || refill_in_buf())
            return (last_get = (unsigned char) *inbuf_p++);
        else
            return (last_get = -1);
    }
bool PLearn::PStreamBuf::good ( ) const [virtual]

Checks if the streambuf is valid and can be written to or read from.

Reimplemented in PLearn::StdPStreamBuf.

Definition at line 267 of file PStreamBuf.cc.

References eof(), is_readable, and is_writable.

{
    if (is_readable)
        return !eof();
    else
        return is_writable;
}

Here is the call graph for this function:

bool PLearn::PStreamBuf::inbufEmpty ( ) const [inline]

Definition at line 130 of file PStreamBuf.h.

    { return !(inbuf_p<inbuf_end); }
bool PLearn::PStreamBuf::isReadable ( ) const [inline]

Definition at line 133 of file PStreamBuf.h.

Referenced by read(), and refill_in_buf().

    { return is_readable; }

Here is the caller graph for this function:

bool PLearn::PStreamBuf::isWritable ( ) const [inline]

Definition at line 136 of file PStreamBuf.h.

Referenced by write().

    { return is_writable; }

Here is the caller graph for this function:

int PLearn::PStreamBuf::peek ( ) [inline]

Definition at line 158 of file PStreamBuf.h.

    {
        if(inbuf_p<inbuf_end || refill_in_buf())
            return (unsigned char) *inbuf_p;
        else
            return -1;
    }
void PLearn::PStreamBuf::put ( char  c) [inline]

Definition at line 176 of file PStreamBuf.h.

References c, PLearn::flush(), and PLERROR.

    {
#ifdef BOUNDCHECK
        if(!isWritable())
            PLERROR("Called PStreamBuf::put on a buffer not marked as writable");
#endif
        if(outbuf_chunksize>0) // buffered
        {
            if(outbuf_p==outbuf_end)
                flush();
            *outbuf_p++ = c;
        }
        else // unbuffered
            write_(&c,1);
    }

Here is the call graph for this function:

void PLearn::PStreamBuf::putback ( char  c)

Character c will be returned by the next get().

If you put back the result of a previous call to get(), make sure it is not EOF.

Definition at line 203 of file PStreamBuf.cc.

References c, inbuf, inbuf_p, and PLERROR.

Referenced by unget().

{
    if(inbuf_p<=inbuf)
        PLERROR("Cannot putback('%c') Input buffer bound reached (you may want to increase the unget_capacity)",c);
  
    inbuf_p--;
    *inbuf_p = c;
}

Here is the caller graph for this function:

PStreamBuf::streamsize PLearn::PStreamBuf::read ( char *  p,
streamsize  n 
)

Reads n chars, unless eof is reached or an error occurs; blocks if needed.

Definition at line 135 of file PStreamBuf.cc.

References inbuf_chunksize, inbuf_end, inbuf_p, isReadable(), last_get, n, PLERROR, read_(), and refill_in_buf().

Referenced by PLearn::FdPStreamBuf::read_(), and PLearn::PStream::readAll().

{
#ifdef BOUNDCHECK
    if(!isReadable())
        PLERROR("Called PStreamBuf::read on a buffer not marked as readable");
#endif

    streamsize nleft = n;

    streamsize inbuf_n = (streamsize)(inbuf_end-inbuf_p);
    if(inbuf_n) // First copy what's left in the buffer
    {
        streamsize k = nleft<inbuf_n ?nleft :inbuf_n;
        memcpy(p,inbuf_p,k);
        inbuf_p += k;
        p += k;
        nleft -= k;
    }

    if(nleft) // need some more ?
    {
        if(nleft>=inbuf_chunksize) // large block: read it directly
        {
            streamsize nr= read_(p,nleft);
            nleft-= nr;
            p+= nr;
            while(nleft > 0 && nr > 0) // need some more and not eof?
            {
                nr= read_(p,nleft);
                nleft-= nr;
                p+= nr;
            }
        }
        else // small block: read it in the buffer first
        {
            inbuf_n = refill_in_buf();
            while(nleft > 0 && inbuf_n > 0) // need some more and not eof?
            {
                streamsize k = nleft<inbuf_n ?nleft :inbuf_n;
                memcpy(p,inbuf_p,k);
                inbuf_p += k;
                nleft -= k;
                p+= k;
                if(nleft > 0)
                    inbuf_n = refill_in_buf();
            }
        }
    }

    streamsize nread = n-nleft;

    if (nread > 0)
        last_get = (unsigned char) p[-1];//p has advanced and now points one after the end
    else
        last_get = EOF;

    return nread;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PStreamBuf::streamsize PLearn::PStreamBuf::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 in PLearn::FdPStreamBuf, PLearn::FilePStreamBuf, PLearn::MPIPStreamBuf, PLearn::NullPStreamBuf, PLearn::PrPStreamBuf, PLearn::ServerLogStreamBuf, PLearn::StdPStreamBuf, and PLearn::StringPStreamBuf.

Definition at line 108 of file PStreamBuf.cc.

References PLERROR.

Referenced by read(), and refill_in_buf().

{
    PLERROR("read_ not implemented for this PStreamBuf");
    return 0;
}

Here is the caller graph for this function:

PStreamBuf::streamsize PLearn::PStreamBuf::refill_in_buf ( ) [private]

Definition at line 121 of file PStreamBuf.cc.

References inbuf, inbuf_chunksize, inbuf_end, inbuf_p, isReadable(), n, PLERROR, read_(), and ungetsize.

Referenced by read().

{
#ifdef BOUNDCHECK
    if(!isReadable())
        PLERROR("Called PStreamBuf::refill_in_buf on a buffer not marked as readable");
#endif

    inbuf_p = inbuf + ungetsize;
    inbuf_end= inbuf_p; //buf empty until read_ finished
    streamsize n = read_(inbuf_p, inbuf_chunksize);
    inbuf_end = inbuf_p + n;
    return n;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::PStreamBuf::setBufferCapacities ( streamsize  inbuf_capacity,
streamsize  outbuf_capacity,
streamsize  unget_capacity 
)

Definition at line 71 of file PStreamBuf.cc.

References flush(), inbuf, inbuf_chunksize, inbuf_end, inbuf_p, outbuf, outbuf_chunksize, outbuf_end, outbuf_p, and ungetsize.

Referenced by PStreamBuf().

{
    if (inbuf_capacity < 1)
        inbuf_capacity = 1;
    if (unget_capacity < 1)
        unget_capacity = 1;
    ungetsize = unget_capacity;
    inbuf_chunksize = inbuf_capacity;
    outbuf_chunksize = outbuf_capacity;
    
    if (inbuf)
        delete[] inbuf;
    if (ungetsize + inbuf_chunksize <= 0)
        inbuf = inbuf_p = inbuf_end = 0;
    else
    {
        inbuf = new char[ungetsize+inbuf_chunksize];
        inbuf_p = inbuf+ungetsize;
        inbuf_end = inbuf_p;
    }

    if (outbuf)
    {
        flush();
        delete[] outbuf;
    }
    if (outbuf_chunksize <= 0)
        outbuf = outbuf_p = outbuf_end = 0;
    else
    {
        outbuf = new char[outbuf_chunksize];
        outbuf_p = outbuf;
        outbuf_end = outbuf+outbuf_chunksize;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::PStreamBuf::unget ( )

Call putback(c) where c is the last character read by get() or read(), stored in 'last_get'.

If last_get == EOF, does nothing. Will crash if one tries to call it twice (use unread() instead), i.e. when last_get == PSTREAMBUF_NO_GET.

Definition at line 212 of file PStreamBuf.cc.

References last_get, PLERROR, PSTREAMBUF_NO_GET, and putback().

{
    if (last_get == PSTREAMBUF_NO_GET)
        PLERROR("In PStreamBuf::unget - You called unget() more than once or you did not call get() first");
    if (last_get != EOF)
        putback((char) last_get);
    last_get = PSTREAMBUF_NO_GET;
}

Here is the call graph for this function:

void PLearn::PStreamBuf::unread ( const char *  p,
streamsize  n 
)

Puts the given characters back in the input buffer so that they're the next thing read.

Definition at line 194 of file PStreamBuf.cc.

References inbuf, inbuf_p, n, and PLERROR.

{
    if(streamsize(inbuf_p-inbuf)<n)
        PLERROR("Cannot unread that many characters: %ld, input buffer bound reached (you may want to increase the unget_capacity)", long(n));
  
    inbuf_p -= n;
    memcpy(inbuf_p,p,n);
}
void PLearn::PStreamBuf::write ( const char *  p,
streamsize  n 
)

Definition at line 233 of file PStreamBuf.cc.

References flush(), isWritable(), n, outbuf_chunksize, outbuf_end, outbuf_p, PLERROR, and write_().

Referenced by PLearn::FdPStreamBuf::write_().

{
#ifdef BOUNDCHECK
    if(!isWritable())
        PLERROR("Called PStreamBuf::write on a buffer not marked as writable");
#endif
    if(outbuf_chunksize>0) // buffered
    {
        streamsize bufrem = (streamsize)(outbuf_end-outbuf_p);
        if(n<=bufrem)
        {
            memcpy(outbuf_p, p, n);
            outbuf_p += n;
        }
        else // n>bufrem
        {
            memcpy(outbuf_p, p, bufrem);
            outbuf_p += bufrem;
            flush();
            p += bufrem;
            n -= bufrem;
            if(n>outbuf_chunksize)
                write_(p, n);
            else
            {
                memcpy(outbuf_p, p, n);
                outbuf_p += n;
            }
        }
    }
    else // unbuffered
        write_(p,n);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::PStreamBuf::write_ ( const char *  p,
streamsize  n 
) [protected, virtual]

writes exactly n characters from p (unbuffered, must flush) Default version issues a PLERROR

Reimplemented in PLearn::FdPStreamBuf, PLearn::FilePStreamBuf, PLearn::MPIPStreamBuf, PLearn::NullPStreamBuf, PLearn::PrPStreamBuf, PLearn::ServerLogStreamBuf, PLearn::StdPStreamBuf, and PLearn::StringPStreamBuf.

Definition at line 116 of file PStreamBuf.cc.

References PLERROR.

Referenced by flush(), and write().

{
    PLERROR("write_ not implemented for this PStreamBuf");
}

Here is the caller graph for this function:


Member Data Documentation

const streamsize PLearn::PStreamBuf::default_ungetsize = 100 [static, protected]

Default size for unget buffer for PStreamBuf and its subclasses.

Definition at line 77 of file PStreamBuf.h.

char* PLearn::PStreamBuf::inbuf [private]

beginning of input buffer

Definition at line 94 of file PStreamBuf.h.

Referenced by putback(), refill_in_buf(), setBufferCapacities(), unread(), and ~PStreamBuf().

Definition at line 93 of file PStreamBuf.h.

Referenced by read(), refill_in_buf(), and setBufferCapacities().

one after last available character

Definition at line 96 of file PStreamBuf.h.

Referenced by read(), refill_in_buf(), and setBufferCapacities().

char* PLearn::PStreamBuf::inbuf_p [private]

position of next character to be read

Definition at line 95 of file PStreamBuf.h.

Referenced by putback(), read(), refill_in_buf(), setBufferCapacities(), and unread().

Definition at line 79 of file PStreamBuf.h.

Referenced by good(), PLearn::StdPStreamBuf::good(), and PLearn::StdPStreamBuf::setIn().

Definition at line 80 of file PStreamBuf.h.

Referenced by good(), PLearn::StdPStreamBuf::good(), and PLearn::StdPStreamBuf::setOut().

Remember the last character read by get() or read().

Could be EOF, or PSTREAMBUF_NO_GET (no character available).

Definition at line 84 of file PStreamBuf.h.

Referenced by read(), and unget().

char* PLearn::PStreamBuf::outbuf [private]

beginning of output buffer

Definition at line 100 of file PStreamBuf.h.

Referenced by flush(), setBufferCapacities(), and ~PStreamBuf().

Definition at line 99 of file PStreamBuf.h.

Referenced by setBufferCapacities(), and write().

one after last reserved character in outbuf

Definition at line 102 of file PStreamBuf.h.

Referenced by setBufferCapacities(), and write().

position of next character to be written

Definition at line 101 of file PStreamBuf.h.

Referenced by flush(), setBufferCapacities(), and write().

Definition at line 92 of file PStreamBuf.h.

Referenced by refill_in_buf(), and setBufferCapacities().


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