summaryrefslogtreecommitdiff
path: root/py/objstringio.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objstringio.c')
-rw-r--r--py/objstringio.c40
1 files changed, 38 insertions, 2 deletions
diff --git a/py/objstringio.c b/py/objstringio.c
index abd4e835e..eb2e516bb 100644
--- a/py/objstringio.c
+++ b/py/objstringio.c
@@ -75,13 +75,18 @@ STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size,
(void)errcode;
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
check_stringio_is_open(o);
- mp_uint_t remaining = o->vstr->alloc - o->pos;
- if (size > remaining) {
+ mp_int_t remaining = o->vstr->alloc - o->pos;
+ mp_uint_t org_len = o->vstr->len;
+ if ((mp_int_t)size > remaining) {
// Take all what's already allocated...
o->vstr->len = o->vstr->alloc;
// ... and add more
vstr_add_len(o->vstr, size - remaining);
}
+ // If there was a seek past EOF, clear the hole
+ if (o->pos > org_len) {
+ memset(o->vstr->buf + org_len, 0, o->pos - org_len);
+ }
memcpy(o->vstr->buf + o->pos, buf, size);
o->pos += size;
if (o->pos > o->vstr->len) {
@@ -90,6 +95,33 @@ STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size,
return size;
}
+STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
+ (void)errcode;
+ mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
+ switch (request) {
+ case MP_STREAM_SEEK: {
+ struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
+ mp_uint_t ref = 0;
+ switch (s->whence) {
+ case 1: // SEEK_CUR
+ ref = o->pos;
+ break;
+ case 2: // SEEK_END
+ ref = o->vstr->len;
+ break;
+ }
+ o->pos = ref + s->offset;
+ s->offset = o->pos;
+ return 0;
+ }
+ case MP_STREAM_FLUSH:
+ return 0;
+ default:
+ *errcode = MP_EINVAL;
+ return MP_STREAM_ERROR;
+ }
+}
+
#define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
@@ -148,6 +180,8 @@ STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_readall), MP_ROM_PTR(&mp_stream_readall_obj) },
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
+ { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
+ { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&stringio_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
@@ -159,12 +193,14 @@ STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
STATIC const mp_stream_p_t stringio_stream_p = {
.read = stringio_read,
.write = stringio_write,
+ .ioctl = stringio_ioctl,
.is_text = true,
};
STATIC const mp_stream_p_t bytesio_stream_p = {
.read = stringio_read,
.write = stringio_write,
+ .ioctl = stringio_ioctl,
};
const mp_obj_type_t mp_type_stringio = {