From 19f8496221d5d3f8101dce1813e6993ea6022d8a Mon Sep 17 00:00:00 2001 From: nick black Date: Mon, 9 Aug 2021 18:13:27 -0500 Subject: [PATCH] implement fbuf_flush() --- src/lib/fbuf.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib/fbuf.h b/src/lib/fbuf.h index 48a3dc1a5..3910b8789 100644 --- a/src/lib/fbuf.h +++ b/src/lib/fbuf.h @@ -243,6 +243,23 @@ fbuf_free(fbuf* f){ } } +// attempt to write the contents of |f| to the FILE |fp|, if there are any +// contents. reset the fbuf either way. if |flushfp| is set, fflush(fp). +static inline int +fbuf_flush(fbuf* f, FILE* fp, bool flushfp){ + int ret = 0; + if(f->used){ + if(fwrite(f->buf, f->used, 1, fp) != 1){ + ret = -1; + } + } + if(flushfp && ret == 0 && fflush(fp) == EOF){ + ret = -1; + } + fbuf_reset(f); + return ret; +} + // attempt to write the contents of |f| to the FILE |fp|, if there are any // contents, and free the fbuf either way. if |flushfp| is set, fflush(fp). static inline int