From e6b3732e90cd05c3adb9b0a85ec8c91f6d3abe92 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Mon, 15 Mar 2021 21:39:11 +0100 Subject: [PATCH] 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 --- lib/printfrr.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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