libdas2
das2 core C utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions
DasBuf Struct Reference

#include <das2/buffer.h>

Detailed Description

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

DasBufnew_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...
 

Member Function Documentation

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.

Parameters
uLenThe length of the raw buffer to allocate
Returns
a new DasBuf allocated on the heap.
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.

Parameters
pThisthe buffer initialize
sBufan pre-allocated character buffer to receive new data
uLenthe 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.

Parameters
pThisThe 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.

Parameters
pThisthe buffer to receive the bytes
sStrthe null-terminated string to write
Returns
0 if the operation succeeded, a positive error code otherwise.
DasErrCode DasBuf_printf ( DasBuf pThis,
const char *  sFmt,
  ... 
)

Write formatted strings to the buffer.

Parameters
pThisthe buffer to receive the bytes
sFmtan sprintf style format string
Returns
0 if the operation succeeded, a positive error code otherwise.
DasErrCode DasBuf_write ( DasBuf pThis,
const void *  pData,
size_t  uLen 
)

Add generic data to the buffer.

Parameters
pThisthe buffer to receive the bytes
pDataa pointer to the bytes to write
uLenthe number of bytes to write
Returns
0 if the operation succeeded, a positive error code otherwise.
size_t DasBuf_written ( const DasBuf pThis)

Get the size of the data in the buffer.

Returns
the number of bytes written to 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.

Returns
Read length remaining.
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.

Warning
If any new bytes are added after the buffer has been stripped then the right read point will be reset to the end of valid data.
Returns
The number of bytes left to read after moving the read boundaries. The return value is the same as what you would get by calling DasBuf_remaining() immediately after this function.
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.

Returns
The number of bytes copied out of the buffer.

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