60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "Directory.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
Directory::Directory(std::string dirname) {
|
|
this->filename = File::normalizeSlashes(dirname);
|
|
}
|
|
|
|
bool_t Directory::open() {
|
|
if(this->handle) return true;
|
|
this->handle = opendir(this->filename.c_str());
|
|
return this->handle != nullptr;
|
|
}
|
|
|
|
bool_t Directory::isOpen() {
|
|
return this->handle != nullptr;
|
|
}
|
|
|
|
void Directory::close() {
|
|
if(this->handle) {
|
|
closedir(this->handle);
|
|
this->handle = nullptr;
|
|
}
|
|
}
|
|
|
|
bool_t Directory::exists() {
|
|
return this->open();
|
|
}
|
|
|
|
void Directory::mkdirp() {
|
|
File::mkdirp(this->filename);
|
|
}
|
|
|
|
std::map<std::string, enum DirectoryChildType> Directory::readDirectory() {
|
|
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;
|
|
}
|
|
|
|
//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;
|
|
}
|
|
|
|
Directory::~Directory() {
|
|
this->close();
|
|
} |