Prusa3d Marlin fork
SdBaseFile.h
Go to the documentation of this file.
1 /* Arduino SdFat Library
2  * Copyright (C) 2009 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 #include "Marlin.h"
21 #ifdef SDSUPPORT
22 
23 #ifndef SdBaseFile_h
24 #define SdBaseFile_h
29 #include "Marlin.h"
30 #include "SdFatConfig.h"
31 #include "SdVolume.h"
32 //------------------------------------------------------------------------------
38 struct filepos_t {
40  uint32_t position;
42  uint32_t cluster;
43  filepos_t() : position(0), cluster(0) {}
44 };
45 
46 // use the gnu style oflag in open()
48 uint8_t const O_READ = 0X01;
50 uint8_t const O_RDONLY = O_READ;
52 uint8_t const O_WRITE = 0X02;
54 uint8_t const O_WRONLY = O_WRITE;
56 uint8_t const O_RDWR = (O_READ | O_WRITE);
58 uint8_t const O_ACCMODE = (O_READ | O_WRITE);
60 uint8_t const O_APPEND = 0X04;
62 uint8_t const O_SYNC = 0X08;
64 uint8_t const O_TRUNC = 0X10;
66 uint8_t const O_AT_END = 0X20;
68 uint8_t const O_CREAT = 0X40;
70 uint8_t const O_EXCL = 0X80;
71 
72 // SdBaseFile class static and const definitions
73 // flags for ls()
75 uint8_t const LS_DATE = 1;
77 uint8_t const LS_SIZE = 2;
79 uint8_t const LS_R = 4;
80 
81 
82 // flags for timestamp
84 uint8_t const T_ACCESS = 1;
86 uint8_t const T_CREATE = 2;
88 uint8_t const T_WRITE = 4;
89 // values for type_
91 uint8_t const FAT_FILE_TYPE_CLOSED = 0;
93 uint8_t const FAT_FILE_TYPE_NORMAL = 1;
95 uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
97 uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
99 uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
102 
110 static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
111  return (year - 1980) << 9 | month << 5 | day;
112 }
118 static inline uint16_t FAT_YEAR(uint16_t fatDate) {
119  return 1980 + (fatDate >> 9);
120 }
126 static inline uint8_t FAT_MONTH(uint16_t fatDate) {
127  return (fatDate >> 5) & 0XF;
128 }
134 static inline uint8_t FAT_DAY(uint16_t fatDate) {
135  return fatDate & 0X1F;
136 }
144 static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
145  return hour << 11 | minute << 5 | second >> 1;
146 }
152 static inline uint8_t FAT_HOUR(uint16_t fatTime) {
153  return fatTime >> 11;
154 }
160 static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
161  return(fatTime >> 5) & 0X3F;
162 }
170 static inline uint8_t FAT_SECOND(uint16_t fatTime) {
171  return 2*(fatTime & 0X1F);
172 }
174 uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
176 uint16_t const FAT_DEFAULT_TIME = (1 << 11);
177 //------------------------------------------------------------------------------
182 class SdBaseFile {
183  public:
186  SdBaseFile(const char* path, uint8_t oflag);
187  ~SdBaseFile() {if(isOpen()) close();}
194  //----------------------------------------------------------------------------
195  // helpers for stream classes
199  void getpos(filepos_t* pos);
203  void setpos(filepos_t* pos);
204  //----------------------------------------------------------------------------
205  bool close();
206  bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
207  bool createContiguous(SdBaseFile* dirFile,
208  const char* path, uint32_t size);
210  uint32_t curCluster() const {return curCluster_;}
212  uint32_t curPosition() const {return curPosition_;}
214  static SdBaseFile* cwd() {return cwd_;}
242  static void dateTimeCallback(
243  void (*dateTime)(uint16_t* date, uint16_t* time)) {
244  dateTime_ = dateTime;
245  }
247  static void dateTimeCallbackCancel() {dateTime_ = 0;}
248  bool dirEntry(dir_t* dir);
249  static void dirName(const dir_t& dir, char* name);
250  bool exists(const char* name);
251  int16_t fgets(char* str, int16_t num, char* delim = 0);
253  uint32_t fileSize() const {return fileSize_;}
255  uint32_t firstCluster() const {return firstCluster_;}
256  bool getFilename(char* name);
258  bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
260  bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;}
262  bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;}
264  bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;}
266  bool isRoot() const {
267  return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32;
268  }
269  void ls( uint8_t flags = 0, uint8_t indent = 0);
270  bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
271  // alias for backward compactability
272  bool makeDir(SdBaseFile* dir, const char* path) {
273  return mkdir(dir, path, false);
274  }
275  bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
276  bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
277  bool open(const char* path, uint8_t oflag = O_READ);
278  bool openNext(SdBaseFile* dirFile, uint8_t oflag);
279  bool openRoot(SdVolume* vol);
280  int peek();
281  static void printFatDate(uint16_t fatDate);
282  static void printFatTime( uint16_t fatTime);
283  bool printName();
284 protected:
285  int16_t read();
286  int16_t read(void* buf, uint16_t nbyte);
287 public:
288  int8_t readDir(dir_t* dir, char* longFilename);
289  static bool remove(SdBaseFile* dirFile, const char* path);
290  bool remove();
292  void rewind() {seekSet(0);}
293  bool rename(SdBaseFile* dirFile, const char* newPath);
294  bool rmdir();
295  // for backward compatibility
296  bool rmDir() {return rmdir();}
297  bool rmRfStar();
302  bool seekCur(int32_t offset) {
303  return seekSet(curPosition_ + offset);
304  }
309  bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);}
310  bool seekSet(uint32_t pos);
311  bool sync();
312  bool timestamp(SdBaseFile* file);
313  bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
314  uint8_t hour, uint8_t minute, uint8_t second);
320  uint8_t type() const {return type_;}
321  bool truncate(uint32_t size);
323  SdVolume* volume() const {return vol_;}
324  int16_t write(const void* buf, uint16_t nbyte);
325 //------------------------------------------------------------------------------
326  protected:
327  // allow SdFat to set cwd_
328  friend class SdFat;
329  // global pointer to cwd dir
330  static SdBaseFile* cwd_;
331  // data time callback function
332  static void (*dateTime_)(uint16_t* date, uint16_t* time);
333  // bits defined in flags_
334  // should be 0X0F
335  static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
336  // sync of directory entry required
337  static uint8_t const F_FILE_DIR_DIRTY = 0X80;
338 
339  // private data
340  uint8_t flags_; // See above for definition of flags_ bits
341  uint8_t fstate_; // error and eof indicator
342  uint8_t type_; // type of file see above for values
343  uint32_t curCluster_; // cluster for current file position
344  uint32_t curPosition_; // current file position in bytes from beginning
345  uint32_t dirBlock_; // block for this files directory entry
346  uint8_t dirIndex_; // index of directory entry in dirBlock
347  uint32_t fileSize_; // file size in bytes
348  uint32_t firstCluster_; // first cluster of file
349  SdVolume* vol_; // volume where file is located
350 
352  bool openParent(SdBaseFile* dir);
353  // private functions
354  bool addCluster();
355  bool addDirCluster();
356  dir_t* cacheDirEntry(uint8_t action);
357  int8_t lsPrintNext( uint8_t flags, uint8_t indent);
358  static bool make83Name(const char* str, uint8_t* name, const char** ptr);
359  bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
360  bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
361  bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
362  dir_t* readDirCache();
363 //------------------------------------------------------------------------------
364 // to be deleted
365  static void printDirName( const dir_t& dir,
366  uint8_t width, bool printSlash);
367 //------------------------------------------------------------------------------
368 // Deprecated functions - suppress cpplint warnings with NOLINT comment
369 #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
370  public:
377  bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
378  return contiguousRange(&bgnBlock, &endBlock);
379  }
388  bool createContiguous(SdBaseFile& dirFile, // NOLINT
389  const char* path, uint32_t size) {
390  return createContiguous(&dirFile, path, size);
391  }
397  static void dateTimeCallback(
398  void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
399  oldDateTime_ = dateTime;
400  dateTime_ = dateTime ? oldToNew : 0;
401  }
406  bool dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
414  bool mkdir(SdBaseFile& dir, const char* path) { // NOLINT
415  return mkdir(&dir, path);
416  }
426  bool open(SdBaseFile& dirFile, // NOLINT
427  const char* path, uint8_t oflag) {
428  return open(&dirFile, path, oflag);
429  }
436  bool open(SdBaseFile& dirFile, const char* path) { // NOLINT
437  return open(dirFile, path, O_RDWR);
438  }
448  bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
449  return open(&dirFile, index, oflag);
450  }
455  bool openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
460  int8_t readDir(dir_t& dir, char* longFilename) {return readDir(&dir, longFilename);} // NOLINT
467  static bool remove(SdBaseFile& dirFile, const char* path) { // NOLINT
468  return remove(&dirFile, path);
469  }
470 //------------------------------------------------------------------------------
471 // rest are private
472  private:
473  static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
474  static void oldToNew(uint16_t* date, uint16_t* time) {
475  uint16_t d;
476  uint16_t t;
477  oldDateTime_(d, t);
478  *date = d;
479  *time = t;
480  }
481 #endif // ALLOW_DEPRECATED_FUNCTIONS
482 };
483 
484 #endif // SdBaseFile_h
485 #endif
uint16_t const FAT_DEFAULT_TIME
Definition: SdBaseFile.h:176
static uint8_t FAT_MINUTE(uint16_t fatTime)
Definition: SdBaseFile.h:160
uint8_t const FAT_FILE_TYPE_ROOT_FIXED
Definition: SdBaseFile.h:95
uint8_t const O_CREAT
Definition: SdBaseFile.h:68
static uint16_t FAT_YEAR(uint16_t fatDate)
Definition: SdBaseFile.h:118
uint8_t const T_WRITE
Definition: SdBaseFile.h:88
uint8_t const O_SYNC
Definition: SdBaseFile.h:62
static uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second)
Definition: SdBaseFile.h:144
uint8_t const O_AT_END
Definition: SdBaseFile.h:66
uint8_t const O_APPEND
Definition: SdBaseFile.h:60
static uint8_t FAT_MONTH(uint16_t fatDate)
Definition: SdBaseFile.h:126
uint16_t const FAT_DEFAULT_DATE
Definition: SdBaseFile.h:174
static uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day)
Definition: SdBaseFile.h:110
uint8_t const O_WRITE
Definition: SdBaseFile.h:52
static uint8_t FAT_DAY(uint16_t fatDate)
Definition: SdBaseFile.h:134
static uint8_t FAT_SECOND(uint16_t fatTime)
Definition: SdBaseFile.h:170
uint8_t const FAT_FILE_TYPE_CLOSED
Definition: SdBaseFile.h:91
uint8_t const O_ACCMODE
Definition: SdBaseFile.h:58
uint8_t const T_CREATE
Definition: SdBaseFile.h:86
uint8_t const T_ACCESS
Definition: SdBaseFile.h:84
uint8_t const FAT_FILE_TYPE_NORMAL
Definition: SdBaseFile.h:93
uint8_t const O_WRONLY
Definition: SdBaseFile.h:54
uint8_t const LS_SIZE
Definition: SdBaseFile.h:77
uint8_t const FAT_FILE_TYPE_ROOT32
Definition: SdBaseFile.h:97
uint8_t const O_TRUNC
Definition: SdBaseFile.h:64
uint8_t const O_EXCL
Definition: SdBaseFile.h:70
uint8_t const FAT_FILE_TYPE_MIN_DIR
Definition: SdBaseFile.h:101
uint8_t const O_RDONLY
Definition: SdBaseFile.h:50
uint8_t const FAT_FILE_TYPE_SUBDIR
Definition: SdBaseFile.h:99
uint8_t const O_READ
Definition: SdBaseFile.h:48
uint8_t const LS_DATE
Definition: SdBaseFile.h:75
uint8_t const O_RDWR
Definition: SdBaseFile.h:56
static uint8_t FAT_HOUR(uint16_t fatTime)
Definition: SdBaseFile.h:152
uint8_t const LS_R
Definition: SdBaseFile.h:79
configuration definitions
SdVolume class.
Base class for SdFile with Print and C++ streams.
Definition: SdBaseFile.h:182
static void dateTimeCallbackCancel()
Definition: SdBaseFile.h:247
uint32_t curCluster() const
Definition: SdBaseFile.h:210
bool dirEntry(dir_t *dir)
Definition: SdBaseFile.cpp:182
bool dirEntry(dir_t &dir)
Definition: SdBaseFile.h:406
int16_t read()
Definition: SdBaseFile.cpp:1029
static void dateTimeCallback(void(*dateTime)(uint16_t *date, uint16_t *time))
Definition: SdBaseFile.h:242
bool close()
Definition: SdBaseFile.cpp:93
static void printFatDate(uint16_t fatDate)
Definition: SdBaseFile.cpp:988
bool printName()
Definition: SdBaseFile.cpp:1017
bool contiguousRange(uint32_t *bgnBlock, uint32_t *endBlock)
Definition: SdBaseFile.cpp:109
SdBaseFile()
Definition: SdBaseFile.h:185
bool sync()
Definition: SdBaseFile.cpp:1511
bool isRoot() const
Definition: SdBaseFile.h:266
bool getFilename(char *name)
Definition: SdBaseFile.cpp:280
bool open(SdBaseFile &dirFile, uint16_t index, uint8_t oflag)
Definition: SdBaseFile.h:448
uint32_t fileSize() const
Definition: SdBaseFile.h:253
bool rmRfStar()
Definition: SdBaseFile.cpp:1395
int16_t fgets(char *str, int16_t num, char *delim=0)
Definition: SdBaseFile.cpp:251
void getpos(filepos_t *pos)
Definition: SdBaseFile.cpp:297
bool writeError
Definition: SdBaseFile.h:193
int16_t write(const void *buf, uint16_t nbyte)
Definition: SdBaseFile.cpp:1732
void setpos(filepos_t *pos)
Definition: SdBaseFile.cpp:1498
bool contiguousRange(uint32_t &bgnBlock, uint32_t &endBlock)
Definition: SdBaseFile.h:377
bool isOpen() const
Definition: SdBaseFile.h:262
static SdBaseFile * cwd()
Definition: SdBaseFile.h:214
bool open(SdBaseFile &dirFile, const char *path, uint8_t oflag)
Definition: SdBaseFile.h:426
bool mkdir(SdBaseFile &dir, const char *path)
Definition: SdBaseFile.h:414
static void printDirName(const dir_t &dir, uint8_t width, bool printSlash)
Definition: SdBaseFile.cpp:945
uint8_t type() const
Definition: SdBaseFile.h:320
bool remove()
Definition: SdBaseFile.cpp:1201
bool seekEnd(int32_t offset=0)
Definition: SdBaseFile.h:309
bool open(SdBaseFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdBaseFile.cpp:712
bool open(SdBaseFile &dirFile, const char *path)
Definition: SdBaseFile.h:436
bool rmdir()
Definition: SdBaseFile.cpp:1354
void ls(uint8_t flags=0, uint8_t indent=0)
Definition: SdBaseFile.cpp:318
void rewind()
Definition: SdBaseFile.h:292
bool isFile() const
Definition: SdBaseFile.h:260
bool isDir() const
Definition: SdBaseFile.h:258
uint32_t firstCluster() const
Definition: SdBaseFile.h:255
static void dirName(const dir_t &dir, char *name)
Definition: SdBaseFile.cpp:205
bool createContiguous(SdBaseFile &dirFile, const char *path, uint32_t size)
Definition: SdBaseFile.h:388
static void dateTimeCallback(void(*dateTime)(uint16_t &date, uint16_t &time))
Definition: SdBaseFile.h:397
int peek()
Definition: SdBaseFile.cpp:930
bool truncate(uint32_t size)
Definition: SdBaseFile.cpp:1670
static void printFatTime(uint16_t fatTime)
Definition: SdBaseFile.cpp:1004
bool exists(const char *name)
Definition: SdBaseFile.cpp:226
bool seekCur(int32_t offset)
Definition: SdBaseFile.h:302
bool isSubDir() const
Definition: SdBaseFile.h:264
bool openRoot(SdVolume *vol)
Definition: SdBaseFile.cpp:893
uint32_t curPosition() const
Definition: SdBaseFile.h:212
bool createContiguous(SdBaseFile *dirFile, const char *path, uint32_t size)
Definition: SdBaseFile.cpp:149
bool mkdir(SdBaseFile *dir, const char *path, bool pFlag=true)
Definition: SdBaseFile.cpp:431
bool rename(SdBaseFile *dirFile, const char *newPath)
Definition: SdBaseFile.cpp:1261
int8_t readDir(dir_t *dir, char *longFilename)
Definition: SdBaseFile.cpp:1114
bool timestamp(SdBaseFile *file)
Definition: SdBaseFile.cpp:1553
bool openNext(SdBaseFile *dirFile, uint8_t oflag)
Definition: SdBaseFile.cpp:794
int8_t readDir(dir_t &dir, char *longFilename)
Definition: SdBaseFile.h:460
static bool remove(SdBaseFile &dirFile, const char *path)
Definition: SdBaseFile.h:467
bool openParent(SdBaseFile *dir)
Definition: SdBaseFile.cpp:836
SdVolume * volume() const
Definition: SdBaseFile.h:323
bool openRoot(SdVolume &vol)
Definition: SdBaseFile.h:455
bool seekSet(uint32_t pos)
Definition: SdBaseFile.cpp:1459
Access FAT16 and FAT32 volumes on SD and SDHC cards.
Definition: SdVolume.h:60
FAT short directory entry.
Definition: SdFatStructs.h:496
internal type for istream do not use in user apps
Definition: SdBaseFile.h:38
uint32_t position
Definition: SdBaseFile.h:40
uint32_t cluster
Definition: SdBaseFile.h:42