From e7ce6f5cd40f6e20c2831da42808d6f93bb9a4ba Mon Sep 17 00:00:00 2001 From: Shwoomple <> Date: Thu, 20 Oct 2022 10:27:00 +0530 Subject: [PATCH] lib/file: Fix write function bugs --- .gitignore | 2 ++ src/lib/file.cpp | 8 ++++++-- src/test/file.cpp | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index d6d4cf1..958b0ca 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ #vscode .vscode + +writeTest \ No newline at end of file diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 5c7863a..301cf17 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -26,7 +26,11 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){ std::filesystem::path filePath = path; - this->size = ErrorOr(std::filesystem::file_size(filePath)); + if(this->mode == 'w' || this->mode == 'a'){ + this->size = ErrorOr(0); + }else{ + this->size = ErrorOr(std::filesystem::file_size(filePath)); + } this->open(); } @@ -166,7 +170,7 @@ ErrorOrVoid File::writeString(tiny_utf8::string data){ } ErrorOr File::open(std::string path, char mode, uint64_t startPosition){ - if (!std::filesystem::exists(path)) { + if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) { return ErrorOr(true, ErrorCodes::FILE_NOT_FOUND, nullptr); } diff --git a/src/test/file.cpp b/src/test/file.cpp index ea49669..c3e26ab 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -48,11 +48,11 @@ int main(){ File *readFile; //writeByte test - writeFile = File::open("resources/unicode_data/writeTest", 'w').value; + writeFile = File::open("resources/writeTest", 'w').value; writeFile->writeByte('a'); writeFile->close(); - readFile = File::open("resources/unicode_data/writeTest", 'r').value; + readFile = File::open("resources/writeTest", 'r').value; uint8_t testByte = readFile->readByte().value; ASSERT(testByte == 'a'); std::cout << "Passed write byte test." << std::endl;