A transparent buffer implementation for C
C Makefile
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
Makefile
README.md
buf-test.c
buf.c
buf.h
package.json

README.md

buf.c

Lightweight buffer library

Implementation

buf.c lets you use regular pointers as expandable buffers. It uses an approach, inspired by sds, of storing metadata before the pointer value.

API

Declare a buffer by declaring a pointer to an item type. For example, a char * can be a buffer, or a struct foo *. Below, TYPE stands for the type that you declared for the buffer.

Initialize an empty buffer by setting it to NULL.

void buf_truncate(TYPE *);

Set the length of the buffer to zero, but don't free the buffer's memory.

void buf_free(TYPE **);

Release allocated memory for the buffer, and mark it as empty buffer (set to NULL).

size_t buf_len(TYPE *);

Get the length of the data in the buffer.

bool buf_alloc(TYPE **, size_t size);

Ensure that the buffer has enough space for some data. Return true for success, false for failure.

bool buf_put(TYPE **, const void *data, size_t size);

Put some data in the buffer. Return true on success, false on failure.

bool buf_append(TYPE **, const void *data, size_t size);

Write the given data at the end of the buffer, expanding the buffer if necessary. Return true on success, false on failure.

Similar projects

stb's stretchy_buffer does the same thing

License

MIT