2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2014-05-15 05:28:19 +01:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-04-26 18:26:14 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/objstr.h"
|
2016-10-13 01:43:28 +01:00
|
|
|
#include "py/objstringio.h"
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
2014-04-26 18:26:14 +01:00
|
|
|
|
2014-05-24 23:03:12 +01:00
|
|
|
#if MICROPY_PY_IO
|
2014-04-26 18:59:39 +01:00
|
|
|
|
2015-01-16 12:36:18 +00:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
|
|
|
STATIC void check_stringio_is_open(const mp_obj_stringio_t *o) {
|
|
|
|
if (o->vstr == NULL) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "I/O operation on closed file"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define check_stringio_is_open(o)
|
|
|
|
#endif
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self);
|
2014-04-26 18:26:14 +01:00
|
|
|
}
|
|
|
|
|
2014-07-27 22:38:58 +01:00
|
|
|
STATIC mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)errcode;
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
|
2015-01-16 12:36:18 +00:00
|
|
|
check_stringio_is_open(o);
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_uint_t remaining = o->vstr->len - o->pos;
|
2014-04-26 18:26:14 +01:00
|
|
|
if (size > remaining) {
|
|
|
|
size = remaining;
|
|
|
|
}
|
|
|
|
memcpy(buf, o->vstr->buf + o->pos, size);
|
|
|
|
o->pos += size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-07-27 22:38:58 +01:00
|
|
|
STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)errcode;
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
|
2015-01-16 12:36:18 +00:00
|
|
|
check_stringio_is_open(o);
|
2016-07-27 23:53:44 +01:00
|
|
|
mp_int_t remaining = o->vstr->alloc - o->pos;
|
|
|
|
mp_uint_t org_len = o->vstr->len;
|
|
|
|
if ((mp_int_t)size > remaining) {
|
2014-04-26 18:26:14 +01:00
|
|
|
// Take all what's already allocated...
|
|
|
|
o->vstr->len = o->vstr->alloc;
|
|
|
|
// ... and add more
|
|
|
|
vstr_add_len(o->vstr, size - remaining);
|
|
|
|
}
|
2016-07-27 23:53:44 +01:00
|
|
|
// 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);
|
|
|
|
}
|
2014-04-26 18:26:14 +01:00
|
|
|
memcpy(o->vstr->buf + o->pos, buf, size);
|
|
|
|
o->pos += size;
|
|
|
|
if (o->pos > o->vstr->len) {
|
|
|
|
o->vstr->len = o->pos;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-07-27 23:14:32 +01:00
|
|
|
STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
(void)errcode;
|
2016-07-27 23:53:44 +01:00
|
|
|
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
|
2016-07-27 23:14:32 +01:00
|
|
|
switch (request) {
|
2016-07-27 23:53:44 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-07-27 23:14:32 +01:00
|
|
|
case MP_STREAM_FLUSH:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
*errcode = MP_EINVAL;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 05:28:19 +01:00
|
|
|
#define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
|
|
|
|
|
2014-04-26 18:26:14 +01:00
|
|
|
STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-01-16 12:36:18 +00:00
|
|
|
check_stringio_is_open(self);
|
2016-05-12 23:35:52 +01:00
|
|
|
// TODO: Try to avoid copying string
|
2014-05-25 22:34:34 +01:00
|
|
|
return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
|
2014-04-26 18:26:14 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
|
|
|
|
|
|
|
|
STATIC mp_obj_t stringio_close(mp_obj_t self_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-01-16 12:36:18 +00:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
2014-04-26 18:26:14 +01:00
|
|
|
vstr_free(self->vstr);
|
|
|
|
self->vstr = NULL;
|
2015-01-16 12:36:18 +00:00
|
|
|
#else
|
|
|
|
vstr_clear(self->vstr);
|
|
|
|
self->vstr->alloc = 0;
|
|
|
|
self->vstr->len = 0;
|
|
|
|
self->pos = 0;
|
|
|
|
#endif
|
2014-04-26 18:26:14 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close);
|
|
|
|
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)n_args;
|
2014-04-26 18:26:14 +01:00
|
|
|
return stringio_close(args[0]);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
|
|
|
|
|
2016-01-03 15:55:55 +00:00
|
|
|
STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
|
2014-04-26 18:26:14 +01:00
|
|
|
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
|
2016-01-03 15:55:55 +00:00
|
|
|
o->base.type = type;
|
2014-04-26 18:26:14 +01:00
|
|
|
o->vstr = vstr_new();
|
|
|
|
o->pos = 0;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2016-01-03 15:55:55 +00:00
|
|
|
STATIC mp_obj_t stringio_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)n_kw; // TODO check n_kw==0
|
2014-05-15 05:28:19 +01:00
|
|
|
mp_obj_stringio_t *o = stringio_new(type_in);
|
2014-04-26 18:26:14 +01:00
|
|
|
|
|
|
|
if (n_args > 0) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
|
2015-11-27 17:01:44 +00:00
|
|
|
stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
|
2014-04-26 18:26:14 +01:00
|
|
|
// Cur ptr is always at the beginning of buffer at the construction
|
|
|
|
o->pos = 0;
|
|
|
|
}
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-04-26 18:26:14 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:38:15 +00:00
|
|
|
STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
2016-10-09 09:56:11 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
2015-11-27 13:38:15 +00:00
|
|
|
{ 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) },
|
2016-07-27 23:53:44 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
|
2016-07-27 23:14:32 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
|
2015-11-27 13:38:15 +00:00
|
|
|
{ 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) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
|
2014-04-26 18:26:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2016-07-27 23:14:32 +01:00
|
|
|
.ioctl = stringio_ioctl,
|
2014-07-27 22:38:58 +01:00
|
|
|
.is_text = true,
|
2014-04-26 18:26:14 +01:00
|
|
|
};
|
|
|
|
|
2014-05-15 05:28:19 +01:00
|
|
|
STATIC const mp_stream_p_t bytesio_stream_p = {
|
|
|
|
.read = stringio_read,
|
|
|
|
.write = stringio_write,
|
2016-07-27 23:14:32 +01:00
|
|
|
.ioctl = stringio_ioctl,
|
2014-05-15 05:28:19 +01:00
|
|
|
};
|
|
|
|
|
2014-04-26 18:26:14 +01:00
|
|
|
const mp_obj_type_t mp_type_stringio = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_StringIO,
|
|
|
|
.print = stringio_print,
|
|
|
|
.make_new = stringio_make_new,
|
|
|
|
.getiter = mp_identity,
|
|
|
|
.iternext = mp_stream_unbuffered_iter,
|
2016-06-18 16:19:24 +01:00
|
|
|
.protocol = &stringio_stream_p,
|
2015-11-27 17:01:44 +00:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
|
2014-04-26 18:26:14 +01:00
|
|
|
};
|
2014-04-26 18:59:39 +01:00
|
|
|
|
2014-05-24 23:03:12 +01:00
|
|
|
#if MICROPY_PY_IO_BYTESIO
|
2014-05-15 05:28:19 +01:00
|
|
|
const mp_obj_type_t mp_type_bytesio = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_BytesIO,
|
|
|
|
.print = stringio_print,
|
|
|
|
.make_new = stringio_make_new,
|
|
|
|
.getiter = mp_identity,
|
|
|
|
.iternext = mp_stream_unbuffered_iter,
|
2016-06-18 16:19:24 +01:00
|
|
|
.protocol = &bytesio_stream_p,
|
2015-11-27 17:01:44 +00:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
|
2014-05-15 05:28:19 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-04-26 18:59:39 +01:00
|
|
|
#endif
|