]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/common/buf.c: enhance the buffer API
authorConsolatis <35009135+Consolatis@users.noreply.github.com>
Sat, 16 Mar 2024 03:39:45 +0000 (04:39 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sat, 16 Mar 2024 15:45:46 +0000 (15:45 +0000)
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.

include/common/buf.h
src/common/buf.c

index 64310c9a7594c42c91cc4f74f3982702e72939cc..1051229a0817277282a7407599c27f59f0919e4c 100644 (file)
@@ -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 */
index 84ae957bb358a558b1d4e13c85c1240e679bf8c8..f6667418f9cbccd3706b077bf92e17b0976785fc 100644 (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);
+}