blob: 04d3d79af3ad18ba6a4db27eaf3c3d677bceb163 [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
85STATIC const mp_map_elem_t bufwriter_locals_dict_table[] = {
86 { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
87};
88STATIC MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
89
90STATIC const mp_stream_p_t bufwriter_stream_p = {
91 .write = bufwriter_write,
92};
93
94STATIC const mp_obj_type_t bufwriter_type = {
95 { &mp_type_type },
96 .name = MP_QSTR_BufferedWriter,
97 .make_new = bufwriter_make_new,
98 .stream_p = &bufwriter_stream_p,
99 .locals_dict = (mp_obj_t)&bufwriter_locals_dict,
100};
101#endif // MICROPY_PY_IO_BUFFEREDWRITER
102
Damien Georgecbf76742015-11-27 13:38:15 +0000103STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
104 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__io) },
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300105 // Note: mp_builtin_open_obj should be defined by port, it's not
106 // part of the core.
Damien Georgecbf76742015-11-27 13:38:15 +0000107 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
Damien Georgeee3fd462014-05-24 23:03:12 +0100108 #if MICROPY_PY_IO_FILEIO
Damien Georgecbf76742015-11-27 13:38:15 +0000109 { MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) },
Paul Sokolovsky9e296662014-05-19 20:59:13 +0300110 #if MICROPY_CPYTHON_COMPAT
Damien Georgecbf76742015-11-27 13:38:15 +0000111 { MP_ROM_QSTR(MP_QSTR_TextIOWrapper), MP_ROM_PTR(&mp_type_textio) },
Paul Sokolovsky9e296662014-05-19 20:59:13 +0300112 #endif
Damien Georgee5039c62015-02-15 13:17:11 +0000113 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000114 { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
Damien Georgeee3fd462014-05-24 23:03:12 +0100115 #if MICROPY_PY_IO_BYTESIO
Damien Georgecbf76742015-11-27 13:38:15 +0000116 { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300117 #endif
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200118 #if MICROPY_PY_IO_BUFFEREDWRITER
119 { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&bufwriter_type) },
120 #endif
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300121};
122
Damien George3b603f22014-11-29 14:39:27 +0000123STATIC MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300124
125const mp_obj_module_t mp_module_io = {
126 .base = { &mp_type_module },
Paul Sokolovskyfbdf2f12014-06-12 01:22:25 +0300127 .name = MP_QSTR__io,
Damien George8b0535e2014-04-05 21:53:54 +0100128 .globals = (mp_obj_dict_t*)&mp_module_io_globals,
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300129};
130
131#endif