From: Mark Wooding Date: Tue, 9 Jul 2024 17:09:03 +0000 (+0100) Subject: struct/buf.c: Add new function `buf_fill' to include arbitrary padding. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/commitdiff_plain/beee81ec8fc4857080e809c759575c4267f7bd1f?ds=sidebyside struct/buf.c: Add new function `buf_fill' to include arbitrary padding. --- diff --git a/struct/buf.3.in b/struct/buf.3.in index f69d204..594cbdd 100644 --- a/struct/buf.3.in +++ b/struct/buf.3.in @@ -68,12 +68,14 @@ .\" @buf_tryextend .\" @buf_get .\" @buf_put +.\" @buf_fill .\" @dbuf_break .\" @dbuf_flip .\" @dbuf_ensure .\" @dbuf_tryextend .\" @dbuf_get .\" @dbuf_put +.\" @dbuf_fill . .\" @buf_getbyte .\" @buf_putbyte @@ -536,6 +538,7 @@ and taking a first argument of type .PP .BI "void *buf_get(buf *" b ", size_t " sz ); .BI "int buf_put(buf *" b ", const void *" p ", size_t " sz ); +.BI "int buf_fill(buf *" b ", int " ch ", size_t " sz ); .PP .BI "int buf_getbyte(buf *" b ); .BI "int buf_putbyte(buf *" b ", int " ch ); @@ -827,6 +830,16 @@ function writes bytes of data starting at .I p to the buffer. If it succeeded, it returns 0; otherwise it returns \-1. +.PP +The +.B buf_fill +function writes +.I sz +copies of the byte +.I ch +to the buffer, as if by calling +.BR memset (3). +If it succeeds, it returns 0; otherwise it returns \-1. . .SS "Formatted buffer access" The function diff --git a/struct/buf.c b/struct/buf.c index 67cc30e..4b02b28 100644 --- a/struct/buf.c +++ b/struct/buf.c @@ -200,6 +200,29 @@ int buf_put(buf *b, const void *p, size_t sz) int (dbuf_put)(dbuf *db, const void *p, size_t sz) { return (dbuf_put(db, p, sz)); } +/* --- @{,d}buf_fill@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @int ch@ = fill character + * @size_t sz@ = size to fill + * + * Returns: Zero if it worked, nonzero if there wasn't enough space. + * + * Use: Write @sz@ bytes with value @ch@ to the buffer, as if with + * @memset@. + */ + +int buf_fill(buf *b, int ch, size_t sz) +{ + void *p; + + p = buf_get(b, sz); if (!p) return (-1); + if (sz) memset(p, ch, sz); + return (0); +} +int (dbuf_fill)(dbuf *db, int ch, size_t sz) + { return (dbuf_fill(db, ch, sz)); } + /* --- @{,d}buf_getbyte@ --- * * * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block diff --git a/struct/buf.h b/struct/buf.h index 2a37220..212bd72 100644 --- a/struct/buf.h +++ b/struct/buf.h @@ -273,6 +273,22 @@ extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/); extern int dbuf_put(dbuf */*db*/, const void */*p*/, size_t /*sz*/); #define dbuf_put(db, p, sz) (buf_put(DBUF_BUF(db), (p), (sz))) +/* --- @{,d}buf_fill@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @int ch@ = fill character + * @size_t sz@ = size to fill + * + * Returns: Zero if it worked, nonzero if there wasn't enough space. + * + * Use: Write @sz@ bytes with value @ch@ to the buffer, as if with + * @memset@. + */ + +extern int buf_fill(buf */*b*/, int /*ch*/, size_t /*sz*/); +extern int dbuf_fill(dbuf */*db*/, int /*ch*/, size_t /*sz*/); +#define dbuf_fill(db, ch, sz) (buf_fill(DBUF_BUF(db), (ch), (sz))) + /* --- @{,d}buf_getbyte@ --- * * * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block