diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-03-24 19:09:00 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-03-24 19:09:00 +0200 |
commit | d4c8e626f280ab096c1341577931d8071972bd4c (patch) | |
tree | 03f9b713f06fa06c42491567e1c58d075117b1f7 /py/stream.c | |
parent | a5d48b1162fc57cb45c591702ed60cffc3f0588b (diff) |
py/stream: Add mp_stream_writeall() helper function.
Spools entire output buffer to a blocking stream (chunk by chunk if
needed).
Diffstat (limited to 'py/stream.c')
-rw-r--r-- | py/stream.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/py/stream.c b/py/stream.c index 82bdffe2b..abc973d9e 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); |