Skip to content

Commit 04693c6

Browse files
authored
Implements seekDir and getNextFileName on FS Lib to improve performance (#7229)
* Implements seekDir and getNextFileName on FS lib to improve performance * getNextFileName return String directly
1 parent 9006751 commit 04693c6

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

Diff for: libraries/FS/src/FS.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ File File::openNextFile(const char* mode)
187187
return _p->openNextFile(mode);
188188
}
189189

190+
boolean File::seekDir(long position){
191+
if(!_p){
192+
return false;
193+
}
194+
return _p->seekDir(position);
195+
}
196+
197+
String File::getNextFileName(void)
198+
{
199+
if (!_p) {
200+
return "";
201+
}
202+
return _p->getNextFileName();
203+
204+
}
205+
190206
void File::rewindDirectory(void)
191207
{
192208
if (!*this) {

Diff for: libraries/FS/src/FS.h

+2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ class File : public Stream
7878
const char* name() const;
7979

8080
boolean isDirectory(void);
81+
boolean seekDir(long position);
8182
File openNextFile(const char* mode = FILE_READ);
83+
String getNextFileName(void);
8284
void rewindDirectory(void);
8385

8486
protected:

Diff for: libraries/FS/src/FSImpl.h

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class FileImpl
4343
virtual const char* name() const = 0;
4444
virtual boolean isDirectory(void) = 0;
4545
virtual FileImplPtr openNextFile(const char* mode) = 0;
46+
virtual boolean seekDir(long position);
47+
virtual String getNextFileName(void);
4648
virtual void rewindDirectory(void) = 0;
4749
virtual operator bool() = 0;
4850
};

Diff for: libraries/FS/src/vfs_api.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,36 @@ FileImplPtr VFSFileImpl::openNextFile(const char* mode)
476476
return std::make_shared<VFSFileImpl>(_fs, name.c_str(), mode);
477477
}
478478

479+
boolean VFSFileImpl::seekDir(long position){
480+
if(!_d){
481+
return false;
482+
}
483+
seekdir(_d, position);
484+
return true;
485+
}
486+
487+
488+
String VFSFileImpl::getNextFileName()
489+
{
490+
if (!_isDirectory || !_d) {
491+
return "";
492+
}
493+
struct dirent *file = readdir(_d);
494+
if (file == NULL) {
495+
return "";
496+
}
497+
if (file->d_type != DT_REG && file->d_type != DT_DIR) {
498+
return "";
499+
}
500+
String fname = String(file->d_name);
501+
String name = String(_path);
502+
if (!fname.startsWith("/") && !name.endsWith("/")) {
503+
name += "/";
504+
}
505+
name += fname;
506+
return name;
507+
}
508+
479509
void VFSFileImpl::rewindDirectory(void)
480510
{
481511
if(!_isDirectory || !_d) {

Diff for: libraries/FS/src/vfs_api.h

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class VFSFileImpl : public FileImpl
7171
const char* name() const override;
7272
time_t getLastWrite() override;
7373
boolean isDirectory(void) override;
74+
boolean seekDir(long position) override;
75+
String getNextFileName(void) override;
7476
FileImplPtr openNextFile(const char* mode) override;
7577
void rewindDirectory(void) override;
7678
operator bool();

0 commit comments

Comments
 (0)