diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 3c20355..38f3e27 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -129,6 +129,22 @@ ErrorOr File::readString(uint64_t bytes){ return ErrorOr(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::open(std::string path, char mode, uint64_t startPosition){ if (!std::filesystem::exists(path)) { return ErrorOr(true, ErrorCodes::FILE_NOT_FOUND, nullptr); diff --git a/src/test/file.cpp b/src/test/file.cpp index 3701cc4..6ec836b 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -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; }