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 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; }