lib/file: Implement cutString function

windows
Shwoomple 2022-12-04 09:59:28 +05:30
parent 44716a55bb
commit 26df433dc5
2 changed files with 46 additions and 2 deletions

View File

@ -65,6 +65,8 @@ void File::open(){
void File::close(){ void File::close(){
this->fileStream.close(); this->fileStream.close();
std::filesystem::path filePath = this->path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
this->isOpen = false; this->isOpen = false;
} }
@ -255,7 +257,7 @@ ErrorOr<uint8_t> File::cutByte(){
std::filesystem::resize_file(this->path, readData.size()); std::filesystem::resize_file(this->path, readData.size());
this->fileStream.seekg(0); this->fileStream.seekg(0);
this->write(readData); this->write(readData);
this->cursorPosition++; //this->cursorPosition++;
delete[] buffer; delete[] buffer;
}catch(std::exception& e){ }catch(std::exception& e){
failure = true; failure = true;
@ -280,7 +282,7 @@ ErrorOr<std::vector<uint8_t>> File::cut(uint64_t length){
std::filesystem::resize_file(this->path, readData.size()); std::filesystem::resize_file(this->path, readData.size());
this->fileStream.seekg(0); this->fileStream.seekg(0);
this->write(readData); this->write(readData);
this->cursorPosition += length; //this->cursorPosition += length;
delete[] buffer; delete[] buffer;
}catch(std::exception& e){ }catch(std::exception& e){
failure = true; failure = true;
@ -289,6 +291,30 @@ ErrorOr<std::vector<uint8_t>> File::cut(uint64_t length){
return failure ? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::UNKNOWN) :ErrorOr<std::vector<uint8_t>>(bytes); return failure ? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::UNKNOWN) :ErrorOr<std::vector<uint8_t>>(bytes);
} }
ErrorOr<tiny_utf8::string> 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<char*>(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<tiny_utf8::string>(true, ErrorCodes::UNKNOWN) : ErrorOr<tiny_utf8::string>(cutString);
}
ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){ ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){
if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) { if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) {
return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr); return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);

View File

@ -177,6 +177,7 @@ int main(){
tiny_utf8::string cutByteString = readFile->readString(readFile->size.value).value; tiny_utf8::string cutByteString = readFile->readString(readFile->size.value).value;
readFile->close(); readFile->close();
ASSERT(modifyFile->cursorPosition == modifyFile->size.value);
ASSERT(cutByte.value == '.'); ASSERT(cutByte.value == '.');
ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple"); ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple");
std::cout << "Passed cut byte test." << std::endl; std::cout << "Passed cut byte test." << std::endl;
@ -191,7 +192,24 @@ int main(){
tiny_utf8::string cutBytesString = readFile->readString(readFile->size.value).value; tiny_utf8::string cutBytesString = readFile->readString(readFile->size.value).value;
readFile->close(); readFile->close();
ASSERT(modifyFile->cursorPosition == 9);
ASSERT(cutBytes.value == std::vector<uint8_t>({' ', 'T', 'H', 'E', ' ', 'C', 'A', 'K', 'E', ' ', 'I', 'S', ' ','A', ' ', 'L', 'I', 'E'})) ASSERT(cutBytes.value == std::vector<uint8_t>({' ', 'T', 'H', 'E', ' ', 'C', 'A', 'K', 'E', ' ', 'I', 'S', ' ','A', ' ', 'L', 'I', 'E'}))
ASSERT(cutBytesString == "Hallo, Hi, Ich bin Shwoomple"); ASSERT(cutBytesString == "Hallo, Hi, Ich bin Shwoomple");
std::cout << "Passed cut test." << std::endl; std::cout << "Passed cut test." << std::endl;
modifyFile->open();
modifyFile->cursorPosition = 0;
ErrorOr<tiny_utf8::string> 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;
} }