Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

313 lines
9.2KB

  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 "utility/FatLib.h"
  28. //------------------------------------------------------------------------------
  29. /** Arduino SD.h style flag for open for read. */
  30. #define FILE_READ O_READ
  31. /** Arduino SD.h style flag for open at EOF for read/write with create. */
  32. #define FILE_WRITE (O_RDWR | O_CREAT | O_AT_END)
  33. //==============================================================================
  34. /**
  35. * \class SdBaseFile
  36. * \brief SdBaseFile base for SdFile and File.
  37. */
  38. class SdBaseFile : public FatFile {
  39. public:
  40. SdBaseFile() {}
  41. /** Constructor with file open.
  42. *
  43. * \param[in] path File location and name.
  44. * \param[in] oflag File open mode.
  45. */
  46. SdBaseFile(const char* path, uint8_t oflag) {
  47. open(path, oflag);
  48. }
  49. using FatFile::ls;
  50. using FatFile::printFatDate;
  51. using FatFile::printFatTime;
  52. using FatFile::printName;
  53. /** List directory contents.
  54. *
  55. * \param[in] flags The inclusive OR of
  56. *
  57. * LS_DATE - %Print file modification date
  58. *
  59. * LS_SIZE - %Print file size.
  60. *
  61. * LS_R - Recursive list of subdirectories.
  62. */
  63. void ls(uint8_t flags = 0) {
  64. ls(&Serial, flags);
  65. }
  66. /** %Print a directory date field.
  67. *
  68. * Format is yyyy-mm-dd.
  69. *
  70. * \param[in] fatDate The date field from a directory entry.
  71. */
  72. static void printFatDate(uint16_t fatDate) {
  73. printFatDate(&Serial, fatDate);
  74. }
  75. /** %Print a directory time field.
  76. *
  77. * Format is hh:mm:ss.
  78. *
  79. * \param[in] fatTime The time field from a directory entry.
  80. */
  81. static void printFatTime(uint16_t fatTime) {
  82. printFatTime(&Serial, fatTime);
  83. }
  84. /** Print a file's name.
  85. *
  86. * \return The value true is returned for success and
  87. * the value false is returned for failure.
  88. */
  89. size_t printName() {
  90. return FatFile::printName(&Serial);
  91. }
  92. };
  93. //==============================================================================
  94. /**
  95. * \class SdFile
  96. * \brief SdFile SdBaseFile with Print.
  97. */
  98. #if SD_FILE_USES_STREAM
  99. class SdFile : public SdBaseFile, public Stream {
  100. #else // SD_FILE_USES_STREAM
  101. class SdFile : public SdBaseFile, public Print {
  102. #endif // SD_FILE_USES_STREAM
  103. public:
  104. SdFile() {}
  105. /** Create a file object and open it in the current working directory.
  106. *
  107. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  108. *
  109. * \param[in] oflag Values for \a oflag are constructed by a
  110. * bitwise-inclusive OR of open flags. see
  111. * SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
  112. */
  113. SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) {}
  114. #if DESTRUCTOR_CLOSES_FILE
  115. ~SdFile() {}
  116. #endif // DESTRUCTOR_CLOSES_FILE
  117. using SdBaseFile::clearWriteError;
  118. using SdBaseFile::getWriteError;
  119. using SdBaseFile::read;
  120. using SdBaseFile::write;
  121. /** \return number of bytes available from the current position to EOF
  122. * or INT_MAX if more than INT_MAX bytes are available.
  123. */
  124. int available() {
  125. uint32_t n = SdBaseFile::available();
  126. return n > INT_MAX ? INT_MAX : n;
  127. }
  128. /** Ensure that any bytes written to the file are saved to the SD card. */
  129. void flush() {
  130. SdBaseFile::sync();
  131. }
  132. /** Return the next available byte without consuming it.
  133. *
  134. * \return The byte if no error and not at eof else -1;
  135. */
  136. int peek() {
  137. return SdBaseFile::peek();
  138. }
  139. /** Read the next byte from a file.
  140. *
  141. * \return For success return the next byte in the file as an int.
  142. * If an error occurs or end of file is reached return -1.
  143. */
  144. int read() {
  145. return SdBaseFile::read();
  146. }
  147. /** Write a byte to a file. Required by the Arduino Print class.
  148. * \param[in] b the byte to be written.
  149. * Use getWriteError to check for errors.
  150. * \return 1 for success and 0 for failure.
  151. */
  152. size_t write(uint8_t b) {
  153. return SdBaseFile::write(b);
  154. }
  155. /** Write a string to a file. Used by the Arduino Print class.
  156. * \param[in] str Pointer to the string.
  157. * Use getWriteError to check for errors.
  158. * \return count of characters written for success or -1 for failure.
  159. */
  160. int write(const char* str) {
  161. return SdBaseFile::write(str, strlen(str));
  162. }
  163. /** Write data to an open file. Form required by Print.
  164. *
  165. * \note Data is moved to the cache but may not be written to the
  166. * storage device until sync() is called.
  167. *
  168. * \param[in] buf Pointer to the location of the data to be written.
  169. *
  170. * \param[in] size Number of bytes to write.
  171. *
  172. * \return For success write() returns the number of bytes written, always
  173. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  174. * include write() is called before a file has been opened, write is called
  175. * for a read-only file, device is full, a corrupt file system or an
  176. * I/O error.
  177. */
  178. size_t write(const uint8_t *buf, size_t size) {
  179. return SdBaseFile::write(buf, size);
  180. }
  181. };
  182. //==============================================================================
  183. /**
  184. * \class File
  185. * \brief Arduino SD.h style File API
  186. */
  187. class File : public SdBaseFile, public Stream {
  188. public:
  189. File() {}
  190. /** Create a file object and open it in the current working directory.
  191. *
  192. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  193. *
  194. * \param[in] oflag Values for \a oflag are constructed by a
  195. * bitwise-inclusive OR of open flags. see
  196. * SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
  197. */
  198. File(const char* path, uint8_t oflag) {
  199. open(path, oflag);
  200. }
  201. using SdBaseFile::clearWriteError;
  202. using SdBaseFile::getWriteError;
  203. /** The parenthesis operator.
  204. *
  205. * \return true if a file is open.
  206. */
  207. operator bool() {
  208. return isOpen();
  209. }
  210. /** \return number of bytes available from the current position to EOF
  211. * or INT_MAX if more than INT_MAX bytes are available.
  212. */
  213. int available() {
  214. uint32_t n = SdBaseFile::available();
  215. return n > INT_MAX ? INT_MAX : n;
  216. }
  217. /** Ensure that any bytes written to the file are saved to the SD card. */
  218. void flush() {
  219. SdBaseFile::sync();
  220. }
  221. /** This function reports if the current file is a directory or not.
  222. * \return true if the file is a directory.
  223. */
  224. bool isDirectory() {
  225. return isDir();
  226. }
  227. /** \return a pointer to the file's short name. */
  228. char* name() {
  229. m_name[0] = 0;
  230. getSFN(m_name);
  231. return m_name;
  232. }
  233. /** Return the next available byte without consuming it.
  234. *
  235. * \return The byte if no error and not at eof else -1;
  236. */
  237. int peek() {
  238. return SdBaseFile::peek();
  239. }
  240. /** \return the current file position. */
  241. uint32_t position() {
  242. return curPosition();
  243. }
  244. /** Opens the next file or folder in a directory.
  245. *
  246. * \param[in] mode open mode flags.
  247. * \return a File object.
  248. */
  249. File openNextFile(uint8_t mode = O_READ) {
  250. File tmpFile;
  251. tmpFile.openNext(this, mode);
  252. return tmpFile;
  253. }
  254. /** Read the next byte from a file.
  255. *
  256. * \return For success return the next byte in the file as an int.
  257. * If an error occurs or end of file is reached return -1.
  258. */
  259. int read() {
  260. return SdBaseFile::read();
  261. }
  262. /** Rewind a file if it is a directory */
  263. void rewindDirectory() {
  264. if (isDir()) {
  265. rewind();
  266. }
  267. }
  268. /**
  269. * Seek to a new position in the file, which must be between
  270. * 0 and the size of the file (inclusive).
  271. *
  272. * \param[in] pos the new file position.
  273. * \return true for success else false.
  274. */
  275. bool seek(uint32_t pos) {
  276. return seekSet(pos);
  277. }
  278. /** \return the file's size. */
  279. uint32_t size() {
  280. return fileSize();
  281. }
  282. /** Write a byte to a file. Required by the Arduino Print class.
  283. * \param[in] b the byte to be written.
  284. * Use getWriteError to check for errors.
  285. * \return 1 for success and 0 for failure.
  286. */
  287. size_t write(uint8_t b) {
  288. return SdBaseFile::write(b);
  289. }
  290. /** Write data to an open file. Form required by Print.
  291. *
  292. * \note Data is moved to the cache but may not be written to the
  293. * storage device until sync() is called.
  294. *
  295. * \param[in] buf Pointer to the location of the data to be written.
  296. *
  297. * \param[in] size Number of bytes to write.
  298. *
  299. * \return For success write() returns the number of bytes written, always
  300. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  301. * include write() is called before a file has been opened, write is called
  302. * for a read-only file, device is full, a corrupt file system or an
  303. * I/O error.
  304. */
  305. size_t write(const uint8_t *buf, size_t size) {
  306. return SdBaseFile::write(buf, size);
  307. }
  308. private:
  309. char m_name[13];
  310. };
  311. #endif // SdFile_h