vFile.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. ******************************************************************************
  3. * @file vFile.c
  4. * @author eming
  5. * @version V1.0.0
  6. * @date 2022-03-21
  7. * @brief 虚拟文件接口,将保存在内部Flash的数据虚拟为文件
  8. ******************************************************************************
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "vFile.h"
  13. #include "user_interface.h"
  14. vFile *vfopen(const uint8_t *dp, uint32_t size)
  15. {
  16. vFile *fp = NULL;
  17. fp = bs_malloc(sizeof(vFile));
  18. if (fp != NULL)
  19. {
  20. fp->curptr = (uint8_t *)dp;
  21. fp->offset = 0;
  22. fp->size = size;
  23. }
  24. return (fp);
  25. }
  26. int vfread(vFile *fp, uint8_t *buff, int len)
  27. {
  28. if (fp != NULL)
  29. {
  30. if ((fp->offset + len) > fp->size)
  31. {
  32. len = fp->size - fp->offset;
  33. }
  34. memcpy(buff, fp->curptr + fp->offset, len);
  35. fp->offset += len;
  36. return (len);
  37. }
  38. return (0);
  39. }
  40. uint8_t *vfgetpos(vFile *fp, uint32_t *position)
  41. {
  42. if (fp != NULL)
  43. {
  44. *position = fp->offset;
  45. return (fp->curptr + fp->offset);
  46. }
  47. return (NULL);
  48. }
  49. int vfsetpos(vFile *fp, uint32_t position)
  50. {
  51. if (fp != NULL)
  52. {
  53. fp->offset = position;
  54. return (fp->offset);
  55. }
  56. return -1;
  57. }
  58. int vfclose(vFile *fp)
  59. {
  60. if (fp != NULL)
  61. {
  62. bs_free(fp);
  63. }
  64. return (0);
  65. }
  66. uint32_t vfgetlen(vFile *fp)
  67. {
  68. return (fp->size);
  69. }
  70. /*******************************************************************************************************
  71. ** End Of File
  72. ********************************************************************************************************/