libdas2
das2 core C utilities
|
#include <das2/buffer.h>
Little buffer class to handle accumulating string data.
DasBuf objects maintain a data buffer with a current write point, a current read point and an end read point. As data are written to the buffer the write point is incremented as well as the end-of-read point. This structure is handy when multiple functions need to contribute encoded data to a single memory buffer, or when multiple functions need to read from a buffer without memory re-allocations or placing null values to stop parsing.
It is hoped that the use of this class cuts down on alot of data copies and sub-string allocations.
Public Member Functions | |
DasBuf * | new_DasBuf (size_t uLen) |
Create a new Read-Write buffer on the heap Allocates a new char buffer of the indicated size, call del_DasBuffer() when finished. More... | |
DasErrCode | DasBuf_initReadWrite (DasBuf *pThis, char *sBuf, size_t uLen) |
Initialize a read-write buffer that points to an external byte array. More... | |
void | DasBuf_reinit (DasBuf *pThis) |
Re-initialize a buffer including read and write points This version can be a little quicker than init_DasBuffer() because it only zero's out the bytes that were written, not the entire buffer. | |
void | del_DasBuf (DasBuf *pThis) |
Free a buffer object along with it's backing store. More... | |
DasErrCode | DasBuf_puts (DasBuf *pThis, const char *sStr) |
Add a string to the buffer. More... | |
DasErrCode | DasBuf_printf (DasBuf *pThis, const char *sFmt,...) |
Write formatted strings to the buffer. More... | |
DasErrCode | DasBuf_write (DasBuf *pThis, const void *pData, size_t uLen) |
Add generic data to the buffer. More... | |
size_t | DasBuf_written (const DasBuf *pThis) |
Get the size of the data in the buffer. More... | |
size_t | DasBuf_unread (const DasBuf *pThis) |
Get the number of bytes remaining from the read begin point to the read end point. More... | |
size_t | DasBuf_strip (DasBuf *pThis) |
Adjust read points so that the data starts and ends on non-space values. More... | |
size_t | DasBuf_read (DasBuf *pThis, char *pOut, size_t uOut) |
Read bytes from a buffer Copies bytes out of a buffer and increments the read point. More... | |
DasBuf * new_DasBuf | ( | size_t | uLen | ) |
Create a new Read-Write buffer on the heap Allocates a new char buffer of the indicated size, call del_DasBuffer() when finished.
uLen | The length of the raw buffer to allocate |
DasErrCode DasBuf_initReadWrite | ( | DasBuf * | pThis, |
char * | sBuf, | ||
size_t | uLen | ||
) |
Initialize a read-write buffer that points to an external byte array.
The write point is reset to the beginning and function zero's all data. The read point is also set to the beginning.
pThis | the buffer initialize |
sBuf | an pre-allocated character buffer to receive new data |
uLen | the length of the pre-allocated buffer |
void del_DasBuf | ( | DasBuf * | pThis | ) |
Free a buffer object along with it's backing store.
Don't use this if the DasBuf_initReadOnly or DasBuf_initReadWrite were given pointers to buffers allocated on the stack. If so, your program will crash.
pThis | The buffer to free. It's good practice to set this pointer to NULL after this function is called |
DasErrCode DasBuf_puts | ( | DasBuf * | pThis, |
const char * | sStr | ||
) |
Add a string to the buffer.
pThis | the buffer to receive the bytes |
sStr | the null-terminated string to write |
DasErrCode DasBuf_printf | ( | DasBuf * | pThis, |
const char * | sFmt, | ||
... | |||
) |
Write formatted strings to the buffer.
pThis | the buffer to receive the bytes |
sFmt | an sprintf style format string |
DasErrCode DasBuf_write | ( | DasBuf * | pThis, |
const void * | pData, | ||
size_t | uLen | ||
) |
Add generic data to the buffer.
pThis | the buffer to receive the bytes |
pData | a pointer to the bytes to write |
uLen | the number of bytes to write |
size_t DasBuf_written | ( | const DasBuf * | pThis | ) |
Get the size of the data in the buffer.
size_t DasBuf_unread | ( | const DasBuf * | pThis | ) |
Get the number of bytes remaining from the read begin point to the read end point.
Normally this returns the difference between the read point and the write point but some operations such as DasBuf_strip() reduce the read end point below the write point.
size_t DasBuf_strip | ( | DasBuf * | pThis | ) |
Adjust read points so that the data starts and ends on non-space values.
This is handy if the buffer contains string data.
size_t DasBuf_read | ( | DasBuf * | pThis, |
char * | pOut, | ||
size_t | uOut | ||
) |
Read bytes from a buffer Copies bytes out of a buffer and increments the read point.
As soon as the read point hits the end of valid data no more bytes are copied.