From e9b09e7b9fde22389ab2070567691217579f431f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Thu, 16 Oct 2014 14:46:18 +0300 Subject: [PATCH] libcore|FS: Added methods for iterating all found files Supports lambda functions. --- .../libcore/include/de/filesys/filesystem.h | 8 ++++++- doomsday/libcore/include/de/libcore.h | 8 +++++++ doomsday/libcore/src/filesys/filesystem.cpp | 23 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/doomsday/libcore/include/de/filesys/filesystem.h b/doomsday/libcore/include/de/filesys/filesystem.h index eb940afbfa..33b9e931b6 100644 --- a/doomsday/libcore/include/de/filesys/filesystem.h +++ b/doomsday/libcore/include/de/filesys/filesystem.h @@ -26,6 +26,7 @@ #include "../System" #include +#include /** * @defgroup fs File System @@ -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 func); + template int findAll(Predicate exclusion, String const &partialPath, FoundFiles &found) const { findAll(partialPath, found); @@ -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 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 diff --git a/doomsday/libcore/include/de/libcore.h b/doomsday/libcore/include/de/libcore.h index edb17cb5ac..7109f76f6f 100644 --- a/doomsday/libcore/include/de/libcore.h +++ b/doomsday/libcore/include/de/libcore.h @@ -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 diff --git a/doomsday/libcore/src/filesys/filesystem.cpp b/doomsday/libcore/src/filesys/filesystem.cpp index 136539350b..53b2faffb1 100644 --- a/doomsday/libcore/src/filesys/filesystem.cpp +++ b/doomsday/libcore/src/filesys/filesystem.cpp @@ -216,6 +216,17 @@ int FileSystem::findAll(String const &path, FoundFiles &found) const return int(found.size()); } +Iteration FileSystem::forAll(String const &partialPath, std::function 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"); @@ -223,6 +234,18 @@ int FileSystem::findAllOfType(String const &typeIdentifier, String const &path, return findAllOfTypes(StringList() << typeIdentifier, path, found); } +Iteration FileSystem::forAllOfType(String const &typeIdentifier, String const &path, + std::function 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");