Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

GBuffer Class Reference

This class implements a general purpose dynamically growing data buffer. More...

List of all members.

Public Member Functions

 GBuffer (unsigned int size, unsigned int growDelta=STANDARD_GROW_DELTA)
 Creates a new GBuffer with a given initial site.

 ~GBuffer (void)
 Destroys the buffer and the data in it.

void add (unsigned char *data, unsigned int size)
 Adds data to the buffer at the end of the buffer.

unsigned char * addPtr (unsigned int size)
 Returns a pointer to the end of the buffer which can be used in library routines that require a buffer as a target (read(...)) for example, ensuring that enough space is left in the buffer.

void added (unsigned int size)
 Moves the end of the buffer by size bytes.

unsigned int get (unsigned char *buffer, unsigned int size)
 Stores up to size bytes of data starting at the current position in a target buffer buffer.

const unsigned char * getPtr (void) const
 Returns a pointer to the current position.

unsigned char * readLine (void)
 Returns a line of text, starting at the current position, ending at the next newline character, in a string.

void shrink (void)
 Reallocates the memory block occupied by the buffer such that the maximal size is equal to the current size, for more efficient memory usage.

unsigned int getSize (void)
 Returns the size of the data currently stored in the buffer.


Private Member Functions

void ensureSize (unsigned int size)
 Makes sure that data of the size given can be inserted into the buffer without a buffer overflow.


Private Attributes

unsigned char * data
 The data.

unsigned int pos
 The current read position.

unsigned int size
 The current size of the buffer.

unsigned int maxSize
 The maximal size of the buffer.

unsigned int growDelta
 The amount of bytes the buffer grows when necessary.


Detailed Description

This class implements a general purpose dynamically growing data buffer.

The buffer provides a readLine method to simplify the retrieval of text data stored in the buffer.

Author:
Holger Wunsch


Constructor & Destructor Documentation

GBuffer::GBuffer unsigned int  size,
unsigned int  growDelta = STANDARD_GROW_DELTA
 

Creates a new GBuffer with a given initial site.

Parameters:
size the initial size of the buffer.
growDelta the amount by which the buffer grows if more space is needed.

GBuffer::~GBuffer void   ) 
 

Destroys the buffer and the data in it.


Member Function Documentation

void GBuffer::add unsigned char *  data,
unsigned int  size
 

Adds data to the buffer at the end of the buffer.

Necessary space is allocated automatically.

Parameters:
data pointer to the data to be added
size the size of the data.

void GBuffer::added unsigned int  size  ) 
 

Moves the end of the buffer by size bytes.

Must and must only be used after data was added to the buffer using an external function like the C library's read(...) in conjunction with addPtr(...).

Parameters:
size the number of bytes the buffer end is to be moved.

unsigned char * GBuffer::addPtr unsigned int  size  ) 
 

Returns a pointer to the end of the buffer which can be used in library routines that require a buffer as a target (read(...)) for example, ensuring that enough space is left in the buffer.

Handle with care! It is up to the caller to make sure that no more data than size is added to the buffer!

After adding data to the buffer with an external function, added(...) must be called, otherwise the data just added is overwritten in the next call of add or addPtr!

Parameters:
size the maximum size of data to be copied to the buffer
Returns:
a pointer to the end of the buffer.

void GBuffer::ensureSize unsigned int  size  )  [private]
 

Makes sure that data of the size given can be inserted into the buffer without a buffer overflow.

There are two strategies to achieve this: If the distance of the current position (the read position) is greater than the new size needed, the contents in the buffer up to the current position are deleted and all subsequent data is moved to the beginning of the buffer.

Otherwise, a larger memory block is allocated.

Parameters:
size the size of the data to be inserted into the buffer.

unsigned int GBuffer::get unsigned char *  buffer,
unsigned int  size
 

Stores up to size bytes of data starting at the current position in a target buffer buffer.

The current position is increased by the amount of data read. Note that the data remains unchanged in the buffer.

Parameters:
buffer the target buffer, which must at least be of size size.
size the size of the target buffer.
Returns:
the number of bytes copied to the target buffer.

const unsigned char * GBuffer::getPtr void   )  const
 

Returns a pointer to the current position.

unsigned int GBuffer::getSize void   ) 
 

Returns the size of the data currently stored in the buffer.

Returns:
the size of the data.

unsigned char * GBuffer::readLine void   ) 
 

Returns a line of text, starting at the current position, ending at the next newline character, in a string.

If no newline character can be found up to the end of the buffer, NULL is returned. The current position is increased by the amount of data read.

Returns:
the line of text (terminated with a newline character
), or NULL if no newline character can be found up to the end of the buffer. Note: It is up to the caller to property free the string when it is not needed any more.

void GBuffer::shrink void   ) 
 

Reallocates the memory block occupied by the buffer such that the maximal size is equal to the current size, for more efficient memory usage.


Member Data Documentation

unsigned char* GBuffer::data [private]
 

The data.

unsigned int GBuffer::growDelta [private]
 

The amount of bytes the buffer grows when necessary.

unsigned int GBuffer::maxSize [private]
 

The maximal size of the buffer.

The maximal size is always kept greater or equal than the current size.

unsigned int GBuffer::pos [private]
 

The current read position.

unsigned int GBuffer::size [private]
 

The current size of the buffer.


Generated on Sun Apr 27 18:26:57 2003 for Grisu by doxygen1.3