lib/file.cpp: Implement cutByte function
parent
99c8f562d6
commit
d794bce288
|
@ -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;
|
||||||
}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;
|
||||||
}catch(std::exception& e){
|
}catch(std::exception& e){
|
||||||
failure = true;
|
failure = true;
|
||||||
}
|
}
|
||||||
|
@ -233,13 +233,36 @@ ErrorOrVoid File::insertString(tiny_utf8::string string){
|
||||||
//TODO: fix hack. tinyutf8 appends "_utf-8" when readData is assigned: tiny_utf8::string((char *) buffer);
|
//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.substr(0, readData.find("_utf-8")));
|
||||||
this->cursorPosition += string.size();
|
this->cursorPosition += string.size();
|
||||||
|
delete buffer;
|
||||||
}catch(std::exception& e){
|
}catch(std::exception& e){
|
||||||
failure = true;
|
failure = true;
|
||||||
}
|
}
|
||||||
return failure ? ErrorOrVoid(true, ErrorCodes::UNKNOWN) : ErrorOrVoid();
|
return failure ? ErrorOrVoid(true, ErrorCodes::UNKNOWN) : ErrorOrVoid();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<uint8_t> File::cutByte(){
|
||||||
|
bool failure = false;
|
||||||
|
uint8_t byte;
|
||||||
|
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);
|
||||||
|
|
||||||
|
byte = readData[this->cursorPosition];
|
||||||
|
readData.erase(readData.begin() + this->cursorPosition);
|
||||||
|
|
||||||
|
this->fileStream.seekg(0);
|
||||||
|
this->write(readData);
|
||||||
|
this->cursorPosition++;
|
||||||
|
delete buffer;
|
||||||
|
}catch(std::exception& e){
|
||||||
|
failure = true;
|
||||||
|
}
|
||||||
|
return failure ? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(byte);
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
@ -123,6 +123,7 @@ int main(){
|
||||||
|
|
||||||
delete appendFile;
|
delete appendFile;
|
||||||
|
|
||||||
|
//test insert functions
|
||||||
File *modifyFile;
|
File *modifyFile;
|
||||||
|
|
||||||
modifyFile = File::open("resources/writeTest", 'm').value;
|
modifyFile = File::open("resources/writeTest", 'm').value;
|
||||||
|
@ -164,4 +165,19 @@ int main(){
|
||||||
|
|
||||||
ASSERT(modifyString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple.");
|
ASSERT(modifyString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple.");
|
||||||
std::cout << "Passed modify string test" << std::endl;
|
std::cout << "Passed modify string test" << std::endl;
|
||||||
|
|
||||||
|
//test cut functions
|
||||||
|
modifyFile->open();
|
||||||
|
modifyFile->cursorPosition = modifyFile->size.value-1;
|
||||||
|
ErrorOr<uint8_t> cutByte = modifyFile->cutByte();
|
||||||
|
modifyFile->close();
|
||||||
|
|
||||||
|
readFile->open();
|
||||||
|
readFile->cursorPosition = 0;
|
||||||
|
tiny_utf8::string cutByteString = readFile->readString(readFile->size.value).value;
|
||||||
|
readFile->close();
|
||||||
|
|
||||||
|
ASSERT(cutByte.value == '.');
|
||||||
|
ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple");
|
||||||
|
std::cout << "Passed cut byte test." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue