Skip to content

Commit

Permalink
New method Directory::findFiles to search files inside directory recu…
Browse files Browse the repository at this point in the history
…rsively (#5)

New method Directory::findFiles to search files inside directory recursively
  • Loading branch information
Medex81 authored Aug 18, 2022
1 parent ed1dde1 commit 74f1b66
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions BananaCore/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ QString Directory::getAbsoluteFilePathFor(
return QDir::cleanPath(path);
}

QObjectList Directory::findFiles(const QStringList &filters) const
{
QObjectList result;
auto projDir = dynamic_cast<AbstractProjectDirectory *>(getTopDirectory());
if (projDir)
findFilesRecursive(result, projDir, QDir(getFilePath()), filters);
return result;
}

void Directory::findFilesRecursive(QObjectList &result,
AbstractProjectDirectory *projectDirectory, QDir currentDir,
const QStringList &filters) const
{
for (auto &entry : currentDir.entryInfoList(
QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot))
{
if (entry.isDir())
{
findFilesRecursive(result, projectDirectory,
QDir(entry.absoluteFilePath()), filters);
} else
{
if (filters.contains(entry.suffix()))
result.append(projectDirectory->addExistingFile(
entry.absoluteFilePath(), false));
}
}
}

Directory::Directory(const QString &name)
: AbstractDirectory(this)
, searched(false)
Expand Down
6 changes: 6 additions & 0 deletions BananaCore/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ SOFTWARE.

#include <memory>
#include <set>
#include <QDir>

namespace Banana
{
class AbstractFile;
class AbstractProjectDirectory;
class Directory
: public ObjectGroup
, public AbstractDirectory
Expand All @@ -58,6 +60,7 @@ class Directory
const QString &path, bool search = false) const;
Q_INVOKABLE inline QString getRelativeFilePathFor(
const QString &path) const;
Q_INVOKABLE QObjectList findFiles(const QStringList &filters) const;

enum class Error
{
Expand Down Expand Up @@ -155,6 +158,9 @@ class Directory
Qt::CaseSensitivity sensitivity) const override;
virtual void sortChildren(QObjectList &children) override;
virtual void childEvent(QChildEvent *event) override;
void findFilesRecursive(QObjectList &result,
AbstractProjectDirectory *projectDirectory, QDir currentDir,
const QStringList &filters) const;

private:
int searchOrder;
Expand Down

0 comments on commit 74f1b66

Please sign in to comment.