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

@ -1,20 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#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);
}