Core/VirtualDirectory: Add GetFileContent method
This commit is contained in:
@@ -44,6 +44,7 @@ namespace Nz
|
||||
template<typename F> void Foreach(F&& callback, bool includeDots = false);
|
||||
|
||||
template<typename F> bool GetEntry(std::string_view path, F&& callback);
|
||||
template<typename F> bool GetFileContent(std::string_view path, F&& callback);
|
||||
|
||||
inline DirectoryEntry& StoreDirectory(std::string_view path, VirtualDirectoryPtr directory);
|
||||
inline PhysicalDirectoryEntry& StoreDirectory(std::string_view path, std::filesystem::path directoryPath);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/VirtualDirectory.hpp>
|
||||
#include <Nazara/Core/File.hpp>
|
||||
#include <Nazara/Core/StringExt.hpp>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
@@ -153,6 +154,45 @@ namespace Nz
|
||||
});
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
bool VirtualDirectory::GetFileContent(std::string_view path, F&& callback)
|
||||
{
|
||||
return GetEntry(path, [&](const Entry& entry)
|
||||
{
|
||||
return std::visit([&](auto&& entry)
|
||||
{
|
||||
using T = std::decay_t<decltype(entry)>;
|
||||
|
||||
using P1 = const void*;
|
||||
using P2 = std::size_t;
|
||||
|
||||
if constexpr (std::is_same_v<T, DataPointerEntry>)
|
||||
{
|
||||
return CallbackReturn(callback, static_cast<P1>(entry.data), SafeCast<P2>(entry.size));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, FileContentEntry>)
|
||||
{
|
||||
return CallbackReturn(callback, static_cast<P1>(entry.data.data()), SafeCast<P2>(entry.data.size()));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, PhysicalFileEntry>)
|
||||
{
|
||||
std::optional<std::vector<UInt8>> source = File::ReadWhole(entry.filePath);
|
||||
if (!source.has_value())
|
||||
return false;
|
||||
|
||||
return CallbackReturn(callback, static_cast<P1>(source->data()), SafeCast<P2>(source->size()));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, DirectoryEntry> || std::is_same_v<T, PhysicalDirectoryEntry>)
|
||||
{
|
||||
NazaraError("entry is a directory");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
static_assert(AlwaysFalse<T>(), "incomplete visitor");
|
||||
}, entry);
|
||||
});
|
||||
}
|
||||
|
||||
inline auto VirtualDirectory::StoreDirectory(std::string_view path, VirtualDirectoryPtr directory) -> DirectoryEntry&
|
||||
{
|
||||
assert(!path.empty());
|
||||
|
||||
Reference in New Issue
Block a user