claw::buffered_istream< Stream > Class Template Reference

This class is made to help reading istreams with a buffer. More...

#include <buffered_istream.hpp>

List of all members.

Public Member Functions

 buffered_istream (stream_type &f)
 Constructor.
 ~buffered_istream ()
 Destructor.
unsigned int remaining () const
 Tell how many bytes are ready in the buffer.
bool read_more (unsigned int n)
 Increase the number of ready bytes to a given number.
const char * get_buffer () const
 Get the input buffer.
char get_next ()
 Get the next value in the buffer and move one byte forward.
bool read (char *buf, unsigned int n)
 Read a range of data.
void move (unsigned int n)
 Move some bytes forward.
void close ()
 Closes this buffer (not the stream).
 operator bool () const
 Tell if there is still datas in the buffer/stream.

Private Types

typedef Stream stream_type
 The type of the stream we will read.

Private Attributes

stream_typem_stream
 The stream we're reading.
char * m_begin
 Pointer to the begining of the buffer.
char * m_end
 Pointer to the first invalid byte after the end of the buffer.
char * m_current
 Pointer to the current not already read valid byte.
unsigned int m_buffer_size
 The size of the allocated buffer.

Detailed Description

template<typename Stream>
class claw::buffered_istream< Stream >

This class is made to help reading istreams with a buffer.

Author:
Julien Jorge

Definition at line 42 of file buffered_istream.hpp.


Member Typedef Documentation

template<typename Stream>
typedef Stream claw::buffered_istream< Stream >::stream_type [private]

The type of the stream we will read.

Definition at line 46 of file buffered_istream.hpp.


Constructor & Destructor Documentation

template<typename Stream >
claw::buffered_istream< Stream >::buffered_istream ( stream_type f  )  [inline]

Constructor.

Parameters:
f The file associated to the stream.

Definition at line 38 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_begin, claw::buffered_istream< Stream >::m_buffer_size, claw::buffered_istream< Stream >::m_current, and claw::buffered_istream< Stream >::m_end.

00039   : m_stream(f), m_begin(NULL), m_end(NULL), m_current(NULL),
00040     m_buffer_size(1024)
00041 {
00042   m_begin = new char[m_buffer_size];
00043   m_end = m_begin;
00044   m_current = m_end;
00045 } // buffered_istream::buffered_istream()

template<typename Stream >
claw::buffered_istream< Stream >::~buffered_istream (  )  [inline]

Destructor.

Definition at line 52 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::close(), and claw::buffered_istream< Stream >::m_begin.

00053 {
00054   close();
00055 
00056   if (m_begin)
00057     delete[] m_begin;
00058 } // buffered_istream::~buffered_istream()


Member Function Documentation

template<typename Stream >
void claw::buffered_istream< Stream >::close (  )  [inline]

Closes this buffer (not the stream).

The cursor of the stream is repositioned according to the remaining data, and the buffer is cleared.

Definition at line 183 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_begin, claw::buffered_istream< Stream >::m_current, claw::buffered_istream< Stream >::m_end, and claw::buffered_istream< Stream >::m_stream.

Referenced by claw::buffered_istream< Stream >::~buffered_istream().

00184 {
00185   m_stream.seekg( m_current - m_end, std::ios_base::cur );
00186   m_current = m_begin;
00187   m_end = m_begin;
00188 } // buffered_istream::close()

template<typename Stream >
const char * claw::buffered_istream< Stream >::get_buffer (  )  const [inline]

Get the input buffer.

Definition at line 118 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_current.

Referenced by claw::graphic::bitmap::reader::rle_bitmap_output_buffer< Coded4bits >::copy().

00119 {
00120   return m_current;
00121 } // buffered_istream::get_buffer()

template<typename Stream >
char claw::buffered_istream< Stream >::get_next (  )  [inline]

Get the next value in the buffer and move one byte forward.

Definition at line 128 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_current, and claw::buffered_istream< Stream >::remaining().

Referenced by claw::graphic::bitmap::reader::rle_bitmap_decoder< OutputBuffer >::read_mode().

00129 {
00130   assert( remaining() >= 1 );
00131 
00132   char result = *m_current;
00133   ++m_current;
00134 
00135   return result;
00136 } // buffered_istream::get_next()

