From d794bce28802ee742944fadfefbc4c91ec0a9f07 Mon Sep 17 00:00:00 2001 From: Shwoomple <> Date: Wed, 26 Oct 2022 19:18:11 +0530 Subject: [PATCH] lib/file.cpp: Implement cutByte function --- src/lib/file.cpp | 29 ++++++++++++++++++++++++++--- src/test/file.cpp | 16 ++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 4913361..92be7a5 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -187,7 +187,7 @@ ErrorOrVoid File::insertByte(uint8_t byte){ this->fileStream.seekg(0); this->write(readData); this->cursorPosition++; - + delete buffer; }catch(std::exception& e){ failure = true; } @@ -209,7 +209,7 @@ ErrorOrVoid File::insert(std::vector data){ this->fileStream.seekg(0); this->write(readData); this->cursorPosition += data.size(); - + delete buffer; }catch(std::exception& e){ failure = true; } @@ -233,13 +233,36 @@ ErrorOrVoid File::insertString(tiny_utf8::string string){ //TODO: fix hack. tinyutf8 appends "_utf-8" when readData is assigned: tiny_utf8::string((char *) buffer); this->writeString(readData.substr(0, readData.find("_utf-8"))); this->cursorPosition += string.size(); - + delete buffer; }catch(std::exception& e){ failure = true; } return failure ? ErrorOrVoid(true, ErrorCodes::UNKNOWN) : ErrorOrVoid(); } +ErrorOr File::cutByte(){ + bool failure = false; + uint8_t byte; + try{ + uint8_t* buffer = new uint8_t[this->size.value]; + std::vector readData; + + this->fileStream.read(reinterpret_cast(buffer), this->size.value); + readData = std::vector(buffer, buffer+this->size.value); + + byte = readData[this->cursorPosition]; + readData.erase(readData.begin() + this->cursorPosition); + + this->fileStream.seekg(0); + this->write(readData); + this->cursorPosition++; + delete buffer; + }catch(std::exception& e){ + failure = true; + } + return failure ? ErrorOr(true, ErrorCodes::UNKNOWN) : ErrorOr(byte); +} + ErrorOr File::open(std::string path, char mode, uint64_t startPosition){ 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 ad5dc5b..29613ef 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -123,6 +123,7 @@ int main(){ delete appendFile; + //test insert functions File *modifyFile; modifyFile = File::open("resources/writeTest", 'm').value; @@ -164,4 +165,19 @@ int main(){ ASSERT(modifyString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple."); std::cout << "Passed modify string test" << std::endl; + + //test cut functions + modifyFile->open(); + modifyFile->cursorPosition = modifyFile->size.value-1; + ErrorOr cutByte = modifyFile->cutByte(); + modifyFile->close(); + + readFile->open(); + readFile->cursorPosition = 0; + tiny_utf8::string cutByteString = readFile->readString(readFile->size.value).value; + readFile->close(); + + ASSERT(cutByte.value == '.'); + ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple"); + std::cout << "Passed cut byte test." << std::endl; }