karoo::stream_buffer Class Reference

This class implements an efficient buffer for reading data that comes in on a socket and for reading it (parsing it). More...

#include <util.h>

List of all members.

Classes

class  packet

Public Member Functions

 stream_buffer (char *buf, size_t buf_length)
 Construct a buffer, but take over ownership of the buffer passed.
size_t size () const
 Get the total number of bytes stored in the stream_buffer.
size_t write (const text &str)
 Write a text string to the buffer.
size_t write (const char *data, size_t sz)
 Write bytes to the buffer.
size_t read (char *data, size_t sz)
 Read from the buffer.
void putBack (char *data, size_t sz)
 Put back previously read bytes to the buffer.
ssize_t dump (int fh)
 Read from the buffer and write directly to a file handle.
size_t write_URL_encoded (const char *data, size_t sz)
 Write data as URL-encoded form.
size_t read_URL_encoded (char *data, size_t sz)
 Read from the buffer, assuming the buffer is storing URL-encoded data.
ssize_t find (char c, size_t from) const
 Find the offset into the buffer where the first instance of the given character is.
void read_URL_params (vector< pair< text, text > > &params)
ssize_t find (const text &boundary, size_t from) const
 Find the offset into the buffer where the first instance of the text string is is.
ssize_t skip_to_multipart_form_data (const text &boundary)
 This method skips to the beginning of the next multipart-form-data buffer and reports its size.
void write_to_ostream (ostream &out) const
 Write the entire buffer to an output stream.
void write_to_ostream (text &out) const
 Write the entire buffer to a text string object.


Detailed Description

This class implements an efficient buffer for reading data that comes in on a socket and for reading it (parsing it).

Rather than continually realloc a buffer and memcpy, this simply keeps a vector of the various buffers as they were read in.


Constructor & Destructor Documentation

karoo::stream_buffer::stream_buffer ( char *  buf,
size_t  buf_length 
)

Construct a buffer, but take over ownership of the buffer passed.

This means that the stream_buffer destructor will free up the memory (which it assumes was allocated using new).

Parameters:
buf the data which this stream_buffer will take ownership of
buf_length the length of the data. Note that the memory pointed to by buf will be delete[]ed by the stream_buffer object when it is destroyed, so the caller MUST NOT free up the memory, and the memory MUST NOT be on the stack.


Member Function Documentation

ssize_t karoo::stream_buffer::dump ( int  fh  ) 

Read from the buffer and write directly to a file handle.

Parameters:
fh the file handle to write to
Returns:
the number of bytes actually read.

ssize_t karoo::stream_buffer::find ( const text boundary,
size_t  from 
) const

Find the offset into the buffer where the first instance of the text string is is.

This is used for getting the offset of a multipart-form-data boundary.

Parameters:
boundary the string to look for
from the offset to start searching from.
Returns:
the offset of the first instance of the string, or -1 if not found.

ssize_t karoo::stream_buffer::find ( char  c,
size_t  from 
) const

Find the offset into the buffer where the first instance of the given character is.

Parameters:
c the character to look for
from the offset to start searching from.
Returns:
the offset of the first instance of the character, or -1 if not found.

void karoo::stream_buffer::putBack ( char *  data,
size_t  sz 
)

Put back previously read bytes to the buffer.

Parameters:
data the data buffer
sz the total number of bytes

size_t karoo::stream_buffer::read ( char *  data,
size_t  sz 
)

Read from the buffer.

Parameters:
data a data buffer where the characters can be received.
sz the total number of bytes that the data buffer can receive
Returns:
the number of bytes actually read.

size_t karoo::stream_buffer::read_URL_encoded ( char *  data,
size_t  sz 
)

Read from the buffer, assuming the buffer is storing URL-encoded data.

Parameters:
data a data buffer where the characters can be received.
sz the total number of bytes that the data buffer can receive
Returns:
the number of bytes actually received (after decoding)

size_t karoo::stream_buffer::size (  )  const

Get the total number of bytes stored in the stream_buffer.

Returns:
the size of the buffer.

ssize_t karoo::stream_buffer::skip_to_multipart_form_data ( const text boundary  ) 

This method skips to the beginning of the next multipart-form-data buffer and reports its size.

Parameters:
boundary the string used by the multipart-form-data as a boundary between buffers.
Returns:
the number of bytes in the buffer, or -1 if there was no buffer found.

size_t karoo::stream_buffer::write ( const char *  data,
size_t  sz 
)

Write bytes to the buffer.

Parameters:
data the bytes to be written
sz the number of bytes to be written
Returns:
the number of bytes actually written.

size_t karoo::stream_buffer::write ( const text str  ) 

Write a text string to the buffer.

Parameters:
str the text string to be appended to the buffer.
Returns:
the number of bytes actually written.

void karoo::stream_buffer::write_to_ostream ( text out  )  const

Write the entire buffer to a text string object.

Parameters:
out the text object that is being streamed to.

void karoo::stream_buffer::write_to_ostream ( ostream &  out  )  const

Write the entire buffer to an output stream.

Parameters:
out the output stream.

size_t karoo::stream_buffer::write_URL_encoded ( const char *  data,
size_t  sz 
)

Write data as URL-encoded form.

Parameters:
data the bytes to be written
sz the number of bytes to be written
Returns:
the number of bytes actually written.


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

Generated on Tue Feb 16 15:04:30 2010 for Karoo by  doxygen 1.5.8