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
.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){ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){
std::filesystem::path filePath = path; std::filesystem::path filePath = path;
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->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
}
this->open(); 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){ 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); return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);
} }

View File

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