lib/file: Fix write function bugs

Soda
Shwoomple 2022-10-20 10:27:00 +05:30
parent 6e57a86338
commit e7ce6f5cd4
3 changed files with 10 additions and 4 deletions

2
.gitignore vendored
View File

@ -23,3 +23,5 @@
#vscode
.vscode
writeTest

View File

@ -26,7 +26,11 @@
File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){
std::filesystem::path filePath = path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
if(this->mode == 'w' || this->mode == 'a'){
this->size = ErrorOr<uint64_t>(0);
}else{
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
}
this->open();
}
@ -166,7 +170,7 @@ ErrorOrVoid File::writeString(tiny_utf8::string data){
}
ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){
if (!std::filesystem::exists(path)) {
if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) {
return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);
}

View File

@ -48,11 +48,11 @@ int main(){
File *readFile;
//writeByte test
writeFile = File::open("resources/unicode_data/writeTest", 'w').value;
writeFile = File::open("resources/writeTest", 'w').value;
writeFile->writeByte('a');
writeFile->close();
readFile = File::open("resources/unicode_data/writeTest", 'r').value;
readFile = File::open("resources/writeTest", 'r').value;
uint8_t testByte = readFile->readByte().value;
ASSERT(testByte == 'a');
std::cout << "Passed write byte test." << std::endl;