|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
-
-
- #ifndef FS_H
- #define FS_H
- #ifdef __cplusplus
-
- #include <Arduino.h>
-
- #define FILE_READ 0
- #define FILE_WRITE 1
- #define FILE_WRITE_BEGIN 2
-
- enum SeekMode {
- SeekSet = 0,
- SeekCur = 1,
- SeekEnd = 2
- };
-
- #define FILE_WHOAMI
-
- class File : public Stream {
- public:
- constexpr File() : f(nullptr) { }
- File(File *file) {
-
-
-
-
-
-
- f = file;
- if (f) f->refcount++;
- }
- File(const File &file) {
-
-
-
- f = file.f;
- if (f) f->refcount++;
- }
- File& operator = (const File &file) {
-
-
-
- invalidate();
- f = file.f;
- if (f) f->refcount++;
- return *this;
- }
- virtual ~File() {
- invalidate();
- }
- #ifdef FILE_WHOAMI
- virtual void whoami() {
- Serial.printf(" File this=%x, f=%x\n", (int)this, (int)f);
- if (f) f->whoami();
- }
- unsigned int getRefcount() {
- return refcount;
- }
- #endif
- virtual size_t read(void *buf, size_t nbyte) {
- return (f) ? f->read(buf, nbyte) : 0;
- }
- virtual size_t write(const void *buf, size_t size) {
- return (f) ? f->write(buf, size) : 0;
- }
- virtual int available() {
- return (f) ? f->available() : 0;
- }
- virtual int peek() {
- return (f) ? f->peek() : -1;
- }
- virtual void flush() {
- if (f) f->flush();
- }
- virtual bool truncate(uint64_t size=0) {
- return (f) ? f->truncate(size) : false;
- }
- virtual bool seek(uint64_t pos, int mode) {
- return (f) ? f->seek(pos, mode) : false;
- }
- virtual uint64_t position() {
- return (f) ? f->position() : 0;
- }
- virtual uint64_t size() {
- return (f) ? f->size() : 0;
- }
- virtual void close() {
- if (f) f->close();
- }
- virtual operator bool() {
- return (f) ? (bool)*f : false;
- }
- virtual const char* name() {
- return (f) ? f->name() : "";
- }
- virtual bool isDirectory() {
- return (f) ? f->isDirectory() : false;
- }
- virtual File openNextFile(uint8_t mode=0) {
- return (f) ? f->openNextFile(mode) : *this;
- }
- virtual void rewindDirectory(void) {
- if (f) f->rewindDirectory();
- }
- bool seek(uint64_t pos) {
- return seek(pos, SeekSet);
- }
- int read() {
- if (!f) return -1;
- unsigned char b;
- if (f->read(&b, 1) < 1) return -1;
- return b;
- }
- size_t write(uint8_t b) {
- return write(&b, 1);
- }
- size_t write(const char *str) {
- return write(str, strlen(str));
- }
- size_t readBytes(char *buffer, size_t length) {
- return read(buffer, length);
- }
- private:
- void invalidate() {
- if (f && --(f->refcount) == 0) delete f;
- }
- union {
-
- File *f;
-
-
- unsigned int refcount;
- };
- };
-
-
- class FS
- {
- public:
- FS() {}
- virtual File open(const char *filename, uint8_t mode = FILE_READ) = 0;
- virtual bool exists(const char *filepath) = 0;
- virtual bool mkdir(const char *filepath) = 0;
- virtual bool rename(const char *oldfilepath, const char *newfilepath) = 0;
- virtual bool remove(const char *filepath) = 0;
- virtual bool rmdir(const char *filepath) = 0;
- virtual uint64_t usedSize() = 0;
- virtual uint64_t totalSize() = 0;
- };
-
-
- #endif
- #endif
|