lib/file: Implement insertByte function

Soda
Shwoomple 2022-10-20 13:02:15 +05:30
parent e7711a3d59
commit c825c73afd
3 changed files with 90 additions and 4 deletions

View File

@ -26,7 +26,7 @@
File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){
std::filesystem::path filePath = path;
if(this->mode == 'w' || this->mode == 'a'){
if(this->mode == 'w' || (this->mode == 'a' && !std::filesystem::exists(filePath))){
this->size = ErrorOr<uint64_t>(0);
}else{
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
@ -43,11 +43,12 @@ File::~File() {
void File::open(){
switch(this->mode){
case 'w':
case 'm':
this->fileStream.open(this->path, std::fstream::out | std::fstream::binary);
break;
case 'm':
this->fileStream.open(this->path, std::fstream::out | std::fstream::in | std::fstream::binary);
break;
case 'r':
case 'e':
this->fileStream.open(this->path, std::fstream::in | std::fstream::binary);
break;
case 'a':
@ -57,6 +58,8 @@ void File::open(){
break;
}
std::filesystem::path filePath = this->path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
this->isOpen = this->fileStream.is_open();
}
@ -169,6 +172,27 @@ ErrorOrVoid File::writeString(tiny_utf8::string data){
return failure ? ErrorOrVoid(true, ErrorCodes::UNKNOWN) : ErrorOrVoid();
}
// Naive implementation of insertByte
ErrorOrVoid File::insertByte(uint8_t byte){
bool failure = false;
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);
readData.insert(readData.begin()+this->cursorPosition, byte);
this->fileStream.seekg(0);
this->write(readData);
}catch(std::exception& e){
failure = true;
}
return failure ? ErrorOrVoid(true, ErrorCodes::UNKNOWN) : ErrorOrVoid();
}
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);

View File

@ -85,7 +85,6 @@ class File {
// w (write: overwrite file deleting its previous contents)
// a (append: write to end of file)
// m (modify: write to file modifying its previous contents)
// e (edit: like read+modify)
//
// A startPosition of 0xFFFFFFFF is considered to be the end of
// the file whatever its size.

View File

@ -54,6 +54,7 @@ int main(){
readFile = File::open("resources/writeTest", 'r').value;
uint8_t testByte = readFile->readByte().value;
readFile->close();
ASSERT(testByte == 'a');
std::cout << "Passed write byte test." << std::endl;
@ -62,8 +63,10 @@ int main(){
writeFile->write(std::vector<uint8_t>({'a', 'b', 'c'}));
writeFile->close();
readFile->open();
readFile->cursorPosition = 0;
tiny_utf8::string testBytes = readFile->readString(3).value;
readFile->close();
ASSERT(testBytes == "abc");
std::cout << "Passed write test." << std::endl;
@ -71,8 +74,68 @@ int main(){
writeFile->writeString(tiny_utf8::string("Hallo"));
writeFile->close();
readFile->open();
readFile->cursorPosition = 0;
tiny_utf8::string testString = readFile->readString(5).value;
readFile->close();
ASSERT(testString == "Hallo");
std::cout << "Passed write string test." << std::endl;
delete writeFile;
File *appendFile;
appendFile = File::open("resources/writeTest", 'a').value;
appendFile->writeByte(',');
appendFile->close();
readFile->open();
readFile->cursorPosition = 0;
tiny_utf8::string appendByteString = readFile->readString(readFile->size.value).value;
readFile->close();
ASSERT(appendByteString == "Hallo,");
std::cout << "Passed append byte test." << std::endl;
appendFile->open();
appendFile->write(std::vector<uint8_t>({' ', 'I', 'c', 'h'}));
appendFile->close();
readFile->open();
readFile->cursorPosition = 0;
tiny_utf8::string appendBytesString = readFile->readString(readFile->size.value).value;
readFile->close();
ASSERT(appendBytesString == "Hallo, Ich");
std::cout << "Passed append test" << std::endl;
appendFile->open();
appendFile->writeString(" bin Shwoomple.");
appendFile->close();
readFile->open();
readFile->cursorPosition = 0;
tiny_utf8::string appendString = readFile->readString(readFile->size.value).value;
readFile->close();
ASSERT(appendString == "Hallo, Ich bin Shwoomple.");
std::cout << "Passed append string test" << std::endl;
delete appendFile;
File *modifyFile;
modifyFile = File::open("resources/writeTest", 'm').value;
modifyFile->cursorPosition = 5;
modifyFile->insertByte(',');
modifyFile->close();
readFile->open();
readFile->cursorPosition = 0;
tiny_utf8::string modifyByteString = readFile->readString(readFile->size.value).value;
readFile->close();
ASSERT(modifyByteString == "Hallo,, Ich bin Shwoomple.");
std::cout << "Passed modify byte test" << std::endl;
}