lib/file: Implement writeByte function
parent
c14504ce0b
commit
c54eb48887
|
@ -129,6 +129,22 @@ ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
|
|||
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){
|
||||
if (!std::filesystem::exists(path)) {
|
||||
return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);
|
||||
|
|
|
@ -38,5 +38,21 @@ int main(){
|
|||
|
||||
//readString test
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue