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.

ArduinoFiles.h 7.6KB

10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file
  22. * \brief PrintFile class
  23. */
  24. #ifndef ArduinoFiles_h
  25. #define ArduinoFiles_h
  26. #include "FatLibConfig.h"
  27. #if ENABLE_ARDUINO_FEATURES
  28. #include "FatFile.h"
  29. #include <limits.h>
  30. //------------------------------------------------------------------------------
  31. /** Arduino SD.h style flag for open for read. */
  32. #define FILE_READ O_READ
  33. /** Arduino SD.h style flag for open at EOF for read/write with create. */
  34. #define FILE_WRITE (O_RDWR | O_CREAT | O_AT_END)
  35. //==============================================================================
  36. /**
  37. * \class PrintFile
  38. * \brief FatFile with Print.
  39. */
  40. class PrintFile : public FatFile, public Print {
  41. public:
  42. PrintFile() {}
  43. /** Create a file object and open it in the current working directory.
  44. *
  45. * \param[in] path A path for a file to be opened.
  46. *
  47. * \param[in] oflag Values for \a oflag are constructed by a
  48. * bitwise-inclusive OR of open flags. see
  49. * FatFile::open(FatFile*, const char*, uint8_t).
  50. */
  51. PrintFile(const char* path, uint8_t oflag) : FatFile(path, oflag) {}
  52. #if DESTRUCTOR_CLOSES_FILE
  53. ~PrintFile() {}
  54. #endif // DESTRUCTOR_CLOSES_FILE
  55. using FatFile::clearWriteError;
  56. using FatFile::getWriteError;
  57. using FatFile::read;
  58. using FatFile::write;
  59. /** \return number of bytes available from the current position to EOF
  60. * or INT_MAX if more than INT_MAX bytes are available.
  61. */
  62. int available() {
  63. uint32_t n = FatFile::available();
  64. return n > INT_MAX ? INT_MAX : n;
  65. }
  66. /** Ensure that any bytes written to the file are saved to the SD card. */
  67. void flush() {
  68. FatFile::sync();
  69. }
  70. /** Return the next available byte without consuming it.
  71. *
  72. * \return The byte if no error and not at eof else -1;
  73. */
  74. int peek() {
  75. return FatFile::peek();
  76. }
  77. /** Read the next byte from a file.
  78. *
  79. * \return For success return the next byte in the file as an int.
  80. * If an error occurs or end of file is reached return -1.
  81. */
  82. // int read() {
  83. // return FatFile::read();
  84. // }
  85. /** Write a byte to a file. Required by the Arduino Print class.
  86. * \param[in] b the byte to be written.
  87. * Use getWriteError to check for errors.
  88. * \return 1 for success and 0 for failure.
  89. */
  90. size_t write(uint8_t b) {
  91. return FatFile::write(b);
  92. }
  93. /** Write data to an open file. Form required by Print.
  94. *
  95. * \note Data is moved to the cache but may not be written to the
  96. * storage device until sync() is called.
  97. *
  98. * \param[in] buf Pointer to the location of the data to be written.
  99. *
  100. * \param[in] size Number of bytes to write.
  101. *
  102. * \return For success write() returns the number of bytes written, always
  103. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  104. * include write() is called before a file has been opened, write is called
  105. * for a read-only file, device is full, a corrupt file system or an
  106. * I/O error.
  107. */
  108. size_t write(const uint8_t *buf, size_t size) {
  109. return FatFile::write(buf, size);
  110. }
  111. };
  112. //==============================================================================
  113. /**
  114. * \class File
  115. * \brief Arduino SD.h style File API
  116. */
  117. #if ARDUINO_FILE_USES_STREAM
  118. class File : public FatFile, public Stream {
  119. #else // ARDUINO_FILE_USES_STREAM
  120. class File : public FatFile, public Print {
  121. #endif // ARDUINO_FILE_USES_STREAM
  122. public:
  123. File() {}
  124. /** Create a file object and open it in the current working directory.
  125. *
  126. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  127. *
  128. * \param[in] oflag Values for \a oflag are constructed by a
  129. * bitwise-inclusive OR of open flags. see
  130. * FatFile::open(FatFile*, const char*, uint8_t).
  131. */
  132. File(const char* path, uint8_t oflag) {
  133. open(path, oflag);
  134. }
  135. using FatFile::clearWriteError;
  136. using FatFile::getWriteError;
  137. using FatFile::read;
  138. using FatFile::write;
  139. /** The parenthesis operator.
  140. *
  141. * \return true if a file is open.
  142. */
  143. operator bool() {
  144. return isOpen();
  145. }
  146. /** \return number of bytes available from the current position to EOF
  147. * or INT_MAX if more than INT_MAX bytes are available.
  148. */
  149. int available() {
  150. uint32_t n = FatFile::available();
  151. return n > INT_MAX ? INT_MAX : n;
  152. }
  153. /** Ensure that any bytes written to the file are saved to the SD card. */
  154. void flush() {
  155. FatFile::sync();
  156. }
  157. /** This function reports if the current file is a directory or not.
  158. * \return true if the file is a directory.
  159. */
  160. bool isDirectory() {
  161. return isDir();
  162. }
  163. /** No longer implemented due to Long File Names.
  164. *
  165. * Use getName(char* name, size_t size).
  166. * \return a pointer to replacement suggestion.
  167. */
  168. const char* name() const {
  169. return "use getName()";
  170. }
  171. /** Return the next available byte without consuming it.
  172. *
  173. * \return The byte if no error and not at eof else -1;
  174. */
  175. int peek() {
  176. return FatFile::peek();
  177. }
  178. /** \return the current file position. */
  179. uint32_t position() {
  180. return curPosition();
  181. }
  182. /** Opens the next file or folder in a directory.
  183. *
  184. * \param[in] mode open mode flags.
  185. * \return a File object.
  186. */
  187. File openNextFile(uint8_t mode = O_READ) {
  188. File tmpFile;
  189. tmpFile.openNext(this, mode);
  190. return tmpFile;
  191. }
  192. /** Read the next byte from a file.
  193. *
  194. * \return For success return the next byte in the file as an int.
  195. * If an error occurs or end of file is reached return -1.
  196. */
  197. int read() {
  198. return FatFile::read();
  199. }
  200. /** Rewind a file if it is a directory */
  201. void rewindDirectory() {
  202. if (isDir()) {
  203. rewind();
  204. }
  205. }
  206. /**
  207. * Seek to a new position in the file, which must be between
  208. * 0 and the size of the file (inclusive).
  209. *
  210. * \param[in] pos the new file position.
  211. * \return true for success else false.
  212. */
  213. bool seek(uint32_t pos) {
  214. return seekSet(pos);
  215. }
  216. /** \return the file's size. */
  217. uint32_t size() {
  218. return fileSize();
  219. }
  220. /** Write a byte to a file. Required by the Arduino Print class.
  221. * \param[in] b the byte to be written.
  222. * Use getWriteError to check for errors.
  223. * \return 1 for success and 0 for failure.
  224. */
  225. size_t write(uint8_t b) {
  226. return FatFile::write(b);
  227. }
  228. /** Write data to an open file. Form required by Print.
  229. *
  230. * \note Data is moved to the cache but may not be written to the
  231. * storage device until sync() is called.
  232. *
  233. * \param[in] buf Pointer to the location of the data to be written.
  234. *
  235. * \param[in] size Number of bytes to write.
  236. *
  237. * \return For success write() returns the number of bytes written, always
  238. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  239. * include write() is called before a file has been opened, write is called
  240. * for a read-only file, device is full, a corrupt file system or an
  241. * I/O error.
  242. */
  243. size_t write(const uint8_t *buf, size_t size) {
  244. return FatFile::write(buf, size);
  245. }
  246. };
  247. #endif // ENABLE_ARDUINO_FEATURES
  248. #endif // ArduinoFiles_h