#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 File::read(const std::uint64_t size) { // create a buffer std::vector buffer(size); // read into the buffer this->stream.read( reinterpret_cast(buffer.data()), static_cast(size) ); // set the write cursor to the read cursor (synchronise) this->stream.seekp(this->stream.tellg()); return buffer; } void File::write(const std::vector& buffer) { // read onto the buffer this->stream.write( reinterpret_cast(buffer.data()), static_cast(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(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); }