123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #ifndef SdBaseFile_h
- #define SdBaseFile_h
- #ifdef __AVR__
- #include <avr/pgmspace.h>
- #else
- #ifndef PGM_P
- #define PGM_P const char*
- #endif
- #ifndef PSTR
- #define PSTR(x) (x)
- #endif
- #ifndef pgm_read_byte
- #define pgm_read_byte(addr) (*(const unsigned char*)(addr))
- #endif
- #ifndef pgm_read_word
- #define pgm_read_word(addr) (*(const uint16_t*)(addr))
- #endif
- #ifndef PROGMEM
- #define PROGMEM const
- #endif
- #endif
- #include <Arduino.h>
- #include <SdFatConfig.h>
- #include <SdVolume.h>
- #include <utility/FatApiConstants.h>
- struct FatPos_t {
-
- uint32_t position;
-
- uint32_t cluster;
- FatPos_t() : position(0), cluster(0) {}
- };
- uint8_t const FAT_FILE_TYPE_CLOSED = 0;
- uint8_t const FAT_FILE_TYPE_NORMAL = 1;
- uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
- uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
- uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
- uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
- class SdBaseFile {
- public:
-
- SdBaseFile() : writeError(false), m_type(FAT_FILE_TYPE_CLOSED) {}
- SdBaseFile(const char* path, uint8_t oflag);
- #if DESTRUCTOR_CLOSES_FILE
- ~SdBaseFile() {if(isOpen()) close();}
- #endif
-
- bool writeError;
-
- bool getWriteError() {return writeError;}
-
- void clearWriteError() {writeError = 0;}
-
-
-
- void getpos(FatPos_t* pos);
-
- void setpos(FatPos_t* pos);
-
-
- uint32_t available() {return fileSize() - curPosition();}
- bool close();
- bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
- bool createContiguous(SdBaseFile* dirFile,
- const char* path, uint32_t size);
-
- uint32_t curCluster() const {return m_curCluster;}
-
- uint32_t curPosition() const {return m_curPosition;}
-
- static SdBaseFile* cwd() {return m_cwd;}
-
- static void dateTimeCallback(
- void (*dateTime)(uint16_t* date, uint16_t* time)) {
- m_dateTime = dateTime;
- }
-
- static void dateTimeCallbackCancel() {m_dateTime = 0;}
- bool dirEntry(dir_t* dir);
- static void dirName(const dir_t& dir, char* name);
- bool exists(const char* name);
- int16_t fgets(char* str, int16_t num, char* delim = 0);
-
- uint32_t fileSize() const {return m_fileSize;}
-
- uint32_t firstCluster() const {return m_firstCluster;}
- bool getFilename(char* name);
-
- bool isDir() const {return m_type >= FAT_FILE_TYPE_MIN_DIR;}
-
- bool isFile() const {return m_type == FAT_FILE_TYPE_NORMAL;}
-
- bool isOpen() const {return m_type != FAT_FILE_TYPE_CLOSED;}
-
- bool isSubDir() const {return m_type == FAT_FILE_TYPE_SUBDIR;}
-
- bool isRoot() const {
- return m_type == FAT_FILE_TYPE_ROOT_FIXED || m_type == FAT_FILE_TYPE_ROOT32;
- }
- void ls(Print* pr, uint8_t flags = 0, uint8_t indent = 0);
- void ls(uint8_t flags = 0);
- bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
-
- bool makeDir(SdBaseFile* dir, const char* path) {
- return mkdir(dir, path, false);
- }
- bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
- bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
- bool open(const char* path, uint8_t oflag = O_READ);
- bool openNext(SdBaseFile* dirFile, uint8_t oflag);
- bool openRoot(SdVolume* vol);
- int peek();
- bool printCreateDateTime(Print* pr);
- static void printFatDate(uint16_t fatDate);
- static void printFatDate(Print* pr, uint16_t fatDate);
- static void printFatTime(uint16_t fatTime);
- static void printFatTime(Print* pr, uint16_t fatTime);
- int printField(float value, char term, uint8_t prec = 2);
- int printField(int16_t value, char term);
- int printField(uint16_t value, char term);
- int printField(int32_t value, char term);
- int printField(uint32_t value, char term);
- bool printModifyDateTime(Print* pr);
- size_t printName();
- size_t printName(Print* pr);
- size_t printFileSize(Print* pr);
- int16_t read();
- int read(void* buf, size_t nbyte);
- int8_t readDir(dir_t* dir);
- static bool remove(SdBaseFile* dirFile, const char* path);
- bool remove();
-
- void rewind() {seekSet(0);}
- bool rename(SdBaseFile* dirFile, const char* newPath);
- bool rmdir();
-
- bool rmDir() {return rmdir();}
- bool rmRfStar();
-
- bool seekCur(int32_t offset) {
- return seekSet(m_curPosition + offset);
- }
-
- bool seekEnd(int32_t offset = 0) {return seekSet(m_fileSize + offset);}
- bool seekSet(uint32_t pos);
- bool sync();
- bool timestamp(SdBaseFile* file);
- bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
- uint8_t hour, uint8_t minute, uint8_t second);
-
- uint8_t type() const {return m_type;}
- bool truncate(uint32_t size);
-
- SdVolume* volume() const {return m_vol;}
- int write(const void* buf, size_t nbyte);
- private:
-
- friend class SdFat;
-
- bool openParent(SdBaseFile* dir);
-
- bool addCluster();
- cache_t* addDirCluster();
- dir_t* cacheDirEntry(uint8_t action);
- int8_t lsPrintNext(Print *pr, uint8_t flags, uint8_t indent);
- static bool make83Name(const char* str, uint8_t* name, const char** ptr);
- bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
- bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
- bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
- dir_t* readDirCache();
- static void setCwd(SdBaseFile* cwd) {m_cwd = cwd;}
- bool setDirSize();
-
-
- static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
-
- static uint8_t const F_FILE_DIR_DIRTY = 0X80;
-
- static SdBaseFile* m_cwd;
-
- static void (*m_dateTime)(uint16_t* date, uint16_t* time);
-
- uint8_t m_flags;
- uint8_t m_type;
- uint8_t m_dirIndex;
- SdVolume* m_vol;
- uint32_t m_curCluster;
- uint32_t m_curPosition;
- uint32_t m_dirBlock;
- uint32_t m_fileSize;
- uint32_t m_firstCluster;
- };
- #endif
|