src/common/buf.c: enhance the buffer API

There is at least one user of the buffer API that reuse a single
buffer by just resetting `buf.len` to `0`. This works as long as
the new user of the buffer actually adds something to the buffer.

However, if we don't add anything but still provide `buf.buf` to
a consumer, the old content will be re-used.

This patch thus adds two new clearing variants to the buffer API:
- `buf_clear()` which doesn't reset the internal allocations
- `buf_reset()` which does free the internal allocations

Additionally, this patch makes `buffer_add_char()` public which
allows adding single characters to an existing buffer. This will
be used in a future PR which implements custom format strings for
the OSD.
This commit is contained in:
Consolatis 2024-03-16 04:39:45 +01:00 committed by Johan Malm
parent b0c2ac1a6d
commit fc9cf5c931
2 changed files with 55 additions and 13 deletions

View file

@ -45,4 +45,23 @@ void buf_init(struct buf *s);
*/
void buf_add(struct buf *s, const char *data);
/**
* buf_add_char - add single char to C string buffer
* @s: buffer
* @data: char to be added
*/
void buf_add_char(struct buf *s, char data);
/**
* buf_clear - clear the buffer, internal allocations are preserved
* @s: buffer
*/
void buf_clear(struct buf *s);
/**
* buf_reset - reset the buffer, internal allocations are free'd
* @s: buffer
*/
void buf_reset(struct buf *s);
#endif /* LABWC_BUF_H */