From d4c8e626f280ab096c1341577931d8071972bd4c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 24 Mar 2016 19:09:00 +0200 Subject: [PATCH] py/stream: Add mp_stream_writeall() helper function. Spools entire output buffer to a blocking stream (chunk by chunk if needed). --- py/stream.c | 15 +++++++++++++++ py/stream.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/py/stream.c b/py/stream.c index 82bdffe2b2..abc973d9e5 100644 --- a/py/stream.c +++ b/py/stream.c @@ -208,6 +208,21 @@ void mp_stream_write_adaptor(void *self, const char *buf, size_t len) { mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len); } +// Works only with blocking streams +mp_uint_t mp_stream_writeall(mp_obj_t stream, const byte *buf, mp_uint_t size, int *errcode) { + mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream); + mp_uint_t sz = size; + while (sz > 0) { + mp_uint_t out_sz = s->type->stream_p->write(s, buf, size, errcode); + if (out_sz == MP_STREAM_ERROR) { + return MP_STREAM_ERROR; + } + buf += out_sz; + sz -= out_sz; + } + return size; +} + STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); diff --git a/py/stream.h b/py/stream.h index 0471777c76..2354fd3c0f 100644 --- a/py/stream.h +++ b/py/stream.h @@ -49,6 +49,9 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self); mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len); +// Helper function to write entire buf to *blocking* stream +mp_uint_t mp_stream_writeall(mp_obj_t stream, const byte *buf, mp_uint_t size, int *errcode); + #if MICROPY_STREAMS_NON_BLOCK // TODO: This is POSIX-specific (but then POSIX is the only real thing, // and anything else just emulates it, right?)