Skip to content

Commit

Permalink
libcore|FS: Added methods for iterating all found files
Browse files Browse the repository at this point in the history
Supports lambda functions.
  • Loading branch information
skyjake committed Oct 16, 2014
1 parent a756222 commit e9b09e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion doomsday/libcore/include/de/filesys/filesystem.h
Expand Up @@ -26,6 +26,7 @@
#include "../System"

#include <QFlags>
#include <functional>

/**
* @defgroup fs File System
Expand Down Expand Up @@ -180,6 +181,8 @@ class DENG2_PUBLIC FileSystem : public System
*/
int findAll(String const &partialPath, FoundFiles &found) const;

Iteration forAll(String const &partialPath, std::function<Iteration (File &)> func);

template <typename Predicate>
int findAll(Predicate exclusion, String const &partialPath, FoundFiles &found) const {
findAll(partialPath, found);
Expand All @@ -189,8 +192,11 @@ class DENG2_PUBLIC FileSystem : public System

int findAllOfType(String const &typeIdentifier, String const &path, FoundFiles &found) const;

int findAllOfTypes(StringList const &typeIdentifiers, String const &path, FoundFiles &found) const;
Iteration forAllOfType(String const &typeIdentifier, String const &path,
std::function<Iteration (File &)> func);

int findAllOfTypes(StringList const &typeIdentifiers, String const &path, FoundFiles &found) const;

/**
* Finds a single file matching a full or partial path. The search is
* done using the file system's index; no recursive descent into
Expand Down
8 changes: 8 additions & 0 deletions doomsday/libcore/include/de/libcore.h
Expand Up @@ -470,6 +470,14 @@ enum ClockDirection {
Clockwise = 1
};

/**
* Status to return from abortable iteration loops that use callbacks per iteration.
*/
enum Iteration {
IterAbort = 0,
IterContinue = 1
};

/**
* All serialization in all contexts use a common protocol version number.
* Whenever anything changes in serialization, the protocol version needs to be
Expand Down
23 changes: 23 additions & 0 deletions doomsday/libcore/src/filesys/filesystem.cpp
Expand Up @@ -216,13 +216,36 @@ int FileSystem::findAll(String const &path, FoundFiles &found) const
return int(found.size());
}

Iteration FileSystem::forAll(String const &partialPath, std::function<Iteration (File &)> func)
{
FoundFiles files;
findAll(partialPath, files);
for(File *f : files)
{
if(func(*f) == IterAbort) return IterAbort;
}
return IterContinue;
}

int FileSystem::findAllOfType(String const &typeIdentifier, String const &path, FoundFiles &found) const
{
LOG_AS("FS::findAllOfType");

return findAllOfTypes(StringList() << typeIdentifier, path, found);
}

Iteration FileSystem::forAllOfType(String const &typeIdentifier, String const &path,
std::function<Iteration (File &)> func)
{
FoundFiles files;
findAllOfType(typeIdentifier, path, files);
for(File *f : files)
{
if(func(*f) == IterAbort) return IterAbort;
}
return IterContinue;
}

int FileSystem::findAllOfTypes(StringList const &typeIdentifiers, String const &path, FoundFiles &found) const
{
LOG_AS("FS::findAllOfTypes");
Expand Down

0 comments on commit e9b09e7

Please sign in to comment.