Improvements to the directory class.

This commit is contained in:
Relintai 2022-02-05 02:45:11 +01:00
parent 6d1eabf64d
commit 37fe94f99e
5 changed files with 62 additions and 24 deletions

View File

@ -30,20 +30,30 @@ void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist)
Ref<Directory> dir;
dir.instance();
ERR_FAIL_COND_MSG(!dir->open_dir(path, true), "Error opening wwwroot! folder: " + String(path));
ERR_FAIL_COND_MSG(dir->open_dir(path) != OK, "Error opening wwwroot! folder: " + String(path));
while (dir->has_next()) {
dir->next();
if (!dir->read()) {
dir->next();
continue;
}
if (dir->current_is_file()) {
String np = dir->current_get_path_cstr();
np = np.substr(wwwroot.size(), np.size() - wwwroot.size());
registered_files.insert(np);
} else {
if (dir->current_is_special_dir()) {
dir->next();
continue;
}
wwwroot_evaluate_dir(dir->current_get_path_cstr());
}
dir->next();
}
dir->close_dir();

View File

@ -45,37 +45,34 @@ void Directory::close_dir() {
bool Directory::has_next() {
return _dir.has_next;
}
void Directory::next() {
tinydir_next(&_dir);
bool Directory::read() {
_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();
}
}
return _read_file_result != -1;
}
void Directory::next() {
tinydir_next(&_dir);
}
bool Directory::current_is_ok() {
return _read_file_result == 01;
}
String Directory::current_get_name() {
return _file.name;
return String(_file.name);
}
String Directory::current_get_path() {
return _file.path;
return String(_file.path);
}
String Directory::current_get_extension() {
return _file.extension;
return String(_file.extension);
}
char *Directory::current_get_name_cstr() {
const char *Directory::current_get_name_cstr() {
return _file.name;
}
char *Directory::current_get_path_cstr() {
const char *Directory::current_get_path_cstr() {
return _file.path;
}
char *Directory::current_get_extension_cstr() {
const char *Directory::current_get_extension_cstr() {
return _file.extension;
}
bool Directory::current_is_file() {
@ -84,6 +81,13 @@ bool Directory::current_is_file() {
bool Directory::current_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] == '.') {
return true;
}
return false;
}
String Directory::read_file(const String &path) {
FILE *f = fopen(path.c_str(), "r");
@ -132,7 +136,7 @@ bool Directory::is_dir_closed() {
}
Directory::Directory() {
_skip_specials = false;
_skip_specials = true;
_read_file_result = 0;
_dir_open = false;
}

View File

@ -15,17 +15,19 @@ public:
void close_dir();
bool has_next();
bool read();
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();
const char *current_get_name_cstr();
const char *current_get_path_cstr();
const char *current_get_extension_cstr();
bool current_is_file();
bool current_is_dir();
bool current_is_special_dir();
String read_file(const String &path);
Error read_file_into(const String &path, String *str);

View File

@ -47,16 +47,21 @@ void PagedArticle::load() {
Ref<Directory> dir;
dir.instance();
ERR_FAIL_COND_MSG(!dir->open_dir(articles_folder.c_str(), false), "Error opening PagedArticle::folder! folder: " + articles_folder);
ERR_FAIL_COND_MSG(dir->open_dir(articles_folder.c_str()) != OK, "Error opening PagedArticle::folder! folder: " + articles_folder);
Vector<String> files;
while (dir->has_next()) {
dir->next();
if (!dir->read()) {
dir->next();
continue;
}
if (dir->current_is_file()) {
files.push_back(dir->current_get_name());
}
dir->next();
}
dir->close_dir();
@ -78,7 +83,7 @@ void PagedArticle::load() {
String fd;
ERR_CONTINUE_MSG(!dir->read_file_into(file_path, &fd), "PagedArticle::load_folder: Error opening file! " + file_path);
ERR_CONTINUE_MSG(dir->read_file_into(file_path, &fd) != OK, "PagedArticle::load_folder: Error opening file! " + file_path);
Utils::markdown_to_html(&fd);
@ -128,6 +133,20 @@ void PagedArticle::generate_summary() {
}
}
String PagedArticle::get_summary() {
return summary_page;
}
void PagedArticle::_notification(const int what) {
switch (what) {
case NOTIFICATION_ENTER_TREE:
load();
break;
default:
break;
}
}
PagedArticle::PagedArticle() :
WebNode() {

View File

@ -19,6 +19,9 @@ public:
void load();
void load_folder(const String &folder, const String &path);
void generate_summary();
String get_summary();
void _notification(const int what);
PagedArticle();
~PagedArticle();