PLearn 0.1
|
#include <pl_streambuf.h>
Public Member Functions | |
pl_streambuf (streambuf &_original_buf, int_type _inbuflen=0) | |
ctor: needs a stream buffer | |
virtual | ~pl_streambuf () |
destructor: delete the allocated input buffer | |
void | seekmark (const pl_streammarker &mark) |
reposition input to the marked position | |
Protected Member Functions | |
virtual int_type | underflow () |
underflow redefined | |
virtual int_type | uflow () |
uflow redefined | |
virtual streamsize | xsgetn (char *s, streamsize n) |
virtual streamsize | xsputn (const char *s, streamsize n) |
virtual int_type | overflow (int_type meta=pl_streambuf::eof) |
overflow redefined | |
virtual int_type | sync () |
sync redefined | |
virtual int_type | pbackfail (int_type c=eof) |
pbackfail redefined | |
int | curpos () const |
return current position within the input buffer | |
Protected Attributes | |
streambuf & | original_buf |
original buffer that needs marking | |
char * | inbuf |
buffer for marking: keeps all chars since first active mark | |
int | inbuflen |
current length of inbuf | |
pl_streammarker * | first_marker |
ptr. to the head of a linked list of active markers | |
Static Protected Attributes | |
static const int | pback_size = 4 |
length of default put back area | |
static const int | min_buf_size = 16 |
minimum buffer size | |
Private Types | |
typedef int | int_type |
typedef char | char_type |
typedef streambuf | inherited |
Static Private Attributes | |
static const int_type | eof = EOF |
Friends | |
class | pl_streammarker |
class | ioassignstream |
pl_streambuf: stream buffer that allows marking. This buffer makes a second layer of buffering over another stream buffer.
Definition at line 64 of file pl_streambuf.h.
typedef char PLearn::pl_streambuf::char_type [private] |
Definition at line 68 of file pl_streambuf.h.
typedef streambuf PLearn::pl_streambuf::inherited [private] |
Definition at line 77 of file pl_streambuf.h.
typedef int PLearn::pl_streambuf::int_type [private] |
Definition at line 67 of file pl_streambuf.h.
ctor: needs a stream buffer
ctor: create an input buffer
Definition at line 47 of file pl_streambuf.cc.
References inbuf, inbuflen, min_buf_size, and pback_size.
:original_buf(_original_buf), inbuf(0), inbuflen(_inbuflen), first_marker(0) { //set minimum size for buffer if(inbuflen < pback_size+1) inbuflen= min_buf_size + pback_size; inbuf= new char[inbuflen]; setg(inbuf+pback_size, inbuf+pback_size, inbuf+pback_size); // reserve a few bytes for a putback area. }
pl_streambuf::~pl_streambuf | ( | ) | [virtual] |
destructor: delete the allocated input buffer
Definition at line 59 of file pl_streambuf.cc.
References inbuf.
int PLearn::pl_streambuf::curpos | ( | ) | const [inline, protected] |
return current position within the input buffer
Definition at line 103 of file pl_streambuf.h.
Referenced by PLearn::pl_streammarker::pl_streammarker().
pl_streambuf::int_type pl_streambuf::overflow | ( | int_type | meta = pl_streambuf::eof | ) | [protected, virtual] |
overflow redefined
Definition at line 163 of file pl_streambuf.cc.
References original_buf.
Referenced by xsputn().
{ return original_buf.sputc(meta); } //(no marking on output == no buffering)
pl_streambuf::int_type pl_streambuf::pbackfail | ( | int_type | c = eof | ) | [protected, virtual] |
pbackfail redefined
Definition at line 178 of file pl_streambuf.cc.
References original_buf.
{ return original_buf.sungetc(); } //< pback before beginning of our buf: pback in underlying streambuf
void pl_streambuf::seekmark | ( | const pl_streammarker & | mark | ) |
reposition input to the marked position
Definition at line 166 of file pl_streambuf.cc.
References inbuf, and PLearn::pl_streammarker::pos.
Referenced by PLearn::readFieldName().
pl_streambuf::int_type pl_streambuf::sync | ( | ) | [protected, virtual] |
sync redefined
Definition at line 169 of file pl_streambuf.cc.
References original_buf.
{ // no marking on output: sync underlying streambuf #if __GNUC__ < 3 && !defined(WIN32) return original_buf.sync(); #else return original_buf.pubsync(); #endif }
pl_streambuf::int_type pl_streambuf::uflow | ( | ) | [protected, virtual] |
uflow redefined
Definition at line 131 of file pl_streambuf.cc.
References c, first_marker, original_buf, and underflow().
Referenced by xsgetn().
{ int c= underflow(); //< get char. at current pos. if(first_marker == 0 && gptr() == egptr()) original_buf.sbumpc(); //< no mark: advance pos. of original streambuf else gbump(1); //< mark(s): advance pos. in our buffer return c; }
pl_streambuf::int_type pl_streambuf::underflow | ( | ) | [protected, virtual] |
underflow redefined
underflow: If no mark, get from underlying buffer.
If buffer is marked, grow/fill buffer as necessary and return next char.
Definition at line 67 of file pl_streambuf.cc.
References eof, first_marker, i, inbuf, inbuflen, min_buf_size, original_buf, and pback_size.
Referenced by uflow().
{ //no marker: return get from original buffer. if(first_marker == 0 && gptr() == egptr()) { //delete buffer if we have a long one that is not used anymore if(inbuflen > min_buf_size + pback_size) { //don't keep copy of last char: underlying buf. will be used for putbacks. delete[] inbuf; inbuflen= min_buf_size + pback_size; inbuf= new char[inbuflen]; setg(inbuf+pback_size, inbuf+pback_size, inbuf+pback_size); } return original_buf.sgetc(); } //marked buffer: //return a buffered char if any is available (is this necessary? -xsm) if(gptr() < egptr()) return *gptr(); int oldbuflen = int(egptr()-inbuf); //< current length used //if at end of buffer, make it twice as long as before if(egptr() == inbuf+inbuflen) { //create a new longer buffer int newbuflen= inbuflen*2; char* newbuf= new char[newbuflen]; //copy from current buf. to new one for(int i= 0; i < inbuflen; ++i) newbuf[i]= inbuf[i]; //reposition get pointers setg(newbuf+pback_size, newbuf+(gptr()-inbuf), newbuf+inbuflen); delete[] inbuf; //< delete prev. buffer inbuf= newbuf; //< point to new buffer inbuflen= newbuflen; //< adjust current buffer length } char* the_egptr= 0; //ptr. to actual end of buf. (not known yet) //fill buffer from underlying streambuf for(int i= oldbuflen; i < inbuflen; ++i) if((original_buf.sgetc() != pl_streambuf::eof && original_buf.in_avail()) || i == oldbuflen) inbuf[i]= original_buf.sbumpc(); //< get a char from underlying streambuf and advance it's pos. else { //no input available: stop filling buffer (set egptr at current pos) the_egptr= inbuf+i; break; } if(the_egptr == 0) //buf. all filled: set egptr at end of buf. the_egptr= inbuf + inbuflen; //set pointers into buffer setg(eback(), gptr(), the_egptr); if(gptr() < egptr()) //< got some new stuff? return *gptr(); //< return next char. return pl_streambuf::eof; //< at eof: return eof. }
streamsize pl_streambuf::xsgetn | ( | char * | s, |
streamsize | n | ||
) | [protected, virtual] |
streamsize pl_streambuf::xsputn | ( | const char * | s, |
streamsize | n | ||
) | [protected, virtual] |
Definition at line 153 of file pl_streambuf.cc.
References c, eof, i, and overflow().
{ int_type c = 1; int i; for(i= 0; i < n && c != pl_streambuf::eof; ++i) c= overflow(static_cast<int_type>(s[i])); return i; }
friend class ioassignstream [friend] |
Definition at line 74 of file pl_streambuf.h.
friend class pl_streammarker [friend] |
Definition at line 73 of file pl_streambuf.h.
const int_type PLearn::pl_streambuf::eof = EOF [static, private] |
Definition at line 71 of file pl_streambuf.h.
Referenced by underflow(), xsgetn(), and xsputn().
pl_streammarker* PLearn::pl_streambuf::first_marker [protected] |
ptr. to the head of a linked list of active markers
Definition at line 87 of file pl_streambuf.h.
Referenced by PLearn::pl_streammarker::pl_streammarker(), uflow(), underflow(), and PLearn::pl_streammarker::~pl_streammarker().
char* PLearn::pl_streambuf::inbuf [protected] |
buffer for marking: keeps all chars since first active mark
Definition at line 82 of file pl_streambuf.h.
Referenced by pl_streambuf(), seekmark(), underflow(), and ~pl_streambuf().
int PLearn::pl_streambuf::inbuflen [protected] |
current length of inbuf
Definition at line 83 of file pl_streambuf.h.
Referenced by pl_streambuf(), and underflow().
const int PLearn::pl_streambuf::min_buf_size = 16 [static, protected] |
minimum buffer size
Definition at line 85 of file pl_streambuf.h.
Referenced by pl_streambuf(), and underflow().
streambuf& PLearn::pl_streambuf::original_buf [protected] |
original buffer that needs marking
Definition at line 81 of file pl_streambuf.h.
Referenced by overflow(), pbackfail(), sync(), uflow(), and underflow().
const int PLearn::pl_streambuf::pback_size = 4 [static, protected] |
length of default put back area
Definition at line 84 of file pl_streambuf.h.
Referenced by pl_streambuf(), and underflow().