Skip to content

getNextFileName: Get info wether filename is a file or directory #8068

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions libraries/FS/src/vfs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,31 @@ 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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That check is good for sure. Can you just remove the spaces before the part so its aligned with other code?

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