Skip to content

getNextFileName: Get info whether filename is a file or directory #8079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libraries/FS/src/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ String File::getNextFileName(void)

}

String File::getNextFileName(bool *isDir)
{
if (!_p) {
return "";
}
return _p->getNextFileName(isDir);

}

void File::rewindDirectory(void)
{
if (!*this) {
Expand Down
1 change: 1 addition & 0 deletions libraries/FS/src/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class File : public Stream
boolean seekDir(long position);
File openNextFile(const char* mode = FILE_READ);
String getNextFileName(void);
String getNextFileName(boolean *isDir);
void rewindDirectory(void);

protected:
Expand Down
1 change: 1 addition & 0 deletions libraries/FS/src/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class FileImpl
virtual FileImplPtr openNextFile(const char* mode) = 0;
virtual boolean seekDir(long position);
virtual String getNextFileName(void);
virtual String getNextFileName(bool *isDir);
virtual void rewindDirectory(void) = 0;
virtual operator bool() = 0;
};
Expand Down
26 changes: 26 additions & 0 deletions libraries/FS/src/vfs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,32 @@ String VFSFileImpl::getNextFileName()
return name;
}

String VFSFileImpl::getNextFileName(bool *isDir)
{
if (!_isDirectory || !_d) {
return "";
}
struct dirent *file = readdir(_d);
if (file == NULL) {
return "";
}
if (file->d_type != DT_REG && file->d_type != DT_DIR) {
return "";
}
String fname = String(file->d_name);
String name = String(_path);
if (!fname.startsWith("/") && !name.endsWith("/")) {
name += "/";
}
name += fname;

// check entry is a directory
if (isDir) {
*isDir = (file->d_type == DT_DIR);
}
return name;
}

void VFSFileImpl::rewindDirectory(void)
{
if(!_isDirectory || !_d) {
Expand Down
1 change: 1 addition & 0 deletions libraries/FS/src/vfs_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class VFSFileImpl : public FileImpl
boolean isDirectory(void) override;
boolean seekDir(long position) override;
String getNextFileName(void) override;
String getNextFileName(bool *isDir) override;
FileImplPtr openNextFile(const char* mode) override;
void rewindDirectory(void) override;
operator bool();
Expand Down