diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 08fa694..912fd1f 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -20,19 +20,14 @@ #include #include #include -#include +#include #include "tinyutf8/tinyutf8.h" #include "file.hpp" 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); - - } - + std::filesystem::path filePath = path; + this->size = ErrorOr(std::filesystem::file_size(filePath)); + switch(this->mode){ case 'w': case 'm': @@ -48,25 +43,10 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), pa default: break; } + this->isOpen = true; + this->eof = false; } -File::File(const File& file){ - this->mode = file.getMode(); - this->isOpen = file.isOpen; - this->eof = file.eof; - this->path = file.path; - this->cursorPosition = file.cursorPosition; - this->size = file.size; - - if(this->isOpen){ - this->open(); - } -} - -File& File::operator=(const File& file){ - File cpFile(file); - return cpFile; -} void File::open(){ switch(this->mode){ @@ -85,12 +65,6 @@ void File::open(){ break; } - try{ - std::filesystem::path filePath = this->path; - this->size = ErrorOr(std::filesystem::file_size(filePath)); - }catch(std::exception& e){ - this->size = ErrorOr(true, ErrorCodes::FILE_NOT_FOUND); - } } void File::close(){ @@ -98,10 +72,6 @@ void File::close(){ this->isOpen = false; } -char File::getMode() const{ - return this->mode; -} - ErrorOr File::readByte(){ if(this->size.isError){ return ErrorOr(true, this->size.errorCode); @@ -129,7 +99,7 @@ ErrorOr> File::read(uint64_t bytes){ this->fileStream.seekg(this->cursorPosition); char* buffer = new char[bytes]; - std::vector data; + std::vector data; try{ this->fileStream.read(buffer, bytes); for(uint64_t i=0; i File::readString(uint64_t bytes){ return ErrorOr(tiny_utf8::string(s)); } -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 +ErrorOr File::open(std::string path, char mode, uint64_t startPosition){ + if (!std::filesystem::exists(path)) { + return ErrorOr(true, ErrorCodes::FILE_NOT_FOUND, nullptr); } - return ErrorOr(file); + //TODO: check access perms + + return ErrorOr(new File(path, mode, startPosition)); } diff --git a/src/lib/file.hpp b/src/lib/file.hpp index 004cde6..f274d0f 100644 --- a/src/lib/file.hpp +++ b/src/lib/file.hpp @@ -45,12 +45,9 @@ class File { // may be error if not a regular file or size cannot be determined ErrorOr size; - File() = default; - File(const File& file); - File& operator=(const File& file); + File() {}; void open(); void close(); - char getMode() const; // only applicable to read and edit modes // moves the cursor to the right of the read section @@ -94,5 +91,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); -}; \ No newline at end of file + static ErrorOr open(std::string path, char mode, uint64_t startPosition=0); +}; diff --git a/src/test/file.cpp b/src/test/file.cpp index 0375dad..214f7c4 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -21,11 +21,12 @@ 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; + 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; std::cout << data << std::endl; -} \ No newline at end of file + +}