Преглед изворни кода

make file IO support more CPython like

Oliver Fabel пре 1 година
родитељ
комит
9da779cc69
3 измењених фајлова са 31 додато и 15 уклоњено
  1. 2 3
      CHANGELOG.md
  2. 1 1
      lib/micropython
  3. 28 11
      lib/micropython-port/mp_flipper_fileio.c

+ 2 - 3
CHANGELOG.md

@@ -10,9 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 * Support for basic file system operations using the `io` module:
-  * Read a file.
-  * Write a file.
-  * Append to a file.
+  * Read and write files.
+  * Open in text or binary mode. 
 
 ## [1.4.0] - 2024-09-29
 

+ 1 - 1
lib/micropython

@@ -1 +1 @@
-Subproject commit 5ec95678f441f564e5bc1897f6b106d955e0a05d
+Subproject commit a92199d3c33b322edc7c98c21af594b43cb6d8d8

+ 28 - 11
lib/micropython-port/mp_flipper_fileio.c

@@ -7,11 +7,16 @@
 #include "mp_flipper_context.h"
 #include "mp_flipper_file_helper.h"
 
-inline void* mp_flipper_file_open(
-    const char* name,
-    mp_flipper_file_access_mode_t access_mode,
-    mp_flipper_file_open_mode_t open_mode,
-    size_t* offset) {
+uint8_t MP_FLIPPER_FILE_ACCESS_MODE_READ = FSAM_READ;
+uint8_t MP_FLIPPER_FILE_ACCESS_MODE_WRITE = FSAM_WRITE;
+
+uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_EXIST = FSOM_OPEN_EXISTING;
+uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_ALWAYS = FSOM_OPEN_ALWAYS;
+uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_APPEND = FSOM_OPEN_APPEND;
+uint8_t MP_FLIPPER_FILE_OPEN_MODE_CREATE_NEW = FSOM_CREATE_NEW;
+uint8_t MP_FLIPPER_FILE_OPEN_MODE_CREATE_ALWAYS = FSOM_CREATE_ALWAYS;
+
+inline void* mp_flipper_file_open(const char* name, uint8_t access_mode, uint8_t open_mode) {
     mp_flipper_context_t* ctx = mp_flipper_context;
 
     File* file = storage_file_alloc(ctx->storage);
@@ -36,7 +41,7 @@ inline void* mp_flipper_file_open(
     return file;
 }
 
-inline int mp_flipper_file_close(void* handle) {
+inline bool mp_flipper_file_close(void* handle) {
     mp_flipper_context_t* ctx = mp_flipper_context;
 
     File* file = handle;
@@ -49,15 +54,27 @@ inline int mp_flipper_file_close(void* handle) {
 
     storage_file_free(file);
 
-    return 0;
+    return true;
 }
 
-inline bool mp_flipper_file_writable(void* handle) {
-    File* file = handle;
+inline size_t mp_flipper_file_seek(void* handle, uint32_t offset) {
+    return storage_file_seek(handle, offset, true);
+}
 
-    // TODO
+inline size_t mp_flipper_file_tell(void* handle) {
+    return storage_file_tell(handle);
+}
 
-    return true;
+inline size_t mp_flipper_file_size(void* handle) {
+    return storage_file_size(handle);
+}
+
+inline bool mp_flipper_file_sync(void* handle) {
+    return storage_file_sync(handle);
+}
+
+inline bool mp_flipper_file_eof(void* handle) {
+    return storage_file_eof(handle);
 }
 
 inline size_t mp_flipper_file_read(void* handle, void* buffer, size_t size, int* errcode) {