Foreach can now be recursive
This commit is contained in:
parent
2ccc5c364c
commit
172d3ea720
|
|
@ -41,7 +41,7 @@ namespace Nz
|
||||||
|
|
||||||
inline bool Exists(std::string_view path);
|
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 GetDirectoryEntry(std::string_view path, F&& callback);
|
||||||
template<typename F> bool GetEntry(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>
|
template<typename F>
|
||||||
void VirtualDirectory::Foreach(F&& callback, bool includeDots)
|
bool VirtualDirectory::Foreach(F&& callback, bool recursive, bool includeDots)
|
||||||
{
|
{
|
||||||
if (includeDots)
|
if (includeDots)
|
||||||
{
|
{
|
||||||
|
|
@ -46,16 +46,26 @@ namespace Nz
|
||||||
{
|
{
|
||||||
Entry parentEntry = DirectoryEntry{ { parent } };
|
Entry parentEntry = DirectoryEntry{ { parent } };
|
||||||
if (!CallbackReturn(callback, std::string_view(".."), parentEntry))
|
if (!CallbackReturn(callback, std::string_view(".."), parentEntry))
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
else if (!CallbackReturn(callback, std::string_view(".."), ourselves))
|
else if (!CallbackReturn(callback, std::string_view(".."), ourselves))
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto&& entry : m_content)
|
for (auto&& entry : m_content)
|
||||||
{
|
{
|
||||||
if (!CallbackReturn(callback, std::string_view(entry.name), std::as_const(entry.entry)))
|
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)
|
if (m_resolver)
|
||||||
|
|
@ -70,9 +80,22 @@ namespace Nz
|
||||||
if (it != m_content.end() && it->name == filename)
|
if (it != m_content.end() && it->name == filename)
|
||||||
return true; //< this was already returned
|
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>
|
template<typename F>
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ TEST_CASE("VirtualDirectory", "[Core][VirtualDirectory]")
|
||||||
}
|
}
|
||||||
if (name != "." && name != "..")
|
if (name != "." && name != "..")
|
||||||
FAIL("Got file " << name);
|
FAIL("Got file " << name);
|
||||||
}, true);
|
}, false, true);
|
||||||
|
|
||||||
CHECK(dot);
|
CHECK(dot);
|
||||||
CHECK(dotDot);
|
CHECK(dotDot);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue