mp_flipper_fileio.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <mp_flipper_fileio.h>
  4. #include <mp_flipper_runtime.h>
  5. #include "mp_flipper_context.h"
  6. #include "mp_flipper_file_helper.h"
  7. uint8_t MP_FLIPPER_FILE_ACCESS_MODE_READ = FSAM_READ;
  8. uint8_t MP_FLIPPER_FILE_ACCESS_MODE_WRITE = FSAM_WRITE;
  9. uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_EXIST = FSOM_OPEN_EXISTING;
  10. uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_ALWAYS = FSOM_OPEN_ALWAYS;
  11. uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_APPEND = FSOM_OPEN_APPEND;
  12. uint8_t MP_FLIPPER_FILE_OPEN_MODE_CREATE_NEW = FSOM_CREATE_NEW;
  13. uint8_t MP_FLIPPER_FILE_OPEN_MODE_CREATE_ALWAYS = FSOM_CREATE_ALWAYS;
  14. inline void* mp_flipper_file_open(const char* name, uint8_t access_mode, uint8_t open_mode) {
  15. mp_flipper_context_t* ctx = mp_flipper_context;
  16. File* file = storage_file_alloc(ctx->storage);
  17. FuriString* path = furi_string_alloc_set_str(name);
  18. do {
  19. if(mp_flipper_try_resolve_filesystem_path(path) == MP_FLIPPER_IMPORT_STAT_NO_EXIST) {
  20. break;
  21. }
  22. if(!storage_file_open(file, furi_string_get_cstr(path), access_mode, open_mode)) {
  23. break;
  24. }
  25. } while(false);
  26. if(!storage_file_is_open(file)) {
  27. storage_file_close(file);
  28. storage_file_free(file);
  29. return NULL;
  30. }
  31. return file;
  32. }
  33. inline bool mp_flipper_file_close(void* handle) {
  34. File* file = handle;
  35. bool success = storage_file_is_open(file) && storage_file_close(file);
  36. storage_file_free(file);
  37. return success;
  38. }
  39. inline size_t mp_flipper_file_seek(void* handle, uint32_t offset) {
  40. return storage_file_seek(handle, offset, true);
  41. }
  42. inline size_t mp_flipper_file_tell(void* handle) {
  43. return storage_file_tell(handle);
  44. }
  45. inline size_t mp_flipper_file_size(void* handle) {
  46. return storage_file_size(handle);
  47. }
  48. inline bool mp_flipper_file_sync(void* handle) {
  49. return storage_file_sync(handle);
  50. }
  51. inline bool mp_flipper_file_eof(void* handle) {
  52. return storage_file_eof(handle);
  53. }
  54. inline size_t mp_flipper_file_read(void* handle, void* buffer, size_t size, int* errcode) {
  55. File* file = handle;
  56. *errcode = 0; // TODO handle error
  57. return storage_file_read(file, buffer, size);
  58. }
  59. inline size_t mp_flipper_file_write(void* handle, const void* buffer, size_t size, int* errcode) {
  60. File* file = handle;
  61. *errcode = 0; // TODO handle error
  62. return storage_file_write(file, buffer, size);
  63. }