|
13 | 13 | #include <sys/stat.h> |
14 | 14 | #include <sys/types.h> |
15 | 15 |
|
16 | | -mp_lexer_t *mp_lexer_new_from_file(const char *filename) { |
17 | | - int fd = open(filename, O_RDONLY); |
18 | | - if (fd < 0) { |
19 | | - return NULL; |
| 16 | +typedef struct _mp_lexer_file_buf_t { |
| 17 | + int fd; |
| 18 | + char buf[20]; |
| 19 | + uint len; |
| 20 | + uint pos; |
| 21 | +} mp_lexer_file_buf_t; |
| 22 | + |
| 23 | +STATIC unichar file_buf_next_char(mp_lexer_file_buf_t *fb) { |
| 24 | + if (fb->pos >= fb->len) { |
| 25 | + if (fb->len < sizeof(fb->buf)) { |
| 26 | + return MP_LEXER_CHAR_EOF; |
| 27 | + } else { |
| 28 | + int n = read(fb->fd, fb->buf, sizeof(fb->buf)); |
| 29 | + if (n <= 0) { |
| 30 | + return MP_LEXER_CHAR_EOF; |
| 31 | + } |
| 32 | + fb->len = n; |
| 33 | + fb->pos = 0; |
| 34 | + } |
20 | 35 | } |
21 | | - uint size = lseek(fd, 0, SEEK_END); |
22 | | - lseek(fd, 0, SEEK_SET); |
23 | | - char *data = m_new(char, size); |
24 | | - int read_size = read(fd, data, size); |
25 | | - close(fd); |
26 | | - if (read_size != size) { |
27 | | - printf("error reading file %s\n", filename); |
28 | | - m_del(char, data, size); |
| 36 | + return fb->buf[fb->pos++]; |
| 37 | +} |
| 38 | + |
| 39 | +STATIC void file_buf_close(mp_lexer_file_buf_t *fb) { |
| 40 | + close(fb->fd); |
| 41 | + m_del_obj(mp_lexer_file_buf_t, fb); |
| 42 | +} |
| 43 | + |
| 44 | +mp_lexer_t *mp_lexer_new_from_file(const char *filename) { |
| 45 | + mp_lexer_file_buf_t *fb = m_new_obj(mp_lexer_file_buf_t); |
| 46 | + fb->fd = open(filename, O_RDONLY); |
| 47 | + if (fb->fd < 0) { |
| 48 | + m_del_obj(mp_lexer_file_buf_t, fb); |
29 | 49 | return NULL; |
30 | 50 | } |
31 | | - |
32 | | - return mp_lexer_new_from_str_len(qstr_from_str(filename), data, size, size); |
| 51 | + int n = read(fb->fd, fb->buf, sizeof(fb->buf)); |
| 52 | + fb->len = n; |
| 53 | + fb->pos = 0; |
| 54 | + return mp_lexer_new(qstr_from_str(filename), fb, (mp_lexer_stream_next_char_t)file_buf_next_char, (mp_lexer_stream_close_t)file_buf_close); |
33 | 55 | } |
34 | 56 |
|
35 | 57 | #endif // MICROPY_ENABLE_LEXER_UNIX |
0 commit comments