CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Public Member Functions | Private Types | Private Attributes
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)

Constructor.

Parameters:
fThe 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.

  : m_stream(f), m_begin(NULL), m_end(NULL), m_current(NULL),
    m_buffer_size(1024)
{
  m_begin = new char[m_buffer_size];
  m_end = m_begin;
  m_current = m_end;
} // buffered_istream::buffered_istream()
template<typename Stream >
claw::buffered_istream< Stream >::~buffered_istream ( )

Destructor.

Definition at line 52 of file buffered_istream.tpp.

{
  close();

  if (m_begin)
    delete[] m_begin;
} // buffered_istream::~buffered_istream()

Member Function Documentation

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

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.

{
  m_stream.seekg( m_current - m_end, std::ios_base::cur );
  m_current = m_begin;
  m_end = m_begin;
} // buffered_istream::close()
template<typename Stream >
const char * claw::buffered_istream< Stream >::get_buffer ( ) const

Get the input buffer.

Definition at line 118 of file buffered_istream.tpp.

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

{
  return m_current;
} // buffered_istream::get_buffer()
template<typename Stream >
char claw::buffered_istream< Stream >::get_next ( )

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

Definition at line 128 of file buffered_istream.tpp.

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

{
  assert( remaining() >= 1 );

  char result = *m_current;
  ++m_current;

  return result;
} // buffered_istream::get_next()
template<typename Stream >
void claw::buffered_istream< Stream >::move ( unsigned int  n)

Move some bytes forward.

Parameters:
nThe number of bytes to skip.

Definition at line 169 of file buffered_istream.tpp.

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

{
  assert( m_current + n <= m_end );
  m_current += n;
} // buffered_istream::move()
template<typename Stream >
claw::buffered_istream< Stream >::operator bool ( ) const

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

Definition at line 195 of file buffered_istream.tpp.

{
  return m_stream || (remaining() > 0);
} // buffered_istream::operator bool()
template<typename Stream >
bool claw::buffered_istream< Stream >::read ( char *  buf,
unsigned int  n 
)

Read a range of data.

Parameters:
bufThe buffer in which we write the read data.
nThe number of bytes to read.

Definition at line 145 of file buffered_istream.tpp.

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

{
  while ( (n != 0) && !!(*this) )
    {
      if ( n > remaining() )
        read_more(m_buffer_size);

      unsigned int len = std::min(n, remaining());

      std::copy( m_current, m_current + len, buf );
      buf += len;
      n -= len;
      m_current += len;
    }

  return n==0;
} // buffered_istream::read()
template<typename Stream >
bool claw::buffered_istream< Stream >::read_more ( unsigned int  n)

Increase the number of ready bytes to a given number.

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

Definition at line 77 of file buffered_istream.tpp.

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

{
  if ( n <= remaining() )
    return true;

  unsigned int r = remaining();

  // we'll reach the end of the buffer
  if ( m_current + n > m_begin + m_buffer_size )
    {
      // try to avoid reallocation
      if (n <= m_buffer_size)
        std::copy(m_current, m_end, m_begin);
      else // not enough space in the buffer
        {
          m_buffer_size = n;

          char* new_buffer = new char[m_buffer_size];

          std::copy(m_current, m_end, new_buffer);

          delete[] m_begin;

          m_begin = new_buffer;
        }

      m_current = m_begin;
      m_end = m_current + r;
    }

  m_stream.read( m_end, n-r );
  m_end += m_stream.gcount();

  return !!m_stream;
} // buffered_istream::read_more()
template<typename Stream >
unsigned int claw::buffered_istream< Stream >::remaining ( ) const

Tell how many bytes are ready in the buffer.

Definition at line 65 of file buffered_istream.tpp.

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

{
  return m_end - m_current;
} // buffered_istream::remaining()

Member Data Documentation

template<typename Stream>
char* claw::buffered_istream< Stream >::m_begin [private]

Pointer to the begining of the buffer.

Definition at line 70 of file buffered_istream.hpp.

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

template<typename Stream>
unsigned int claw::buffered_istream< Stream >::m_buffer_size [private]

The size of the allocated buffer.

Definition at line 80 of file buffered_istream.hpp.

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

template<typename Stream>
char* claw::buffered_istream< Stream >::m_current [private]

Pointer to the current not already read valid byte.

Definition at line 77 of file buffered_istream.hpp.

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

template<typename Stream>
char* claw::buffered_istream< Stream >::m_end [private]

Pointer to the first invalid byte after the end of the buffer.

Definition at line 74 of file buffered_istream.hpp.

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

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

The stream we're reading.

Definition at line 67 of file buffered_istream.hpp.


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