Atlas-Launcher/source/script/module/filesystem/File.cpp

65 lines
1.8 KiB
C++

#include "File.hpp"
#include "utils/file/openMode.hpp"
File::File(const vfs::VirtualFileSystem* fileSystem, const std::filesystem::path& virtualPath) {
this->fileSystem = fileSystem;
this->virtualPath = virtualPath;
}
void File::open(const std::string& mode) {
// allocate the internal file
this->stream = std::fstream(this->getRealPath(), atlas::utils::file::getOpenMode(mode));
}
std::vector<std::uint8_t> File::read(const std::uint64_t size) {
// create a buffer
std::vector<std::uint8_t> buffer(size);
// read into the buffer
this->stream.read(
reinterpret_cast<std::fstream::char_type*>(buffer.data()),
static_cast<std::streamsize>(size)
);
// set the write cursor to the read cursor (synchronise)
this->stream.seekp(this->stream.tellg());
return buffer;
}
void File::write(const std::vector<std::uint8_t>& buffer) {
// read onto the buffer
this->stream.write(
reinterpret_cast<const std::fstream::char_type*>(buffer.data()),
static_cast<std::streamsize>(buffer.size())
);
// set the read cursor to the write cursor (synchronise)
this->stream.seekg(this->stream.tellp());
}
std::size_t File::tell() {
// return the stream cursor position.
// The read and write cursors are always synchronised.
return this->stream.tellp();
}
void File::seek(const std::size_t position) {
// set the new stream cursor position.
// synchronise the two cursors.
this->stream.seekg(static_cast<std::streamoff>(position));
this->stream.seekp(this->stream.tellg());
}
void File::close() {
// close the stream
this->stream.close();
}
std::filesystem::path File::getRealPath() const {
// get the real path from the internal virtual path
return this->fileSystem->resolve(this->virtualPath);
}