First pass at prefab tool (done)
This commit is contained in:
@@ -12,24 +12,24 @@ Directory::Directory(std::string dirname) {
|
||||
}
|
||||
|
||||
bool_t Directory::open() {
|
||||
if(this->handle) return true;
|
||||
this->handle = opendir(this->filename.c_str());
|
||||
return this->handle != nullptr;
|
||||
if(!this->exists()) return false;
|
||||
if (handle != std::filesystem::end(handle)) return true;
|
||||
handle = std::filesystem::directory_iterator(filename);
|
||||
return handle != std::filesystem::end(handle);
|
||||
}
|
||||
|
||||
bool_t Directory::isOpen() {
|
||||
return this->handle != nullptr;
|
||||
return handle != std::filesystem::end(handle);
|
||||
}
|
||||
|
||||
void Directory::close() {
|
||||
if(this->handle) {
|
||||
closedir(this->handle);
|
||||
this->handle = nullptr;
|
||||
if(this->isOpen()) {
|
||||
handle = std::filesystem::directory_iterator();
|
||||
}
|
||||
}
|
||||
|
||||
bool_t Directory::exists() {
|
||||
return this->open();
|
||||
return std::filesystem::exists(filename);
|
||||
}
|
||||
|
||||
void Directory::mkdirp() {
|
||||
@@ -37,20 +37,18 @@ void Directory::mkdirp() {
|
||||
}
|
||||
|
||||
std::map<std::string, enum DirectoryChildType> Directory::readDirectory() {
|
||||
this->close();
|
||||
if(!this->open()) return {};
|
||||
|
||||
std::map<std::string, enum DirectoryChildType> children;
|
||||
|
||||
struct dirent *dp;
|
||||
while((dp = readdir(this->handle)) != NULL) {
|
||||
if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
|
||||
continue;
|
||||
for(const auto& entry : handle) {
|
||||
if (entry.is_directory()) {
|
||||
children[entry.path().filename().string()] = DIRECTORY_CHILD_TYPE_DIRECTORY;
|
||||
} else if (entry.is_regular_file()) {
|
||||
children[entry.path().filename().string()] = DIRECTORY_CHILD_TYPE_FILE;
|
||||
}
|
||||
|
||||
//Skip anything that isn't a file or directory (symlinks, etc.)
|
||||
if(dp->d_type != DT_DIR && dp->d_type != DT_REG) continue;
|
||||
std::string fn = std::string(dp->d_name);
|
||||
children[fn] = dp->d_type == DT_DIR ? DIRECTORY_CHILD_TYPE_DIRECTORY : DIRECTORY_CHILD_TYPE_FILE;
|
||||
}
|
||||
|
||||
this->close();
|
||||
return children;
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "util/File.hpp"
|
||||
#include <filesystem>
|
||||
|
||||
namespace Dawn {
|
||||
enum DirectoryChildType {
|
||||
@@ -15,7 +16,7 @@ namespace Dawn {
|
||||
class Directory {
|
||||
public:
|
||||
std::string filename;
|
||||
DIR *handle = nullptr;
|
||||
std::filesystem::directory_iterator handle;
|
||||
|
||||
Directory(std::string dir);
|
||||
|
||||
|
Reference in New Issue
Block a user