summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2021-03-15 21:39:11 +0100
committerDavid Lamparter <equinox@diac24.net>2021-03-22 12:50:27 +0100
commite6b3732e90cd05c3adb9b0a85ec8c91f6d3abe92 (patch)
tree78736a4852ef91bf88563a9e2396bbd74bdc1a02
parent992c42ef011f56551686b7336bedf5c5e6100f2c (diff)
lib: add bputs/bputch fbuf (bprintfrr) helpers
Just small utilities for when you already have a struct fbuf (i.e. when bprintfrr() is used to construct longer text from multiple pieces.) Signed-off-by: David Lamparter <equinox@diac24.net>
-rw-r--r--lib/printfrr.h24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/printfrr.h b/lib/printfrr.h
index a775e1517b..418e839d97 100644
--- a/lib/printfrr.h
+++ b/lib/printfrr.h
@@ -160,6 +160,30 @@ void printfrr_ext_reg(const struct printfrr_ext *);
} \
/* end */
+/* fbuf helper functions */
+
+static inline ssize_t bputs(struct fbuf *buf, const char *str)
+{
+ size_t len = strlen(str);
+ size_t ncopy;
+
+ if (!buf)
+ return len;
+
+ ncopy = MIN(len, (size_t)(buf->buf + buf->len - buf->pos));
+ memcpy(buf->pos, str, ncopy);
+ buf->pos += ncopy;
+
+ return len;
+}
+
+static inline ssize_t bputch(struct fbuf *buf, char ch)
+{
+ if (buf && buf->pos < buf->buf + buf->len)
+ *buf->pos++ = ch;
+ return 1;
+}
+
#ifdef __cplusplus
}
#endif