lib/file: Switch to using pointers for passing File objects around

Soda
BodgeMaster 2022-09-30 20:10:28 +02:00
parent c204aa7d76
commit 89baeebc65
3 changed files with 23 additions and 57 deletions

View File

@ -25,13 +25,8 @@
#include "file.hpp"
File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){
try{
std::filesystem::path filePath = path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
}catch(std::exception& e){
this->size = ErrorOr<uint64_t>(true, ErrorCodes::FILE_NOT_FOUND);
}
std::filesystem::path filePath = path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
switch(this->mode){
case 'w':
@ -48,25 +43,10 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), pa
default:
break;
}
this->isOpen = true;
this->eof = false;
}
File::File(const File& file){
this->mode = file.getMode();
this->isOpen = file.isOpen;
this->eof = file.eof;
this->path = file.path;
this->cursorPosition = file.cursorPosition;
this->size = file.size;
if(this->isOpen){
this->open();
}
}
File& File::operator=(const File& file){
File cpFile(file);
return cpFile;
}
void File::open(){
switch(this->mode){
@ -85,12 +65,6 @@ void File::open(){
break;
}
try{
std::filesystem::path filePath = this->path;
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
}catch(std::exception& e){
this->size = ErrorOr<uint64_t>(true, ErrorCodes::FILE_NOT_FOUND);
}
}
void File::close(){
@ -98,10 +72,6 @@ void File::close(){
this->isOpen = false;
}
char File::getMode() const{
return this->mode;
}
ErrorOr<uint8_t> File::readByte(){
if(this->size.isError){
return ErrorOr<uint8_t>(true, this->size.errorCode);
@ -158,14 +128,12 @@ ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
return ErrorOr<tiny_utf8::string>(tiny_utf8::string(s));
}
ErrorOr<File> File::open(std::string path, char mode, uint64_t startPosition){
File file(path, mode, startPosition);
file.isOpen = true;
file.eof = false;
if(file.size.isError){
return ErrorOr<File>(true, file.size.errorCode); //if file is not found
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);
}
return ErrorOr<File>(file);
//TODO: check access perms
return ErrorOr<File*>(new File(path, mode, startPosition));
}

View File

@ -45,12 +45,9 @@ class File {
// may be error if not a regular file or size cannot be determined
ErrorOr<uint64_t> size;
File() = default;
File(const File& file);
File& operator=(const File& file);
File() {};
void open();
void close();
char getMode() const;
// only applicable to read and edit modes
// moves the cursor to the right of the read section
@ -94,5 +91,5 @@ class File {
//
// A startPosition of 0xFFFFFFFF is considered to be the end of
// the file whatever its size.
static ErrorOr<File> open(std::string path, char mode, uint64_t startPosition=0);
static ErrorOr<File*> open(std::string path, char mode, uint64_t startPosition=0);
};

View File

@ -21,11 +21,12 @@ int main(){
std::cout << "################################################################################" << std::endl;
std::cout << "File tests" << std::endl;
std::cout << "################################################################################" << std::endl;
File file;
File* file;
//read test
file = File::open("../../resources/unicode_data/normal_utf-8", 'r').value;
tiny_utf8::string data = file.readString(file.size.value).value;
std::cout << file.size.isError << std::endl;
file = File::open("resources/unicode_data/normal_utf-8", 'r').value;
tiny_utf8::string data = file->readString(file->size.value).value;
std::cout << file->size.isError << std::endl;
std::cout << data << std::endl;
}