LCOV - code coverage report
Current view: top level - py - reader.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 52 59 88.1 %
Date: 2022-12-01 09:37:31 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                 :     166571 : STATIC mp_uint_t mp_reader_mem_readbyte(void *data) {
      43                 :     166571 :     mp_reader_mem_t *reader = (mp_reader_mem_t *)data;
      44         [ +  + ]:     166571 :     if (reader->cur < reader->end) {
      45                 :     160390 :         return *reader->cur++;
      46                 :            :     } else {
      47                 :            :         return MP_READER_EOF;
      48                 :            :     }
      49                 :            : }
      50                 :            : 
      51                 :       1468 : STATIC void mp_reader_mem_close(void *data) {
      52                 :       1468 :     mp_reader_mem_t *reader = (mp_reader_mem_t *)data;
      53         [ -  + ]:       1468 :     if (reader->free_len > 0) {
      54                 :          0 :         m_del(char, (char *)reader->beg, reader->free_len);
      55                 :            :     }
      56                 :       1468 :     m_del_obj(mp_reader_mem_t, reader);
      57                 :       1468 : }
      58                 :            : 
      59                 :       1616 : void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len) {
      60                 :       1616 :     mp_reader_mem_t *rm = m_new_obj(mp_reader_mem_t);
      61                 :       1616 :     rm->free_len = free_len;
      62                 :       1616 :     rm->beg = buf;
      63                 :       1616 :     rm->cur = buf;
      64                 :       1616 :     rm->end = buf + len;
      65                 :       1616 :     reader->data = rm;
      66                 :       1616 :     reader->readbyte = mp_reader_mem_readbyte;
      67                 :       1616 :     reader->close = mp_reader_mem_close;
      68                 :       1616 : }
      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, const char *filename) {
     138                 :            :     MP_THREAD_GIL_EXIT();
     139                 :            :     int fd = open(filename, O_RDONLY, 0644);
     140                 :            :     MP_THREAD_GIL_ENTER();
     141                 :            :     if (fd < 0) {
     142                 :            :         mp_raise_OSError_with_filename(errno, 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