2022-02-04 23:17:46 +01:00
|
|
|
#include "directory.h"
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
Error Directory::open_dir(const String &path, bool skip_specials) {
|
|
|
|
if (_dir_open) {
|
2022-02-04 23:17:46 +01:00
|
|
|
return ERR_CANT_ACQUIRE_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_skip_specials = skip_specials;
|
|
|
|
|
|
|
|
if (tinydir_open(&_dir, path.c_str()) == -1) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
_dir_open = true;
|
2022-02-04 23:17:46 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
Error Directory::open_dir(const char *path, bool skip_specials) {
|
|
|
|
if (_dir_open) {
|
2022-02-04 23:25:09 +01:00
|
|
|
return ERR_CANT_ACQUIRE_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_skip_specials = skip_specials;
|
|
|
|
|
|
|
|
if (tinydir_open(&_dir, path) == -1) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
_dir_open = true;
|
2022-02-04 23:25:09 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
void Directory::close_dir() {
|
|
|
|
if (!_dir_open) {
|
2022-02-04 23:17:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tinydir_close(&_dir);
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
_dir_open = false;
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Directory::has_next() {
|
|
|
|
return _dir.has_next;
|
|
|
|
}
|
2022-02-05 02:45:11 +01:00
|
|
|
bool Directory::read() {
|
2022-02-04 23:17:46 +01:00
|
|
|
_read_file_result = tinydir_readfile(&_dir, &_file);
|
|
|
|
|
2022-02-05 02:45:11 +01:00
|
|
|
return _read_file_result != -1;
|
|
|
|
}
|
2022-02-05 13:56:30 +01:00
|
|
|
bool Directory::next() {
|
|
|
|
if (!_dir.has_next) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rres = read();
|
|
|
|
while (!rres && _dir.has_next) {
|
|
|
|
tinydir_next(&_dir);
|
|
|
|
rres = read();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rres) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dir.has_next) {
|
|
|
|
tinydir_next(&_dir);
|
|
|
|
}
|
|
|
|
|
2022-02-05 14:00:07 +01:00
|
|
|
if (_skip_specials && current_is_dir() && current_is_special_dir()) {
|
|
|
|
return next();
|
|
|
|
}
|
2022-02-05 13:56:30 +01:00
|
|
|
|
|
|
|
return true;
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Directory::current_is_ok() {
|
|
|
|
return _read_file_result == 01;
|
|
|
|
}
|
|
|
|
String Directory::current_get_name() {
|
2022-02-05 02:45:11 +01:00
|
|
|
return String(_file.name);
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
String Directory::current_get_path() {
|
2022-02-05 02:45:11 +01:00
|
|
|
return String(_file.path);
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
String Directory::current_get_extension() {
|
2022-02-05 02:45:11 +01:00
|
|
|
return String(_file.extension);
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
2022-02-05 02:45:11 +01:00
|
|
|
const char *Directory::current_get_name_cstr() {
|
2022-02-04 23:17:46 +01:00
|
|
|
return _file.name;
|
|
|
|
}
|
2022-02-05 02:45:11 +01:00
|
|
|
const char *Directory::current_get_path_cstr() {
|
2022-02-04 23:17:46 +01:00
|
|
|
return _file.path;
|
|
|
|
}
|
2022-02-05 02:45:11 +01:00
|
|
|
const char *Directory::current_get_extension_cstr() {
|
2022-02-04 23:17:46 +01:00
|
|
|
return _file.extension;
|
|
|
|
}
|
|
|
|
bool Directory::current_is_file() {
|
|
|
|
return !_file.is_dir;
|
|
|
|
}
|
2022-02-04 23:25:09 +01:00
|
|
|
bool Directory::current_is_dir() {
|
2022-02-04 23:17:46 +01:00
|
|
|
return _file.is_dir;
|
|
|
|
}
|
2022-02-05 02:45:11 +01:00
|
|
|
bool Directory::current_is_special_dir() {
|
|
|
|
if (_file.name[0] == '.' && _file.name[1] == '\0' || _file.name[0] == '.' && _file.name[1] == '.') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-04 23:17:46 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-02-04 23:33:39 +01:00
|
|
|
Error Directory::read_file_into(const String &path, String *str) {
|
|
|
|
if (!str) {
|
|
|
|
return ERR_PARAMETER_RANGE_ERROR;
|
|
|
|
}
|
2022-02-04 23:17:46 +01:00
|
|
|
|
|
|
|
FILE *f = fopen(path.c_str(), "r");
|
|
|
|
|
2022-02-04 23:33:39 +01:00
|
|
|
if (!f) {
|
|
|
|
return ERR_FILE_CANT_OPEN;
|
|
|
|
}
|
2022-02-04 23:17:46 +01:00
|
|
|
|
|
|
|
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);
|
2022-02-04 23:33:39 +01:00
|
|
|
|
|
|
|
return OK;
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
bool Directory::is_dir_open() {
|
|
|
|
return _dir_open;
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
2022-02-04 23:36:04 +01:00
|
|
|
bool Directory::is_dir_closed() {
|
|
|
|
return !_dir_open;
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Directory::Directory() {
|
2022-02-05 02:45:11 +01:00
|
|
|
_skip_specials = true;
|
2022-02-04 23:17:46 +01:00
|
|
|
_read_file_result = 0;
|
2022-02-04 23:36:04 +01:00
|
|
|
_dir_open = false;
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
Directory::~Directory() {
|
2022-02-04 23:36:04 +01:00
|
|
|
if (is_dir_open()) {
|
|
|
|
close_dir();
|
2022-02-04 23:17:46 +01:00
|
|
|
}
|
|
|
|
}
|