diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 15a0b45..c6ffb31 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -65,6 +65,8 @@ void File::open(){ void File::close(){ this->fileStream.close(); + std::filesystem::path filePath = this->path; + this->size = ErrorOr(std::filesystem::file_size(filePath)); this->isOpen = false; } @@ -255,7 +257,7 @@ ErrorOr File::cutByte(){ std::filesystem::resize_file(this->path, readData.size()); this->fileStream.seekg(0); this->write(readData); - this->cursorPosition++; + //this->cursorPosition++; delete[] buffer; }catch(std::exception& e){ failure = true; @@ -280,7 +282,7 @@ ErrorOr> File::cut(uint64_t length){ std::filesystem::resize_file(this->path, readData.size()); this->fileStream.seekg(0); this->write(readData); - this->cursorPosition += length; + //this->cursorPosition += length; delete[] buffer; }catch(std::exception& e){ failure = true; @@ -289,6 +291,30 @@ ErrorOr> File::cut(uint64_t length){ return failure ? ErrorOr>(true, ErrorCodes::UNKNOWN) :ErrorOr>(bytes); } +ErrorOr File::cutString(uint64_t length){ + bool failure = false; + tiny_utf8::string cutString; + 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, 0, this->size.value); + + cutString = readData.substr(this->cursorPosition, length); + + std::filesystem::resize_file(this->path, readData.size()-cutString.size()); + this->fileStream.seekg(0); + this->writeString(readData.substr(this->cursorPosition+length)); + //this->cursorPosition += length; + delete[] buffer; + }catch(std::exception& e){ + failure = true; + } + + return failure ? ErrorOr(true, ErrorCodes::UNKNOWN) : ErrorOr(cutString); +} + 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 eddc358..0cacab6 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -177,6 +177,7 @@ int main(){ tiny_utf8::string cutByteString = readFile->readString(readFile->size.value).value; readFile->close(); + ASSERT(modifyFile->cursorPosition == modifyFile->size.value); ASSERT(cutByte.value == '.'); ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple"); std::cout << "Passed cut byte test." << std::endl; @@ -191,7 +192,24 @@ int main(){ tiny_utf8::string cutBytesString = readFile->readString(readFile->size.value).value; readFile->close(); + + ASSERT(modifyFile->cursorPosition == 9); ASSERT(cutBytes.value == std::vector({' ', 'T', 'H', 'E', ' ', 'C', 'A', 'K', 'E', ' ', 'I', 'S', ' ','A', ' ', 'L', 'I', 'E'})) ASSERT(cutBytesString == "Hallo, Hi, Ich bin Shwoomple"); std::cout << "Passed cut test." << std::endl; + + modifyFile->open(); + modifyFile->cursorPosition = 0; + ErrorOr cutString = modifyFile->cutString(7); + modifyFile->close(); + + readFile->open(); + readFile->cursorPosition = 0; + tiny_utf8::string cutReadString = readFile->readString(readFile->size.value).value; + readFile->close(); + + ASSERT(modifyFile->cursorPosition == 0); + ASSERT(cutString.value == "Hallo, "); + ASSERT(cutReadString == "Hi, Ich bin Shwoomple"); + std::cout << "Passed cutString test." << std::endl; }