mirror of
https://github.com/labwc/labwc.git
synced 2026-03-06 01:40:15 -05:00
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:
parent
b0c2ac1a6d
commit
fc9cf5c931
2 changed files with 55 additions and 13 deletions
|
|
@ -45,4 +45,23 @@ void buf_init(struct buf *s);
|
||||||
*/
|
*/
|
||||||
void buf_add(struct buf *s, const char *data);
|
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 */
|
#endif /* LABWC_BUF_H */
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,10 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0-only
|
// SPDX-License-Identifier: GPL-2.0-only
|
||||||
|
#include <assert.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "common/buf.h"
|
#include "common/buf.h"
|
||||||
#include "common/mem.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
|
void
|
||||||
buf_expand_tilde(struct buf *s)
|
buf_expand_tilde(struct buf *s)
|
||||||
{
|
{
|
||||||
|
|
@ -24,7 +14,7 @@ buf_expand_tilde(struct buf *s)
|
||||||
if (s->buf[i] == '~') {
|
if (s->buf[i] == '~') {
|
||||||
buf_add(&new, getenv("HOME"));
|
buf_add(&new, getenv("HOME"));
|
||||||
} else {
|
} else {
|
||||||
buf_add_one_char(&new, s->buf[i]);
|
buf_add_char(&new, s->buf[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(s->buf);
|
free(s->buf);
|
||||||
|
|
@ -76,7 +66,7 @@ buf_expand_shell_variables(struct buf *s)
|
||||||
buf_add(&new, p);
|
buf_add(&new, p);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
buf_add_one_char(&new, s->buf[i]);
|
buf_add_char(&new, s->buf[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(environment_variable.buf);
|
free(environment_variable.buf);
|
||||||
|
|
@ -89,6 +79,8 @@ buf_expand_shell_variables(struct buf *s)
|
||||||
void
|
void
|
||||||
buf_init(struct buf *s)
|
buf_init(struct buf *s)
|
||||||
{
|
{
|
||||||
|
/* we can't assert(!s->buf) here because struct may be uninitialized */
|
||||||
|
|
||||||
s->alloc = 256;
|
s->alloc = 256;
|
||||||
s->buf = xmalloc(s->alloc);
|
s->buf = xmalloc(s->alloc);
|
||||||
s->buf[0] = '\0';
|
s->buf[0] = '\0';
|
||||||
|
|
@ -98,6 +90,8 @@ buf_init(struct buf *s)
|
||||||
void
|
void
|
||||||
buf_add(struct buf *s, const char *data)
|
buf_add(struct buf *s, const char *data)
|
||||||
{
|
{
|
||||||
|
assert(s->buf);
|
||||||
|
|
||||||
if (!data || data[0] == '\0') {
|
if (!data || data[0] == '\0') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -110,3 +104,32 @@ buf_add(struct buf *s, const char *data)
|
||||||
s->len += len;
|
s->len += len;
|
||||||
s->buf[s->len] = 0;
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue