diff --git a/src/lib/error.hpp b/src/lib/error.hpp index 5380322..515797f 100644 --- a/src/lib/error.hpp +++ b/src/lib/error.hpp @@ -93,7 +93,7 @@ namespace ErrorCodes { //file errors const uint8_t FILE_READ_FAILED = 9; - const uint8_t FILE_NOT_FOUND = 10; + const uint8_t FILE_NOT_FOUND = 10;brea const uint8_t UNIMPLEMENTED = 254; diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 11d30cf..179cddc 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -26,6 +26,14 @@ #include "error.h" File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){ + try{ + std::filesystem::path filePath = path; + this->size = ErrorOr(std::filesystem::file_size(filePath)); + }catch(std::exception e){ + this->size = ErrorOr(true, ErrorCodes::FILE_NOT_FOUND); + + } + switch(this->mode){ case 'w': case 'm': @@ -41,13 +49,6 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), pa default: break; } - - try{ - std::filesystem::path filePath = path; - this->size = ErrorOr(std::filesystem::file_size(filePath)); - }catch(std::exception e){ - this->size = ErrorOr(true, ErrorCodes::FILE_NOT_FOUND); - } } File::File(const File& file){ @@ -158,14 +159,14 @@ ErrorOr File::readString(uint64_t bytes){ return ErrorOr(tiny_utf8::string(s)); } -ErrorOr File::open(std::string path, char mode, uint64_t startPosition){ - File *file = new File(path, mode, startPosition); - file->isOpen = true; - file->eof = false; +ErrorOr File::open(std::string path, char mode, uint64_t startPosition){ + File file(path, mode, startPosition); + file.isOpen = true; + file.eof = false; - if(file->size.isError){ - return ErrorOr(true, file->size.errorCode); //if file is not found + if(file.size.isError){ + return ErrorOr(true, file.size.errorCode); //if file is not found } - return ErrorOr(file); + return ErrorOr(file); } diff --git a/src/lib/file.hpp b/src/lib/file.hpp index d0e5229..004cde6 100644 --- a/src/lib/file.hpp +++ b/src/lib/file.hpp @@ -94,5 +94,5 @@ class File { // // A startPosition of 0xFFFFFFFF is considered to be the end of // the file whatever its size. - static ErrorOr open(std::string path, char mode, uint64_t startPosition=0); + static ErrorOr open(std::string path, char mode, uint64_t startPosition=0); }; \ No newline at end of file diff --git a/src/test/file.cpp b/src/test/file.cpp index 48bd9e4..0375dad 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -21,11 +21,11 @@ int main(){ std::cout << "################################################################################" << std::endl; std::cout << "File tests" << std::endl; std::cout << "################################################################################" << std::endl; - File *file; + File file; //read test file = File::open("../../resources/unicode_data/normal_utf-8", 'r').value; - tiny_utf8::string data = file->readString(file->size.value).value; - std::cout << file->size.isError << std::endl; + tiny_utf8::string data = file.readString(file.size.value).value; + std::cout << file.size.isError << std::endl; std::cout << data << std::endl; } \ No newline at end of file