common: remove buf_init(), add BUF_INIT and buf_move()

Add a BUF_INIT macro, which makes it easier to initialize a struct buf
to an empty string (without a heap allocation).

Add buf_move() to move the contents of one struct buf to another (the
source is reset to BUF_INIT, analogous to C++ move-assignment).

Use buf_reset() instead of directly calling `free(s->buf)` since the
internal buf may not always be allocated by malloc() now.
This commit is contained in:
John Lindgren 2024-04-14 14:20:57 -04:00 committed by Johan Malm
parent 343918dee0
commit 0573f16693
12 changed files with 122 additions and 90 deletions

View file

@ -87,10 +87,10 @@ find_dir(struct ctx *ctx)
{
char *debug = getenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME");
struct buf prefix = BUF_INIT;
for (int i = 0; ctx->dirs[i].path; i++) {
struct dir d = ctx->dirs[i];
struct buf prefix;
buf_init(&prefix);
buf_clear(&prefix);
/*
* Replace (rather than augment) $HOME/.config with
@ -100,7 +100,6 @@ find_dir(struct ctx *ctx)
char *pfxenv = getenv(d.prefix);
buf_add(&prefix, pfxenv ? pfxenv : d.default_prefix);
if (!prefix.len) {
free(prefix.buf);
continue;
}
@ -130,8 +129,8 @@ find_dir(struct ctx *ctx)
wl_list_append(ctx->list, &path->link);
}
g_strfreev(prefixes);
free(prefix.buf);
}
buf_reset(&prefix);
}
void