old-nall/vfs/memory/file.hpp

49 lines
1.2 KiB
C++
Raw Normal View History

2016-07-03 11:35:22 +00:00
#pragma once
namespace nall { namespace vfs { namespace memory {
struct file : vfs::file {
~file() { delete[] _data; }
2016-08-09 18:27:40 +00:00
static auto open(const uint8_t* data, uintmax size) -> vfs::shared::file {
2016-07-03 11:35:22 +00:00
auto instance = shared_pointer<file>{new file};
instance->_open(data, size);
return instance;
}
2016-08-09 18:27:40 +00:00
auto size() const -> uintmax override { return _size; }
auto offset() const -> uintmax override { return _offset; }
2016-07-03 11:35:22 +00:00
2016-08-09 18:27:40 +00:00
auto seek(intmax offset, index mode) -> void override {
if(mode == index::absolute) _offset = (uintmax)offset;
if(mode == index::relative) _offset += (intmax)offset;
2016-07-03 11:35:22 +00:00
}
auto read() -> uint8_t override {
if(_offset >= _size) return 0x00;
return _data[_offset++];
}
auto write(uint8_t data) -> void override {
if(_offset >= _size) return;
_data[_offset++] = data;
}
private:
file() = default;
file(const file&) = delete;
auto operator=(const file&) -> file& = delete;
2016-08-09 18:27:40 +00:00
auto _open(const uint8_t* data, uintmax size) -> void {
2016-07-03 11:35:22 +00:00
_size = size;
_data = new uint8_t[size];
nall::memory::copy(_data, data, size);
}
uint8_t* _data = nullptr;
2016-08-09 18:27:40 +00:00
uintmax _size = 0;
uintmax _offset = 0;
2016-07-03 11:35:22 +00:00
};
}}}