LCOV - code coverage report
Current view: top level - extmod - vfs_reader.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 29 30 96.7 %
Date: 2022-12-01 09:37:31 Functions: 3 3 100.0 %
Branches: 8 10 80.0 %

           Branch data     Line data    Source code
       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                 :            : 
      37                 :            : typedef 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                 :            : 
      44                 :    3836699 : STATIC mp_uint_t mp_reader_vfs_readbyte(void *data) {
      45                 :    3836699 :     mp_reader_vfs_t *reader = (mp_reader_vfs_t *)data;
      46         [ +  + ]:    3836699 :     if (reader->pos >= reader->len) {
      47         [ +  + ]:     164128 :         if (reader->len < sizeof(reader->buf)) {
      48                 :            :             return MP_READER_EOF;
      49                 :            :         } else {
      50                 :     157920 :             int errcode;
      51                 :     157920 :             reader->len = mp_stream_rw(reader->file, reader->buf, sizeof(reader->buf),
      52                 :            :                 &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
      53         [ +  - ]:     157920 :             if (errcode != 0) {
      54                 :            :                 // TODO handle errors properly
      55                 :        113 :                 return MP_READER_EOF;
      56                 :            :             }
      57         [ +  + ]:     157920 :             if (reader->len == 0) {
      58                 :            :                 return MP_READER_EOF;
      59                 :            :             }
      60                 :     157807 :             reader->pos = 0;
      61                 :            :         }
      62                 :            :     }
      63                 :    3830378 :     return reader->buf[reader->pos++];
      64                 :            : }
      65                 :            : 
      66                 :       3400 : STATIC void mp_reader_vfs_close(void *data) {
      67                 :       3400 :     mp_reader_vfs_t *reader = (mp_reader_vfs_t *)data;
      68                 :       3400 :     mp_stream_close(reader->file);
      69                 :       3400 :     m_del_obj(mp_reader_vfs_t, reader);
      70                 :       3400 : }
      71                 :            : 
      72                 :       3420 : void mp_reader_new_file(mp_reader_t *reader, const char *filename) {
      73                 :       3420 :     mp_reader_vfs_t *rf = m_new_obj(mp_reader_vfs_t);
      74                 :       6840 :     mp_obj_t args[2] = {
      75                 :       3420 :         mp_obj_new_str(filename, strlen(filename)),
      76                 :            :         MP_OBJ_NEW_QSTR(MP_QSTR_rb),
      77                 :            :     };
      78                 :       3420 :     rf->file = mp_vfs_open(MP_ARRAY_SIZE(args), &args[0], (mp_map_t *)&mp_const_empty_map);
      79                 :       3416 :     int errcode;
      80                 :       3416 :     rf->len = mp_stream_rw(rf->file, rf->buf, sizeof(rf->buf), &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
      81         [ -  + ]:       3416 :     if (errcode != 0) {
      82                 :          0 :         mp_raise_OSError(errcode);
      83                 :            :     }
      84                 :       3416 :     rf->pos = 0;
      85                 :       3416 :     reader->data = rf;
      86                 :       3416 :     reader->readbyte = mp_reader_vfs_readbyte;
      87                 :       3416 :     reader->close = mp_reader_vfs_close;
      88                 :       3416 : }
      89                 :            : 
      90                 :            : #endif // MICROPY_READER_VFS

Generated by: LCOV version 1.15-5-g462f71d