hl7parse
Classes | Typedefs | Functions
buffer.h File Reference

generic growing buffer library More...

#include <stdlib.h>
#include <string.h>
Include dependency graph for buffer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  buf
 dynamically growing generic bytearray More...
 

Typedefs

typedef struct buf buf_t
 dynamically growing generic bytearray
 

Functions

buf_tnew_buf_t ()
 allocate initial memory
 
void free_buf (buf_t *buffer)
 release all memory of buffer More...
 
int append_buf_str (buf_t *buffer, int size, unsigned char *data)
 add data to buffer More...
 
int append_bufc (buf_t *buffer, unsigned char c)
 append a character More...
 

Detailed Description

generic growing buffer library

Function Documentation

◆ append_buf_str()

int append_buf_str ( buf_t buffer,
int  size,
unsigned char *  data 
)

add data to buffer

we excpect buffer always to be at least 1 byte long and terminated by \0.

The output buffer will always be \0 terminated.

to prevent constant reallocs() on small chunks, the allocated size buf_t.allocated is doubled when more memory is required.

If a large chunk is added that is larger than buf_t.allocated * 2 then the current size + the size of the new chunk is calculated and reallocated.

Parameters
bufferthe buffer to work on
sizesize of the new chunk
databyte array to papend
Returns
0 on success, error code otherwise
Bug:
questionable interface design

input trats data as byte array and requires an absolute length. internally we set a '\0' char to the last byte, potentially loosing data.

on the other hand append_bufc() doesn't care, how should it.

◆ append_bufc()

int append_bufc ( buf_t buffer,
unsigned char  c 
)

append a character

slightly faster than append_buf_str(...) which takes a C character as input.

Parameters
bufferthe buffer to work on
cthe character to add
Returns
0 on success, error code otherwise

◆ free_buf()

void free_buf ( buf_t buffer)

release all memory of buffer

Parameters
buffer