1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

enhancements on the FS Api

This commit is contained in:
ficeto
2015-05-16 11:03:39 +03:00
parent c4dca3bd50
commit fe5c6671a8
2 changed files with 31 additions and 27 deletions

View File

@ -144,14 +144,22 @@ void FSFile::close() {
_file = 0;
}
char * FSFile::name(){
return (char*)_stats.name;
}
bool FSFile::isDirectory(void) {
return _stats.type == SPIFFS_TYPE_DIR;
}
void FSFile::rewindDirectory() {
if (! _file || !isDirectory()) return;
if (!_file || !isDirectory()) return;
SPIFFS_closedir(&_dir);
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
}
FSFile FSFile::openNextFile(){
if (! _file || !isDirectory()) return FSFile();
if (!_file || !isDirectory()) return FSFile();
struct spiffs_dirent e;
struct spiffs_dirent *pe = &e;
if ((pe = SPIFFS_readdir(&_dir, pe))){
@ -161,30 +169,36 @@ FSFile FSFile::openNextFile(){
}
uint32_t FSFile::size() {
if(! _file) return 0;
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0) return 0;
if(!_file) return 0;
if(_stats.size) return _stats.size;
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
_stats.size = SPIFFS_tell(&_filesystemStorageHandle, _file);
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
return _stats.size;
}
int FSFile::available() {
if (!_file) return 0;
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
return size() - pos;
}
uint32_t FSFile::seek(uint32_t pos) {
if (! _file || isDirectory()) return 0;
if (!_file) return 0;
return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
}
uint32_t FSFile::position() {
if (! _file || isDirectory()) return 0;
if (!_file) return 0;
return SPIFFS_tell(&_filesystemStorageHandle, _file);
}
bool FSFile::eof() {
if (! _file || isDirectory()) return 0;
if (!_file) return 0;
return SPIFFS_eof(&_filesystemStorageHandle, _file);
}
bool FSFile::isDirectory(void) {
return _stats.type == SPIFFS_TYPE_DIR;
}
int FSFile::read(void *buf, uint16_t nbyte) {
if (! _file || isDirectory()) return -1;
return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte);
@ -204,12 +218,6 @@ int FSFile::peek() {
return c;
}
int FSFile::available() {
if (! _file || isDirectory()) return 0;
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
return _stats.size - pos;
}
size_t FSFile::write(const uint8_t *buf, size_t size){
if (! _file || isDirectory()) return 0;
int res = SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
@ -226,10 +234,10 @@ void FSFile::flush(){
SPIFFS_fflush(&_filesystemStorageHandle, _file);
}
uint32_t FSFile::remove(){
bool FSFile::remove(){
if (! _file) return 0;
return SPIFFS_fremove(&_filesystemStorageHandle, _file);
_file = 0;
close();
return SPIFFS_remove(&_filesystemStorageHandle, (const char *)_stats.name) == 0;
}
int FSFile::lastError(){
@ -239,7 +247,3 @@ int FSFile::lastError(){
void FSFile::clearError(){
_filesystemStorageHandle.errno = SPIFFS_OK;
}
char * FSFile::name(){
return (char*)_stats.name;
}

View File

@ -26,8 +26,8 @@
class String;
#define FSFILE_READ SPIFFS_RDONLY
#define FSFILE_WRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT)
#define FSFILE_OVERWRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT)
#define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT)
#define FSFILE_OVERWRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT)
class FSFile : public Stream {
private:
@ -47,11 +47,11 @@ public:
virtual void flush();
int read(void *buf, uint16_t nbyte);
uint32_t seek(uint32_t pos);
uint32_t remove();
uint32_t position();
uint32_t size();
bool eof();
void close();
bool remove();
int lastError();
void clearError();
operator bool() { return _file > 0; }