From 8dea1f2d3128e073a28bd294944d483c96409a47 Mon Sep 17 00:00:00 2001 From: Shwoomple <> Date: Thu, 20 Oct 2022 15:12:15 +0530 Subject: [PATCH] lib/file: Implement modify functions --- src/lib/file.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/test/file.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 4a93ca2..4913361 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -186,6 +186,53 @@ ErrorOrVoid File::insertByte(uint8_t byte){ readData.insert(readData.begin()+this->cursorPosition, byte); this->fileStream.seekg(0); this->write(readData); + this->cursorPosition++; + + }catch(std::exception& e){ + failure = true; + } + return failure ? ErrorOrVoid(true, ErrorCodes::UNKNOWN) : ErrorOrVoid(); +} + + +ErrorOrVoid File::insert(std::vector data){ + bool failure = false; + + 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); + + readData.insert(readData.begin()+this->cursorPosition, data.begin(), data.end()); + this->fileStream.seekg(0); + this->write(readData); + this->cursorPosition += data.size(); + + }catch(std::exception& e){ + failure = true; + } + return failure ? ErrorOrVoid(true, ErrorCodes::UNKNOWN) : ErrorOrVoid(); +} + +ErrorOrVoid File::insertString(tiny_utf8::string string){ + bool failure = false; + + try{ + uint8_t* buffer = new uint8_t[this->size.value]; + tiny_utf8::string readData; + + this->fileStream.read(reinterpret_cast(buffer), this->size.value); + + readData = tiny_utf8::string((char *) buffer); + readData.insert(readData.begin()+this->cursorPosition, string); + + this->fileStream.seekg(0); + + //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(); }catch(std::exception& e){ failure = true; diff --git a/src/test/file.cpp b/src/test/file.cpp index 39472d5..ad5dc5b 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -138,4 +138,30 @@ int main(){ ASSERT(modifyByteString == "Hallo,, Ich bin Shwoomple."); std::cout << "Passed modify byte test" << std::endl; + + modifyFile->open(); + modifyFile->cursorPosition = 6; + modifyFile->insert(std::vector({' ', 'H', 'i'})); + modifyFile->close(); + + readFile->open(); + readFile->cursorPosition = 0; + tiny_utf8::string modifyBytesString = readFile->readString(readFile->size.value).value; + readFile->close(); + + ASSERT(modifyBytesString == "Hallo, Hi, Ich bin Shwoomple."); + std::cout << "Passed modify test" << std::endl; + + modifyFile->open(); + modifyFile->cursorPosition = 9; + modifyFile->insertString(" THE CAKE IS A LIE"); + modifyFile->close(); + + readFile->open(); + readFile->cursorPosition = 0; + tiny_utf8::string modifyString = readFile->readString(readFile->size.value).value; + readFile->close(); + + ASSERT(modifyString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple."); + std::cout << "Passed modify string test" << std::endl; }