lib/file: Implement writeByte function

Soda
Shwoomple 2022-10-18 20:57:02 +05:30
parent c14504ce0b
commit c54eb48887
2 changed files with 33 additions and 1 deletions

View File

@ -129,6 +129,22 @@ ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
return ErrorOr<tiny_utf8::string>(tiny_utf8::string(s)); return ErrorOr<tiny_utf8::string>(tiny_utf8::string(s));
} }
ErrorOrVoid File::writeByte(uint8_t byte){
bool failure = false;
try{
this->fileStream.seekg(this->cursorPosition);
this->fileStream << byte;
this->cursorPosition++;
}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){ ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){
if (!std::filesystem::exists(path)) { if (!std::filesystem::exists(path)) {
return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr); return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);

View File

@ -38,5 +38,21 @@ int main(){
//readString test //readString test
tiny_utf8::string data = file->readString(5).value; tiny_utf8::string data = file->readString(5).value;
std::cout << data << std::endl; ASSERT(data == "tring");
std::cout << "Passed read string test." << std::endl;
file->close();
delete file;
//Write Tests
File *writeFile;
File *readFile;
writeFile = File::open("resources/unicode_data/writeTest", 'w').value;
writeFile->writeByte('a');
writeFile->close();
readFile = File::open("resources/unicode_data/writeTest", 'r').value;
uint8_t testByte = readFile->readByte().value;
ASSERT(testByte == 'a');
std::cout << "Passed write byte test." << std::endl;
} }