Files
devilutionX/Source/data/file.cpp
T
ephphathaandGleb Mazovetskiy 32c3316743 Load experience data from file
Also added an iterator based API, though it's not useful for this use-case. Might be nice in the future?

The field/record iterators is single-pass input iterators with shared state.
To avoid rescanning fields unnecessarily parseInt currently can only be called once, it would be possible to make these iterators bidirectional with a bit of extra state (holding onto the start pointer)

Co-authored-by: Gleb Mazovetskiy <[email protected]>
2023-08-24 05:46:44 +02:00

22 lines
693 B
C++

#include "file.hpp"
#include "engine/assets.hpp"
namespace devilution {
tl::expected<DataFile, DataFile::Error> DataFile::load(std::string_view path)
{
AssetRef ref = FindAsset(path);
if (!ref.ok())
return tl::unexpected { Error::NotFound };
const size_t size = ref.size();
// TODO: It should be possible to stream the data file contents instead of copying the whole thing into memory
std::unique_ptr<char[]> data { new char[size] };
{
AssetHandle handle = OpenAsset(std::move(ref));
if (!handle.ok() || !handle.read(data.get(), size))
return tl::unexpected { Error::ReadError };
}
return DataFile { std::move(data), size };
}
} // namespace devilution