lv_omega_fs.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include "lv_omega_fs.h"
  5. /**
  6. * Open a file from the PC
  7. * @param file_p pointer to a FILE* variable
  8. * @param fn absolute path to the file
  9. // * @param fn name of the file.
  10. * @param mode element of 'fs_mode_t' enum or its 'OR' connection (e.g. FS_MODE_WR | FS_MODE_RD)
  11. * @return LV_FS_RES_OK: no error, the file is opened
  12. * any error from lv_fs_res_t enum
  13. */
  14. /*static*/ lv_fs_res_t pcfs_open (void * file_p, const char * fn, lv_fs_mode_t mode)
  15. {
  16. errno = 0;
  17. const char * flags = "";
  18. if(mode == LV_FS_MODE_WR) flags = "wb";
  19. else if(mode == LV_FS_MODE_RD) flags = "rb";
  20. else if(mode == (LV_FS_MODE_WR | LV_FS_MODE_RD)) flags = "a+";
  21. char buf[256];
  22. // onion.io: set the path to the file based on whether the file is an absolute or relative path
  23. if (fn[0] == '/') {
  24. sprintf(buf, "%s", fn);
  25. }
  26. else {
  27. /*Make the path relative to the current directory (the projects root folder)*/
  28. sprintf(buf, "./%s", fn);
  29. }
  30. pc_file_t f = fopen(buf, flags);
  31. // pc_file_t f = fopen(fn, flags);
  32. if((long int)f <= 0) {
  33. // onion.io: tmp debug
  34. printf("pcfs_open: cannot open file '%s'\n", buf);
  35. return LV_FS_RES_UNKNOWN;
  36. }
  37. else {
  38. fseek(f, 0, SEEK_SET);
  39. /* 'file_p' is pointer to a file descriptor and
  40. * we need to store our file descriptor here*/
  41. pc_file_t *fp = file_p; /*Just avoid the confusing casings*/
  42. *fp = f;
  43. }
  44. return LV_FS_RES_OK;
  45. }
  46. /**
  47. * Close an opened file
  48. * @param file_p pointer to a FILE* variable. (opened with lv_ufs_open)
  49. * @return LV_FS_RES_OK: no error, the file is read
  50. * any error from lv__fs_res_t enum
  51. */
  52. /*static*/ lv_fs_res_t pcfs_close (void * file_p)
  53. {
  54. pc_file_t *fp = file_p; /*Just avoid the confusing casings*/
  55. fclose(*fp);
  56. return LV_FS_RES_OK;
  57. }
  58. /**
  59. * Read data from an opened file
  60. * @param file_p pointer to a FILE variable.
  61. * @param buf pointer to a memory block where to store the read data
  62. * @param btr number of Bytes To Read
  63. * @param br the real number of read bytes (Byte Read)
  64. * @return LV_FS_RES_OK: no error, the file is read
  65. * any error from lv__fs_res_t enum
  66. */
  67. /*static*/ lv_fs_res_t pcfs_read (void * file_p, void * buf, uint32_t btr, uint32_t * br)
  68. {
  69. pc_file_t *fp = file_p; /*Just avoid the confusing casings*/
  70. *br = fread(buf, 1, btr, *fp);
  71. return LV_FS_RES_OK;
  72. }
  73. /**
  74. * Set the read write pointer. Also expand the file size if necessary.
  75. * @param file_p pointer to a FILE* variable. (opened with lv_ufs_open )
  76. * @param pos the new position of read write pointer
  77. * @return LV_FS_RES_OK: no error, the file is read
  78. * any error from lv__fs_res_t enum
  79. */
  80. /*static*/ lv_fs_res_t pcfs_seek (void * file_p, uint32_t pos)
  81. {
  82. pc_file_t *fp = file_p; /*Just avoid the confusing casings*/
  83. fseek(*fp, pos, SEEK_SET);
  84. return LV_FS_RES_OK;
  85. }
  86. /**
  87. * Give the position of the read write pointer
  88. * @param file_p pointer to a FILE* variable.
  89. * @param pos_p pointer to to store the result
  90. * @return LV_FS_RES_OK: no error, the file is read
  91. * any error from lv__fs_res_t enum
  92. */
  93. /*static*/ lv_fs_res_t pcfs_tell (void * file_p, uint32_t * pos_p)
  94. {
  95. pc_file_t *fp = file_p; /*Just avoid the confusing casings*/
  96. *pos_p = ftell(*fp);
  97. return LV_FS_RES_OK;
  98. }