implement fbuf_flush()

This commit is contained in:
nick black 2021-08-09 18:13:27 -05:00
parent c223367073
commit 19f8496221

View File

@ -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 // 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). // contents, and free the fbuf either way. if |flushfp| is set, fflush(fp).
static inline int static inline int