Moved file io to a new FileAccess class from Directory.

This commit is contained in:
Relintai 2024-01-05 23:07:12 +01:00
parent 39cd2e7f98
commit 15d6c92dfd
5 changed files with 128 additions and 95 deletions

View File

@ -35,6 +35,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/vector3i.cpp -o sfw/core/vect
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/vector4.cpp -o sfw/core/vector4.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/vector4.cpp -o sfw/core/vector4.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/vector4i.cpp -o sfw/core/vector4i.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/vector4i.cpp -o sfw/core/vector4i.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/file_access.cpp -o sfw/core/file_access.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/directory.cpp -o sfw/core/directory.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/directory.cpp -o sfw/core/directory.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/pool_vector.cpp -o sfw/core/pool_vector.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/pool_vector.cpp -o sfw/core/pool_vector.o
@ -104,7 +105,7 @@ ccache g++ -Wall -lm -ldl -lpthread -lX11 -D_REENTRANT -g sfw/core/aabb.o sfw/c
sfw/core/vector2.o sfw/core/vector2i.o sfw/core/vector3.o \ sfw/core/vector2.o sfw/core/vector2i.o sfw/core/vector3.o \
sfw/core/vector3i.o sfw/core/vector4.o sfw/core/vector4i.o \ sfw/core/vector3i.o sfw/core/vector4.o sfw/core/vector4i.o \
sfw/core/pool_vector.o sfw/core/pool_allocator.o sfw/core/mutex.o sfw/core/stime.o \ sfw/core/pool_vector.o sfw/core/pool_allocator.o sfw/core/mutex.o sfw/core/stime.o \
sfw/core/directory.o \ sfw/core/directory.o sfw/core/file_access.o\
sfw/object/object.o sfw/object/reference.o sfw/object/core_string_names.o \ sfw/object/object.o sfw/object/reference.o sfw/object/core_string_names.o \
sfw/object/variant.o sfw/object/variant_op.o sfw/object/psignal.o \ sfw/object/variant.o sfw/object/variant_op.o sfw/object/psignal.o \
sfw/object/array.o sfw/object/dictionary.o sfw/object/ref_ptr.o \ sfw/object/array.o sfw/object/dictionary.o sfw/object/ref_ptr.o \

View File

@ -121,92 +121,6 @@ bool Directory::current_is_special_dir() {
return false; return false;
} }
String Directory::read_file(const String &path) {
FILE *f = fopen(path.utf8().get_data(), "r");
ERR_FAIL_COND_V_MSG(!f, String(), "Error opening file! " + path);
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
CharString cs;
cs.resize(fsize);
fread(cs.ptrw(), 1, fsize, f);
fclose(f);
return String::utf8(cs.ptr());
}
Vector<uint8_t> Directory::read_file_bin(const String &path) {
FILE *f = fopen(path.utf8().get_data(), "rb");
Vector<uint8_t> fd;
ERR_FAIL_COND_V_MSG(!f, fd, "Error opening file! " + path);
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
fd.resize(fsize);
fread(fd.ptrw(), 1, fsize, f);
fclose(f);
return fd;
}
Error Directory::read_file_into_bin(const String &path, Vector<uint8_t> *data) {
if (!data) {
return ERR_PARAMETER_RANGE_ERROR;
}
FILE *f = fopen(path.utf8().get_data(), "rb");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
data->resize(fsize);
fread(data->ptrw(), 1, fsize, f);
fclose(f);
return OK;
}
Error Directory::write_file(const String &path, const String &str) {
FILE *f = fopen(path.utf8().get_data(), "w");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fwrite(str.utf8().ptr(), sizeof(char), str.size(), f);
fclose(f);
return OK;
}
Error Directory::write_file_bin(const String &path, const Vector<uint8_t> &data) {
FILE *f = fopen(path.utf8().get_data(), "wb");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fwrite(data.ptr(), sizeof(uint8_t), data.size(), f);
fclose(f);
return OK;
}
bool Directory::is_dir_open() { bool Directory::is_dir_open() {
return _dir_open; return _dir_open;
} }

View File

@ -31,14 +31,6 @@ public:
bool current_is_dir(); bool current_is_dir();
bool current_is_special_dir(); bool current_is_special_dir();
String read_file(const String &path);
Vector<uint8_t> read_file_bin(const String &path);
Error read_file_into_bin(const String &path, Vector<uint8_t> *data);
Error write_file(const String &path, const String &str);
Error write_file_bin(const String &path, const Vector<uint8_t> &data);
bool is_dir_open(); bool is_dir_open();
bool is_dir_closed(); bool is_dir_closed();

98
sfw/core/file_access.cpp Normal file
View File

@ -0,0 +1,98 @@
//--STRIP
#include "file_access.h"
#include <cstdio>
//--STRIP
String FileAccess::read_file(const String &path) {
FILE *f = fopen(path.utf8().get_data(), "r");
ERR_FAIL_COND_V_MSG(!f, String(), "Error opening file! " + path);
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
CharString cs;
cs.resize(fsize);
fread(cs.ptrw(), 1, fsize, f);
fclose(f);
return String::utf8(cs.ptr());
}
Vector<uint8_t> FileAccess::read_file_bin(const String &path) {
FILE *f = fopen(path.utf8().get_data(), "rb");
Vector<uint8_t> fd;
ERR_FAIL_COND_V_MSG(!f, fd, "Error opening file! " + path);
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
fd.resize(fsize);
fread(fd.ptrw(), 1, fsize, f);
fclose(f);
return fd;
}
Error FileAccess::read_file_into_bin(const String &path, Vector<uint8_t> *data) {
if (!data) {
return ERR_PARAMETER_RANGE_ERROR;
}
FILE *f = fopen(path.utf8().get_data(), "rb");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
data->resize(fsize);
fread(data->ptrw(), 1, fsize, f);
fclose(f);
return OK;
}
Error FileAccess::write_file(const String &path, const String &str) {
FILE *f = fopen(path.utf8().get_data(), "w");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fwrite(str.utf8().ptr(), sizeof(char), str.size(), f);
fclose(f);
return OK;
}
Error FileAccess::write_file_bin(const String &path, const Vector<uint8_t> &data) {
FILE *f = fopen(path.utf8().get_data(), "wb");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fwrite(data.ptr(), sizeof(uint8_t), data.size(), f);
fclose(f);
return OK;
}
FileAccess::FileAccess() {
}
FileAccess::~FileAccess() {
}

28
sfw/core/file_access.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef FILE_ACCESS_H
#define FILE_ACCESS_H
//--STRIP
#include "core/error_list.h"
#include "core/ustring.h"
//--STRIP
class FileAccess {
public:
//TODO should probably have some simple buffered open / close / write / read api.
String read_file(const String &path);
Vector<uint8_t> read_file_bin(const String &path);
Error read_file_into_bin(const String &path, Vector<uint8_t> *data);
Error write_file(const String &path, const String &str);
Error write_file_bin(const String &path, const Vector<uint8_t> &data);
FileAccess();
virtual ~FileAccess();
private:
};
#endif