template<typename Stream >
void claw::buffered_istream< Stream >::move ( unsigned int  n  )  [inline]

Move some bytes forward.

Parameters:
n The number of bytes to skip.

Definition at line 169 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_current, and claw::buffered_istream< Stream >::m_end.

Referenced by claw::graphic::bitmap::reader::rle_bitmap_output_buffer< Coded4bits >::copy().

00170 {
00171   assert( m_current + n <= m_end );
00172   m_current += n;
00173 } // buffered_istream::move()

template<typename Stream >
claw::buffered_istream< Stream >::operator bool (  )  const [inline]

Tell if there is still datas in the buffer/stream.

Definition at line 195 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_stream, and claw::buffered_istream< Stream >::remaining().

00196 {
00197   return m_stream || (remaining() > 0);
00198 } // buffered_istream::operator bool()

template<typename Stream >
bool claw::buffered_istream< Stream >::read ( char *  buf,
unsigned int  n 
) [inline]

Read a range of data.

Parameters:
buf The buffer in which we write the read data.
n The number of bytes to read.

Definition at line 145 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_buffer_size, claw::buffered_istream< Stream >::m_current, claw::buffered_istream< Stream >::read_more(), and claw::buffered_istream< Stream >::remaining().

Referenced by claw::graphic::pcx::reader::load_256_color_mapped().

00146 {
00147   while ( (n != 0) && !!(*this) )
00148     {
00149       if ( n > remaining() )
00150         read_more(m_buffer_size);
00151 
00152       unsigned int len = std::min(n, remaining());
00153 
00154       std::copy( m_current, m_current + len, buf );
00155       buf += len;
00156       n -= len;
00157       m_current += len;
00158     }
00159 
00160   return n==0;
00161 } // buffered_istream::read()

template<typename Stream >
bool claw::buffered_istream< Stream >::read_more ( unsigned int  n  )  [inline]

Increase the number of ready bytes to a given number.

Parameters:
n The number of bytes you need.
Remarks:
This method reads n - remaining() bytes from the file.

Definition at line 77 of file buffered_istream.tpp.

References claw::buffered_istream< Stream >::m_begin, claw::buffered_istream< Stream >::m_buffer_size, claw::buffered_istream< Stream >::m_current, claw::buffered_istream< Stream >::m_end, claw::buffered_istream< Stream >::m_stream, and claw::buffered_istream< Stream >::remaining().

Referenced by claw::graphic::bitmap::reader::rle_bitmap_output_buffer< Coded4bits >::copy(), claw::buffered_istream< Stream >::read(), and claw::graphic::bitmap::reader::rle_bitmap_decoder< OutputBuffer >::read_mode().

00078 {
00079   if ( n <= remaining() )
00080     return true;
00081 
00082   unsigned int r = remaining();
00083 
00084   // we'll reach the end of the buffer
00085   if ( m_current + n > m_begin + m_buffer_size )
00086     {
00087       // try to avoid reallocation
00088       if (n <= m_buffer_size)
00089         std::copy(m_current, m_end, m_begin);
00090       else // not enough space in the buffer
00091         {
00092           m_buffer_size = n;
00093 
00094           char* new_buffer = new char[m_buffer_size];
00095 
00096           std::copy(m_current, m_end, new_buffer);
00097 
00098           delete[] m_begin;
00099 
00100           m_begin = new_buffer;
00101         }
00102 
00103       m_current = m_begin;
00104       m_end = m_current + r;
00105     }
00106 
00107   m_stream.read( m_end, n-r );
00108   m_end += m_stream.gcount();
00109 
00110   return !!m_stream;
00111 } // buffered_istream::read_more()

template<typename Stream >
unsigned int claw::buffered_istream< Stream >::remaining (  )  const [inline]

Member Data Documentation

template<typename Stream>
char* claw::buffered_istream< Stream >::m_begin [private]
template<typename Stream>
unsigned int claw::buffered_istream< Stream >::m_buffer_size [private]
template<typename Stream>
char* claw::buffered_istream< Stream >::m_current [private]
template<typename Stream>
char* claw::buffered_istream< Stream >::m_end [private]
template<typename Stream>
stream_type& claw::buffered_istream< Stream >::m_stream [private]

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

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1