blob: eb0cc4eb3649a72a946722b95d61008aa577aad4 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskya47b64a2014-05-15 07:28:19 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030028#include <stdio.h>
29#include <string.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/objstr.h"
33#include "py/runtime.h"
34#include "py/stream.h"
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030035
Damien Georgeee3fd462014-05-24 23:03:12 +010036#if MICROPY_PY_IO
Paul Sokolovsky100cd362014-04-26 20:59:39 +030037
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030038typedef struct _mp_obj_stringio_t {
39 mp_obj_base_t base;
40 vstr_t *vstr;
41 // StringIO has single pointer used for both reading and writing
Damien George40f3c022014-07-03 13:25:24 +010042 mp_uint_t pos;
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030043} mp_obj_stringio_t;
44
stijnbf195412015-01-16 13:36:18 +010045#if MICROPY_CPYTHON_COMPAT
46STATIC void check_stringio_is_open(const mp_obj_stringio_t *o) {
47 if (o->vstr == NULL) {
48 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "I/O operation on closed file"));
49 }
50}
51#else
52#define check_stringio_is_open(o)
53#endif
54
Damien George7f9d1d62015-04-09 23:56:15 +010055STATIC void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000056 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000057 mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
Damien George7f9d1d62015-04-09 23:56:15 +010058 mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self);
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030059}
60
Damien Georgeadf0f2a2014-07-27 22:38:58 +010061STATIC mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000062 (void)errcode;
Damien George999cedb2015-11-27 17:01:44 +000063 mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
stijnbf195412015-01-16 13:36:18 +010064 check_stringio_is_open(o);
Damien George40f3c022014-07-03 13:25:24 +010065 mp_uint_t remaining = o->vstr->len - o->pos;
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030066 if (size > remaining) {
67 size = remaining;
68 }
69 memcpy(buf, o->vstr->buf + o->pos, size);
70 o->pos += size;
71 return size;
72}
73
Damien Georgeadf0f2a2014-07-27 22:38:58 +010074STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000075 (void)errcode;
Damien George999cedb2015-11-27 17:01:44 +000076 mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
stijnbf195412015-01-16 13:36:18 +010077 check_stringio_is_open(o);
Paul Sokolovsky3990b172016-07-28 01:53:44 +030078 mp_int_t remaining = o->vstr->alloc - o->pos;
79 mp_uint_t org_len = o->vstr->len;
80 if ((mp_int_t)size > remaining) {
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030081 // Take all what's already allocated...
82 o->vstr->len = o->vstr->alloc;
83 // ... and add more
84 vstr_add_len(o->vstr, size - remaining);
85 }
Paul Sokolovsky3990b172016-07-28 01:53:44 +030086 // If there was a seek past EOF, clear the hole
87 if (o->pos > org_len) {
88 memset(o->vstr->buf + org_len, 0, o->pos - org_len);
89 }
Paul Sokolovskycb9dc082014-04-26 20:26:14 +030090 memcpy(o->vstr->buf + o->pos, buf, size);
91 o->pos += size;
92 if (o->pos > o->vstr->len) {
93 o->vstr->len = o->pos;
94 }
95 return size;
96}
97
Paul Sokolovskyf039ac52016-07-28 01:14:32 +030098STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
99 (void)errcode;
Paul Sokolovsky3990b172016-07-28 01:53:44 +0300100 mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
Paul Sokolovskyf039ac52016-07-28 01:14:32 +0300101 switch (request) {
Paul Sokolovsky3990b172016-07-28 01:53:44 +0300102 case MP_STREAM_SEEK: {
103 struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
104 mp_uint_t ref = 0;
105 switch (s->whence) {
106 case 1: // SEEK_CUR
107 ref = o->pos;
108 break;
109 case 2: // SEEK_END
110 ref = o->vstr->len;
111 break;
112 }
113 o->pos = ref + s->offset;
114 s->offset = o->pos;
115 return 0;
116 }
Paul Sokolovskyf039ac52016-07-28 01:14:32 +0300117 case MP_STREAM_FLUSH:
118 return 0;
119 default:
120 *errcode = MP_EINVAL;
121 return MP_STREAM_ERROR;
122 }
123}
124
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300125#define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
126
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300127STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000128 mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
stijnbf195412015-01-16 13:36:18 +0100129 check_stringio_is_open(self);
Paul Sokolovsky2ae66972016-05-13 01:35:52 +0300130 // TODO: Try to avoid copying string
Damien Georgef600a6a2014-05-25 22:34:34 +0100131 return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300132}
133STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
134
135STATIC mp_obj_t stringio_close(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000136 mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
stijnbf195412015-01-16 13:36:18 +0100137#if MICROPY_CPYTHON_COMPAT
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300138 vstr_free(self->vstr);
139 self->vstr = NULL;
stijnbf195412015-01-16 13:36:18 +0100140#else
141 vstr_clear(self->vstr);
142 self->vstr->alloc = 0;
143 self->vstr->len = 0;
144 self->pos = 0;
145#endif
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300146 return mp_const_none;
147}
148STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close);
149
Damien George4b72b3a2016-01-03 14:21:40 +0000150STATIC mp_obj_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000151 (void)n_args;
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300152 return stringio_close(args[0]);
153}
154STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
155
Damien George5b3f0b72016-01-03 15:55:55 +0000156STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300157 mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
Damien George5b3f0b72016-01-03 15:55:55 +0000158 o->base.type = type;
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300159 o->vstr = vstr_new();
160 o->pos = 0;
161 return o;
162}
163
Damien George5b3f0b72016-01-03 15:55:55 +0000164STATIC 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) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000165 (void)n_kw; // TODO check n_kw==0
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300166 mp_obj_stringio_t *o = stringio_new(type_in);
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300167
168 if (n_args > 0) {
169 mp_buffer_info_t bufinfo;
170 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
Damien George999cedb2015-11-27 17:01:44 +0000171 stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300172 // Cur ptr is always at the beginning of buffer at the construction
173 o->pos = 0;
174 }
Damien George999cedb2015-11-27 17:01:44 +0000175 return MP_OBJ_FROM_PTR(o);
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300176}
177
Damien Georgecbf76742015-11-27 13:38:15 +0000178STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
179 { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
Paul Sokolovskyd22a04d2016-10-09 11:56:11 +0300180 { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
Damien Georgecbf76742015-11-27 13:38:15 +0000181 { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
182 { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
Paul Sokolovsky3990b172016-07-28 01:53:44 +0300183 { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
Paul Sokolovskyf039ac52016-07-28 01:14:32 +0300184 { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
Damien Georgecbf76742015-11-27 13:38:15 +0000185 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&stringio_close_obj) },
186 { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
187 { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
188 { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300189};
190
191STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
192
193STATIC const mp_stream_p_t stringio_stream_p = {
194 .read = stringio_read,
195 .write = stringio_write,
Paul Sokolovskyf039ac52016-07-28 01:14:32 +0300196 .ioctl = stringio_ioctl,
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100197 .is_text = true,
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300198};
199
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300200STATIC const mp_stream_p_t bytesio_stream_p = {
201 .read = stringio_read,
202 .write = stringio_write,
Paul Sokolovskyf039ac52016-07-28 01:14:32 +0300203 .ioctl = stringio_ioctl,
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300204};
205
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300206const mp_obj_type_t mp_type_stringio = {
207 { &mp_type_type },
208 .name = MP_QSTR_StringIO,
209 .print = stringio_print,
210 .make_new = stringio_make_new,
211 .getiter = mp_identity,
212 .iternext = mp_stream_unbuffered_iter,
Paul Sokolovsky07209f82016-06-18 18:19:24 +0300213 .protocol = &stringio_stream_p,
Damien George999cedb2015-11-27 17:01:44 +0000214 .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300215};
Paul Sokolovsky100cd362014-04-26 20:59:39 +0300216
Damien Georgeee3fd462014-05-24 23:03:12 +0100217#if MICROPY_PY_IO_BYTESIO
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300218const mp_obj_type_t mp_type_bytesio = {
219 { &mp_type_type },
220 .name = MP_QSTR_BytesIO,
221 .print = stringio_print,
222 .make_new = stringio_make_new,
223 .getiter = mp_identity,
224 .iternext = mp_stream_unbuffered_iter,
Paul Sokolovsky07209f82016-06-18 18:19:24 +0300225 .protocol = &bytesio_stream_p,
Damien George999cedb2015-11-27 17:01:44 +0000226 .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300227};
228#endif
229
Paul Sokolovsky100cd362014-04-26 20:59:39 +0300230#endif