From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Sat, 16 Mar 2024 03:39:45 +0000 (+0100) Subject: src/common/buf.c: enhance the buffer API X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=fc9cf5c9316c13640c4a2e97b26acd826cc93be2;p=proto%2Flabwc.git 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. --- diff --git a/include/common/buf.h b/include/common/buf.h index 64310c9a..1051229a 100644 --- a/include/common/buf.h +++ b/include/common/buf.h @@ -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 */ diff --git a/src/common/buf.c b/src/common/buf.c index 84ae957b..f6667418 100644 --- a/src/common/buf.c +++ b/src/common/buf.c @@ -1,20 +1,10 @@ // SPDX-License-Identifier: GPL-2.0-only +#include #include #include #include "common/buf.h" #include "common/mem.h" -static void -buf_add_one_char(struct buf *s, char ch) -{ - if (s->alloc <= s->len + 1) { - s->alloc = s->alloc * 3 / 2 + 16; - s->buf = xrealloc(s->buf, s->alloc); - } - s->buf[s->len++] = ch; - s->buf[s->len] = '\0'; -} - void buf_expand_tilde(struct buf *s) { @@ -24,7 +14,7 @@ buf_expand_tilde(struct buf *s) if (s->buf[i] == '~') { buf_add(&new, getenv("HOME")); } else { - buf_add_one_char(&new, s->buf[i]); + buf_add_char(&new, s->buf[i]); } } free(s->buf); @@ -76,7 +66,7 @@ buf_expand_shell_variables(struct buf *s) buf_add(&new, p); } } else { - buf_add_one_char(&new, s->buf[i]); + buf_add_char(&new, s->buf[i]); } } free(environment_variable.buf); @@ -89,6 +79,8 @@ buf_expand_shell_variables(struct buf *s) void buf_init(struct buf *s) { + /* we can't assert(!s->buf) here because struct may be uninitialized */ + s->alloc = 256; s->buf = xmalloc(s->alloc); s->buf[0] = '\0'; @@ -98,6 +90,8 @@ buf_init(struct buf *s) void buf_add(struct buf *s, const char *data) { + assert(s->buf); + if (!data || data[0] == '\0') { return; } @@ -110,3 +104,32 @@ buf_add(struct buf *s, const char *data) s->len += len; s->buf[s->len] = 0; } + +void +buf_add_char(struct buf *s, char ch) +{ + assert(s->buf); + + if (s->alloc <= s->len + 1) { + s->alloc = s->alloc * 3 / 2 + 16; + s->buf = xrealloc(s->buf, s->alloc); + } + s->buf[s->len++] = ch; + s->buf[s->len] = '\0'; +} + +void +buf_clear(struct buf *s) +{ + assert(s->buf); + + s->len = 0; + s->buf[0] = '\0'; +} + +void +buf_reset(struct buf *s) +{ + zfree(s->buf); + buf_init(s); +}