Data Structures | Macros | Functions
buffer.h File Reference

Automatically growing buffer. More...

Data Structures

struct  vtm_buf
 

Macros

#define VTM_BUF_GET_AVAIL(BUF_PTR, NUM)
 
#define VTM_BUF_GET_AVAIL_TOTAL(BUF_PTR)   ((BUF_PTR)->used - (BUF_PTR)->read)
 
#define VTM_BUF_GETC(BUF_PTR)   ((BUF_PTR)->data[(BUF_PTR)->read++])
 
#define VTM_BUF_PUT_AVAIL(BUF_PTR, NUM)
 
#define VTM_BUF_PUT_AVAIL_TOTAL(BUF_PTR)   ((BUF_PTR)->len - (BUF_PTR)->used)
 
#define VTM_BUF_PUT_PTR(BUF_PTR)   ((BUF_PTR)->data + (BUF_PTR)->used)
 
#define VTM_BUF_PUT_INC(BUF_PTR, NUM)   ((BUF_PTR)->used += NUM)
 
#define VTM_BUF_PROCESS_ALL(BUF_PTR)   (BUF_PTR)->read = (BUF_PTR)->used
 

Functions

VTM_API struct vtm_bufvtm_buf_new (enum vtm_byteorder order)
 
VTM_API void vtm_buf_init (struct vtm_buf *buf, enum vtm_byteorder order)
 
VTM_API void vtm_buf_release (struct vtm_buf *buf)
 
VTM_API void vtm_buf_free (struct vtm_buf *buf)
 
VTM_API void vtm_buf_clear (struct vtm_buf *buf)
 
VTM_API int vtm_buf_ensure (struct vtm_buf *buf, size_t space)
 
VTM_API void vtm_buf_discard_processed (struct vtm_buf *buf)
 
VTM_API int vtm_buf_mark_processed (struct vtm_buf *buf, size_t num)
 
VTM_API int vtm_buf_geto (struct vtm_buf *buf, void *dst, size_t len, enum vtm_byteorder dst_order)
 
VTM_API int vtm_buf_getm (struct vtm_buf *buf, void *dst, size_t len)
 
VTM_API int vtm_buf_puto (struct vtm_buf *buf, const void *src, size_t len, enum vtm_byteorder src_order)
 
VTM_API int vtm_buf_putm (struct vtm_buf *buf, const void *src, size_t len)
 
VTM_API int vtm_buf_puts (struct vtm_buf *buf, const char *str)
 
VTM_API int vtm_buf_putc (struct vtm_buf *buf, unsigned char c)
 

Macro Definition Documentation

#define VTM_BUF_GET_AVAIL (   BUF_PTR,
  NUM 
)
Value:
((BUF_PTR)->read + NUM <= (BUF_PTR)->used && \
(BUF_PTR)->read + NUM >= (BUF_PTR)->read)

Checks if buffer has NUM unprocessed bytes available

#define VTM_BUF_GET_AVAIL_TOTAL (   BUF_PTR)    ((BUF_PTR)->used - (BUF_PTR)->read)

Gets the total number of unprocessed bytes

#define VTM_BUF_GETC (   BUF_PTR)    ((BUF_PTR)->data[(BUF_PTR)->read++])

Reads one character from the buffer

#define VTM_BUF_PUT_AVAIL (   BUF_PTR,
  NUM 
)
Value:
((BUF_PTR)->used + NUM <= (BUF_PTR)->len && \
(BUF_PTR)->used + NUM >= (BUF_PTR)->used)

Checks if buffer has space for additional NUM bytes

#define VTM_BUF_PUT_AVAIL_TOTAL (   BUF_PTR)    ((BUF_PTR)->len - (BUF_PTR)->used)

Space left in the buffer in bytes

#define VTM_BUF_PUT_PTR (   BUF_PTR)    ((BUF_PTR)->data + (BUF_PTR)->used)

Get pointer to begin of unused memory

#define VTM_BUF_PUT_INC (   BUF_PTR,
  NUM 
)    ((BUF_PTR)->used += NUM)

Increases the number of used bytes

#define VTM_BUF_PROCESS_ALL (   BUF_PTR)    (BUF_PTR)->read = (BUF_PTR)->used

Marks all bytes in the buffer as processed

Function Documentation

VTM_API struct vtm_buf* vtm_buf_new ( enum vtm_byteorder  order)

