diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-11-17 00:16:14 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-11-17 00:16:14 +0200 |
commit | 838eb1fa2d365ee085082b26e8b8b6ec657af572 (patch) | |
tree | 4e3546f54910bc831928a4c749c3375d40abe6a3 /py/stream.c | |
parent | f4a6a577ab133781c06aec029c806c878555730a (diff) | |
download | micropython-838eb1fa2d365ee085082b26e8b8b6ec657af572.tar.gz micropython-838eb1fa2d365ee085082b26e8b8b6ec657af572.zip |
stream: Implement seek operation support via ioctl, wrapped in generic method.
Also, implement for unix port.
Diffstat (limited to 'py/stream.c')
-rw-r--r-- | py/stream.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/py/stream.c b/py/stream.c index a8561dbbc9..1361d6c722 100644 --- a/py/stream.c +++ b/py/stream.c @@ -380,6 +380,32 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) { return MP_OBJ_STOP_ITERATION; } +STATIC mp_obj_t stream_seek(mp_uint_t n_args, const mp_obj_t *args) { + struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; + if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { + // CPython: io.UnsupportedOperation, OSError subclass + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); + } + + struct mp_stream_seek_t seek_s; + // TODO: Could be uint64 + seek_s.offset = mp_obj_get_int(args[1]); + seek_s.whence = 0; + if (n_args == 3) { + seek_s.whence = mp_obj_get_int(args[2]); + } + + int error; + mp_uint_t res = o->type->stream_p->ioctl(o, MP_STREAM_SEEK, (mp_uint_t)&seek_s, &error); + if (res == MP_STREAM_ERROR) { + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); + } + + // TODO: Could be uint64 + return mp_obj_new_int_from_uint(seek_s.offset); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek); + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto); MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); |