blob: 9d669f2bfe686fcda9d014dc3e158f948f71725c [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 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
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010027#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030028#include "misc.h"
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010029
Damien George58ebde42014-05-21 20:32:59 +010030#if MICROPY_HELPER_LEXER_UNIX
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010031
Damiena5185f42013-10-20 14:41:27 +010032#include <stdio.h>
33#include <unistd.h>
34#include <fcntl.h>
Damien Georgeb5021562014-04-13 13:25:05 +010035#include <sys/stat.h>
36#include <sys/types.h>
Damiena5185f42013-10-20 14:41:27 +010037
Damien George55baff42014-01-21 21:40:13 +000038#include "qstr.h"
Damiena5185f42013-10-20 14:41:27 +010039#include "lexer.h"
Damien George9193f892014-01-08 15:28:26 +000040#include "lexerunix.h"
Damiena5185f42013-10-20 14:41:27 +010041
Damien Georgeffa9bdd2014-04-10 15:00:58 +010042typedef struct _mp_lexer_file_buf_t {
43 int fd;
Damien George94fbe972014-07-30 11:46:05 +010044 byte buf[20];
45 mp_uint_t len;
46 mp_uint_t pos;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010047} mp_lexer_file_buf_t;
Damiena5185f42013-10-20 14:41:27 +010048
Damien George94fbe972014-07-30 11:46:05 +010049STATIC mp_uint_t file_buf_next_byte(mp_lexer_file_buf_t *fb) {
Damien Georgeffa9bdd2014-04-10 15:00:58 +010050 if (fb->pos >= fb->len) {
Damien George185f9c12014-04-28 11:43:28 +010051 if (fb->len == 0) {
Damien George94fbe972014-07-30 11:46:05 +010052 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010053 } else {
54 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
55 if (n <= 0) {
Damien George185f9c12014-04-28 11:43:28 +010056 fb->len = 0;
Damien George94fbe972014-07-30 11:46:05 +010057 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010058 }
59 fb->len = n;
60 fb->pos = 0;
61 }
62 }
63 return fb->buf[fb->pos++];
64}
65
66STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
67 close(fb->fd);
68 m_del_obj(mp_lexer_file_buf_t, fb);
69}
70
71mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
72 mp_lexer_file_buf_t *fb = m_new_obj(mp_lexer_file_buf_t);
73 fb->fd = open(filename, O_RDONLY);
74 if (fb->fd < 0) {
75 m_del_obj(mp_lexer_file_buf_t, fb);
76 return NULL;
77 }
78 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
79 fb->len = n;
80 fb->pos = 0;
Damien George94fbe972014-07-30 11:46:05 +010081 return mp_lexer_new(qstr_from_str(filename), fb, (mp_lexer_stream_next_byte_t)file_buf_next_byte, (mp_lexer_stream_close_t)file_buf_close);
Damiena5185f42013-10-20 14:41:27 +010082}
Damien George66028ab2014-01-03 14:03:48 +000083
Damien George58ebde42014-05-21 20:32:59 +010084#endif // MICROPY_HELPER_LEXER_UNIX