LCOV - code coverage report
Current view: top level - py - reader.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-7-g548babf8a.info Lines: 52 59 88.1 %
Date: 2024-10-30 09:06:48 Functions: 6 6 100.0 %
Branches: 11 16 68.8 %

           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-2016 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 <assert.h>
      29                 :            : 
      30                 :            : #include "py/runtime.h"
      31                 :            : #include "py/mperrno.h"
      32                 :            : #include "py/mpthread.h"
      33                 :            : #include "py/reader.h"
      34                 :            : 
      35                 :            : typedef struct _mp_reader_mem_t {
      36                 :            :     size_t free_len; // if >0 mem is freed on close by: m_free(beg, free_len)
      37                 :            :     const byte *beg;
      38                 :            :     const byte *cur;
      39                 :            :     const byte *end;
      40                 :            : } mp_reader_mem_t;
      41                 :            : 
      42                 :     168793 : static mp_uint_t mp_reader_mem_readbyte(void *data) {
      43                 :     168793 :     mp_reader_mem_t *reader = (mp_reader_mem_t *)data;
      44         [ +  + ]:     168793 :     if (reader->cur < reader->end) {
      45                 :     162189 :         return *reader->cur++;
      46                 :            :     } else {
      47                 :            :         return MP_READER_EOF;
      48                 :            :     }
      49                 :            : }
      50                 :            : 
      51                 :       1718 : static void mp_reader_mem_close(void *data) {
      52                 :       1718 :     mp_reader_mem_t *reader = (mp_reader_mem_t *)data;
      53         [ -  + ]:       1718 :     if (reader->free_len > 0) {
      54                 :          0 :         m_del(char, (char *)reader->beg, reader->free_len);
      55                 :            :     }
      56                 :       1718 :     m_del_obj(mp_reader_mem_t, reader);
      57                 :       1718 : }
      58                 :            : 
      59                 :       1724 : void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len) {
      60                 :       1724 :     mp_reader_mem_t *rm = m_new_obj(mp_reader_mem_t);
      61                 :       1724 :     rm->free_len = free_len;
      62                 :       1724 :     rm->beg = buf;
      63                 :       1724 :     rm->cur = buf;
      64                 :       1724 :     rm->end = buf + len;
      65                 :       1724 :     reader->data = rm;
      66                 :       1724 :     reader->readbyte = mp_reader_mem_readbyte;
      67                 :       1724 :     reader->close = mp_reader_mem_close;
      68                 :       1724 : }
      69                 :            : 
      70                 :            : #if MICROPY_READER_POSIX
      71                 :            : 
      72                 :            : #include <sys/stat.h>
      73                 :            : #include <fcntl.h>
      74                 :            : #include <unistd.h>
      75                 :            : 
      76                 :            : typedef struct _mp_reader_posix_t {
      77                 :            :     bool close_fd;
      78                 :            :     int fd;
      79                 :            :     size_t len;
      80                 :            :     size_t pos;
      81                 :            :     byte buf[20];
      82                 :            : } mp_reader_posix_t;
      83                 :            : 
      84                 :         81 : static mp_uint_t mp_reader_posix_readbyte(void *data) {
      85                 :         81 :     mp_reader_posix_t *reader = (mp_reader_posix_t *)data;
      86         [ +  + ]:         81 :     if (reader->pos >= reader->len) {
      87         [ +  + ]:          6 :         if (reader->len == 0) {
      88                 :            :             return MP_READER_EOF;
      89                 :            :         } else {
      90                 :          4 :             MP_THREAD_GIL_EXIT();
      91                 :          4 :             int n = read(reader->fd, reader->buf, sizeof(reader->buf));
      92                 :          4 :             MP_THREAD_GIL_ENTER();
      93         [ +  + ]:          4 :             if (n <= 0) {
      94                 :          1 :                 reader->len = 0;
      95                 :          1 :                 return MP_READER_EOF;
      96                 :            :             }
      97                 :          3 :             reader->len = n;
      98                 :          3 :             reader->pos = 0;
      99                 :            :         }
     100                 :            :     }
     101                 :         78 :     return reader->buf[reader->pos++];
     102                 :            : }
     103                 :            : 
     104                 :          1 : static void mp_reader_posix_close(void *data) {
     105                 :          1 :     mp_reader_posix_t *reader = (mp_reader_posix_t *)data;
     106         [ -  + ]:          1 :     if (reader->close_fd) {
     107                 :          0 :         MP_THREAD_GIL_EXIT();
     108                 :          0 :         close(reader->fd);
     109                 :          1 :         MP_THREAD_GIL_ENTER();
     110                 :            :     }
     111                 :          1 :     m_del_obj(mp_reader_posix_t, reader);
     112                 :          1 : }
     113                 :            : 
     114                 :          1 : void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) {
     115                 :          1 :     mp_reader_posix_t *rp = m_new_obj(mp_reader_posix_t);
     116                 :          1 :     rp->close_fd = close_fd;
     117                 :          1 :     rp->fd = fd;
     118                 :          1 :     MP_THREAD_GIL_EXIT();
     119                 :          1 :     int n = read(rp->fd, rp->buf, sizeof(rp->buf));
     120         [ -  + ]:          1 :     if (n == -1) {
     121         [ #  # ]:          0 :         if (close_fd) {
     122                 :          0 :             close(fd);
     123                 :            :         }
     124                 :          0 :         MP_THREAD_GIL_ENTER();
     125                 :          0 :         mp_raise_OSError(errno);
     126                 :            :     }
     127                 :          1 :     MP_THREAD_GIL_ENTER();
     128                 :          1 :     rp->len = n;
     129                 :          1 :     rp->pos = 0;
     130                 :          1 :     reader->data = rp;
     131                 :          1 :     reader->readbyte = mp_reader_posix_readbyte;
     132                 :          1 :     reader->close = mp_reader_posix_close;
     133                 :          1 : }
     134                 :            : 
     135                 :            : #if !MICROPY_VFS_POSIX
     136                 :            : // If MICROPY_VFS_POSIX is defined then this function is provided by the VFS layer
     137                 :            : void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
     138                 :            :     MP_THREAD_GIL_EXIT();
     139                 :            :     int fd = open(qstr_str(filename), O_RDONLY, 0644);
     140                 :            :     MP_THREAD_GIL_ENTER();
     141                 :            :     if (fd < 0) {
     142                 :            :         mp_raise_OSError_with_filename(errno, qstr_str(filename));
     143                 :            :     }
     144                 :            :     mp_reader_new_file_from_fd(reader, fd, true);
     145                 :            : }
     146                 :            : #endif
     147                 :            : 
     148                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d