[Filesystem] Cleanup

This commit is contained in:
Diavolo 2022-08-11 12:31:19 +02:00
parent 49ed76807b
commit 7b1b135e3f
No known key found for this signature in database
GPG Key ID: FA77F074E98D98A5
2 changed files with 29 additions and 27 deletions

View File

@ -15,21 +15,23 @@ namespace Components
int handle; int handle;
const auto len = Game::FS_FOpenFileReadForThread(filePath.data(), &handle, thread); const auto len = Game::FS_FOpenFileReadForThread(filePath.data(), &handle, thread);
if (handle) if (!handle)
{ {
auto* buf = AllocateFile(len + 1); return;
[[maybe_unused]] auto bytesRead = Game::FS_Read(buf, len, handle);
assert(bytesRead == len);
buf[len] = '\0';
Game::FS_FCloseFile(handle);
this->buffer.append(buf, len);
FreeFile(buf);
} }
auto* buf = AllocateFile(len + 1);
[[maybe_unused]] auto bytesRead = Game::FS_Read(buf, len, handle);
assert(bytesRead == len);
buf[len] = '\0';
Game::FS_FCloseFile(handle);
this->buffer.append(buf, len);
FreeFile(buf);
} }
void FileSystem::RawFile::read() void FileSystem::RawFile::read()

View File

@ -8,7 +8,7 @@ namespace Components
class AbstractFile class AbstractFile
{ {
public: public:
virtual ~AbstractFile() {}; virtual ~AbstractFile() = default;
virtual bool exists() = 0; virtual bool exists() = 0;
virtual std::string getName() = 0; virtual std::string getName() = 0;
@ -19,12 +19,12 @@ namespace Components
{ {
public: public:
File() = default; File() = default;
File(std::string file) : filePath{std::move(file)} { this->read(); }; File(std::string file) : filePath{std::move(file)} { this->read(); }
File(std::string file, Game::FsThread thread) : filePath{std::move(file)} { this->read(thread); }; File(std::string file, Game::FsThread thread) : filePath{std::move(file)} { this->read(thread); }
bool exists() override { return !this->buffer.empty(); }; bool exists() override { return !this->buffer.empty(); }
std::string getName() override { return this->filePath; }; std::string getName() override { return this->filePath; }
std::string& getBuffer() override { return this->buffer; }; std::string& getBuffer() override { return this->buffer; }
private: private:
std::string filePath; std::string filePath;
@ -36,12 +36,12 @@ namespace Components
class RawFile : public AbstractFile class RawFile : public AbstractFile
{ {
public: public:
RawFile() {}; RawFile() = default;
RawFile(const std::string& file) : filePath(file) { this->read(); }; RawFile(std::string file) : filePath(std::move(file)) { this->read(); }
bool exists() override { return !this->buffer.empty(); }; bool exists() override { return !this->buffer.empty(); }
std::string getName() override { return this->filePath; }; std::string getName() override { return this->filePath; }
std::string& getBuffer() override { return this->buffer; }; std::string& getBuffer() override { return this->buffer; }
private: private:
std::string filePath; std::string filePath;
@ -53,7 +53,7 @@ namespace Components
class FileReader class FileReader
{ {
public: public:
FileReader() : handle(0), size(-1), name() {}; FileReader() : handle(0), size(-1), name() {}
FileReader(const std::string& file); FileReader(const std::string& file);
~FileReader(); ~FileReader();
@ -73,8 +73,8 @@ namespace Components
class FileWriter class FileWriter
{ {
public: public:
FileWriter(const std::string& file, bool append = false) : handle(0), filePath(file) { this->open(append); }; FileWriter(std::string file, bool append = false) : handle(0), filePath(std::move(file)) { this->open(append); }
~FileWriter() { this->close(); }; ~FileWriter() { this->close(); }
void write(const std::string& data); void write(const std::string& data);