vfs_reader.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/stream.h"
  30. #include "py/reader.h"
  31. #include "extmod/vfs.h"
  32. #if MICROPY_READER_VFS
  33. typedef struct _mp_reader_vfs_t {
  34. mp_obj_t file;
  35. uint16_t len;
  36. uint16_t pos;
  37. byte buf[24];
  38. } mp_reader_vfs_t;
  39. STATIC mp_uint_t mp_reader_vfs_readbyte(void *data) {
  40. mp_reader_vfs_t *reader = (mp_reader_vfs_t*)data;
  41. if (reader->pos >= reader->len) {
  42. if (reader->len < sizeof(reader->buf)) {
  43. return MP_READER_EOF;
  44. } else {
  45. int errcode;
  46. reader->len = mp_stream_rw(reader->file, reader->buf, sizeof(reader->buf),
  47. &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
  48. if (errcode != 0) {
  49. // TODO handle errors properly
  50. return MP_READER_EOF;
  51. }
  52. if (reader->len == 0) {
  53. return MP_READER_EOF;
  54. }
  55. reader->pos = 0;
  56. }
  57. }
  58. return reader->buf[reader->pos++];
  59. }
  60. STATIC void mp_reader_vfs_close(void *data) {
  61. mp_reader_vfs_t *reader = (mp_reader_vfs_t*)data;
  62. mp_stream_close(reader->file);
  63. m_del_obj(mp_reader_vfs_t, reader);
  64. }
  65. void mp_reader_new_file(mp_reader_t *reader, const char *filename) {
  66. mp_reader_vfs_t *rf = m_new_obj(mp_reader_vfs_t);
  67. mp_obj_t arg = mp_obj_new_str(filename, strlen(filename));
  68. rf->file = mp_vfs_open(1, &arg, (mp_map_t*)&mp_const_empty_map);
  69. int errcode;
  70. rf->len = mp_stream_rw(rf->file, rf->buf, sizeof(rf->buf), &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
  71. if (errcode != 0) {
  72. mp_raise_OSError(errcode);
  73. }
  74. rf->pos = 0;
  75. reader->data = rf;
  76. reader->readbyte = mp_reader_vfs_readbyte;
  77. reader->close = mp_reader_vfs_close;
  78. }
  79. #endif // MICROPY_READER_VFS