blob: 96805d2911f2d11b60f3571898222a38e6d4388d [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
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +020027#include <assert.h>
28#include <string.h>
29
30#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/builtin.h"
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +020032#include "py/stream.h"
Paul Sokolovsky98a627d2014-04-03 14:57:53 +030033
Damien Georgeee3fd462014-05-24 23:03:12 +010034#if MICROPY_PY_IO
Paul Sokolovsky98a627d2014-04-03 14:57:53 +030035
Paul Sokolovsky9e296662014-05-19 20:59:13 +030036extern const mp_obj_type_t mp_type_fileio;
37extern const mp_obj_type_t mp_type_textio;
38
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +020039#if MICROPY_PY_IO_BUFFEREDWRITER
40typedef struct _mp_obj_bufwriter_t {
41 mp_obj_base_t base;
42 mp_obj_t stream;
43 size_t alloc;
44 size_t len;
45 byte buf[0];
46} mp_obj_bufwriter_t;
47
48STATIC mp_obj_t bufwriter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
49 mp_arg_check_num(n_args, n_kw, 2, 2, false);
50 size_t alloc = mp_obj_get_int(args[1]);
51 mp_obj_bufwriter_t *o = m_new_obj_var(mp_obj_bufwriter_t, byte, alloc);
52 o->base.type = type;
53 o->stream = args[0];
54 o->alloc = alloc;
55 o->len = 0;
56 return o;
57}
58
59STATIC mp_uint_t bufwriter_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
60 mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
61
62 mp_uint_t org_size = size;
63
64 while (size > 0) {
65 mp_uint_t rem = self->alloc - self->len;
66 if (size < rem) {
67 memcpy(self->buf + self->len, buf, size);
68 self->len += size;
69 return org_size;
70 }
71
Paul Sokolovsky2c81b9b2016-03-25 14:59:30 +020072 // Buffer flushing policy here is to flush entire buffer all the time.
73 // This allows e.g. to have a block device as backing storage and write
74 // entire block to it. memcpy below is not ideal and could be optimized
75 // in some cases. But the way it is now it at least ensures that buffer
76 // is word-aligned, to guard against obscure cases when it matters, e.g.
77 // https://github.com/micropython/micropython/issues/1863
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +020078 memcpy(self->buf + self->len, buf, rem);
79 buf = (byte*)buf + rem;
80 size -= rem;
81 mp_uint_t out_sz = mp_stream_writeall(self->stream, self->buf, self->alloc, errcode);
82 if (out_sz == MP_STREAM_ERROR) {
83 return MP_STREAM_ERROR;
84 }
85 self->len = 0;
86 }
87
88 return org_size;
89}
90
Paul Sokolovsky063e6e72016-03-25 14:33:38 +020091STATIC mp_obj_t bufwriter_flush(mp_obj_t self_in) {
92 mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
93
94 if (self->len != 0) {
95 int err;
96 mp_uint_t out_sz = mp_stream_writeall(self->stream, self->buf, self->len, &err);
97 self->len = 0;
98 if (out_sz == MP_STREAM_ERROR) {
99 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(err)));
100 }
101 }
102
103 return mp_const_none;
104}
105STATIC MP_DEFINE_CONST_FUN_OBJ_1(bufwriter_flush_obj, bufwriter_flush);
106
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200107STATIC const mp_map_elem_t bufwriter_locals_dict_table[] = {
108 { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
Paul Sokolovsky063e6e72016-03-25 14:33:38 +0200109 { MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&bufwriter_flush_obj },
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200110};
111STATIC MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
112
113STATIC const mp_stream_p_t bufwriter_stream_p = {
114 .write = bufwriter_write,
115};
116
117STATIC const mp_obj_type_t bufwriter_type = {
118 { &mp_type_type },
119 .name = MP_QSTR_BufferedWriter,
120 .make_new = bufwriter_make_new,
121 .stream_p = &bufwriter_stream_p,
122 .locals_dict = (mp_obj_t)&bufwriter_locals_dict,
123};
124#endif // MICROPY_PY_IO_BUFFEREDWRITER
125
Damien Georgecbf76742015-11-27 13:38:15 +0000126STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
127 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__io) },
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300128 // Note: mp_builtin_open_obj should be defined by port, it's not
129 // part of the core.
Damien Georgecbf76742015-11-27 13:38:15 +0000130 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
Damien Georgeee3fd462014-05-24 23:03:12 +0100131 #if MICROPY_PY_IO_FILEIO
Damien Georgecbf76742015-11-27 13:38:15 +0000132 { MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) },
Paul Sokolovsky9e296662014-05-19 20:59:13 +0300133 #if MICROPY_CPYTHON_COMPAT
Damien Georgecbf76742015-11-27 13:38:15 +0000134 { MP_ROM_QSTR(MP_QSTR_TextIOWrapper), MP_ROM_PTR(&mp_type_textio) },
Paul Sokolovsky9e296662014-05-19 20:59:13 +0300135 #endif
Damien Georgee5039c62015-02-15 13:17:11 +0000136 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000137 { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
Damien Georgeee3fd462014-05-24 23:03:12 +0100138 #if MICROPY_PY_IO_BYTESIO
Damien Georgecbf76742015-11-27 13:38:15 +0000139 { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300140 #endif
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200141 #if MICROPY_PY_IO_BUFFEREDWRITER
142 { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&bufwriter_type) },
143 #endif
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300144};
145
Damien George3b603f22014-11-29 14:39:27 +0000146STATIC MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300147
148const mp_obj_module_t mp_module_io = {
149 .base = { &mp_type_module },
Paul Sokolovskyfbdf2f12014-06-12 01:22:25 +0300150 .name = MP_QSTR__io,
Damien George8b0535e2014-04-05 21:53:54 +0100151 .globals = (mp_obj_dict_t*)&mp_module_io_globals,
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300152};
153
154#endif