Compare commits
2 Commits
f784948c3e
...
9bda607649
Author | SHA1 | Date |
---|---|---|
Shwoomple | 9bda607649 | |
Shwoomple | 76dd30c45a |
|
@ -187,7 +187,7 @@ ErrorOrVoid File::insertByte(uint8_t byte){
|
|||
this->fileStream.seekg(0);
|
||||
this->write(readData);
|
||||
this->cursorPosition++;
|
||||
delete buffer;
|
||||
delete[] buffer;
|
||||
}catch(std::exception& e){
|
||||
failure = true;
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ ErrorOrVoid File::insert(std::vector<uint8_t> data){
|
|||
this->fileStream.seekg(0);
|
||||
this->write(readData);
|
||||
this->cursorPosition += data.size();
|
||||
delete buffer;
|
||||
delete[] buffer;
|
||||
}catch(std::exception& e){
|
||||
failure = true;
|
||||
}
|
||||
|
@ -230,10 +230,9 @@ ErrorOrVoid File::insertString(tiny_utf8::string 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->writeString(readData);
|
||||
this->cursorPosition += string.size();
|
||||
delete buffer;
|
||||
delete[] buffer;
|
||||
}catch(std::exception& e){
|
||||
failure = true;
|
||||
}
|
||||
|
@ -257,13 +256,39 @@ ErrorOr<uint8_t> File::cutByte(){
|
|||
this->fileStream.seekg(0);
|
||||
this->write(readData);
|
||||
this->cursorPosition++;
|
||||
delete buffer;
|
||||
delete[] buffer;
|
||||
}catch(std::exception& e){
|
||||
failure = true;
|
||||
}
|
||||
return failure ? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(byte);
|
||||
}
|
||||
|
||||
ErrorOr<std::vector<uint8_t>> File::cut(uint64_t length){
|
||||
bool failure = false;
|
||||
std::vector<uint8_t> bytes;
|
||||
|
||||
try{
|
||||
uint8_t* buffer = new uint8_t[this->size.value];
|
||||
std::vector<uint8_t> readData;
|
||||
|
||||
this->fileStream.read(reinterpret_cast<char*>(buffer), this->size.value);
|
||||
readData = std::vector<uint8_t>(buffer, buffer+this->size.value);
|
||||
|
||||
bytes = std::vector<uint8_t>(readData.begin() + this->cursorPosition, readData.begin() + (this->cursorPosition + length));
|
||||
readData.erase(readData.begin() + this->cursorPosition, readData.begin() + (this->cursorPosition + length));
|
||||
|
||||
std::filesystem::resize_file(this->path, readData.size());
|
||||
this->fileStream.seekg(0);
|
||||
this->write(readData);
|
||||
this->cursorPosition += length;
|
||||
delete[] buffer;
|
||||
}catch(std::exception& e){
|
||||
failure = true;
|
||||
}
|
||||
|
||||
return failure ? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::UNKNOWN) :ErrorOr<std::vector<uint8_t>>(bytes);
|
||||
}
|
||||
|
||||
ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){
|
||||
if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) {
|
||||
return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);
|
||||
|
|
|
@ -180,4 +180,18 @@ int main(){
|
|||
ASSERT(cutByte.value == '.');
|
||||
ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple");
|
||||
std::cout << "Passed cut byte test." << std::endl;
|
||||
|
||||
modifyFile->open();
|
||||
modifyFile->cursorPosition = 9;
|
||||
ErrorOr<std::vector<uint8_t>> cutBytes = modifyFile->cut(18);
|
||||
modifyFile->close();
|
||||
|
||||
readFile->open();
|
||||
readFile->cursorPosition = 0;
|
||||
tiny_utf8::string cutBytesString = readFile->readString(readFile->size.value).value;
|
||||
readFile->close();
|
||||
|
||||
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");
|
||||
std::cout << "Passed cut test." << std::endl;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue