Skip to content

Commit 453a2a3

Browse files
committed
extmod/vfs_fat: Add lexer, move from stmhal port for reuse.
1 parent 701c415 commit 453a2a3

3 files changed

Lines changed: 82 additions & 46 deletions

File tree

extmod/vfs_fat_lexer.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
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+
27+
#include <stdio.h>
28+
29+
#include "py/lexer.h"
30+
#include "lib/fatfs/ff.h"
31+
32+
typedef struct _mp_lexer_file_buf_t {
33+
FIL fp;
34+
byte buf[20];
35+
uint16_t len;
36+
uint16_t pos;
37+
} mp_lexer_file_buf_t;
38+
39+
STATIC mp_uint_t file_buf_next_byte(mp_lexer_file_buf_t *fb) {
40+
if (fb->pos >= fb->len) {
41+
if (fb->len < sizeof(fb->buf)) {
42+
return MP_LEXER_EOF;
43+
} else {
44+
UINT n;
45+
f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n);
46+
if (n == 0) {
47+
return MP_LEXER_EOF;
48+
}
49+
fb->len = n;
50+
fb->pos = 0;
51+
}
52+
}
53+
return fb->buf[fb->pos++];
54+
}
55+
56+
STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
57+
f_close(&fb->fp);
58+
m_del_obj(mp_lexer_file_buf_t, fb);
59+
}
60+
61+
mp_lexer_t *fat_vfs_lexer_new_from_file(const char *filename) {
62+
mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t);
63+
if (fb == NULL) {
64+
return NULL;
65+
}
66+
FRESULT res = f_open(&fb->fp, filename, FA_READ);
67+
if (res != FR_OK) {
68+
m_del_obj(mp_lexer_file_buf_t, fb);
69+
return NULL;
70+
}
71+
UINT n;
72+
f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n);
73+
fb->len = n;
74+
fb->pos = 0;
75+
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);
76+
}

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ PY_O_BASENAME = \
174174
../extmod/vfs_fat_ffconf.o \
175175
../extmod/vfs_fat_diskio.o \
176176
../extmod/vfs_fat_file.o \
177+
../extmod/vfs_fat_lexer.o \
177178
../extmod/vfs_fat_misc.o \
178179
../extmod/moduos_dupterm.o \
179180

stmhal/lexerfatfs.c

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of the Micro Python project, http://micropython.org/
2+
* This file is part of the MicroPython project, http://micropython.org/
33
*
44
* The MIT License (MIT)
55
*
@@ -24,53 +24,12 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <stdio.h>
28-
2927
#include "py/lexer.h"
30-
#include "lib/fatfs/ff.h"
31-
32-
typedef struct _mp_lexer_file_buf_t {
33-
FIL fp;
34-
byte buf[20];
35-
uint16_t len;
36-
uint16_t pos;
37-
} mp_lexer_file_buf_t;
3828

39-
STATIC mp_uint_t file_buf_next_byte(mp_lexer_file_buf_t *fb) {
40-
if (fb->pos >= fb->len) {
41-
if (fb->len < sizeof(fb->buf)) {
42-
return MP_LEXER_EOF;
43-
} else {
44-
UINT n;
45-
f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n);
46-
if (n == 0) {
47-
return MP_LEXER_EOF;
48-
}
49-
fb->len = n;
50-
fb->pos = 0;
51-
}
52-
}
53-
return fb->buf[fb->pos++];
54-
}
55-
56-
STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
57-
f_close(&fb->fp);
58-
m_del_obj(mp_lexer_file_buf_t, fb);
59-
}
29+
mp_lexer_t *fat_vfs_lexer_new_from_file(const char *filename);
6030

31+
// TODO: Instead of such shims, probably better to let port #define
32+
// mp_lexer_new_from_file to a function it wants to use.
6133
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
62-
mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t);
63-
if (fb == NULL) {
64-
return NULL;
65-
}
66-
FRESULT res = f_open(&fb->fp, filename, FA_READ);
67-
if (res != FR_OK) {
68-
m_del_obj(mp_lexer_file_buf_t, fb);
69-
return NULL;
70-
}
71-
UINT n;
72-
f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n);
73-
fb->len = n;
74-
fb->pos = 0;
75-
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);
34+
return fat_vfs_lexer_new_from_file(filename);
7635
}

0 commit comments

Comments
 (0)