X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/13ee740655965453b2f3fcd7093d2d8b13839903..d93e9a33ee30faed3945b2a7acfc46b84197d432:/struct/buf.h diff --git a/struct/buf.h b/struct/buf.h index 33dc6ff..8201c49 100644 --- a/struct/buf.h +++ b/struct/buf.h @@ -273,6 +273,58 @@ 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_alignskip@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @size_t m, a@ = alignment multiple and offset + * + * Returns: Zero if it worked, nonzero if there wasn't enough space. + * + * Use: Advance the buffer position as little as possible such that + * it is @a@ greater than a multiple of @m@. This doesn't write + * anything to the buffer, so it's probably not suitable for + * output: use @buf_alignfill@ instead. + */ + +extern int buf_alignskip(buf */*b*/, size_t /*m*/, size_t /*a*/); +extern int dbuf_alignskip(dbuf */*db*/, size_t /*m*/, size_t /*a*/); +#define dbuf_alignskip(db, m, a) (buf_alignskip(DBUF_BUF(db), (m), (a))) + +/* --- @{,d}buf_alignfill@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @int ch@ = fill character + * @size_t m, a@ = alignment multiple and offset + * + * Returns: Zero if it worked, nonzero if there wasn't enough space. + * + * Use: Fill the buffer with as few copies of @ch@ as possible, as if + * by @memset@, to advance the buffer position to a value @a@ + * greater than a multiple of @m@. + */ + +extern int buf_alignfill(buf */*b*/, int /*ch*/, size_t /*m*/, size_t /*a*/); +extern int (dbuf_alignfill)(dbuf */*db*/, int /*ch*/, + size_t /*m*/, size_t /*a*/); +#define dbuf_alignfill(db, ch, m, a) \ + (buf_alignfill(DBUF_BUF(db), (ch), (a), (m))) + /* --- @{,d}buf_getbyte@ --- * * * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block @@ -400,6 +452,60 @@ extern int dbuf_putk64b(dbuf */*db*/, kludge64 /*w*/); #define dbuf_putk64l(db, w) (buf_putk64l(DBUF_BUF(db), (w))) #define dbuf_putk64b(db, w) (buf_putk64b(DBUF_BUF(db), (w))) +/* --- @buf_getf{32,64}{,l,b} --- * + * + * Arguments: @buf *b@ = a buffer to read from + * @float *x_out@, @double *x_out@ = where to put the result + * + * Returns: Zero on success, %$-1$% on failure (and the buffer is + * broken). + * + * Use: Get an IEEE Binary32 or Binary64 value from the buffer. + * Conversion is performed using the `fltfmt' machinery, with + * the usual round-to-nearest/ties-to-even rounding mode. + */ + +extern int buf_getf32(buf */*b*/, float */*x_out*/); +extern int buf_getf32l(buf */*b*/, float */*x_out*/); +extern int buf_getf32b(buf */*b*/, float */*x_out*/); +#define dbuf_getf32(db, x_out) (buf_getf32(DBUF_BUF(db), (x_out))) +#define dbuf_getf32l(db, x_out) (buf_getf32l(DBUF_BUF(db), (x_out))) +#define dbuf_getf32b(db, x_out) (buf_getf32b(DBUF_BUF(db), (x_out))) + +extern int buf_getf64(buf */*b*/, double */*x_out*/); +extern int buf_getf64l(buf */*b*/, double */*x_out*/); +extern int buf_getf64b(buf */*b*/, double */*x_out*/); +#define dbuf_getf64(db, x_out) (buf_getf64(DBUF_BUF(db), (x_out))) +#define dbuf_getf64l(db, x_out) (buf_getf64l(DBUF_BUF(db), (x_out))) +#define dbuf_getf64b(db, x_out) (buf_getf64b(DBUF_BUF(db), (x_out))) + +/* --- @buf_putf{32,64}{,l,b} --- * + * + * Arguments: @buf *b@ = a buffer to write to + * @double x@ = a number to write + * + * Returns: Zero on success, %$-1$% on failure (and the buffer is + * broken). + * + * Use: Get an IEEE Binary32 or Binary64 value from the buffer. + * Conversion is performed using the `fltfmt' machinery, with + * the usual round-to-nearest/ties-to-even rounding mode. + */ + +extern int buf_putf32(buf */*b*/, float /*x*/); +extern int buf_putf32l(buf */*b*/, float /*x*/); +extern int buf_putf32b(buf */*b*/, float /*x*/); +#define dbuf_putf32(db, x) (buf_putf32(DBUF_BUF(db), (x))) +#define dbuf_putf32l(db, x) (buf_putf32l(DBUF_BUF(db), (x))) +#define dbuf_putf32b(db, x) (buf_putf32b(DBUF_BUF(db), (x))) + +extern int buf_putf64(buf */*b*/, double /*x*/); +extern int buf_putf64l(buf */*b*/, double /*x*/); +extern int buf_putf64b(buf */*b*/, double /*x*/); +#define dbuf_putf64(db, x) (buf_putf64(DBUF_BUF(db), (x))) +#define dbuf_putf64l(db, x) (buf_putf64l(DBUF_BUF(db), (x))) +#define dbuf_putf64b(db, x) (buf_putf64b(DBUF_BUF(db), (x))) + /* --- @{,d}buf_getmem{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block @@ -610,63 +716,6 @@ BUF_DOSUFFIXES(BUF_DECL_PUTSTR_) #define dbuf_putstr64b(db, p) (buf_putstr64b(DBUF_BUF(db), (p))) #define dbuf_putstrz(db, p) (buf_putstrz(DBUF_BUF(db), (p))) -/* --- @{,d}buf_getf64{,l,b} --- * - * - * Arguments: @buf *b@ = pointer to a buffer block - * @double *x_out@ = where to put the result - * - * Returns: Zero on success, @-1@ on failure (and the buffer is broken). - * - * If the system supports NaNs, then any encoded NaN is returned - * as the value of @NAN@ in @@; otherwise, this function - * reports failure. - * - * In general, values are rounded to the nearest available - * value, in the way that the system usually rounds. If the - * system doesn't support infinities, then any encoded infinity - * is reported as the largest-possible-magnitude finite value - * instead. - */ - -extern int buf_getf64(buf */*b*/, double */*x_out*/); -extern int buf_getf64l(buf */*b*/, double */*x_out*/); -extern int buf_getf64b(buf */*b*/, double */*x_out*/); -extern int dbuf_getf64(dbuf */*db*/, double */*x_out*/); -extern int dbuf_getf64l(dbuf */*db*/, double */*x_out*/); -extern int dbuf_getf64b(dbuf */*db*/, double */*x_out*/); -#define dbuf_getf64(db, x_out) (buf_getf64(DBUF_BUF(db), (x_out))) -#define dbuf_getf64l(db, x_out) (buf_getf64l(DBUF_BUF(db), (x_out))) -#define dbuf_getf64b(db, x_out) (buf_getf64b(DBUF_BUF(db), (x_out))) - -/* --- @{,d}buf_putf64{,l,b} --- * - * - * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block - * @double x@ = a number to write - * - * Returns: Zero on success, @-1@ on failure (and the buffer is broken). - * - * On C89, this function can't detect negative zero so these - * will be silently written as positive zero. - * - * This function doesn't distinguish NaNs. Any NaN is written - * as a quiet NaN with all payload bits zero. - * - * A finite value with too large a magnitude to be represented - * is rounded to the appropriate infinity. Other finite values - * are rounded as necessary, in the usual IEEE 754 round-to- - * nearest-or-even way. - */ - -extern int buf_putf64(buf */*b*/, double /*x*/); -extern int buf_putf64l(buf */*b*/, double /*x*/); -extern int buf_putf64b(buf */*b*/, double /*x*/); -extern int dbuf_putf64(dbuf */*db*/, double /*x*/); -extern int dbuf_putf64l(dbuf */*db*/, double /*x*/); -extern int dbuf_putf64b(dbuf */*db*/, double /*x*/); -#define dbuf_putf64(db, x) (buf_putf64(DBUF_BUF(db), (x))) -#define dbuf_putf64l(db, x) (buf_putf64l(DBUF_BUF(db), (x))) -#define dbuf_putf64b(db, x) (buf_putf64b(DBUF_BUF(db), (x))) - /* --- @{,D}BUF_ENCLOSETAG@ --- * * * Arguments: @tag@ = a control-structure macro tag