23#include "utility/system_io/file.hpp"
35 _content.push_back(
static_cast<std::byte
>(c));
40 const std::vector<std::byte> &content)
50 if (ptr ==
nullptr || size == 0 || nmemb == 0) {
54 if (size > std::numeric_limits<size_t>::max() / nmemb) {
58 if (_pos > _content.size()) {
59 _pos = _content.size();
62 const size_t bytesToWrite = size * nmemb;
63 const auto *bytes =
static_cast<const std::byte *
>(ptr);
64 const size_t lenBefore = _content.size();
66 _content.insert(_content.begin() +
static_cast<std::ptrdiff_t
>(_pos), bytes,
67 bytes + bytesToWrite);
69 return (_content.size() - lenBefore) / size;
74 if (ptr ==
nullptr || size == 0 || count == 0 || _pos >= _content.size()) {
78 if (size > std::numeric_limits<size_t>::max() / count) {
82 size_t toRead = size * count;
83 toRead = std::min(toRead, _content.size() - _pos);
84 std::memcpy(ptr, _content.data() + _pos, toRead);
91 if (size == 0 || count == 0 || _pos >= _content.size()) {
96 if (size > std::numeric_limits<size_t>::max() / count) {
101 size_t toRead = std::min(size * count, _content.size() - _pos);
103 std::memcpy(str.data(), _content.data() + _pos, toRead);
105 return toRead / size;
117 newPos =
static_cast<long>(_pos) + offset;
120 newPos =
static_cast<long>(_content.size()) + offset;
130 _pos = std::min(
static_cast<size_t>(newPos), _content.size());
147 if (count == 0 || _pos >= _content.size()) {
151 size_t toRemove = std::min(count, _content.size() - _pos);
152 _content.erase(_content.begin() +
static_cast<std::ptrdiff_t
>(_pos),
154 +
static_cast<std::ptrdiff_t
>(_pos + toRemove));
std::string content() const
Returns the content of the file as a string view.
File(const std::string &path, const std::string &content)
Constructs a File object with its content and size.
size_t read(void *ptr, size_t size, size_t count)
Reads data from the file.
~File()
Destructs the File object.
size_t remove(size_t count)
Removes a number of elements from the current position.
Seek
The Seek enum represents the different seek modes for file operations.
@ END
Seek from the current position in the file
@ CUR
Seek from the beginning of the file
std::vector< std::byte > _content
In-memory content of the asset.
int seek(long offset, Seek whence)
Seeks to a specific position in the file.
void clear()
Clears the content of the file.
size_t write(const void *ptr, size_t size, size_t nmemb)
Writes data to the file.
size_t tell() const
Returns the current position in the file.