Soldered SD Card Arduino Library 1.0.0
Easily read and write files to and form the SD card breakout! A fork of the original SDFat library by Bill Greiman.
Loading...
Searching...
No Matches
FsFile.h
Go to the documentation of this file.
1
25#ifndef FsFile_h
26#define FsFile_h
31#include "ExFatLib/ExFatLib.h"
32#include "FatLib/FatLib.h"
33#include "FsNew.h"
39{
40 public:
43 {
44 }
45
52 FsBaseFile(const char *path, oflag_t oflag)
53 {
54 open(path, oflag);
55 }
56
58 {
59 close();
60 }
61
65 FsBaseFile(const FsBaseFile &from);
70 FsBaseFile &operator=(const FsBaseFile &from);
75 operator bool() const
76 {
77 return isOpen();
78 }
79
82 int available() const
83 {
84 return m_fFile ? m_fFile->available() : m_xFile ? m_xFile->available() : 0;
85 }
86
89 uint64_t available64() const
90 {
91 return m_fFile ? m_fFile->available32() : m_xFile ? m_xFile->available64() : 0;
92 }
93
95 {
96 if (m_fFile)
97 m_fFile->clearWriteError();
98 if (m_xFile)
99 m_xFile->clearWriteError();
100 }
101
106 bool close();
117 bool contiguousRange(uint32_t *bgnSector, uint32_t *endSector)
118 {
119 return m_fFile ? m_fFile->contiguousRange(bgnSector, endSector)
120 : m_xFile ? m_xFile->contiguousRange(bgnSector, endSector)
121 : false;
122 }
123
124 uint64_t curPosition() const
125 {
126 return m_fFile ? m_fFile->curPosition() : m_xFile ? m_xFile->curPosition() : 0;
127 }
128
129 uint32_t dirIndex() const
130 {
131 return m_fFile ? m_fFile->dirIndex() : m_xFile ? m_xFile->dirIndex() : 0;
132 }
133
144 bool exists(const char *path)
145 {
146 return m_fFile ? m_fFile->exists(path) : m_xFile ? m_xFile->exists(path) : false;
147 }
148
151 void fgetpos(fspos_t *pos) const
152 {
153 if (m_fFile)
154 m_fFile->fgetpos(pos);
155 if (m_xFile)
156 m_xFile->fgetpos(pos);
157 }
158
178 int fgets(char *str, int num, char *delim = nullptr)
179 {
180 return m_fFile ? m_fFile->fgets(str, num, delim) : m_xFile ? m_xFile->fgets(str, num, delim) : -1;
181 }
182
183 uint64_t fileSize() const
184 {
185 return m_fFile ? m_fFile->fileSize() : m_xFile ? m_xFile->fileSize() : 0;
186 }
187
188 uint32_t firstSector() const
189 {
190 return m_fFile ? m_fFile->firstSector() : m_xFile ? m_xFile->firstSector() : 0;
191 }
192
193 void flush()
194 {
195 sync();
196 }
197
200 void fsetpos(const fspos_t *pos)
201 {
202 if (m_fFile)
203 m_fFile->fsetpos(pos);
204 if (m_xFile)
205 m_xFile->fsetpos(pos);
206 }
207
214 bool getAccessDateTime(uint16_t *pdate, uint16_t *ptime)
215 {
216 return m_fFile ? m_fFile->getAccessDateTime(pdate, ptime)
217 : m_xFile ? m_xFile->getAccessDateTime(pdate, ptime)
218 : false;
219 }
220
227 bool getCreateDateTime(uint16_t *pdate, uint16_t *ptime)
228 {
229 return m_fFile ? m_fFile->getCreateDateTime(pdate, ptime)
230 : m_xFile ? m_xFile->getCreateDateTime(pdate, ptime)
231 : false;
232 }
233
234 uint8_t getError() const
235 {
236 return m_fFile ? m_fFile->getError() : m_xFile ? m_xFile->getError() : 0XFF;
237 }
238
245 bool getModifyDateTime(uint16_t *pdate, uint16_t *ptime)
246 {
247 return m_fFile ? m_fFile->getModifyDateTime(pdate, ptime)
248 : m_xFile ? m_xFile->getModifyDateTime(pdate, ptime)
249 : false;
250 }
251
260 size_t getName(char *name, size_t len)
261 {
262 *name = 0;
263 return m_fFile ? m_fFile->getName(name, len) : m_xFile ? m_xFile->getName(name, len) : 0;
264 }
265
267 bool getWriteError() const
268 {
269 return m_fFile ? m_fFile->getWriteError() : m_xFile ? m_xFile->getWriteError() : true;
270 }
271
276 bool isBusy()
277 {
278 return m_fFile ? m_fFile->isBusy() : m_xFile ? m_xFile->isBusy() : true;
279 }
280
281 bool isContiguous() const
282 {
283#if USE_FAT_FILE_FLAG_CONTIGUOUS
284 return m_fFile ? m_fFile->isContiguous() : m_xFile ? m_xFile->isContiguous() : false;
285#else // USE_FAT_FILE_FLAG_CONTIGUOUS
286 return m_xFile ? m_xFile->isContiguous() : false;
287#endif // USE_FAT_FILE_FLAG_CONTIGUOUS
288 }
289
290 bool isDir() const
291 {
292 return m_fFile ? m_fFile->isDir() : m_xFile ? m_xFile->isDir() : false;
293 }
294
297 bool isDirectory() const
298 {
299 return isDir();
300 }
301
302 bool isFile() const
303 {
304 return m_fFile ? m_fFile->isFile() : m_xFile ? m_xFile->isFile() : false;
305 }
306
307 bool isHidden() const
308 {
309 return m_fFile ? m_fFile->isHidden() : m_xFile ? m_xFile->isHidden() : false;
310 }
311
312 bool isOpen() const
313 {
314 return m_fFile || m_xFile;
315 }
316
317 bool isReadable() const
318 {
319 return m_fFile ? m_fFile->isReadable() : m_xFile ? m_xFile->isReadable() : false;
320 }
321
322 bool isReadOnly() const
323 {
324 return m_fFile ? m_fFile->isReadOnly() : m_xFile ? m_xFile->isReadOnly() : false;
325 }
326
327 bool isSubDir() const
328 {
329 return m_fFile ? m_fFile->isSubDir() : m_xFile ? m_xFile->isSubDir() : false;
330 }
331
332 bool isWritable() const
333 {
334 return m_fFile ? m_fFile->isWritable() : m_xFile ? m_xFile->isWritable() : false;
335 }
336#if ENABLE_ARDUINO_SERIAL
347 bool ls(uint8_t flags)
348 {
349 return ls(&Serial, flags);
350 }
351
352 bool ls()
353 {
354 return ls(&Serial);
355 }
356#endif // ENABLE_ARDUINO_SERIAL
363 bool ls(print_t *pr)
364 {
365 return m_fFile ? m_fFile->ls(pr) : m_xFile ? m_xFile->ls(pr) : false;
366 }
367
380 bool ls(print_t *pr, uint8_t flags)
381 {
382 return m_fFile ? m_fFile->ls(pr, flags) : m_xFile ? m_xFile->ls(pr, flags) : false;
383 }
384
395 bool mkdir(FsBaseFile *dir, const char *path, bool pFlag = true);
401 const char *name() const
402 {
403 return "use getName()";
404 }
405
446 bool open(FsBaseFile *dir, const char *path, oflag_t oflag = O_RDONLY);
460 bool open(FsBaseFile *dir, uint32_t index, oflag_t oflag);
472 bool open(FsVolume *vol, const char *path, oflag_t oflag);
482 bool open(const char *path, oflag_t oflag = O_RDONLY)
483 {
484 return FsVolume::m_cwv && open(FsVolume::m_cwv, path, oflag);
485 }
486
491 bool openNext(FsBaseFile *dir, oflag_t oflag = O_RDONLY);
498 bool openRoot(FsVolume *vol);
500 uint64_t position() const
501 {
502 return curPosition();
503 }
504
508 int peek()
509 {
510 return m_fFile ? m_fFile->peek() : m_xFile ? m_xFile->peek() : -1;
511 }
512
523 bool preAllocate(uint64_t length)
524 {
525 return m_fFile ? length < (1ULL << 32) && m_fFile->preAllocate(length)
526 : m_xFile ? m_xFile->preAllocate(length)
527 : false;
528 }
529
536 {
537 return m_fFile ? m_fFile->printAccessDateTime(pr) : m_xFile ? m_xFile->printAccessDateTime(pr) : 0;
538 }
539
546 {
547 return m_fFile ? m_fFile->printCreateDateTime(pr) : m_xFile ? m_xFile->printCreateDateTime(pr) : 0;
548 }
549
555 size_t printField(double value, char term, uint8_t prec = 2)
556 {
557 return m_fFile ? m_fFile->printField(value, term, prec) : m_xFile ? m_xFile->printField(value, term, prec) : 0;
558 }
559
565 size_t printField(float value, char term, uint8_t prec = 2)
566 {
567 return printField(static_cast<double>(value), term, prec);
568 }
569
574 template <typename Type> size_t printField(Type value, char term)
575 {
576 return m_fFile ? m_fFile->printField(value, term) : m_xFile ? m_xFile->printField(value, term) : 0;
577 }
578
586 {
587 return m_fFile ? m_fFile->printFileSize(pr) : m_xFile ? m_xFile->printFileSize(pr) : 0;
588 }
589
596 {
597 return m_fFile ? m_fFile->printModifyDateTime(pr) : m_xFile ? m_xFile->printModifyDateTime(pr) : 0;
598 }
599
605 size_t printName(print_t *pr)
606 {
607 return m_fFile ? m_fFile->printName(pr) : m_xFile ? m_xFile->printName(pr) : 0;
608 }
609
614 int read()
615 {
616 uint8_t b;
617 return read(&b, 1) == 1 ? b : -1;
618 }
619
632 int read(void *buf, size_t count)
633 {
634 return m_fFile ? m_fFile->read(buf, count) : m_xFile ? m_xFile->read(buf, count) : -1;
635 }
636
646 bool remove();
661 bool remove(const char *path)
662 {
663 return m_fFile ? m_fFile->remove(path) : m_xFile ? m_xFile->remove(path) : false;
664 }
665
671 bool rename(const char *newPath)
672 {
673 return m_fFile ? m_fFile->rename(newPath) : m_xFile ? m_xFile->rename(newPath) : false;
674 }
675
682 bool rename(FsBaseFile *dirFile, const char *newPath)
683 {
684 return m_fFile ? m_fFile->rename(dirFile->m_fFile, newPath)
685 : m_xFile ? m_xFile->rename(dirFile->m_xFile, newPath)
686 : false;
687 }
688
689 void rewind()
690 {
691 if (m_fFile)
692 m_fFile->rewind();
693 if (m_xFile)
694 m_xFile->rewind();
695 }
696
698 {
699 if (isDir())
700 rewind();
701 }
702
714 bool rmdir();
721 bool seek(uint64_t pos)
722 {
723 return seekSet(pos);
724 }
725
729 bool seekCur(int64_t offset)
730 {
731 return seekSet(curPosition() + offset);
732 }
733
738 bool seekEnd(int64_t offset = 0)
739 {
740 return seekSet(fileSize() + offset);
741 }
742
748 bool seekSet(uint64_t pos)
749 {
750 return m_fFile ? pos < (1ULL << 32) && m_fFile->seekSet(pos) : m_xFile ? m_xFile->seekSet(pos) : false;
751 }
752
753 uint64_t size() const
754 {
755 return fileSize();
756 }
757
762 bool sync()
763 {
764 return m_fFile ? m_fFile->sync() : m_xFile ? m_xFile->sync() : false;
765 }
766
798 bool timestamp(uint8_t flags, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute,
799 uint8_t second)
800 {
801 return m_fFile ? m_fFile->timestamp(flags, year, month, day, hour, minute, second)
802 : m_xFile ? m_xFile->timestamp(flags, year, month, day, hour, minute, second)
803 : false;
804 }
805
809 bool truncate()
810 {
811 return m_fFile ? m_fFile->truncate() : m_xFile ? m_xFile->truncate() : false;
812 }
813
820 bool truncate(uint64_t length)
821 {
822 return m_fFile ? length < (1ULL << 32) && m_fFile->truncate(length)
823 : m_xFile ? m_xFile->truncate(length)
824 : false;
825 }
826
831 size_t write(uint8_t b)
832 {
833 return write(&b, 1);
834 }
835
850 size_t write(const void *buf, size_t count)
851 {
852 return m_fFile ? m_fFile->write(buf, count) : m_xFile ? m_xFile->write(buf, count) : 0;
853 }
854
855 private:
857 FatFile *m_fFile = nullptr;
858 ExFatFile *m_xFile = nullptr;
859};
860
864class FsFile : public StreamFile<FsBaseFile, uint64_t>
865{
866 public:
872 FsFile openNextFile(oflag_t oflag = O_RDONLY)
873 {
874 FsFile tmpFile;
875 tmpFile.openNext(this, oflag);
876 return tmpFile;
877 }
878};
879#endif // FsFile_h
int oflag_t
Definition FsApiConstants.h:45
uint32_t newalign_t
Definition FsNew.h:31
Print print_t
Definition SysCall.h:66
Basic file class.
Definition ExFatFile.h:95
Basic file class.
Definition FatFile.h:112
FsBaseFile class.
Definition FsFile.h:39
size_t write(uint8_t b)
Definition FsFile.h:831
uint64_t size() const
Definition FsFile.h:753
bool openRoot(FsVolume *vol)
Definition FsFile.cpp:200
size_t printModifyDateTime(print_t *pr)
Definition FsFile.h:595
size_t printName(print_t *pr)
Definition FsFile.h:605
bool isSubDir() const
Definition FsFile.h:327
void rewind()
Definition FsFile.h:689
newalign_t m_fileMem[FS_ALIGN_DIM(ExFatFile, FatFile)]
Definition FsFile.h:856
size_t printAccessDateTime(print_t *pr)
Definition FsFile.h:535
bool truncate()
Definition FsFile.h:809
const char * name() const
Definition FsFile.h:401
bool isOpen() const
Definition FsFile.h:312
bool isReadOnly() const
Definition FsFile.h:322
size_t printCreateDateTime(print_t *pr)
Definition FsFile.h:545
uint32_t dirIndex() const
Definition FsFile.h:129
FatFile * m_fFile
Definition FsFile.h:857
size_t printField(Type value, char term)
Definition FsFile.h:574
bool seekSet(uint64_t pos)
Definition FsFile.h:748
bool truncate(uint64_t length)
Definition FsFile.h:820
void fsetpos(const fspos_t *pos)
Definition FsFile.h:200
bool isContiguous() const
Definition FsFile.h:281
bool rename(FsBaseFile *dirFile, const char *newPath)
Definition FsFile.h:682
bool getCreateDateTime(uint16_t *pdate, uint16_t *ptime)
Definition FsFile.h:227
uint64_t available64() const
Definition FsFile.h:89
uint64_t curPosition() const
Definition FsFile.h:124
uint64_t position() const
Definition FsFile.h:500
void rewindDirectory()
Definition FsFile.h:697
size_t printField(float value, char term, uint8_t prec=2)
Definition FsFile.h:565
bool seekCur(int64_t offset)
Definition FsFile.h:729
FsBaseFile(const char *path, oflag_t oflag)
Definition FsFile.h:52
bool remove(const char *path)
Definition FsFile.h:661
size_t printFileSize(print_t *pr)
Definition FsFile.h:585
bool ls(print_t *pr, uint8_t flags)
Definition FsFile.h:380
uint8_t getError() const
Definition FsFile.h:234
bool isWritable() const
Definition FsFile.h:332
FsBaseFile()
Definition FsFile.h:42
bool exists(const char *path)
Definition FsFile.h:144
bool ls(uint8_t flags)
Definition FsFile.h:347
bool isBusy()
Definition FsFile.h:276
ExFatFile * m_xFile
Definition FsFile.h:858
bool mkdir(FsBaseFile *dir, const char *path, bool pFlag=true)
Definition FsFile.cpp:76
bool remove()
Definition FsFile.cpp:228
size_t printField(double value, char term, uint8_t prec=2)
Definition FsFile.h:555
void clearWriteError()
Definition FsFile.h:94
bool close()
Definition FsFile.cpp:61
size_t write(const void *buf, size_t count)
Definition FsFile.h:850
bool open(FsBaseFile *dir, const char *path, oflag_t oflag=O_RDONLY)
Definition FsFile.cpp:128
bool isFile() const
Definition FsFile.h:302
void fgetpos(fspos_t *pos) const
Definition FsFile.h:151
int available() const
Definition FsFile.h:82
~FsBaseFile()
Definition FsFile.h:57
int fgets(char *str, int num, char *delim=nullptr)
Definition FsFile.h:178
bool isDirectory() const
Definition FsFile.h:297
bool rmdir()
Definition FsFile.cpp:249
bool openNext(FsBaseFile *dir, oflag_t oflag=O_RDONLY)
Definition FsFile.cpp:176
bool preAllocate(uint64_t length)
Definition FsFile.h:523
int read()
Definition FsFile.h:614
bool timestamp(uint8_t flags, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
Definition FsFile.h:798
uint32_t firstSector() const
Definition FsFile.h:188
bool getWriteError() const
Definition FsFile.h:267
bool sync()
Definition FsFile.h:762
bool ls()
Definition FsFile.h:352
size_t getName(char *name, size_t len)
Definition FsFile.h:260
bool isHidden() const
Definition FsFile.h:307
void flush()
Definition FsFile.h:193
int peek()
Definition FsFile.h:508
bool isReadable() const
Definition FsFile.h:317
bool getAccessDateTime(uint16_t *pdate, uint16_t *ptime)
Definition FsFile.h:214
bool seekEnd(int64_t offset=0)
Definition FsFile.h:738
bool contiguousRange(uint32_t *bgnSector, uint32_t *endSector)
Definition FsFile.h:117
FsBaseFile & operator=(const FsBaseFile &from)
Definition FsFile.cpp:43
bool seek(uint64_t pos)
Definition FsFile.h:721
int read(void *buf, size_t count)
Definition FsFile.h:632
uint64_t fileSize() const
Definition FsFile.h:183
bool ls(print_t *pr)
Definition FsFile.h:363
bool getModifyDateTime(uint16_t *pdate, uint16_t *ptime)
Definition FsFile.h:245
bool open(const char *path, oflag_t oflag=O_RDONLY)
Definition FsFile.h:482
bool rename(const char *newPath)
Definition FsFile.h:671
bool isDir() const
Definition FsFile.h:290
FsBaseFile file with Arduino Stream.
Definition FsFile.h:865
FsFile openNextFile(oflag_t oflag=O_RDONLY)
Definition FsFile.h:872
FsVolume class.
Definition FsVolume.h:41
static FsVolume * m_cwv
Definition FsVolume.h:405
StreamFile()
Definition ArduinoFiles.h:71
static bool value
Definition DigitalPin.h:210
Definition FsStructs.h:133