Dawn/src/dawntools/util/Directory.cpp

58 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->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 handle != std::filesystem::end(handle);
}
void Directory::close() {
if(this->isOpen()) {
handle = std::filesystem::directory_iterator();
}
}
bool_t Directory::exists() {
return std::filesystem::exists(filename);
}
void Directory::mkdirp() {
File::mkdirp(this->filename);
}
std::map<std::string, enum DirectoryChildType> Directory::readDirectory() {
this->close();
if(!this->open()) return {};
std::map<std::string, enum DirectoryChildType> children;
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;
}
}
this->close();
return children;
}
Directory::~Directory() {
this->close();
}