1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013-2017 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
27#include <stdio.h>
28#include <string.h>
29
30#include "py/runtime.h"
31#include "py/stream.h"
32#include "py/reader.h"
33#include "extmod/vfs.h"
34
35#if MICROPY_READER_VFS
36
37typedef struct _mp_reader_vfs_t {
38 mp_obj_t file;
39 uint16_t len;
40 uint16_t pos;
41 byte buf[24];
42} mp_reader_vfs_t;
43
44STATIC mp_uint_t mp_reader_vfs_readbyte(void *data) {
45 mp_reader_vfs_t *reader = (mp_reader_vfs_t *)data;
46 if (reader->pos >= reader->len) {
47 if (reader->len < sizeof(reader->buf)) {
48 return MP_READER_EOF;
49 } else {
50 int errcode;
51 reader->len = mp_stream_rw(reader->file, reader->buf, sizeof(reader->buf),
52 &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
53 if (errcode != 0) {
54 // TODO handle errors properly
55 return MP_READER_EOF;
56 }
57 if (reader->len == 0) {
58 return MP_READER_EOF;
59 }
60 reader->pos = 0;
61 }
62 }
63 return reader->buf[reader->pos++];
64}
65
66STATIC void mp_reader_vfs_close(void *data) {
67 mp_reader_vfs_t *reader = (mp_reader_vfs_t *)data;
68 mp_stream_close(reader->file);
69 m_del_obj(mp_reader_vfs_t, reader);
70}
71
72void mp_reader_new_file(mp_reader_t *reader, const char *filename) {
73 mp_reader_vfs_t *rf = m_new_obj(mp_reader_vfs_t);
74 mp_obj_t args[2] = {
75 mp_obj_new_str(filename, strlen(filename)),
76 MP_OBJ_NEW_QSTR(MP_QSTR_rb),
77 };
78 rf->file = mp_vfs_open(MP_ARRAY_SIZE(args), &args[0], (mp_map_t *)&mp_const_empty_map);
79 int errcode;
80 rf->len = mp_stream_rw(rf->file, rf->buf, sizeof(rf->buf), &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
81 if (errcode != 0) {
82 mp_raise_OSError(errcode);
83 }
84 rf->pos = 0;
85 reader->data = rf;
86 reader->readbyte = mp_reader_vfs_readbyte;
87 reader->close = mp_reader_vfs_close;
88}
89
90#endif // MICROPY_READER_VFS
91