mp_flipper_fileio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include "mp_flipper_fileio.h"
  4. #include "py/obj.h"
  5. #include "py/stream.h"
  6. #include "py/runtime.h"
  7. extern const mp_obj_type_t mp_flipper_fileio_type;
  8. typedef struct _mp_flipper_fileio_file_descriptor_t {
  9. mp_obj_base_t base;
  10. void* name;
  11. void* handle;
  12. size_t offset;
  13. mp_flipper_file_access_mode_t access_mode;
  14. mp_flipper_file_open_mode_t open_mode;
  15. } mp_flipper_fileio_file_descriptor_t;
  16. void* mp_flipper_file_new_file_descriptor(
  17. void* name,
  18. void* handle,
  19. size_t offset,
  20. mp_flipper_file_access_mode_t access_mode,
  21. mp_flipper_file_open_mode_t open_mode) {
  22. mp_flipper_fileio_file_descriptor_t* fd = mp_obj_malloc(mp_flipper_fileio_file_descriptor_t, &mp_flipper_fileio_type);
  23. fd->name = name;
  24. fd->handle = handle;
  25. fd->offset = offset;
  26. fd->access_mode = access_mode;
  27. fd->open_mode = open_mode;
  28. return fd;
  29. }
  30. static mp_uint_t mp_flipper_fileio_read(mp_obj_t self, void* buf, mp_uint_t size, int* errcode) {
  31. mp_flipper_fileio_file_descriptor_t* fd = (mp_flipper_fileio_file_descriptor_t*)self;
  32. return mp_flipper_file_read(fd->handle, buf, size, errcode);
  33. }
  34. static mp_uint_t mp_flipper_fileio_write(mp_obj_t self, const void* buf, mp_uint_t size, int* errcode) {
  35. mp_flipper_fileio_file_descriptor_t* fd = (mp_flipper_fileio_file_descriptor_t*)self;
  36. return mp_flipper_file_write(fd->handle, buf, size, errcode);
  37. }
  38. static mp_obj_t mp_flipper_fileio_close(mp_obj_t self) {
  39. mp_flipper_fileio_file_descriptor_t* fd = (mp_flipper_fileio_file_descriptor_t*)self;
  40. int errorno = mp_flipper_file_close(fd->handle, fd->name);
  41. if(errorno != 0) {
  42. mp_raise_OSError(errorno);
  43. }
  44. return mp_const_none;
  45. }
  46. static MP_DEFINE_CONST_FUN_OBJ_1(mp_flipper_fileio_close_obj, mp_flipper_fileio_close);
  47. static mp_obj_t mp_flipper_fileio_name(mp_obj_t self) {
  48. mp_flipper_fileio_file_descriptor_t* fd = (mp_flipper_fileio_file_descriptor_t*)self;
  49. size_t size = 0;
  50. char* name = mp_flipper_file_name(fd->name, &size);
  51. return mp_obj_new_str(name, size);
  52. }
  53. static MP_DEFINE_CONST_FUN_OBJ_1(mp_flipper_fileio_name_obj, mp_flipper_fileio_name);
  54. static mp_obj_t mp_flipper_fileio___exit___(size_t n_args, const mp_obj_t* args) {
  55. return mp_flipper_fileio_close(args[0]);
  56. }
  57. static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_flipper_fileio___exit___obj, 4, 4, mp_flipper_fileio___exit___);
  58. static mp_obj_t mp_flipper_fileio_writable(mp_obj_t self) {
  59. return mp_flipper_file_writable((void*)self) ? mp_const_true : mp_const_false;
  60. }
  61. static MP_DEFINE_CONST_FUN_OBJ_1(mp_flipper_fileio_writable_obj, mp_flipper_fileio_writable);
  62. static const mp_map_elem_t mp_flipper_file_locals_dict_table[] = {
  63. {MP_OBJ_NEW_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_flipper_fileio_close_obj)},
  64. {MP_OBJ_NEW_QSTR(MP_QSTR_name), MP_ROM_PTR(&mp_flipper_fileio_name_obj)},
  65. {MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj)},
  66. {MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_flipper_fileio___exit___obj)},
  67. {MP_OBJ_NEW_QSTR(MP_QSTR_writable), MP_ROM_PTR(&mp_flipper_fileio_writable_obj)},
  68. {MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj)},
  69. {MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj)},
  70. {MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
  71. {MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj)},
  72. };
  73. static MP_DEFINE_CONST_DICT(mp_flipper_file_locals_dict, mp_flipper_file_locals_dict_table);
  74. static const mp_stream_p_t mp_flipper_fileio_stream_p = {
  75. .read = mp_flipper_fileio_read,
  76. .write = mp_flipper_fileio_write,
  77. };
  78. MP_DEFINE_CONST_OBJ_TYPE(
  79. mp_flipper_fileio_type,
  80. MP_QSTR_FileIO,
  81. MP_TYPE_FLAG_NONE,
  82. protocol,
  83. &mp_flipper_fileio_stream_p,
  84. locals_dict,
  85. &mp_flipper_file_locals_dict);