lib/file: Implement broken file functions
parent
c1d7801436
commit
3449e3b9c4
|
@ -93,7 +93,7 @@ namespace ErrorCodes {
|
|||
|
||||
//file errors
|
||||
const uint8_t FILE_READ_FAILED = 9;
|
||||
const uint8_t FILE_NOT_FOUND = 10;
|
||||
const uint8_t FILE_NOT_FOUND = 10;brea
|
||||
|
||||
const uint8_t UNIMPLEMENTED = 254;
|
||||
|
||||
|
|
|
@ -26,6 +26,14 @@
|
|||
#include "error.h"
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
switch(this->mode){
|
||||
case 'w':
|
||||
case 'm':
|
||||
|
@ -41,13 +49,6 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), pa
|
|||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
File::File(const File& file){
|
||||
|
@ -158,14 +159,14 @@ 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 = new File(path, mode, startPosition);
|
||||
file->isOpen = true;
|
||||
file->eof = false;
|
||||
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
|
||||
if(file.size.isError){
|
||||
return ErrorOr<File>(true, file.size.errorCode); //if file is not found
|
||||
}
|
||||
|
||||
return ErrorOr<File*>(file);
|
||||
return ErrorOr<File>(file);
|
||||
}
|
||||
|
|
|
@ -94,5 +94,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);
|
||||
};
|
|
@ -21,11 +21,11 @@ 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;
|
||||
tiny_utf8::string data = file.readString(file.size.value).value;
|
||||
std::cout << file.size.isError << std::endl;
|
||||
std::cout << data << std::endl;
|
||||
}
|
Loading…
Reference in New Issue