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

@ -288,15 +288,15 @@ button_xbm_load(const char *button_name, struct lab_data_buffer **buffer,
/* Read file into memory as it's easier to tokenize that way */
char filename[4096] = { 0 };
button_filename(button_name, filename, sizeof(filename));
char *token_buffer = grab_file(filename);
if (token_buffer) {
struct token *tokens = tokenize_xbm(token_buffer);
free(token_buffer);
struct buf token_buf = grab_file(filename);
if (token_buf.len) {
struct token *tokens = tokenize_xbm(token_buf.buf);
pixmap = parse_xbm_tokens(tokens);
if (tokens) {
free(tokens);
}
}
buf_reset(&token_buf);
if (!pixmap.data) {
return;
}