|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +#include "nlr.h" |
| 5 | +#include "misc.h" |
| 6 | +#include "mpconfig.h" |
| 7 | +#include "qstr.h" |
| 8 | +#include "obj.h" |
| 9 | +#include "runtime.h" |
| 10 | +#include "stream.h" |
| 11 | + |
| 12 | +typedef struct _mp_obj_stringio_t { |
| 13 | + mp_obj_base_t base; |
| 14 | + vstr_t *vstr; |
| 15 | + // StringIO has single pointer used for both reading and writing |
| 16 | + machine_uint_t pos; |
| 17 | +} mp_obj_stringio_t; |
| 18 | + |
| 19 | +STATIC void stringio_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 20 | + mp_obj_stringio_t *self = self_in; |
| 21 | + print(env, "<io.StringIO 0x%x>", self->vstr); |
| 22 | +} |
| 23 | + |
| 24 | +STATIC machine_int_t stringio_read(mp_obj_t o_in, void *buf, machine_uint_t size, int *errcode) { |
| 25 | + mp_obj_stringio_t *o = o_in; |
| 26 | + machine_uint_t remaining = o->vstr->len - o->pos; |
| 27 | + if (size > remaining) { |
| 28 | + size = remaining; |
| 29 | + } |
| 30 | + memcpy(buf, o->vstr->buf + o->pos, size); |
| 31 | + o->pos += size; |
| 32 | + return size; |
| 33 | +} |
| 34 | + |
| 35 | +STATIC machine_int_t stringio_write(mp_obj_t o_in, const void *buf, machine_uint_t size, int *errcode) { |
| 36 | + mp_obj_stringio_t *o = o_in; |
| 37 | + machine_uint_t remaining = o->vstr->alloc - o->pos; |
| 38 | + if (size > remaining) { |
| 39 | + // Take all what's already allocated... |
| 40 | + o->vstr->len = o->vstr->alloc; |
| 41 | + // ... and add more |
| 42 | + vstr_add_len(o->vstr, size - remaining); |
| 43 | + } |
| 44 | + memcpy(o->vstr->buf + o->pos, buf, size); |
| 45 | + o->pos += size; |
| 46 | + if (o->pos > o->vstr->len) { |
| 47 | + o->vstr->len = o->pos; |
| 48 | + } |
| 49 | + return size; |
| 50 | +} |
| 51 | + |
| 52 | +STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) { |
| 53 | + mp_obj_stringio_t *self = self_in; |
| 54 | + return mp_obj_new_str((byte*)self->vstr->buf, self->vstr->len, false); |
| 55 | +} |
| 56 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue); |
| 57 | + |
| 58 | +STATIC mp_obj_t stringio_close(mp_obj_t self_in) { |
| 59 | + mp_obj_stringio_t *self = self_in; |
| 60 | + vstr_free(self->vstr); |
| 61 | + self->vstr = NULL; |
| 62 | + return mp_const_none; |
| 63 | +} |
| 64 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close); |
| 65 | + |
| 66 | +mp_obj_t stringio___exit__(uint n_args, const mp_obj_t *args) { |
| 67 | + return stringio_close(args[0]); |
| 68 | +} |
| 69 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__); |
| 70 | + |
| 71 | +STATIC mp_obj_stringio_t *stringio_new() { |
| 72 | + mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t); |
| 73 | + o->base.type = &mp_type_stringio; |
| 74 | + o->vstr = vstr_new(); |
| 75 | + o->pos = 0; |
| 76 | + return o; |
| 77 | +} |
| 78 | + |
| 79 | +STATIC mp_obj_t stringio_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
| 80 | + mp_obj_stringio_t *o = stringio_new(); |
| 81 | + |
| 82 | + if (n_args > 0) { |
| 83 | + mp_buffer_info_t bufinfo; |
| 84 | + mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
| 85 | + stringio_write(o, bufinfo.buf, bufinfo.len, NULL); |
| 86 | + // Cur ptr is always at the beginning of buffer at the construction |
| 87 | + o->pos = 0; |
| 88 | + } |
| 89 | + return o; |
| 90 | +} |
| 91 | + |
| 92 | +STATIC const mp_map_elem_t stringio_locals_dict_table[] = { |
| 93 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, |
| 94 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, |
| 95 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, |
| 96 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, |
| 97 | + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&stringio_close_obj }, |
| 98 | + { MP_OBJ_NEW_QSTR(MP_QSTR_getvalue), (mp_obj_t)&stringio_getvalue_obj }, |
| 99 | + { MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj }, |
| 100 | + { MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&stringio___exit___obj }, |
| 101 | +}; |
| 102 | + |
| 103 | +STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table); |
| 104 | + |
| 105 | +STATIC const mp_stream_p_t stringio_stream_p = { |
| 106 | + .read = stringio_read, |
| 107 | + .write = stringio_write, |
| 108 | +}; |
| 109 | + |
| 110 | +const mp_obj_type_t mp_type_stringio = { |
| 111 | + { &mp_type_type }, |
| 112 | + .name = MP_QSTR_StringIO, |
| 113 | + .print = stringio_print, |
| 114 | + .make_new = stringio_make_new, |
| 115 | + .getiter = mp_identity, |
| 116 | + .iternext = mp_stream_unbuffered_iter, |
| 117 | + .stream_p = &stringio_stream_p, |
| 118 | + .locals_dict = (mp_obj_t)&stringio_locals_dict, |
| 119 | +}; |
0 commit comments