This class is made to help reading istreams with a buffer. More...
#include <buffered_istream.hpp>
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_type & | m_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. |
This class is made to help reading istreams with a buffer.
Definition at line 42 of file buffered_istream.hpp.
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.
claw::buffered_istream< Stream >::buffered_istream | ( | stream_type & | f | ) | [inline] |
Constructor.
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.
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.
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().
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()
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().
void claw::buffered_istream< Stream >::move | ( | unsigned int | n | ) | [inline] |
Move some bytes forward.
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().
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().
bool claw::buffered_istream< Stream >::read | ( | char * | buf, | |
unsigned int | n | |||
) | [inline] |
Read a range of data.
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()
bool claw::buffered_istream< Stream >::read_more | ( | unsigned int | n | ) | [inline] |
Increase the number of ready bytes to a given number.
n | The number of bytes you need. |
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()
unsigned int claw::buffered_istream< Stream >::remaining | ( | ) | const [inline] |
Tell how many bytes are ready in the buffer.
Definition at line 65 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(), claw::buffered_istream< Stream >::get_next(), claw::buffered_istream< Stream >::operator bool(), claw::buffered_istream< Stream >::read(), claw::graphic::bitmap::reader::rle_bitmap_decoder< OutputBuffer >::read_mode(), and claw::buffered_istream< Stream >::read_more().
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(), claw::buffered_istream< Stream >::close(), claw::buffered_istream< Stream >::read_more(), and claw::buffered_istream< Stream >::~buffered_istream().
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(), claw::buffered_istream< Stream >::read(), and claw::buffered_istream< Stream >::read_more().
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(), claw::buffered_istream< Stream >::close(), claw::buffered_istream< Stream >::get_buffer(), claw::buffered_istream< Stream >::get_next(), claw::buffered_istream< Stream >::move(), claw::buffered_istream< Stream >::read(), claw::buffered_istream< Stream >::read_more(), and claw::buffered_istream< Stream >::remaining().
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(), claw::buffered_istream< Stream >::close(), claw::buffered_istream< Stream >::move(), claw::buffered_istream< Stream >::read_more(), and claw::buffered_istream< Stream >::remaining().
stream_type& claw::buffered_istream< Stream >::m_stream [private] |
The stream we're reading.
Definition at line 67 of file buffered_istream.hpp.
Referenced by claw::buffered_istream< Stream >::close(), claw::buffered_istream< Stream >::operator bool(), and claw::buffered_istream< Stream >::read_more().