static bool gu_out_begin_buf(GuOut* out, size_t req, GuExn* err) { GuOutStream* stream = out->stream; if (gu_out_is_buffering(out)) { if (out->buf_curr < 0) { return true; } else { gu_out_end_buf(out, err); if (!gu_ok(err)) { return false; } } } if (stream->begin_buf) { size_t sz = 0; uint8_t* buf = stream->begin_buf(stream, req, &sz, err); gu_assert(sz <= PTRDIFF_MAX); if (buf) { out->buf_end = &buf[sz]; out->buf_curr = -(ptrdiff_t) sz; out->buf_size = sz; return true; } } return false; }
GuOut gu_init_out(GuOutStream* stream) { gu_require(stream != NULL); GuOut out = { .buf_end = NULL, .buf_curr = 0, .stream = stream, .fini.fn = NULL }; return out; } static bool gu_out_is_buffering(GuOut* out) { return !!out->buf_end; } static void gu_out_end_buf(GuOut* out, GuExn* err) { if (!gu_out_is_buffering(out)) { return; } GuOutStream* stream = out->stream; size_t curr_len = ((ptrdiff_t)out->buf_size) + out->buf_curr; stream->end_buf(stream, curr_len, err); out->buf_end = NULL; out->buf_size = out->buf_curr = 0; } static bool gu_out_begin_buf(GuOut* out, size_t req, GuExn* err) { GuOutStream* stream = out->stream; if (gu_out_is_buffering(out)) { if (out->buf_curr < 0) { return true; } else { gu_out_end_buf(out, err); if (!gu_ok(err)) { return false; } } } if (stream->begin_buf) { size_t sz = 0; uint8_t* buf = stream->begin_buf(stream, req, &sz, err); gu_assert(sz <= PTRDIFF_MAX); if (buf) { out->buf_end = &buf[sz]; out->buf_curr = -(ptrdiff_t) sz; out->buf_size = sz; return true; } } return false; }
static size_t gu_out_output(GuOut* out, const uint8_t* src, size_t len, GuExn* err) { gu_out_end_buf(out, err); if (!gu_ok(err)) { return 0; } return out->stream->output(out->stream, src, len, err); }
static void gu_out_fini(GuFinalizer* self) { GuOut* out = gu_container(self, GuOut, fini); if (gu_out_is_buffering(out)) { GuPool* pool = gu_local_pool(); GuExn* err = gu_new_exn(pool); gu_out_end_buf(out, err); gu_pool_free(pool); } }
void gu_out_flush(GuOut* out, GuExn* err) { GuOutStream* stream = out->stream; if (out->buf_end) { gu_out_end_buf(out, err); if (!gu_ok(err)) { return; } } if (stream->flush) { stream->flush(stream, err); } }