ucx
UAP Common Extensions
Data Structures | Macros | Functions
buffer.h File Reference

Advanced buffer implementation. More...

#include "ucx.h"
#include <sys/types.h>
#include <stdio.h>

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

UcxBufferucx_buffer_new (void *space, size_t capacity, int flags)
 Creates a new buffer. More...
 
void ucx_buffer_free (UcxBuffer *buffer)
 Destroys a buffer. More...
 
UcxBufferucx_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...
 

Detailed Description

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.

Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ ucx_buffer_clear

#define ucx_buffer_clear (   buffer)
Value:
memset((buffer)->space, 0, (buffer)->size); \
(buffer)->size = 0; (buffer)->pos = 0;

Clears the buffer by resetting the position and deleting the data.

The data is deleted by a zeroing it with call to memset().

Parameters
bufferthe buffer to be cleared

◆ ucx_buffer_clone

#define ucx_buffer_clone (   src,
  flags 
)    ucx_buffer_extract(src, 0, (src)->capacity, flags)

A shorthand macro for the full extraction of the buffer.

Parameters
srcthe source buffer
flagsfeature mask for the new buffer
Returns
a new buffer with the extracted content

◆ ucx_buffer_to_sstr

#define ucx_buffer_to_sstr (   buffer)    sstrn((buffer)->space, (buffer)->size)

Returns the complete buffer content as sstr_t.

Parameters
bufferthe buffer
Returns
the result of sstrn() with the buffer space and size as arguments

Function Documentation

◆ ucx_buffer_eof()

int ucx_buffer_eof ( UcxBuffer buffer)

Tests, if the buffer position has exceeded the buffer capacity.

Parameters
bufferthe buffer to test
Returns
non-zero, if the current buffer position has exceeded the last available byte of the buffer.

◆ ucx_buffer_extend()

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.

Parameters
bufferthe buffer to extend
additional_bytesthe number of additional bytes the buffer shall at least hold
Returns
0 on success or a non-zero value on failure

◆ ucx_buffer_extract()

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.

Note: the UCX_BUFFER_AUTOFREE feature is enforced for the new buffer.

Parameters
srcthe source buffer
startthe start position of extraction
lengththe count of bytes to extract (must not be zero)
flagsfeature mask for the new buffer
Returns
a new buffer containing the extraction

◆ ucx_buffer_free()

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.

Parameters
bufferthe buffer to destroy

◆ ucx_buffer_getc()

int ucx_buffer_getc ( UcxBuffer buffer)

Gets a character from a buffer.

The current position of the buffer is increased after a successful read.

Parameters
bufferthe buffer to read from
Returns
the character as int value or EOF, if the end of the buffer is reached

◆ ucx_buffer_new()

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.

Parameters
spacepointer to the memory area, or NULL to allocate new memory
capacitythe capacity of the buffer
flagsbuffer features (see UcxBuffer.flags)
Returns
the new buffer

◆ ucx_buffer_putc()

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.

Parameters
bufferthe buffer to write to
cthe character to write as int value
Returns
the byte that has bean written as int value or EOF when the end of the stream is reached and automatic extension is not enabled or not possible

◆ ucx_buffer_puts()

size_t ucx_buffer_puts ( UcxBuffer buffer,
const char *  str 
)

Writes a string to a buffer.

Parameters
bufferthe buffer
strthe string
Returns
the number of bytes written

◆ ucx_buffer_read()

size_t ucx_buffer_read ( void *  ptr,
size_t  size,
size_t  nitems,
UcxBuffer buffer 
)

Reads data from a UcxBuffer.

The position of the buffer is increased by the number of bytes read.

Parameters
ptra pointer to the memory area where to store the read data
sizethe length of one element
nitemsthe element count
bufferthe UcxBuffer to read from
Returns
the total number of elements read

◆ ucx_buffer_seek()

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

Parameters
buffer
offsetposition offset relative to whence
whenceone of SEEK_SET, SEEK_CUR or SEEK_END
Returns
0 on success, non-zero if the position is invalid

◆ ucx_buffer_shift()

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.

Parameters
bufferthe buffer
shiftthe shift offset (negative means left shift)
Returns
0 on success, non-zero if a required auto-extension fails

◆ ucx_buffer_shift_left()

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.

Parameters
bufferthe buffer
shiftthe shift offset
Returns
always zero
See also
ucx_buffer_shift()

◆ ucx_buffer_shift_right()

int ucx_buffer_shift_right ( UcxBuffer buffer,
size_t  shift 
)

Shifts the buffer to the right.

See ucx_buffer_shift() for details.

Parameters
bufferthe buffer
shiftthe shift offset
Returns
0 on success, non-zero if a required auto-extension fails
See also
ucx_buffer_shift()

◆ ucx_buffer_write()

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.

Parameters
ptra pointer to the memory area containing the bytes to be written
sizethe length of one element
nitemsthe element count
bufferthe UcxBuffer to write to
Returns
the total count of bytes written