Added a directory class to the core. It's directly using tinydir for now.

This commit is contained in:
Relintai 2022-02-04 23:17:46 +01:00
parent ae81caf9a1
commit b38b2f4755
2 changed files with 168 additions and 0 deletions

121
core/os/directory.cpp Normal file
View File

@ -0,0 +1,121 @@
#include "directory.h"
Error Directory::open(const String &path, bool skip_specials) {
if (_open) {
return ERR_CANT_ACQUIRE_RESOURCE;
}
_skip_specials = skip_specials;
if (tinydir_open(&_dir, path.c_str()) == -1) {
return FAILED;
}
_open = true;
return OK;
}
void Directory::close() {
if (!_open) {
return;
}
tinydir_close(&_dir);
_open = false;
}
bool Directory::has_next() {
return _dir.has_next;
}
void Directory::next() {
tinydir_next(&_dir);
_read_file_result = tinydir_readfile(&_dir, &_file);
if (_skip_specials && current_is_ok()) {
if ((_file.name[0] == '.' && _file.name[1] == '\0') || (_file.name[0] == '.' && _file.name[1] == '.' && _file.name[1] == '\0')) {
next();
}
}
}
bool Directory::current_is_ok() {
return _read_file_result == 01;
}
String Directory::current_get_name() {
return _file.name;
}
String Directory::current_get_path() {
return _file.path;
}
String Directory::current_get_extension() {
return _file.extension;
}
char *Directory::current_get_name_cstr() {
return _file.name;
}
char *Directory::current_get_path_cstr() {
return _file.path;
}
char *Directory::current_get_extension_cstr() {
return _file.extension;
}
bool Directory::current_is_file() {
return !_file.is_dir;
}
bool Directory::current_is_directory() {
return _file.is_dir;
}
String Directory::read_file(const String &path) {
FILE *f = fopen(path.c_str(), "r");
String 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); */
fread(fd.dataw(), 1, fsize, f);
fclose(f);
return fd;
}
void Directory::read_file_into(const String &path, String *str) {
ERR_FAIL_COND(!str);
FILE *f = fopen(path.c_str(), "r");
ERR_FAIL_COND_MSG(!f, "Error opening file! " + path);
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);
}
bool Directory::is_open() {
return _open;
}
bool Directory::is_closed() {
return !_open;
}
Directory::Directory() {
_skip_specials = false;
_read_file_result = 0;
_open = false;
}
Directory::~Directory() {
if (is_open()) {
close();
}
}

47
core/os/directory.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef DIRECTORY_H
#define DIRECTORY_H
#include "core/string.h"
#include "core/error_list.h"
#include <tinydir/tinydir.h>
#include "core/reference.h"
class Directory : public Reference {
RCPP_OBJECT(Directory, Reference);
public:
Error open(const String &path, bool skip_specials = false);
void close();
bool has_next();
void next();
bool current_is_ok();
String current_get_name();
String current_get_path();
String current_get_extension();
char *current_get_name_cstr();
char *current_get_path_cstr();
char *current_get_extension_cstr();
bool current_is_file();
bool current_is_directory();
String read_file(const String &path);
void read_file_into(const String &path, String *str);
bool is_open();
bool is_closed();
Directory();
virtual ~Directory();
private:
bool _skip_specials;
int _read_file_result;
tinydir_dir _dir;
tinydir_file _file;
bool _open;
};
#endif