Foreach can now be recursive
This commit is contained in:
@@ -41,7 +41,7 @@ namespace Nz
|
||||
|
||||
inline bool Exists(std::string_view path);
|
||||
|
||||
template<typename F> void Foreach(F&& callback, bool includeDots = false);
|
||||
template<typename F> bool Foreach(F&& callback, bool recursive = false, bool includeDots = false);
|
||||
|
||||
template<typename F> bool GetDirectoryEntry(std::string_view path, F&& callback);
|
||||
template<typename F> bool GetEntry(std::string_view path, F&& callback);
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Nz
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void VirtualDirectory::Foreach(F&& callback, bool includeDots)
|
||||
bool VirtualDirectory::Foreach(F&& callback, bool recursive, bool includeDots)
|
||||
{
|
||||
if (includeDots)
|
||||
{
|
||||
@@ -46,16 +46,26 @@ namespace Nz
|
||||
{
|
||||
Entry parentEntry = DirectoryEntry{ { parent } };
|
||||
if (!CallbackReturn(callback, std::string_view(".."), parentEntry))
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
else if (!CallbackReturn(callback, std::string_view(".."), ourselves))
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto&& entry : m_content)
|
||||
{
|
||||
if (!CallbackReturn(callback, std::string_view(entry.name), std::as_const(entry.entry)))
|
||||
return;
|
||||
return false;
|
||||
|
||||
if (recursive)
|
||||
{
|
||||
if (std::holds_alternative<DirectoryEntry>(entry.entry))
|
||||
{
|
||||
DirectoryEntry& child = std::get<DirectoryEntry>(entry.entry);
|
||||
if (!child.directory->Foreach(callback, recursive, includeDots))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_resolver)
|
||||
@@ -70,9 +80,22 @@ namespace Nz
|
||||
if (it != m_content.end() && it->name == filename)
|
||||
return true; //< this was already returned
|
||||
|
||||
return CallbackReturn(callback, filename, std::move(entry));
|
||||
if (!CallbackReturn(callback, filename, entry))
|
||||
return false;
|
||||
|
||||
if (recursive)
|
||||
{
|
||||
if (std::holds_alternative<DirectoryEntry>(entry))
|
||||
{
|
||||
DirectoryEntry& child = std::get<DirectoryEntry>(entry);
|
||||
if (!child.directory->Foreach(callback, recursive, includeDots))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
|
||||
Reference in New Issue
Block a user