Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifndef SdBaseFile_h
  21. #define SdBaseFile_h
  22. /**
  23. * \file
  24. * \brief SdBaseFile class
  25. */
  26. #ifdef __AVR__
  27. #include <avr/pgmspace.h>
  28. #else // __AVR__
  29. #ifndef PGM_P
  30. /** pointer to flash for ARM */
  31. #define PGM_P const char*
  32. #endif // PGM_P
  33. #ifndef PSTR
  34. /** store literal string in flash for ARM */
  35. #define PSTR(x) (x)
  36. #endif // PSTR
  37. #ifndef pgm_read_byte
  38. /** read 8-bits from flash for ARM */
  39. #define pgm_read_byte(addr) (*(const unsigned char*)(addr))
  40. #endif // pgm_read_byte
  41. #ifndef pgm_read_word
  42. /** read 16-bits from flash for ARM */
  43. #define pgm_read_word(addr) (*(const uint16_t*)(addr))
  44. #endif // pgm_read_word
  45. #ifndef PROGMEM
  46. /** store in flash for ARM */
  47. #define PROGMEM const
  48. #endif // PROGMEM
  49. #endif // __AVR__
  50. #include <Arduino.h>
  51. #include <SdFatConfig.h>
  52. #include <SdVolume.h>
  53. #include <utility/FatApiConstants.h>
  54. //------------------------------------------------------------------------------
  55. /**
  56. * \struct FatPos_t
  57. * \brief internal type for istream
  58. * do not use in user apps
  59. */
  60. struct FatPos_t {
  61. /** stream position */
  62. uint32_t position;
  63. /** cluster for position */
  64. uint32_t cluster;
  65. FatPos_t() : position(0), cluster(0) {}
  66. };
  67. // values for m_type
  68. /** This file has not been opened. */
  69. uint8_t const FAT_FILE_TYPE_CLOSED = 0;
  70. /** A normal file */
  71. uint8_t const FAT_FILE_TYPE_NORMAL = 1;
  72. /** A FAT12 or FAT16 root directory */
  73. uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
  74. /** A FAT32 root directory */
  75. uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
  76. /** A subdirectory file*/
  77. uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
  78. /** Test value for directory type */
  79. uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
  80. //------------------------------------------------------------------------------
  81. /**
  82. * \class SdBaseFile
  83. * \brief Base class for SdFile with Print and C++ streams.
  84. */
  85. class SdBaseFile {
  86. public:
  87. /** Create an instance. */
  88. SdBaseFile() : writeError(false), m_type(FAT_FILE_TYPE_CLOSED) {}
  89. SdBaseFile(const char* path, uint8_t oflag);
  90. #if DESTRUCTOR_CLOSES_FILE
  91. ~SdBaseFile() {if(isOpen()) close();}
  92. #endif // DESTRUCTOR_CLOSES_FILE
  93. /**
  94. * writeError is set to true if an error occurs during a write().
  95. * Set writeError to false before calling print() and/or write() and check
  96. * for true after calls to print() and/or write().
  97. */
  98. bool writeError;
  99. /** \return value of writeError */
  100. bool getWriteError() {return writeError;}
  101. /** Set writeError to zero */
  102. void clearWriteError() {writeError = 0;}
  103. //----------------------------------------------------------------------------
  104. // helpers for stream classes
  105. /** get position for streams
  106. * \param[out] pos struct to receive position
  107. */
  108. void getpos(FatPos_t* pos);
  109. /** set position for streams
  110. * \param[out] pos struct with value for new position
  111. */
  112. void setpos(FatPos_t* pos);
  113. //----------------------------------------------------------------------------
  114. /** \return number of bytes available from yhe current position to EOF */
  115. uint32_t available() {return fileSize() - curPosition();}
  116. bool close();
  117. bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
  118. bool createContiguous(SdBaseFile* dirFile,
  119. const char* path, uint32_t size);
  120. /** \return The current cluster number for a file or directory. */
  121. uint32_t curCluster() const {return m_curCluster;}
  122. /** \return The current position for a file or directory. */
  123. uint32_t curPosition() const {return m_curPosition;}
  124. /** \return Current working directory */
  125. static SdBaseFile* cwd() {return m_cwd;}
  126. /** Set the date/time callback function
  127. *
  128. * \param[in] dateTime The user's call back function. The callback
  129. * function is of the form:
  130. *
  131. * \code
  132. * void dateTime(uint16_t* date, uint16_t* time) {
  133. * uint16_t year;
  134. * uint8_t month, day, hour, minute, second;
  135. *
  136. * // User gets date and time from GPS or real-time clock here
  137. *
  138. * // return date using FAT_DATE macro to format fields
  139. * *date = FAT_DATE(year, month, day);
  140. *
  141. * // return time using FAT_TIME macro to format fields
  142. * *time = FAT_TIME(hour, minute, second);
  143. * }
  144. * \endcode
  145. *
  146. * Sets the function that is called when a file is created or when
  147. * a file's directory entry is modified by sync(). All timestamps,
  148. * access, creation, and modify, are set when a file is created.
  149. * sync() maintains the last access date and last modify date/time.
  150. *
  151. * See the timestamp() function.
  152. */
  153. static void dateTimeCallback(
  154. void (*dateTime)(uint16_t* date, uint16_t* time)) {
  155. m_dateTime = dateTime;
  156. }
  157. /** Cancel the date/time callback function. */
  158. static void dateTimeCallbackCancel() {m_dateTime = 0;}
  159. bool dirEntry(dir_t* dir);
  160. static void dirName(const dir_t& dir, char* name);
  161. bool exists(const char* name);
  162. int16_t fgets(char* str, int16_t num, char* delim = 0);
  163. /** \return The total number of bytes in a file or directory. */
  164. uint32_t fileSize() const {return m_fileSize;}
  165. /** \return The first cluster number for a file or directory. */
  166. uint32_t firstCluster() const {return m_firstCluster;}
  167. bool getFilename(char* name);
  168. /** \return True if this is a directory else false. */
  169. bool isDir() const {return m_type >= FAT_FILE_TYPE_MIN_DIR;}
  170. /** \return True if this is a normal file else false. */
  171. bool isFile() const {return m_type == FAT_FILE_TYPE_NORMAL;}
  172. /** \return True if this is an open file/directory else false. */
  173. bool isOpen() const {return m_type != FAT_FILE_TYPE_CLOSED;}
  174. /** \return True if this is a subdirectory else false. */
  175. bool isSubDir() const {return m_type == FAT_FILE_TYPE_SUBDIR;}
  176. /** \return True if this is the root directory. */
  177. bool isRoot() const {
  178. return m_type == FAT_FILE_TYPE_ROOT_FIXED || m_type == FAT_FILE_TYPE_ROOT32;
  179. }
  180. void ls(Print* pr, uint8_t flags = 0, uint8_t indent = 0);
  181. void ls(uint8_t flags = 0);
  182. bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
  183. // alias for backward compactability
  184. bool makeDir(SdBaseFile* dir, const char* path) {
  185. return mkdir(dir, path, false);
  186. }
  187. bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
  188. bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
  189. bool open(const char* path, uint8_t oflag = O_READ);
  190. bool openNext(SdBaseFile* dirFile, uint8_t oflag);
  191. bool openRoot(SdVolume* vol);
  192. int peek();
  193. bool printCreateDateTime(Print* pr);
  194. static void printFatDate(uint16_t fatDate);
  195. static void printFatDate(Print* pr, uint16_t fatDate);
  196. static void printFatTime(uint16_t fatTime);
  197. static void printFatTime(Print* pr, uint16_t fatTime);
  198. int printField(int16_t value, char term);
  199. int printField(uint16_t value, char term);
  200. int printField(int32_t value, char term);
  201. int printField(uint32_t value, char term);
  202. bool printModifyDateTime(Print* pr);
  203. size_t printName();
  204. size_t printName(Print* pr);
  205. size_t printFileSize(Print* pr);
  206. int16_t read();
  207. int read(void* buf, size_t nbyte);
  208. int8_t readDir(dir_t* dir);
  209. static bool remove(SdBaseFile* dirFile, const char* path);
  210. bool remove();
  211. /** Set the file's current position to zero. */
  212. void rewind() {seekSet(0);}
  213. bool rename(SdBaseFile* dirFile, const char* newPath);
  214. bool rmdir();
  215. // for backward compatibility
  216. bool rmDir() {return rmdir();}
  217. bool rmRfStar();
  218. /** Set the files position to current position + \a pos. See seekSet().
  219. * \param[in] offset The new position in bytes from the current position.
  220. * \return true for success or false for failure.
  221. */
  222. bool seekCur(int32_t offset) {
  223. return seekSet(m_curPosition + offset);
  224. }
  225. /** Set the files position to end-of-file + \a offset. See seekSet().
  226. * \param[in] offset The new position in bytes from end-of-file.
  227. * \return true for success or false for failure.
  228. */
  229. bool seekEnd(int32_t offset = 0) {return seekSet(m_fileSize + offset);}
  230. bool seekSet(uint32_t pos);
  231. bool sync();
  232. bool timestamp(SdBaseFile* file);
  233. bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
  234. uint8_t hour, uint8_t minute, uint8_t second);
  235. /** Type of file. You should use isFile() or isDir() instead of type()
  236. * if possible.
  237. *
  238. * \return The file or directory type.
  239. */
  240. uint8_t type() const {return m_type;}
  241. bool truncate(uint32_t size);
  242. /** \return SdVolume that contains this file. */
  243. SdVolume* volume() const {return m_vol;}
  244. int write(const void* buf, size_t nbyte);
  245. //------------------------------------------------------------------------------
  246. private:
  247. // allow SdFat to set m_cwd
  248. friend class SdFat;
  249. /** experimental don't use */
  250. bool openParent(SdBaseFile* dir);
  251. // private functions
  252. bool addCluster();
  253. cache_t* addDirCluster();
  254. dir_t* cacheDirEntry(uint8_t action);
  255. int8_t lsPrintNext(Print *pr, uint8_t flags, uint8_t indent);
  256. static bool make83Name(const char* str, uint8_t* name, const char** ptr);
  257. bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
  258. bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
  259. bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
  260. dir_t* readDirCache();
  261. static void setCwd(SdBaseFile* cwd) {m_cwd = cwd;}
  262. bool setDirSize();
  263. // bits defined in m_flags
  264. // should be 0X0F
  265. static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
  266. // sync of directory entry required
  267. static uint8_t const F_FILE_DIR_DIRTY = 0X80;
  268. // global pointer to cwd dir
  269. static SdBaseFile* m_cwd;
  270. // data time callback function
  271. static void (*m_dateTime)(uint16_t* date, uint16_t* time);
  272. // private data
  273. uint8_t m_flags; // See above for definition of m_flags bits
  274. uint8_t m_type; // type of file see above for values
  275. uint8_t m_dirIndex; // index of directory entry in dirBlock
  276. SdVolume* m_vol; // volume where file is located
  277. uint32_t m_curCluster; // cluster for current file position
  278. uint32_t m_curPosition; // current file position in bytes from beginning
  279. uint32_t m_dirBlock; // block for this files directory entry
  280. uint32_t m_fileSize; // file size in bytes
  281. uint32_t m_firstCluster; // first cluster of file
  282. };
  283. #endif // SdBaseFile_h