Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 7 | * Copyright (c) 2014 Paul Sokolovsky |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 8 | * |
| 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 Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 31 | #include "py/nlr.h" |
| 32 | #include "py/objstr.h" |
| 33 | #include "py/runtime.h" |
| 34 | #include "py/stream.h" |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 35 | |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 36 | #if MICROPY_PY_IO |
Paul Sokolovsky | 100cd36 | 2014-04-26 20:59:39 +0300 | [diff] [blame] | 37 | |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 38 | typedef 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 George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 42 | mp_uint_t pos; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 43 | } mp_obj_stringio_t; |
| 44 | |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 45 | #if MICROPY_CPYTHON_COMPAT |
| 46 | STATIC 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 George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 55 | STATIC void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 56 | (void)kind; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 57 | mp_obj_stringio_t *self = self_in; |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 58 | mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 59 | } |
| 60 | |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 61 | STATIC mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 62 | (void)errcode; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 63 | mp_obj_stringio_t *o = o_in; |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 64 | check_stringio_is_open(o); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 65 | mp_uint_t remaining = o->vstr->len - o->pos; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 66 | 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 George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 74 | STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 75 | (void)errcode; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 76 | mp_obj_stringio_t *o = o_in; |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 77 | check_stringio_is_open(o); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 78 | mp_uint_t remaining = o->vstr->alloc - o->pos; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 79 | if (size > remaining) { |
| 80 | // Take all what's already allocated... |
| 81 | o->vstr->len = o->vstr->alloc; |
| 82 | // ... and add more |
| 83 | vstr_add_len(o->vstr, size - remaining); |
| 84 | } |
| 85 | memcpy(o->vstr->buf + o->pos, buf, size); |
| 86 | o->pos += size; |
| 87 | if (o->pos > o->vstr->len) { |
| 88 | o->vstr->len = o->pos; |
| 89 | } |
| 90 | return size; |
| 91 | } |
| 92 | |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 93 | #define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes) |
| 94 | |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 95 | STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) { |
| 96 | mp_obj_stringio_t *self = self_in; |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 97 | check_stringio_is_open(self); |
Damien George | f600a6a | 2014-05-25 22:34:34 +0100 | [diff] [blame] | 98 | return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 99 | } |
| 100 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue); |
| 101 | |
| 102 | STATIC mp_obj_t stringio_close(mp_obj_t self_in) { |
| 103 | mp_obj_stringio_t *self = self_in; |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 104 | #if MICROPY_CPYTHON_COMPAT |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 105 | vstr_free(self->vstr); |
| 106 | self->vstr = NULL; |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 107 | #else |
| 108 | vstr_clear(self->vstr); |
| 109 | self->vstr->alloc = 0; |
| 110 | self->vstr->len = 0; |
| 111 | self->pos = 0; |
| 112 | #endif |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 113 | return mp_const_none; |
| 114 | } |
| 115 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close); |
| 116 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 117 | STATIC mp_obj_t stringio___exit__(mp_uint_t n_args, const mp_obj_t *args) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 118 | (void)n_args; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 119 | return stringio_close(args[0]); |
| 120 | } |
| 121 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__); |
| 122 | |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 123 | STATIC mp_obj_stringio_t *stringio_new(mp_obj_t type_in) { |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 124 | mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t); |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 125 | o->base.type = type_in; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 126 | o->vstr = vstr_new(); |
| 127 | o->pos = 0; |
| 128 | return o; |
| 129 | } |
| 130 | |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 131 | STATIC mp_obj_t stringio_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 132 | (void)n_kw; // TODO check n_kw==0 |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 133 | mp_obj_stringio_t *o = stringio_new(type_in); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 134 | |
| 135 | if (n_args > 0) { |
| 136 | mp_buffer_info_t bufinfo; |
| 137 | mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
| 138 | stringio_write(o, bufinfo.buf, bufinfo.len, NULL); |
| 139 | // Cur ptr is always at the beginning of buffer at the construction |
| 140 | o->pos = 0; |
| 141 | } |
| 142 | return o; |
| 143 | } |
| 144 | |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame^] | 145 | STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = { |
| 146 | { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
| 147 | { MP_ROM_QSTR(MP_QSTR_readall), MP_ROM_PTR(&mp_stream_readall_obj) }, |
| 148 | { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, |
| 149 | { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
| 150 | { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&stringio_close_obj) }, |
| 151 | { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) }, |
| 152 | { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, |
| 153 | { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) }, |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table); |
| 157 | |
| 158 | STATIC const mp_stream_p_t stringio_stream_p = { |
| 159 | .read = stringio_read, |
| 160 | .write = stringio_write, |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 161 | .is_text = true, |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 162 | }; |
| 163 | |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 164 | STATIC const mp_stream_p_t bytesio_stream_p = { |
| 165 | .read = stringio_read, |
| 166 | .write = stringio_write, |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 167 | }; |
| 168 | |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 169 | const mp_obj_type_t mp_type_stringio = { |
| 170 | { &mp_type_type }, |
| 171 | .name = MP_QSTR_StringIO, |
| 172 | .print = stringio_print, |
| 173 | .make_new = stringio_make_new, |
| 174 | .getiter = mp_identity, |
| 175 | .iternext = mp_stream_unbuffered_iter, |
| 176 | .stream_p = &stringio_stream_p, |
| 177 | .locals_dict = (mp_obj_t)&stringio_locals_dict, |
| 178 | }; |
Paul Sokolovsky | 100cd36 | 2014-04-26 20:59:39 +0300 | [diff] [blame] | 179 | |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 180 | #if MICROPY_PY_IO_BYTESIO |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 181 | const mp_obj_type_t mp_type_bytesio = { |
| 182 | { &mp_type_type }, |
| 183 | .name = MP_QSTR_BytesIO, |
| 184 | .print = stringio_print, |
| 185 | .make_new = stringio_make_new, |
| 186 | .getiter = mp_identity, |
| 187 | .iternext = mp_stream_unbuffered_iter, |
| 188 | .stream_p = &bytesio_stream_p, |
| 189 | .locals_dict = (mp_obj_t)&stringio_locals_dict, |
| 190 | }; |
| 191 | #endif |
| 192 | |
Paul Sokolovsky | 100cd36 | 2014-04-26 20:59:39 +0300 | [diff] [blame] | 193 | #endif |