Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

258 lines
9.1KB

  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 SdFile class
  23. */
  24. #ifndef SdFile_h
  25. #define SdFile_h
  26. #include <limits.h>
  27. #include <SdBaseFile.h>
  28. //------------------------------------------------------------------------------
  29. /**
  30. * \class SdFile
  31. * \brief SdBaseFile with Arduino Stream.
  32. */
  33. #if SD_FILE_USES_STREAM
  34. class SdFile : public SdBaseFile, public Stream {
  35. #else // SD_FILE_USES_STREAM
  36. class SdFile : public SdBaseFile, public Print {
  37. #endif // SD_FILE_USES_STREAM
  38. public:
  39. SdFile() {}
  40. SdFile(const char* name, uint8_t oflag);
  41. #if DESTRUCTOR_CLOSES_FILE
  42. ~SdFile() {}
  43. #endif // DESTRUCTOR_CLOSES_FILE
  44. /** \return number of bytes available from the current position to EOF
  45. * or INT_MAX if more than INT_MAX bytes are available.
  46. */
  47. int available() {
  48. uint32_t n = SdBaseFile::available();
  49. return n > INT_MAX ? INT_MAX : n;
  50. }
  51. /** Ensure that any bytes written to the file are saved to the SD card. */
  52. void flush() {SdBaseFile::sync();}
  53. /** Return the next available byte without consuming it.
  54. *
  55. * \return The byte if no error and not at eof else -1;
  56. */
  57. int peek() {return SdBaseFile::peek();}
  58. /** Read the next byte from a file.
  59. *
  60. * \return For success return the next byte in the file as an int.
  61. * If an error occurs or end of file is reached return -1.
  62. */
  63. int read() {return SdBaseFile::read();}
  64. /** Read data from a file starting at the current position.
  65. *
  66. * \param[out] buf Pointer to the location that will receive the data.
  67. *
  68. * \param[in] nbyte Maximum number of bytes to read.
  69. *
  70. * \return For success read() returns the number of bytes read.
  71. * A value less than \a nbyte, including zero, will be returned
  72. * if end of file is reached.
  73. * If an error occurs, read() returns -1. Possible errors include
  74. * read() called before a file has been opened, corrupt file system
  75. * or an I/O error occurred.
  76. */
  77. int read(void* buf, size_t nbyte) {return SdBaseFile::read(buf, nbyte);}
  78. /** \return value of writeError */
  79. bool getWriteError() {return SdBaseFile::getWriteError();}
  80. /** Set writeError to zero */
  81. void clearWriteError() {SdBaseFile::clearWriteError();}
  82. size_t write(uint8_t b);
  83. int write(const char* str);
  84. /** Write data to an open file.
  85. *
  86. * \note Data is moved to the cache but may not be written to the
  87. * storage device until sync() is called.
  88. *
  89. * \param[in] buf Pointer to the location of the data to be written.
  90. *
  91. * \param[in] nbyte Number of bytes to write.
  92. *
  93. * \return For success write() returns the number of bytes written, always
  94. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  95. * include write() is called before a file has been opened, write is called
  96. * for a read-only file, device is full, a corrupt file system or an
  97. * I/O error.
  98. */
  99. int write(const void* buf, size_t nbyte);
  100. /** Write data to an open file. Form required by Print.
  101. *
  102. * \note Data is moved to the cache but may not be written to the
  103. * storage device until sync() is called.
  104. *
  105. * \param[in] buf Pointer to the location of the data to be written.
  106. *
  107. * \param[in] size Number of bytes to write.
  108. *
  109. * \return For success write() returns the number of bytes written, always
  110. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  111. * include write() is called before a file has been opened, write is called
  112. * for a read-only file, device is full, a corrupt file system or an
  113. * I/O error.
  114. */
  115. size_t write(const uint8_t *buf, size_t size) {
  116. return SdBaseFile::write(buf, size);}
  117. void write_P(PGM_P str);
  118. void writeln_P(PGM_P str);
  119. };
  120. //------------------------------------------------------------------------------
  121. /** Arduino SD.h style flag for open for read. */
  122. #define FILE_READ O_READ
  123. /** Arduino SD.h style flag for open at EOF for read/write with create. */
  124. #define FILE_WRITE (O_RDWR | O_CREAT | O_AT_END)
  125. /**
  126. * \class File
  127. * \brief Arduino SD.h style File API
  128. */
  129. class File : public SdBaseFile, public Stream {
  130. public:
  131. /** The parenthesis operator.
  132. *
  133. * \return true if a file is open.
  134. */
  135. operator bool() {return isOpen();}
  136. /** \return number of bytes available from the current position to EOF
  137. * or INT_MAX if more than INT_MAX bytes are available.
  138. */
  139. int available() {
  140. uint32_t n = SdBaseFile::available();
  141. return n > INT_MAX ? INT_MAX : n;
  142. }
  143. /** Set writeError to zero */
  144. void clearWriteError() {SdBaseFile::clearWriteError();}
  145. /** Ensure that any bytes written to the file are saved to the SD card. */
  146. void flush() {sync();}
  147. /** \return value of writeError */
  148. bool getWriteError() {return SdBaseFile::getWriteError();}
  149. /** This function reports if the current file is a directory or not.
  150. * \return true if the file is a directory.
  151. */
  152. bool isDirectory() {return isDir();}
  153. /** \return a pointer to the file's name. */
  154. char* name() {
  155. m_name[0] = 0;
  156. getFilename(m_name);
  157. return m_name;
  158. }
  159. /** Return the next available byte without consuming it.
  160. *
  161. * \return The byte if no error and not at eof else -1;
  162. */
  163. int peek() {return SdBaseFile::peek();}
  164. /** \return the current file position. */
  165. uint32_t position() {return curPosition();}
  166. /** Opens the next file or folder in a directory.
  167. *
  168. * \param[in] mode open mode flags.
  169. * \return a File object.
  170. */
  171. File openNextFile(uint8_t mode = O_READ) {
  172. File tmpFile;
  173. tmpFile.openNext(this, mode);
  174. return tmpFile;
  175. }
  176. /** Read the next byte from a file.
  177. *
  178. * \return For success return the next byte in the file as an int.
  179. * If an error occurs or end of file is reached return -1.
  180. */
  181. int read() {return SdBaseFile::read();}
  182. /** Read data from a file starting at the current position.
  183. *
  184. * \param[out] buf Pointer to the location that will receive the data.
  185. *
  186. * \param[in] nbyte Maximum number of bytes to read.
  187. *
  188. * \return For success read() returns the number of bytes read.
  189. * A value less than \a nbyte, including zero, will be returned
  190. * if end of file is reached.
  191. * If an error occurs, read() returns -1. Possible errors include
  192. * read() called before a file has been opened, corrupt file system
  193. * or an I/O error occurred.
  194. */
  195. int read(void* buf, size_t nbyte) {return SdBaseFile::read(buf, nbyte);}
  196. /** Rewind a file if it is a directory */
  197. void rewindDirectory() {
  198. if (isDir()) rewind();
  199. }
  200. /**
  201. * Seek to a new position in the file, which must be between
  202. * 0 and the size of the file (inclusive).
  203. *
  204. * \param[in] pos the new file position.
  205. * \return true for success else false.
  206. */
  207. bool seek(uint32_t pos) {return seekSet(pos);}
  208. /** \return the file's size. */
  209. uint32_t size() {return fileSize();}
  210. /** Write a byte to a file. Required by the Arduino Print class.
  211. * \param[in] b the byte to be written.
  212. * Use getWriteError to check for errors.
  213. * \return 1 for success and 0 for failure.
  214. */
  215. size_t write(uint8_t b) {
  216. return SdBaseFile::write(&b, 1) == 1 ? 1 : 0;
  217. }
  218. /** Write data to an open file.
  219. *
  220. * \note Data is moved to the cache but may not be written to the
  221. * storage device until sync() is called.
  222. *
  223. * \param[in] buf Pointer to the location of the data to be written.
  224. *
  225. * \param[in] nbyte Number of bytes to write.
  226. *
  227. * \return For success write() returns the number of bytes written, always
  228. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  229. * include write() is called before a file has been opened, write is called
  230. * for a read-only file, device is full, a corrupt file system or an
  231. * I/O error.
  232. */
  233. int write(const void* buf, size_t nbyte);
  234. /** Write data to an open file. Form required by Print.
  235. *
  236. * \note Data is moved to the cache but may not be written to the
  237. * storage device until sync() is called.
  238. *
  239. * \param[in] buf Pointer to the location of the data to be written.
  240. *
  241. * \param[in] size Number of bytes to write.
  242. *
  243. * \return For success write() returns the number of bytes written, always
  244. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  245. * include write() is called before a file has been opened, write is called
  246. * for a read-only file, device is full, a corrupt file system or an
  247. * I/O error.
  248. */
  249. size_t write(const uint8_t *buf, size_t size) {
  250. return SdBaseFile::write(buf, size);
  251. }
  252. private:
  253. char m_name[13];
  254. };
  255. #endif // SdFile_h