Numworks Epsilon  1.4.1
Graphing Calculator Operating System
reader.c
Go to the documentation of this file.
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/reader.h"
33 
34 typedef struct _mp_reader_mem_t {
35  size_t free_len; // if >0 mem is freed on close by: m_free(beg, free_len)
36  const byte *beg;
37  const byte *cur;
38  const byte *end;
40 
43  if (reader->cur < reader->end) {
44  return *reader->cur++;
45  } else {
46  return MP_READER_EOF;
47  }
48 }
49 
52  if (reader->free_len > 0) {
53  m_del(char, (char*)reader->beg, reader->free_len);
54  }
55  m_del_obj(mp_reader_mem_t, reader);
56 }
57 
58 void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len) {
60  rm->free_len = free_len;
61  rm->beg = buf;
62  rm->cur = buf;
63  rm->end = buf + len;
64  reader->data = rm;
66  reader->close = mp_reader_mem_close;
67 }
68 
69 #if MICROPY_READER_POSIX
70 
71 #include <sys/stat.h>
72 #include <fcntl.h>
73 #include <unistd.h>
74 
75 typedef struct _mp_reader_posix_t {
76  bool close_fd;
77  int fd;
78  size_t len;
79  size_t pos;
80  byte buf[20];
81 } mp_reader_posix_t;
82 
83 STATIC mp_uint_t mp_reader_posix_readbyte(void *data) {
84  mp_reader_posix_t *reader = (mp_reader_posix_t*)data;
85  if (reader->pos >= reader->len) {
86  if (reader->len == 0) {
87  return MP_READER_EOF;
88  } else {
89  int n = read(reader->fd, reader->buf, sizeof(reader->buf));
90  if (n <= 0) {
91  reader->len = 0;
92  return MP_READER_EOF;
93  }
94  reader->len = n;
95  reader->pos = 0;
96  }
97  }
98  return reader->buf[reader->pos++];
99 }
100 
101 STATIC void mp_reader_posix_close(void *data) {
102  mp_reader_posix_t *reader = (mp_reader_posix_t*)data;
103  if (reader->close_fd) {
104  close(reader->fd);
105  }
106  m_del_obj(mp_reader_posix_t, reader);
107 }
108 
109 void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) {
110  mp_reader_posix_t *rp = m_new_obj(mp_reader_posix_t);
111  rp->close_fd = close_fd;
112  rp->fd = fd;
113  int n = read(rp->fd, rp->buf, sizeof(rp->buf));
114  if (n == -1) {
115  if (close_fd) {
116  close(fd);
117  }
119  }
120  rp->len = n;
121  rp->pos = 0;
122  reader->data = rp;
123  reader->readbyte = mp_reader_posix_readbyte;
124  reader->close = mp_reader_posix_close;
125 }
126 
127 void mp_reader_new_file(mp_reader_t *reader, const char *filename) {
128  int fd = open(filename, O_RDONLY, 0644);
129  if (fd < 0) {
131  }
132  mp_reader_new_file_from_fd(reader, fd, true);
133 }
134 
135 #endif
int errno
Definition: errno.c:3
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
const byte * beg
Definition: reader.c:36
struct _mp_reader_mem_t mp_reader_mem_t
STATIC mp_uint_t mp_reader_mem_readbyte(void *data)
Definition: reader.c:41
def data
Definition: i18n.py:176
void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd)
size_t free_len
Definition: reader.c:35
#define m_del(type, ptr, num)
Definition: misc.h:77
const byte * end
Definition: reader.c:38
void mp_reader_new_file(mp_reader_t *reader, const char *filename)
#define STATIC
Definition: mpconfig.h:1178
const byte * cur
Definition: reader.c:37
#define m_del_obj(type, ptr)
Definition: misc.h:80
NORETURN void mp_raise_OSError(int errno_)
Definition: runtime.c:1464
#define MP_READER_EOF
Definition: reader.h:34
unsigned char byte
Definition: misc.h:37
void(* close)(void *data)
Definition: reader.h:39
void * data
Definition: reader.h:37
mp_uint_t(* readbyte)(void *data)
Definition: reader.h:38
void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len)
Definition: reader.c:58
#define m_new_obj(type)
Definition: misc.h:60
STATIC void mp_reader_mem_close(void *data)
Definition: reader.c:50