Moved out directory from old and updated it to work.

This commit is contained in:
Relintai 2024-01-05 22:55:20 +01:00
parent 3614947989
commit 39cd2e7f98
4 changed files with 61 additions and 65 deletions

View File

@ -35,6 +35,8 @@ 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/vector4i.cpp -o sfw/core/vector4i.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_allocator.cpp -o sfw/core/pool_allocator.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/core/mutex.cpp -o sfw/core/mutex.o
@ -102,6 +104,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/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/directory.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/array.o sfw/object/dictionary.o sfw/object/ref_ptr.o \

View File

@ -1,5 +1,11 @@
//--STRIP
#include "directory.h"
#include "3rd_tinydir.h"
#include <cstdio>
//--STRIP
Error Directory::open_dir(const String &path, bool skip_specials) {
if (_dir_open) {
return ERR_CANT_ACQUIRE_RESOURCE;
@ -7,7 +13,7 @@ Error Directory::open_dir(const String &path, bool skip_specials) {
_skip_specials = skip_specials;
if (tinydir_open(&_dir, path.c_str()) == -1) {
if (tinydir_open(_dir, path.utf8().get_data()) == -1) {
return FAILED;
}
@ -23,7 +29,7 @@ Error Directory::open_dir(const char *path, bool skip_specials) {
_skip_specials = skip_specials;
if (tinydir_open(&_dir, path) == -1) {
if (tinydir_open(_dir, path) == -1) {
return FAILED;
}
@ -37,27 +43,31 @@ void Directory::close_dir() {
return;
}
tinydir_close(&_dir);
tinydir_close(_dir);
_dir_open = false;
}
bool Directory::has_next() {
return _dir.has_next;
if (!_dir) {
return false;
}
return _dir->has_next;
}
bool Directory::read() {
_read_file_result = tinydir_readfile(&_dir, &_file);
_read_file_result = tinydir_readfile(_dir, _file);
return _read_file_result != -1;
}
bool Directory::next() {
if (!_dir.has_next) {
if (!_dir->has_next) {
return false;
}
bool rres = read();
while (!rres && _dir.has_next) {
tinydir_next(&_dir);
while (!rres && _dir->has_next) {
tinydir_next(_dir);
rres = read();
}
@ -65,8 +75,8 @@ bool Directory::next() {
return false;
}
if (_dir.has_next) {
tinydir_next(&_dir);
if (_dir->has_next) {
tinydir_next(_dir);
}
if (_skip_specials && current_is_dir() && current_is_special_dir()) {
@ -80,31 +90,31 @@ bool Directory::current_is_ok() {
return _read_file_result == 01;
}
String Directory::current_get_name() {
return String(_file.name);
return String(_file->name);
}
String Directory::current_get_path() {
return String(_file.path);
return String(_file->path);
}
String Directory::current_get_extension() {
return String(_file.extension);
return String(_file->extension);
}
const char *Directory::current_get_name_cstr() {
return _file.name;
return _file->name;
}
const char *Directory::current_get_path_cstr() {
return _file.path;
return _file->path;
}
const char *Directory::current_get_extension_cstr() {
return _file.extension;
return _file->extension;
}
bool Directory::current_is_file() {
return !_file.is_dir;
return !_file->is_dir;
}
bool Directory::current_is_dir() {
return _file.is_dir;
return _file->is_dir;
}
bool Directory::current_is_special_dir() {
if (_file.name[0] == '.' && _file.name[1] == '\0' || _file.name[0] == '.' && _file.name[1] == '.') {
if ((_file->name[0] == '.' && _file->name[1] == '\0') || (_file->name[0] == '.' && _file->name[1] == '.')) {
return true;
}
@ -112,49 +122,25 @@ bool Directory::current_is_special_dir() {
}
String Directory::read_file(const String &path) {
FILE *f = fopen(path.c_str(), "r");
FILE *f = fopen(path.utf8().get_data(), "r");
String fd;
ERR_FAIL_COND_V_MSG(!f, fd, "Error opening file! " + path);
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); */
fd.resize(fsize);
CharString cs;
cs.resize(fsize);
fread(fd.dataw(), 1, fsize, f);
fread(cs.ptrw(), 1, fsize, f);
fclose(f);
return fd;
}
Error Directory::read_file_into(const String &path, String *str) {
if (!str) {
return ERR_PARAMETER_RANGE_ERROR;
}
FILE *f = fopen(path.c_str(), "r");
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); */
str->resize(fsize);
fread(str->dataw(), 1, fsize, f);
fclose(f);
return OK;
return String::utf8(cs.ptr());
}
Vector<uint8_t> Directory::read_file_bin(const String &path) {
FILE *f = fopen(path.c_str(), "rb");
FILE *f = fopen(path.utf8().get_data(), "rb");
Vector<uint8_t> fd;
@ -166,7 +152,7 @@ Vector<uint8_t> Directory::read_file_bin(const String &path) {
fd.resize(fsize);
fread(fd.dataw(), 1, fsize, f);
fread(fd.ptrw(), 1, fsize, f);
fclose(f);
return fd;
@ -177,7 +163,7 @@ Error Directory::read_file_into_bin(const String &path, Vector<uint8_t> *data) {
return ERR_PARAMETER_RANGE_ERROR;
}
FILE *f = fopen(path.c_str(), "rb");
FILE *f = fopen(path.utf8().get_data(), "rb");
if (!f) {
return ERR_FILE_CANT_OPEN;
@ -189,33 +175,33 @@ Error Directory::read_file_into_bin(const String &path, Vector<uint8_t> *data) {
data->resize(fsize);
fread(data->dataw(), 1, fsize, f);
fread(data->ptrw(), 1, fsize, f);
fclose(f);
return OK;
}
Error Directory::write_file(const String &path, const String &str) {
FILE *f = fopen(path.c_str(), "w");
FILE *f = fopen(path.utf8().get_data(), "w");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fwrite(str.data(), sizeof(char), str.size(), f);
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.c_str(), "wb");
FILE *f = fopen(path.utf8().get_data(), "wb");
if (!f) {
return ERR_FILE_CANT_OPEN;
}
fwrite(data.data(), sizeof(uint8_t), data.size(), f);
fwrite(data.ptr(), sizeof(uint8_t), data.size(), f);
fclose(f);
return OK;
@ -232,9 +218,14 @@ Directory::Directory() {
_skip_specials = true;
_read_file_result = 0;
_dir_open = false;
_dir = memnew(tinydir_dir);
_file = memnew(tinydir_file);
}
Directory::~Directory() {
if (is_dir_open()) {
close_dir();
}
memdelete(_dir);
memdelete(_file);
}

View File

@ -1,14 +1,16 @@
#ifndef DIRECTORY_H
#define DIRECTORY_H
#include "core/string.h"
//--STRIP
#include "core/error_list.h"
#include "3rd_tinydir.h"
#include "core/ustring.h"
//--STRIP
#include "core/reference.h"
struct tinydir_file;
struct tinydir_dir;
class Directory : public Reference {
SFW_OBJECT(Directory, Reference);
class Directory {
public:
Error open_dir(const String &path, bool skip_specials = true);
Error open_dir(const char *path, bool skip_specials = true);
@ -30,7 +32,7 @@ public:
bool current_is_special_dir();
String read_file(const String &path);
Error read_file_into(const String &path, String *str);
Vector<uint8_t> read_file_bin(const String &path);
Error read_file_into_bin(const String &path, Vector<uint8_t> *data);
@ -46,8 +48,8 @@ public:
private:
bool _skip_specials;
int _read_file_result;
tinydir_dir _dir;
tinydir_file _file;
tinydir_dir *_dir;
tinydir_file *_file;
bool _dir_open;
};