Creates a new buffer on the heap.

Parameters
orderthe byteorder of the buffer
Returns
pointer to created buffer
NULL if an error occured
VTM_API void vtm_buf_init ( struct vtm_buf buf,
enum vtm_byteorder  order 
)

Initializes given buffer.

Parameters
bufthe buffer that should be initialized
orderthe byteorder of the buffer
VTM_API void vtm_buf_release ( struct vtm_buf buf)

Releases the buffer and all allocated resources

Parameters
bufthe buffer that should be released
VTM_API void vtm_buf_free ( struct vtm_buf buf)

Releases the buffer and frees the pointer to it.

After this call the buffer pointer is no longer valid.

Parameters
bufthe buffer that should be freed
VTM_API void vtm_buf_clear ( struct vtm_buf buf)

Resets the buffer so that there are no more used or processed bytes.

Parameters
bufthe buffer that should be cleared
VTM_API int vtm_buf_ensure ( struct vtm_buf buf,
size_t  space 
)

Ensures that the buffer has enough free space to store the given number of bytes.

Parameters
bufthe buffer
spacethe number of bytes
Returns
VTM_OK if the buffer has enough free space
VTM_E_MALLOC or VTM_ERROR if an error occured
VTM_API void vtm_buf_discard_processed ( struct vtm_buf buf)

Removes already processed bytes from the buffer.

Parameters
bufthe buffer that should discard processed bytes
VTM_API int vtm_buf_mark_processed ( struct vtm_buf buf,
size_t  num 
)

Marks the given number of bytes as processed.

Parameters
bufthe buffer where the bytes should be marked
numthe number of bytes that should be marked
Returns
VTM_OK if the bytes were successfully marked
VTM_E_OVERFLOW if the number of processed bytes would overflow
VTM_API int vtm_buf_geto ( struct vtm_buf buf,
void *  dst,
size_t  len,
enum vtm_byteorder  dst_order 
)

Copies given amount of bytes from the buffer to destination.

If necessary the data is transformed to match the destination byte order.

Parameters
bufthe buffer where the data should be read from
dstthe target buffer where the data is copied to
lenthe number of bytes that should be copied
dst_orderthe output byteorder
Returns
VTM_OK if the bytes were read successfully
VTM_ERROR if any error has already occured in a previous call and the buffer was not reset
VTM_API int vtm_buf_getm ( struct vtm_buf buf,
void *  dst,
size_t  len 
)

Copies given amount of bytes from the buffer to destination.

Parameters
bufthe buffer where the data should be read from
dstthe target buffer where the data is copied to
lenthe number of bytes that should be copied
Returns
VTM_OK if the bytes were read successfully
VTM_ERROR if any error has already occured in a previous call and the buffer was not reset
VTM_API int vtm_buf_puto ( struct vtm_buf buf,
const void *  src,
size_t  len,
enum vtm_byteorder  src_order 
)

Stores given amount of bytes in the buffer.

If necessary the data is transformed to match the byte order of the buffer.

Parameters
bufthe buffer where the data should be stored
srcthe source where the data is copied from
lenthe number of bytes that should be copied
src_orderthe input byteorder
Returns
VTM_OK if the bytes were stored successfully
VTM_ERROR if any error has already occured in a previous call and the buffer was not reset
VTM_API int vtm_buf_putm ( struct vtm_buf buf,
const void *  src,
size_t  len 
)

Stores given amount of bytes in the buffer.

Parameters
bufthe buffer where the data should be stored
srcthe source where the data is copied from
lenthe number of bytes that should be copied
Returns
VTM_OK if the bytes were stored successfully
VTM_ERROR if any error has already occured in a previous call and the buffer was not reset
VTM_API int vtm_buf_puts ( struct vtm_buf buf,
const char *  str 
)

Stores given string in the buffer.

Parameters
bufthe buffer where the string should be stored
strthe source string that is copied to the buffer
Returns
VTM_OK if the string was stored successfully
VTM_ERROR if any error has already occured in a previous call and the buffer was not reset
VTM_API int vtm_buf_putc ( struct vtm_buf buf,
unsigned char  c 
)

Stores given character in the buffer.

Parameters
bufthe buffer where the character should be stored
cthe character that should be stored
Returns
VTM_OK if the bytes were stored successfully
VTM_ERROR if any error has already occured in a previous call and the buffer was not reset