Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objstringio.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  * Copyright (c) 2014 Paul Sokolovsky
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 
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include "py/objstr.h"
32 #include "py/objstringio.h"
33 #include "py/runtime.h"
34 #include "py/stream.h"
35 
36 #if MICROPY_PY_IO
37 
38 #if MICROPY_CPYTHON_COMPAT
39 STATIC void check_stringio_is_open(const mp_obj_stringio_t *o) {
40  if (o->vstr == NULL) {
41  mp_raise_ValueError("I/O operation on closed file");
42  }
43 }
44 #else
45 #define check_stringio_is_open(o)
46 #endif
47 
48 STATIC void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
49  (void)kind;
50  mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
51  mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self);
52 }
53 
54 STATIC mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
55  (void)errcode;
57  check_stringio_is_open(o);
58  if (o->vstr->len <= o->pos) { // read to EOF, or seeked to EOF or beyond
59  return 0;
60  }
61  mp_uint_t remaining = o->vstr->len - o->pos;
62  if (size > remaining) {
63  size = remaining;
64  }
65  memcpy(buf, o->vstr->buf + o->pos, size);
66  o->pos += size;
67  return size;
68 }
69 
70 STATIC void stringio_copy_on_write(mp_obj_stringio_t *o) {
71  const void *buf = o->vstr->buf;
72  o->vstr->buf = m_new(char, o->vstr->len);
73  memcpy(o->vstr->buf, buf, o->vstr->len);
74  o->vstr->fixed_buf = false;
75  o->ref_obj = MP_OBJ_NULL;
76 }
77 
78 STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
79  (void)errcode;
81  check_stringio_is_open(o);
82 
83  if (o->vstr->fixed_buf) {
84  stringio_copy_on_write(o);
85  }
86 
87  mp_uint_t new_pos = o->pos + size;
88  if (new_pos < size) {
89  // Writing <size> bytes will overflow o->pos beyond limit of mp_uint_t.
90  *errcode = MP_EFBIG;
91  return MP_STREAM_ERROR;
92  }
93  mp_uint_t org_len = o->vstr->len;
94  if (new_pos > o->vstr->alloc) {
95  // Take all what's already allocated...
96  o->vstr->len = o->vstr->alloc;
97  // ... and add more
98  vstr_add_len(o->vstr, new_pos - o->vstr->alloc);
99  }
100  // If there was a seek past EOF, clear the hole
101  if (o->pos > org_len) {
102  memset(o->vstr->buf + org_len, 0, o->pos - org_len);
103  }
104  memcpy(o->vstr->buf + o->pos, buf, size);
105  o->pos = new_pos;
106  if (new_pos > o->vstr->len) {
107  o->vstr->len = new_pos;
108  }
109  return size;
110 }
111 
112 STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
113  (void)errcode;
114  mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
115  switch (request) {
116  case MP_STREAM_SEEK: {
117  struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
118  mp_uint_t ref = 0;
119  switch (s->whence) {
120  case MP_SEEK_CUR:
121  ref = o->pos;
122  break;
123  case MP_SEEK_END:
124  ref = o->vstr->len;
125  break;
126  }
127  mp_uint_t new_pos = ref + s->offset;
128 
129  // For MP_SEEK_SET, offset is unsigned
130  if (s->whence != MP_SEEK_SET && s->offset < 0) {
131  if (new_pos > ref) {
132  // Negative offset from SEEK_CUR or SEEK_END went past 0.
133  // CPython sets position to 0, POSIX returns an EINVAL error
134  new_pos = 0;
135  }
136  } else if (new_pos < ref) {
137  // positive offset went beyond the limit of mp_uint_t
138  *errcode = MP_EINVAL; // replace with MP_EOVERFLOW when defined
139  return MP_STREAM_ERROR;
140  }
141  s->offset = o->pos = new_pos;
142  return 0;
143  }
144  case MP_STREAM_FLUSH:
145  return 0;
146  default:
147  *errcode = MP_EINVAL;
148  return MP_STREAM_ERROR;
149  }
150 }
151 
152 #define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
153 
154 STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
155  mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
156  check_stringio_is_open(self);
157  // TODO: Try to avoid copying string
158  return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
159 }
160 STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
161 
162 STATIC mp_obj_t stringio_close(mp_obj_t self_in) {
163  mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
164 #if MICROPY_CPYTHON_COMPAT
165  vstr_free(self->vstr);
166  self->vstr = NULL;
167 #else
168  vstr_clear(self->vstr);
169  self->vstr->alloc = 0;
170  self->vstr->len = 0;
171  self->pos = 0;
172 #endif
173  return mp_const_none;
174 }
175 STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close);
176 
177 STATIC mp_obj_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
178  (void)n_args;
179  return stringio_close(args[0]);
180 }
181 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
182 
183 STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
185  o->base.type = type;
186  o->pos = 0;
187  o->ref_obj = MP_OBJ_NULL;
188  return o;
189 }
190 
191 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) {
192  (void)n_kw; // TODO check n_kw==0
193 
194  mp_uint_t sz = 16;
195  bool initdata = false;
196  mp_buffer_info_t bufinfo;
197 
198  mp_obj_stringio_t *o = stringio_new(type_in);
199 
200  if (n_args > 0) {
201  if (MP_OBJ_IS_INT(args[0])) {
202  sz = mp_obj_get_int(args[0]);
203  } else {
204  mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
205 
206  if (MP_OBJ_IS_STR_OR_BYTES(args[0])) {
207  o->vstr = m_new_obj(vstr_t);
208  vstr_init_fixed_buf(o->vstr, bufinfo.len, bufinfo.buf);
209  o->vstr->len = bufinfo.len;
210  o->ref_obj = args[0];
211  return MP_OBJ_FROM_PTR(o);
212  }
213 
214  sz = bufinfo.len;
215  initdata = true;
216  }
217  }
218 
219  o->vstr = vstr_new(sz);
220 
221  if (initdata) {
222  stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
223  // Cur ptr is always at the beginning of buffer at the construction
224  o->pos = 0;
225  }
226  return MP_OBJ_FROM_PTR(o);
227 }
228 
229 STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
230  { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
231  { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
232  { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
233  { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
234  { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
235  { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
236  { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&stringio_close_obj) },
237  { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
238  { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
239  { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
240 };
241 
242 STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
243 
244 STATIC const mp_stream_p_t stringio_stream_p = {
245  .read = stringio_read,
246  .write = stringio_write,
247  .ioctl = stringio_ioctl,
248  .is_text = true,
249 };
250 
251 STATIC const mp_stream_p_t bytesio_stream_p = {
252  .read = stringio_read,
253  .write = stringio_write,
254  .ioctl = stringio_ioctl,
255 };
256 
258  { &mp_type_type },
259  .name = MP_QSTR_StringIO,
260  .print = stringio_print,
261  .make_new = stringio_make_new,
262  .getiter = mp_identity_getiter,
263  .iternext = mp_stream_unbuffered_iter,
264  .protocol = &stringio_stream_p,
265  .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
266 };
267 
268 #if MICROPY_PY_IO_BYTESIO
270  { &mp_type_type },
271  .name = MP_QSTR_BytesIO,
272  .print = stringio_print,
273  .make_new = stringio_make_new,
274  .getiter = mp_identity_getiter,
275  .iternext = mp_stream_unbuffered_iter,
276  .protocol = &bytesio_stream_p,
277  .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
278 };
279 #endif
280 
281 #endif
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf)
Definition: vstr.c:57
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
Definition: misc.h:142
#define MP_BUFFER_READ
Definition: obj.h:454
#define MP_EINVAL
Definition: mperrno.h:112
void * memset(void *b, int c, size_t len)
Definition: memset.c:3
#define mp_const_none
Definition: obj.h:614
#define MP_DEFINE_CONST_DICT(dict_name, table_name)
Definition: obj.h:317
#define MP_SEEK_CUR
Definition: stream.h:62
mp_off_t offset
Definition: stream.h:56
char * buf
Definition: misc.h:145
mp_uint_t(* read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode)
Definition: obj.h:467
#define MP_ROM_QSTR(q)
Definition: obj.h:241
vstr_t * vstr_new(size_t alloc)
Definition: vstr.c:77
unsigned int uintptr_t
Definition: stdint.h:14
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
#define MP_ROM_PTR(p)
Definition: obj.h:242
size_t len
Definition: obj.h:447
size_t len
Definition: misc.h:144
mp_int_t mp_obj_get_int(mp_const_obj_t arg)
Definition: obj.c:225
mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self)
Definition: stream.c:429
#define STATIC
Definition: mpconfig.h:1178
#define MP_STREAM_FLUSH
Definition: stream.h:35
char * vstr_add_len(vstr_t *vstr, size_t len)
Definition: vstr.c:124
const mp_obj_type_t mp_type_stringio
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name)
Definition: obj.h:285
mp_print_kind_t
Definition: obj.h:412
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
mp_obj_base_t base
Definition: objstringio.h:32
#define MP_STREAM_SEEK
Definition: stream.h:36
args
Definition: i18n.py:175
#define MP_OBJ_IS_INT(o)
Definition: obj.h:255
#define MP_SEEK_END
Definition: stream.h:63
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name)
Definition: obj.h:297
#define MP_EFBIG
Definition: mperrno.h:117
unsigned char byte
Definition: misc.h:37
#define MP_STREAM_ERROR
Definition: stream.h:32
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
NORETURN void mp_raise_ValueError(const char *msg)
Definition: runtime.c:1456
mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf)
Definition: obj.c:507
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags)
Definition: obj.c:524
#define MP_SEEK_SET
Definition: stream.h:61
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte *data, size_t len)
Definition: objstr.c:1981
void vstr_free(vstr_t *vstr)
Definition: vstr.c:83
const mp_obj_type_t mp_type_bytesio
qstr name
Definition: obj.h:478
bool fixed_buf
Definition: misc.h:146
uint64_t mp_obj_t
Definition: obj.h:39
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
#define MP_OBJ_IS_STR_OR_BYTES(o)
Definition: obj.h:257
#define m_new_obj(type)
Definition: misc.h:60
void vstr_clear(vstr_t *vstr)
Definition: vstr.c:70
size_t alloc
Definition: misc.h:143
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3
void * buf
Definition: obj.h:446
#define m_new(type, num)
Definition: misc.h:57