ders::buf_reader Class Reference

Provides buffered read functions for classes that implement readable interface. More...

#include <buf_rdwr.hpp>

List of all members.

Public Member Functions

 buf_reader (mem_pool &m, readable &rdbl, int bufsz=4 *1024)
 ~buf_reader ()
int read (void *b, int n)
int read (sh_text &buf, int n)
int read ()
bool read_line (sh_text &line, bool noendl=true)
void reset ()
const char * begin () const
const char * end () const


Detailed Description

Provides buffered read functions for classes that implement readable interface.

Definition at line 46 of file buf_rdwr.hpp.


Constructor & Destructor Documentation

ders::buf_reader::buf_reader ( mem_pool m,
readable rdbl,
int  bufsz = 4*1024 
)

Definition at line 37 of file buf_rdwr.cpp.

00037                                                              :
00038   mp(m), rd(rdbl), bsz(bufsz)
00039 {
00040  assert(bsz>0);
00041 
00042  buf=static_cast<unsigned char*>(mp.allocate(bsz));
00043  reset();
00044 }

ders::buf_reader::~buf_reader (  )  [inline]

Definition at line 60 of file buf_rdwr.hpp.

00060 { mp.deallocate(buf, bsz); }


Member Function Documentation

int ders::buf_reader::read ( void *  b,
int  n 
)

Definition at line 46 of file buf_rdwr.cpp.

00047 {
00048  assert(n>=0);
00049 
00050  if (n==0) return 0;
00051 
00052  if (it==lim) {
00053     int rc=rd.read(buf, bsz);
00054     switch (rc) {
00055            case -1: return -1;
00056            case  0: return 0;
00057     }
00058 
00059     it=0;
00060     lim=rc;
00061  }
00062 
00063  if (n<=lim-it) {
00064     memcpy(b, buf, n);
00065     it+=n;
00066 
00067     return n;
00068  }
00069 
00070  // n>lim-it
00071  int rdn=lim-it;
00072  memcpy(b, buf, rdn);
00073  reset();
00074 
00075  b=static_cast<unsigned char*>(b)+rdn;
00076  n-=rdn;
00077 
00078  int rc=rd.read(b, n);
00079  return (rc!=-1) ? rdn+rc : rdn;
00080 }

int ders::buf_reader::read ( sh_text buf,
int  n 
)

Definition at line 82 of file buf_rdwr.cpp.

00083 {
00084  assert(n>=0);
00085 
00086  buf->clear();
00087  buf->reserve(n);
00088 
00089  int rc=read(buf->begin(), n);
00090  assert(rc<=n);
00091  if (rc!=-1) buf->uninitialized_resize(rc);
00092 
00093  return rc;
00094 }

int ders::buf_reader::read (  )  [inline]

Definition at line 64 of file buf_rdwr.hpp.

00064 { return (it<lim) ? buf[it++] : read_impl(); }

bool ders::buf_reader::read_line ( sh_text line,
bool  noendl = true 
)

Definition at line 96 of file buf_rdwr.cpp.

00097 {
00098  line->clear();
00099 
00100  for (int ch; ; ) {
00101      if ((ch=read())==-1) return line->size()!=0;
00102 
00103      if (ch!='\n') line->append(ch);
00104      else {
00105           if (!noendl) line->append(ch);
00106           else {
00107                if (line->size()>0 && line->back()=='\r')
00108                   line->uninitialized_resize(line->size()-1);
00109           }
00110 
00111           break;
00112      }
00113  }
00114 
00115  return true;
00116 }

void ders::buf_reader::reset (  )  [inline]

Definition at line 67 of file buf_rdwr.hpp.

00067 { it=lim=0; }

const char* ders::buf_reader::begin (  )  const [inline]

Definition at line 69 of file buf_rdwr.hpp.

00069 { return reinterpret_cast<char*>(buf)+it; }

const char* ders::buf_reader::end (  )  const [inline]

Definition at line 70 of file buf_rdwr.hpp.

00070 { return reinterpret_cast<char*>(buf)+lim; }


The documentation for this class was generated from the following files:

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5