lib/file: Switch to using pointers for passing File objects around
parent
c204aa7d76
commit
89baeebc65
|
@ -20,19 +20,14 @@
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "tinyutf8/tinyutf8.h"
|
#include "tinyutf8/tinyutf8.h"
|
||||||
#include "file.hpp"
|
#include "file.hpp"
|
||||||
|
|
||||||
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){
|
||||||
try{
|
std::filesystem::path filePath = path;
|
||||||
std::filesystem::path filePath = path;
|
this->size = ErrorOr<uint64_t>(std::filesystem::file_size(filePath));
|
||||||
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){
|
switch(this->mode){
|
||||||
case 'w':
|
case 'w':
|
||||||
case 'm':
|
case 'm':
|
||||||
|
@ -48,25 +43,10 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), pa
|
||||||
default:
|
default:
|
||||||
break;
|
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(){
|
void File::open(){
|
||||||
switch(this->mode){
|
switch(this->mode){
|
||||||
|
@ -85,12 +65,6 @@ void File::open(){
|
||||||
break;
|
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(){
|
void File::close(){
|
||||||
|
@ -98,10 +72,6 @@ void File::close(){
|
||||||
this->isOpen = false;
|
this->isOpen = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char File::getMode() const{
|
|
||||||
return this->mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<uint8_t> File::readByte(){
|
ErrorOr<uint8_t> File::readByte(){
|
||||||
if(this->size.isError){
|
if(this->size.isError){
|
||||||
return ErrorOr<uint8_t>(true, this->size.errorCode);
|
return ErrorOr<uint8_t>(true, this->size.errorCode);
|
||||||
|
@ -129,7 +99,7 @@ ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){
|
||||||
this->fileStream.seekg(this->cursorPosition);
|
this->fileStream.seekg(this->cursorPosition);
|
||||||
|
|
||||||
char* buffer = new char[bytes];
|
char* buffer = new char[bytes];
|
||||||
std::vector<uint8_t> data;
|
std::vector<uint8_t> data;
|
||||||
try{
|
try{
|
||||||
this->fileStream.read(buffer, bytes);
|
this->fileStream.read(buffer, bytes);
|
||||||
for(uint64_t i=0; i<bytes; i++){
|
for(uint64_t i=0; i<bytes; i++){
|
||||||
|
@ -158,14 +128,12 @@ ErrorOr<tiny_utf8::string> File::readString(uint64_t bytes){
|
||||||
return ErrorOr<tiny_utf8::string>(tiny_utf8::string(s));
|
return ErrorOr<tiny_utf8::string>(tiny_utf8::string(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<File> File::open(std::string path, char mode, uint64_t startPosition){
|
ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){
|
||||||
File file(path, mode, startPosition);
|
if (!std::filesystem::exists(path)) {
|
||||||
file.isOpen = true;
|
return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);
|
||||||
file.eof = false;
|
|
||||||
|
|
||||||
if(file.size.isError){
|
|
||||||
return ErrorOr<File>(true, file.size.errorCode); //if file is not found
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ErrorOr<File>(file);
|
//TODO: check access perms
|
||||||
|
|
||||||
|
return ErrorOr<File*>(new File(path, mode, startPosition));
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,12 +45,9 @@ class File {
|
||||||
// may be error if not a regular file or size cannot be determined
|
// may be error if not a regular file or size cannot be determined
|
||||||
ErrorOr<uint64_t> size;
|
ErrorOr<uint64_t> size;
|
||||||
|
|
||||||
File() = default;
|
File() {};
|
||||||
File(const File& file);
|
|
||||||
File& operator=(const File& file);
|
|
||||||
void open();
|
void open();
|
||||||
void close();
|
void close();
|
||||||
char getMode() const;
|
|
||||||
|
|
||||||
// only applicable to read and edit modes
|
// only applicable to read and edit modes
|
||||||
// moves the cursor to the right of the read section
|
// 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
|
// A startPosition of 0xFFFFFFFF is considered to be the end of
|
||||||
// the file whatever its size.
|
// 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,12 @@ int main(){
|
||||||
std::cout << "################################################################################" << std::endl;
|
std::cout << "################################################################################" << std::endl;
|
||||||
std::cout << "File tests" << std::endl;
|
std::cout << "File tests" << std::endl;
|
||||||
std::cout << "################################################################################" << std::endl;
|
std::cout << "################################################################################" << std::endl;
|
||||||
File file;
|
File* file;
|
||||||
|
|
||||||
//read test
|
//read test
|
||||||
file = File::open("../../resources/unicode_data/normal_utf-8", 'r').value;
|
file = File::open("resources/unicode_data/normal_utf-8", 'r').value;
|
||||||
tiny_utf8::string data = file.readString(file.size.value).value;
|
tiny_utf8::string data = file->readString(file->size.value).value;
|
||||||
std::cout << file.size.isError << std::endl;
|
std::cout << file->size.isError << std::endl;
|
||||||
std::cout << data << std::endl;
|
std::cout << data << std::endl;
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue