You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.0KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // FatFS wrappers.
  19. #ifndef AVRLIBX_FILESYSTEM_FILE_H_
  20. #define AVRLIBX_FILESYSTEM_FILE_H_
  21. #include <string.h>
  22. #include "avrlibx/avrlibx.h"
  23. #include "avrlibx/filesystem/filesystem.h"
  24. namespace avrlibx {
  25. enum FileAttributes {
  26. FS_READ = FA_READ,
  27. FS_WRITE = FA_WRITE,
  28. FS_OPEN_EXISTING = FA_OPEN_EXISTING,
  29. FS_OPEN_ALWAYS = FA_OPEN_ALWAYS,
  30. FS_CREATE_NEW = FA_CREATE_NEW,
  31. FS_CREATE_ALWAYS = FA_CREATE_ALWAYS
  32. };
  33. class File {
  34. public:
  35. File();
  36. ~File();
  37. FilesystemStatus Open(const char* file_name, const char* mode) {
  38. return Open(file_name, mode, 0);
  39. }
  40. FilesystemStatus Open(const char* file_name, uint8_t attributes) {
  41. return Open(file_name, attributes, 0);
  42. }
  43. FilesystemStatus Open(
  44. const char* file_name,
  45. const char* mode,
  46. uint16_t retry_timeout);
  47. FilesystemStatus Open(
  48. const char* file_name,
  49. uint8_t attributes,
  50. uint16_t retry_timeout);
  51. FilesystemStatus Seek(uint32_t position);
  52. FilesystemStatus Close();
  53. FilesystemStatus Truncate();
  54. FilesystemStatus Sync();
  55. FilesystemStatus Read(uint8_t* data, uint16_t size, uint16_t* read);
  56. FilesystemStatus Write(
  57. const uint8_t* data,
  58. uint16_t size,
  59. uint16_t* written);
  60. FilesystemStatus Write(
  61. const char* data,
  62. uint16_t size,
  63. uint16_t* written) {
  64. return Write(static_cast<const uint8_t*>(static_cast<const void*>(
  65. data)), size, written);
  66. }
  67. FilesystemStatus Read(
  68. char* data,
  69. uint16_t size,
  70. uint16_t* read) {
  71. return Read(static_cast<uint8_t*>(static_cast<void*>(
  72. data)), size, read);
  73. }
  74. uint16_t Read(uint8_t* data, uint16_t size) {
  75. uint16_t read;
  76. return Read(data, size, &read) == FS_OK ? read : 0;
  77. }
  78. uint16_t Write(const uint8_t* data, uint16_t size) {
  79. uint16_t written;
  80. return Write(data, size, &written) == FS_OK ? written : 0;
  81. }
  82. uint8_t eof() const { return f_eof(&f_); }
  83. uint8_t error() const { return f_error(&f_); }
  84. uint32_t tell() const { return f_tell(&f_); }
  85. uint32_t size() const { return f_size(&f_); }
  86. private:
  87. uint8_t opened_;
  88. FIL f_;
  89. DISALLOW_COPY_AND_ASSIGN(File);
  90. };
  91. } // namespace avrlibx
  92. #endif // AVRLIBX_FILESYSTEM_FILE_H_