29
29
30
30
#include "shared-bindings/dotenv/__init__.h"
31
31
32
- #include "extmod/vfs.h"
33
- #include "extmod/vfs_fat.h"
34
32
#include "py/mpstate.h"
35
33
#include "py/objstr.h"
36
34
#include "supervisor/filesystem.h"
37
35
36
+ #if defined(UNIX )
37
+ typedef FILE * file_arg ;
38
+ STATIC bool open_file (const char * name , file_arg * active_file ) {
39
+ FILE * result = fopen (name , "r" );
40
+ if (result ) {
41
+ * active_file = result ;
42
+ }
43
+ return result != NULL ;
44
+ }
45
+ STATIC void close_file (file_arg * active_file ) {
46
+ fclose (* active_file );
47
+ }
48
+ STATIC uint8_t get_next_character (file_arg * active_file ) {
49
+ int value = fgetc (* active_file );
50
+ if (value == EOF ) {
51
+ return 0 ;
52
+ }
53
+ return value ;
54
+ }
55
+ STATIC void seek_minus_one (file_arg * active_file ) {
56
+ fseek (* active_file , -1 , SEEK_CUR );
57
+ }
58
+ #else
59
+ #include "extmod/vfs.h"
60
+ #include "extmod/vfs_fat.h"
61
+ typedef FIL file_arg ;
62
+ STATIC bool open_file (const char * name , file_arg * active_file ) {
63
+ FATFS * fs = filesystem_circuitpy ();
64
+ FRESULT result = f_open (fs , active_file , name , FA_READ );
65
+ return result == FR_OK ;
66
+ }
67
+ STATIC void close_file (file_arg * active_file ) {
68
+ // nothing
69
+ }
70
+
38
71
// Return 0 if there is no next character (EOF).
39
72
STATIC uint8_t get_next_character (FIL * active_file ) {
40
73
uint8_t character = 0 ;
@@ -43,10 +76,14 @@ STATIC uint8_t get_next_character(FIL *active_file) {
43
76
f_read (active_file , & character , 1 , & quantity_read );
44
77
return character ;
45
78
}
79
+ STATIC void seek_minus_one (file_arg * active_file ) {
80
+ f_lseek (active_file , f_tell (active_file ) - 1 );
81
+ }
82
+ #endif
46
83
47
84
// Discard whitespace, except for newlines, returning the next character after the whitespace.
48
85
// Return 0 if there is no next character (EOF).
49
- STATIC uint8_t consume_whitespace (FIL * active_file ) {
86
+ STATIC uint8_t consume_whitespace (file_arg * active_file ) {
50
87
uint8_t character ;
51
88
do {
52
89
character = get_next_character (active_file );
@@ -56,7 +93,7 @@ STATIC uint8_t consume_whitespace(FIL *active_file) {
56
93
57
94
// Starting at the start of a new line, determines if the key matches the given
58
95
// key. File pointer is set to be just before the = after the key.
59
- STATIC bool key_matches (FIL * active_file , const char * key ) {
96
+ STATIC bool key_matches (file_arg * active_file , const char * key ) {
60
97
uint8_t character ;
61
98
character = consume_whitespace (active_file );
62
99
if (character == 0 ) {
@@ -99,7 +136,7 @@ STATIC bool key_matches(FIL *active_file, const char *key) {
99
136
}
100
137
if (character == '=' || character == '\n' || character == '#' || character == 0 ) {
101
138
// Rewind one so the value, if any, can be found.
102
- f_lseek (active_file , f_tell ( active_file ) - 1 );
139
+ seek_minus_one (active_file );
103
140
} else {
104
141
// We're followed by something else that is invalid syntax.
105
142
matches = false;
@@ -108,7 +145,7 @@ STATIC bool key_matches(FIL *active_file, const char *key) {
108
145
return matches && key_pos == key_len ;
109
146
}
110
147
111
- STATIC bool next_line (FIL * active_file ) {
148
+ STATIC bool next_line (file_arg * active_file ) {
112
149
uint8_t character ;
113
150
bool quoted = false;
114
151
bool escaped = false;
@@ -135,7 +172,7 @@ STATIC bool next_line(FIL *active_file) {
135
172
return character != 0 ;
136
173
}
137
174
138
- STATIC mp_int_t read_value (FIL * active_file , char * value , size_t value_len ) {
175
+ STATIC mp_int_t read_value (file_arg * active_file , char * value , size_t value_len ) {
139
176
uint8_t character ;
140
177
// Consume spaces before "=", and get first character of interest.
141
178
character = consume_whitespace (active_file );
@@ -184,7 +221,7 @@ STATIC mp_int_t read_value(FIL *active_file, char *value, size_t value_len) {
184
221
if (!quoted && (character == '\n' || (character == '#' && !first_char ))) {
185
222
if (character == '\n' ) {
186
223
// Rewind one so the next_line can find the \n.
187
- f_lseek (active_file , f_tell ( active_file ) - 1 );
224
+ seek_minus_one (active_file );
188
225
}
189
226
break ;
190
227
}
@@ -208,10 +245,8 @@ STATIC mp_int_t read_value(FIL *active_file, char *value, size_t value_len) {
208
245
}
209
246
210
247
mp_int_t dotenv_get_key (const char * path , const char * key , char * value , mp_int_t value_len ) {
211
- FIL active_file ;
212
- FATFS * fs = filesystem_circuitpy ();
213
- FRESULT result = f_open (fs , & active_file , path , FA_READ );
214
- if (result != FR_OK ) {
248
+ file_arg active_file ;
249
+ if (!open_file (path , & active_file )) {
215
250
return -1 ;
216
251
}
217
252
@@ -224,7 +259,7 @@ mp_int_t dotenv_get_key(const char *path, const char *key, char *value, mp_int_t
224
259
225
260
read_ok = next_line (& active_file );
226
261
}
227
- f_close (& active_file );
262
+ close_file (& active_file );
228
263
return actual_value_len ;
229
264
}
230
265
0 commit comments