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.

SdFile.h 9.6KB

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