Compare commits
No commits in common. "9bda607649101e3364b4d4b53ed8b684c6d3641e" and "f784948c3ebee5524de9d4c917dce093427b1913" have entirely different histories.
9bda607649
...
f784948c3e
|
@ -187,7 +187,7 @@ ErrorOrVoid File::insertByte(uint8_t byte){
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -209,7 +209,7 @@ ErrorOrVoid File::insert(std::vector<uint8_t> data){
|
||||||
this->fileStream.seekg(0);
|
this->fileStream.seekg(0);
|
||||||
this->write(readData);
|
this->write(readData);
|
||||||
this->cursorPosition += data.size();
|
this->cursorPosition += data.size();
|
||||||
delete[] buffer;
|
delete buffer;
|
||||||
}catch(std::exception& e){
|
}catch(std::exception& e){
|
||||||
failure = true;
|
failure = true;
|
||||||
}
|
}
|
||||||
|
@ -230,9 +230,10 @@ ErrorOrVoid File::insertString(tiny_utf8::string string){
|
||||||
|
|
||||||
this->fileStream.seekg(0);
|
this->fileStream.seekg(0);
|
||||||
|
|
||||||
this->writeString(readData);
|
//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();
|
this->cursorPosition += string.size();
|
||||||
delete[] buffer;
|
delete buffer;
|
||||||
}catch(std::exception& e){
|
}catch(std::exception& e){
|
||||||
failure = true;
|
failure = true;
|
||||||
}
|
}
|
||||||
|
@ -256,39 +257,13 @@ ErrorOr<uint8_t> File::cutByte(){
|
||||||
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;
|
||||||
}
|
}
|
||||||
return failure ? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(byte);
|
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){
|
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);
|
||||||
|
|
|
@ -180,18 +180,4 @@ int main(){
|
||||||
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;
|
||||||
|
|
||||||
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