From 056c1e6b11f39bb3b0cde088955167dd1e594c2b Mon Sep 17 00:00:00 2001 From: Shwoomple <> Date: Sun, 2 Oct 2022 08:38:39 +0530 Subject: [PATCH] lib/file.cpp: fix read fubctions. --- src/lib/file.cpp | 35 ++++------------------------------- src/test/file.cpp | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 912fd1f..9ec7461 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -27,22 +27,7 @@ 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)); - - switch(this->mode){ - case 'w': - case 'm': - this->fileStream.open(path, std::fstream::out | std::fstream::binary); - break; - case 'r': - case 'e': - this->fileStream.open(path, std::fstream::in | std::fstream::binary); - break; - case 'a': - this->fileStream.open(path, std::fstream::app | std::fstream::binary); - break; - default: - break; - } + this->open(); this->isOpen = true; this->eof = false; } @@ -73,29 +58,20 @@ void File::close(){ } ErrorOr File::readByte(){ - if(this->size.isError){ - return ErrorOr(true, this->size.errorCode); - } - - this->cursorPosition += 1; this->fileStream.seekg(this->cursorPosition); char* byte = new char; try{ this->fileStream.read(byte, 1); - delete byte; }catch(std::exception& e){ return ErrorOr(true, ErrorCodes::FILE_READ_FAILED); } + + this->cursorPosition += 1; return ErrorOr((uint8_t) *byte); } ErrorOr> File::read(uint64_t bytes){ - if(this->size.isError){ - return ErrorOr>(true, this->size.errorCode); - } - - this->cursorPosition += 1; this->fileStream.seekg(this->cursorPosition); char* buffer = new char[bytes]; @@ -109,14 +85,11 @@ ErrorOr> File::read(uint64_t bytes){ }catch(std::exception& e){ return ErrorOr>(true, ErrorCodes::FILE_READ_FAILED); } + this->cursorPosition += bytes; return ErrorOr>(data); } ErrorOr File::readString(uint64_t bytes){ - if(this->size.isError){ - return ErrorOr(true, this->size.errorCode); - } - ErrorOr> data = this->read(bytes); if(data.isError){ return ErrorOr(true, data.errorCode); diff --git a/src/test/file.cpp b/src/test/file.cpp index 214f7c4..3701cc4 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -13,6 +13,7 @@ // version 3 along with this program. // If not, see https://www.gnu.org/licenses/agpl-3.0.en.html +#include #include "assert.hpp" #include "tinyutf8/tinyutf8.h" #include "../lib/file.hpp" @@ -23,10 +24,19 @@ int main(){ std::cout << "################################################################################" << std::endl; File* file; - //read test + //readByte 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; - std::cout << data << std::endl; + uint8_t byte = file->readByte().value; + ASSERT(byte == 'T'); + std::cout << "Passed read byte test." << std::endl; + //read test + std::vector bytes = file->read(5).value; + //cursorPosition has already moved forward by one + ASSERT(bytes == std::vector({'e', 's', 't', ' ', 's'})); + std::cout << "Passed read test." << std::endl; + + //readString test + tiny_utf8::string data = file->readString(5).value; + std::cout << data << std::endl; }