blob: d41e9592bfda2ab599ca42b0edffcbbe5cd51d41 [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
72 memcpy(self->buf + self->len, buf, rem);
73 buf = (byte*)buf + rem;
74 size -= rem;
75 mp_uint_t out_sz = mp_stream_writeall(self->stream, self->buf, self->alloc, errcode);
76 if (out_sz == MP_STREAM_ERROR) {
77 return MP_STREAM_ERROR;
78 }
79 self->len = 0;
80 }
81
82 return org_size;
83}
84
Paul Sokolovsky063e6e72016-03-25 14:33:38 +020085STATIC mp_obj_t bufwriter_flush(mp_obj_t self_in) {
86 mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
87
88 if (self->len != 0) {
89 int err;
90 mp_uint_t out_sz = mp_stream_writeall(self->stream, self->buf, self->len, &err);
91 self->len = 0;
92 if (out_sz == MP_STREAM_ERROR) {
93 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(err)));
94 }
95 }
96
97 return mp_const_none;
98}
99STATIC MP_DEFINE_CONST_FUN_OBJ_1(bufwriter_flush_obj, bufwriter_flush);
100
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200101STATIC const mp_map_elem_t bufwriter_locals_dict_table[] = {
102 { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
Paul Sokolovsky063e6e72016-03-25 14:33:38 +0200103 { MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&bufwriter_flush_obj },
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200104};
105STATIC MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
106
107STATIC const mp_stream_p_t bufwriter_stream_p = {
108 .write = bufwriter_write,
109};
110
111STATIC const mp_obj_type_t bufwriter_type = {
112 { &mp_type_type },
113 .name = MP_QSTR_BufferedWriter,
114 .make_new = bufwriter_make_new,
115 .stream_p = &bufwriter_stream_p,
116 .locals_dict = (mp_obj_t)&bufwriter_locals_dict,
117};
118#endif // MICROPY_PY_IO_BUFFEREDWRITER
119
Damien Georgecbf76742015-11-27 13:38:15 +0000120STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
121 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__io) },
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300122 // Note: mp_builtin_open_obj should be defined by port, it's not
123 // part of the core.
Damien Georgecbf76742015-11-27 13:38:15 +0000124 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
Damien Georgeee3fd462014-05-24 23:03:12 +0100125 #if MICROPY_PY_IO_FILEIO
Damien Georgecbf76742015-11-27 13:38:15 +0000126 { MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) },
Paul Sokolovsky9e296662014-05-19 20:59:13 +0300127 #if MICROPY_CPYTHON_COMPAT
Damien Georgecbf76742015-11-27 13:38:15 +0000128 { MP_ROM_QSTR(MP_QSTR_TextIOWrapper), MP_ROM_PTR(&mp_type_textio) },
Paul Sokolovsky9e296662014-05-19 20:59:13 +0300129 #endif
Damien Georgee5039c62015-02-15 13:17:11 +0000130 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000131 { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
Damien Georgeee3fd462014-05-24 23:03:12 +0100132 #if MICROPY_PY_IO_BYTESIO
Damien Georgecbf76742015-11-27 13:38:15 +0000133 { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300134 #endif
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200135 #if MICROPY_PY_IO_BUFFEREDWRITER
136 { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&bufwriter_type) },
137 #endif
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300138};
139
Damien George3b603f22014-11-29 14:39:27 +0000140STATIC MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300141
142const mp_obj_module_t mp_module_io = {
143 .base = { &mp_type_module },
Paul Sokolovskyfbdf2f12014-06-12 01:22:25 +0300144 .name = MP_QSTR__io,
Damien George8b0535e2014-04-05 21:53:54 +0100145 .globals = (mp_obj_dict_t*)&mp_module_io_globals,
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300146};
147
148#endif