ucx
UAP Common Extensions
|
Advanced buffer implementation. More...
Go to the source code of this file.
Data Structures | |
struct | UcxBuffer |
UCX Buffer. More... | |
Macros | |
#define | UCX_BUFFER_DEFAULT 0x00 |
No buffer features enabled (all flags cleared). | |
#define | UCX_BUFFER_AUTOFREE 0x01 |
If this flag is enabled, the buffer will automatically free its contents. | |
#define | UCX_BUFFER_AUTOEXTEND 0x02 |
If this flag is enabled, the buffer will automatically extends its capacity. | |
#define | ucx_buffer_clone(src, flags) ucx_buffer_extract(src, 0, (src)->capacity, flags) |
A shorthand macro for the full extraction of the buffer. More... | |
#define | ucx_buffer_clear(buffer) |
Clears the buffer by resetting the position and deleting the data. More... | |
#define | ucx_buffer_to_sstr(buffer) sstrn((buffer)->space, (buffer)->size) |
Returns the complete buffer content as sstr_t. More... | |
Functions | |
UcxBuffer * | ucx_buffer_new (void *space, size_t capacity, int flags) |
Creates a new buffer. More... | |
void | ucx_buffer_free (UcxBuffer *buffer) |
Destroys a buffer. More... | |
UcxBuffer * | ucx_buffer_extract (UcxBuffer *src, size_t start, size_t length, int flags) |
Creates a new buffer and fills it with extracted content from another buffer. More... | |
int | ucx_buffer_shift (UcxBuffer *buffer, off_t shift) |
Shifts the contents of the buffer by the given offset. More... | |
int | ucx_buffer_shift_right (UcxBuffer *buffer, size_t shift) |
Shifts the buffer to the right. More... | |
int | ucx_buffer_shift_left (UcxBuffer *buffer, size_t shift) |
Shifts the buffer to the left. More... | |
int | ucx_buffer_seek (UcxBuffer *buffer, off_t offset, int whence) |
Moves the position of the buffer. More... | |
int | ucx_buffer_eof (UcxBuffer *buffer) |
Tests, if the buffer position has exceeded the buffer capacity. More... | |
int | ucx_buffer_extend (UcxBuffer *buffer, size_t additional_bytes) |
Extends the capacity of the buffer. More... | |
size_t | ucx_buffer_write (const void *ptr, size_t size, size_t nitems, UcxBuffer *buffer) |
Writes data to a UcxBuffer. More... | |
size_t | ucx_buffer_read (void *ptr, size_t size, size_t nitems, UcxBuffer *buffer) |
Reads data from a UcxBuffer. More... | |
int | ucx_buffer_putc (UcxBuffer *buffer, int c) |
Writes a character to a buffer. More... | |
int | ucx_buffer_getc (UcxBuffer *buffer) |
Gets a character from a buffer. More... | |
size_t | ucx_buffer_puts (UcxBuffer *buffer, const char *str) |
Writes a string to a buffer. More... | |
Advanced buffer implementation.
Instances of UcxBuffer can be used to read from or to write to like one would do with a stream. This allows the use of ucx_stream_copy() to copy contents from one buffer to another.
Some features for convenient use of the buffer can be enabled. See the documentation of the macro constants for more information.
#define ucx_buffer_clear | ( | buffer | ) |
Clears the buffer by resetting the position and deleting the data.
The data is deleted by a zeroing it with call to memset()
.
buffer | the buffer to be cleared |
#define ucx_buffer_clone | ( | src, | |
flags | |||
) | ucx_buffer_extract(src, 0, (src)->capacity, flags) |
A shorthand macro for the full extraction of the buffer.
src | the source buffer |
flags | feature mask for the new buffer |
#define ucx_buffer_to_sstr | ( | buffer | ) | sstrn((buffer)->space, (buffer)->size) |
int ucx_buffer_eof | ( | UcxBuffer * | buffer | ) |
Tests, if the buffer position has exceeded the buffer capacity.
buffer | the buffer to test |
int ucx_buffer_extend | ( | UcxBuffer * | buffer, |
size_t | additional_bytes | ||
) |
Extends the capacity of the buffer.
Note: The buffer capacity increased by a power of two. I.e. the buffer capacity is doubled, as long as it would not hold the current content plus the additional required bytes.
Attention: the argument provided is the number of additional bytes the buffer shall hold. It is NOT the total number of bytes the buffer shall hold.
buffer | the buffer to extend |
additional_bytes | the number of additional bytes the buffer shall at least hold |
Creates a new buffer and fills it with extracted content from another buffer.
Note: the UCX_BUFFER_AUTOFREE feature is enforced for the new buffer.
src | the source buffer |
start | the start position of extraction |
length | the count of bytes to extract (must not be zero) |
flags | feature mask for the new buffer |
void ucx_buffer_free | ( | UcxBuffer * | buffer | ) |
Destroys a buffer.
If the UCX_BUFFER_AUTOFREE feature is enabled, the contents of the buffer are also freed.
buffer | the buffer to destroy |
int ucx_buffer_getc | ( | UcxBuffer * | buffer | ) |
Gets a character from a buffer.
The current position of the buffer is increased after a successful read.
buffer | the buffer to read from |
int
value or EOF
, if the end of the buffer is reached UcxBuffer* ucx_buffer_new | ( | void * | space, |
size_t | capacity, | ||
int | flags | ||
) |
Creates a new buffer.
Note: you may provide NULL
as argument for space
. Then this function will allocate the space and enforce the UCX_BUFFER_AUTOFREE flag.
space | pointer to the memory area, or NULL to allocate new memory |
capacity | the capacity of the buffer |
flags | buffer features (see UcxBuffer.flags) |
int ucx_buffer_putc | ( | UcxBuffer * | buffer, |
int | c | ||
) |
Writes a character to a buffer.
The least significant byte of the argument is written to the buffer. If the end of the buffer is reached and UCX_BUFFER_AUTOEXTEND feature is enabled, the buffer capacity is extended by ucx_buffer_extend(). If the feature is disabled or buffer extension fails, EOF
is returned.
On successful write the position of the buffer is increased.
buffer | the buffer to write to |
c | the character to write as int value |
int
value or EOF
when the end of the stream is reached and automatic extension is not enabled or not possible size_t ucx_buffer_puts | ( | UcxBuffer * | buffer, |
const char * | str | ||
) |
Writes a string to a buffer.
buffer | the buffer |
str | the string |
size_t ucx_buffer_read | ( | void * | ptr, |
size_t | size, | ||
size_t | nitems, | ||
UcxBuffer * | buffer | ||
) |
int ucx_buffer_seek | ( | UcxBuffer * | buffer, |
off_t | offset, | ||
int | whence | ||
) |
Moves the position of the buffer.
The new position is relative to the whence
argument.
SEEK_SET marks the start of the buffer. SEEK_CUR marks the current position. SEEK_END marks the end of the buffer.
With an offset of zero, this function sets the buffer position to zero (SEEK_SET), the buffer size (SEEK_END) or leaves the buffer position unchanged (SEEK_CUR).
buffer | |
offset | position offset relative to whence |
whence | one of SEEK_SET, SEEK_CUR or SEEK_END |
int ucx_buffer_shift | ( | UcxBuffer * | buffer, |
off_t | shift | ||
) |
Shifts the contents of the buffer by the given offset.
If the offset is positive, the contents are shifted to the right. If auto extension is enabled, the buffer grows, if necessary. In case the auto extension fails, this function returns a non-zero value and no contents are changed. If auto extension is disabled, the contents that do not fit into the buffer are discarded.
If the offset is negative, the contents are shifted to the left where the first shift
bytes are discarded. The new size of the buffer is the old size minus the absolute shift value. If this value is larger than the buffer size, the buffer is emptied (but not cleared, see the security note below).
The buffer position gets shifted alongside with the content but is kept within the boundaries of the buffer.
Security note: the shifting operation does not erase the previously occupied memory cells. You can easily do that manually, e.g. by calling memset(buffer->space, 0, shift)
for a right shift or memset(buffer->size, 0, buffer->capacity-buffer->size)
for a left shift.
buffer | the buffer |
shift | the shift offset (negative means left shift) |
int ucx_buffer_shift_left | ( | UcxBuffer * | buffer, |
size_t | shift | ||
) |
Shifts the buffer to the left.
See ucx_buffer_shift() for details. Note, however, that this method expects a positive shift offset.
Since a left shift cannot fail due to memory allocation problems, this function always returns zero.
buffer | the buffer |
shift | the shift offset |
int ucx_buffer_shift_right | ( | UcxBuffer * | buffer, |
size_t | shift | ||
) |
Shifts the buffer to the right.
See ucx_buffer_shift() for details.
buffer | the buffer |
shift | the shift offset |
size_t ucx_buffer_write | ( | const void * | ptr, |
size_t | size, | ||
size_t | nitems, | ||
UcxBuffer * | buffer | ||
) |
Writes data to a UcxBuffer.
The position of the buffer is increased by the number of bytes written.
ptr | a pointer to the memory area containing the bytes to be written |
size | the length of one element |
nitems | the element count |
buffer | the UcxBuffer to write to |