rcpp_framework/core/os/directory.cpp

172 lines
2.9 KiB
C++
Raw Normal View History

#include "directory.h"
2022-02-04 23:36:04 +01:00
Error Directory::open_dir(const String &path, bool skip_specials) {
if (_dir_open) {
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;
return OK;
}
2022-02-04 23:36:04 +01:00
Error Directory::open_dir(const char *path, bool skip_specials) {
if (_dir_open) {
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;
return OK;
}
2022-02-04 23:36:04 +01:00
void Directory::close_dir() {
if (!_dir_open) {
return;
}
tinydir_close(&_dir);
2022-02-04 23:36:04 +01:00
_dir_open = false;
}
bool Directory::has_next() {
return _dir.has_next;
}
2022-02-05 02:45:11 +01:00
bool Directory::read() {
_read_file_result = tinydir_readfile(&_dir, &_file);
2022-02-05 02:45:11 +01:00
return _read_file_result != -1;
}
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);
}
// if (_skip_specials && current_is_dir() && current_is_special_dir()) {
// return next();
// }
// tinydir_next(&_dir);
return true;
}
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);
}
String Directory::current_get_path() {
2022-02-05 02:45:11 +01:00
return String(_file.path);
}
String Directory::current_get_extension() {
2022-02-05 02:45:11 +01:00
return String(_file.extension);
}
2022-02-05 02:45:11 +01:00
const char *Directory::current_get_name_cstr() {
return _file.name;
}
2022-02-05 02:45:11 +01:00
const char *Directory::current_get_path_cstr() {
return _file.path;
}
2022-02-05 02:45:11 +01:00
const char *Directory::current_get_extension_cstr() {
return _file.extension;
}
bool Directory::current_is_file() {
return !_file.is_dir;
}
bool Directory::current_is_dir() {
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;
}
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;
}
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;
}
2022-02-04 23:36:04 +01:00
bool Directory::is_dir_open() {
return _dir_open;
}
2022-02-04 23:36:04 +01:00
bool Directory::is_dir_closed() {
return !_dir_open;
}
Directory::Directory() {
2022-02-05 02:45:11 +01:00
_skip_specials = true;
_read_file_result = 0;
2022-02-04 23:36:04 +01:00
_dir_open = false;
}
Directory::~Directory() {
2022-02-04 23:36:04 +01:00
if (is_dir_open()) {
close_dir();
}
}