amuse/lib/DirectoryEnumerator.cpp

304 lines
8.1 KiB
C++
Raw Normal View History

#ifdef _WIN32
#include <windows.h>
2017-12-29 07:57:22 +00:00
#include <cstdio>
#else
#include <dirent.h>
#endif
2021-06-30 20:21:20 +00:00
#ifdef __cpp_lib_ranges
2021-06-30 18:15:40 +00:00
#include <ranges>
2021-06-30 20:21:20 +00:00
#endif
2016-06-22 22:15:53 +00:00
#include <sys/stat.h>
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
2016-07-14 04:54:46 +00:00
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
2016-06-22 22:15:53 +00:00
#endif
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
2016-07-14 04:54:46 +00:00
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
2016-06-22 22:15:53 +00:00
#endif
#include <map>
#include "amuse/DirectoryEnumerator.hpp"
2018-12-08 05:20:09 +00:00
namespace amuse {
2021-06-30 18:15:40 +00:00
DirectoryEnumerator::DirectoryEnumerator(std::string_view path, Mode mode, bool sizeSort, bool reverse, bool noHidden) {
2018-12-08 05:20:09 +00:00
Sstat theStat;
2021-06-30 18:15:40 +00:00
if (Stat(path.data(), &theStat) || !S_ISDIR(theStat.st_mode)) {
2018-12-08 05:20:09 +00:00
return;
2021-06-30 18:15:40 +00:00
}
#if _WIN32
2021-06-30 18:15:40 +00:00
std::wstring wc = nowide::widen(path);
wc += L"/*";
2018-12-08 05:20:09 +00:00
WIN32_FIND_DATAW d;
HANDLE dir = FindFirstFileW(wc.c_str(), &d);
2021-06-30 18:15:40 +00:00
if (dir == INVALID_HANDLE_VALUE) {
2018-12-08 05:20:09 +00:00
return;
2021-06-30 18:15:40 +00:00
}
2018-12-08 05:20:09 +00:00
switch (mode) {
case Mode::Native:
do {
2021-06-30 18:15:40 +00:00
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:20:09 +00:00
Sstat st;
2021-06-30 18:15:40 +00:00
if (Stat(fp.c_str(), &st)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
2018-12-08 05:20:09 +00:00
size_t sz = 0;
bool isDir = false;
2021-06-30 18:15:40 +00:00
if (S_ISDIR(st.st_mode)) {
2018-12-08 05:20:09 +00:00
isDir = true;
2021-06-30 18:15:40 +00:00
} else if (S_ISREG(st.st_mode)) {
2018-12-08 05:20:09 +00:00
sz = st.st_size;
2021-06-30 18:15:40 +00:00
} else {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
2021-06-30 18:15:40 +00:00
m_entries.emplace_back(fp, fileName, sz, isDir);
2018-12-08 05:20:09 +00:00
} while (FindNextFileW(dir, &d));
break;
case Mode::DirsThenFilesSorted:
case Mode::DirsSorted: {
2021-06-30 18:15:40 +00:00
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:20:09 +00:00
do {
2021-06-30 18:15:40 +00:00
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:20:09 +00:00
Sstat st;
2021-06-30 18:15:40 +00:00
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
sort.emplace(fileName, Entry{fp, fileName, 0, true});
2018-12-08 05:20:09 +00:00
} while (FindNextFileW(dir, &d));
2018-12-08 05:20:09 +00:00
m_entries.reserve(sort.size());
2021-06-30 18:15:40 +00:00
if (reverse) {
for (auto& it : std::ranges::reverse_view(sort)) {
m_entries.emplace_back(std::move(it.second));
}
} else {
for (auto& e : sort) {
m_entries.emplace_back(std::move(e.second));
}
}
2021-06-30 18:15:40 +00:00
if (mode == Mode::DirsSorted) {
2018-12-08 05:20:09 +00:00
break;
2021-06-30 18:15:40 +00:00
}
2018-12-08 05:20:09 +00:00
FindClose(dir);
dir = FindFirstFileW(wc.c_str(), &d);
}
case Mode::FilesSorted: {
2021-06-30 18:15:40 +00:00
if (mode == Mode::FilesSorted) {
2018-12-08 05:20:09 +00:00
m_entries.clear();
2021-06-30 18:15:40 +00:00
}
2018-12-08 05:20:09 +00:00
if (sizeSort) {
std::multimap<size_t, Entry> sort;
do {
2021-06-30 18:15:40 +00:00
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:20:09 +00:00
Sstat st;
2021-06-30 18:15:40 +00:00
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
sort.emplace(st.st_size, Entry{fp, fileName, static_cast<size_t>(st.st_size), false});
2018-12-08 05:20:09 +00:00
} while (FindNextFileW(dir, &d));
2021-06-30 18:15:40 +00:00
m_entries.reserve(m_entries.size() + sort.size());
if (reverse) {
for (auto& it : std::ranges::reverse_view(sort)) {
m_entries.emplace_back(std::move(it.second));
}
} else {
for (auto& e : sort) {
m_entries.emplace_back(std::move(e.second));
}
}
2018-12-08 05:20:09 +00:00
} else {
2021-06-30 18:15:40 +00:00
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:20:09 +00:00
do {
2021-06-30 18:15:40 +00:00
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:20:09 +00:00
Sstat st;
2021-06-30 18:15:40 +00:00
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode)) {
2018-12-08 05:20:09 +00:00
continue;
2021-06-30 18:15:40 +00:00
}
sort.emplace(fileName, Entry{fp, fileName, static_cast<size_t>(st.st_size), false});
2018-12-08 05:20:09 +00:00
} while (FindNextFileW(dir, &d));
2021-06-30 18:15:40 +00:00
m_entries.reserve(m_entries.size() + sort.size());
if (reverse) {
for (auto& e : std::ranges::reverse_view(sort)) {
m_entries.emplace_back(std::move(e.second));
}
} else {
for (auto& e : sort) {
m_entries.emplace_back(std::move(e.second));
}
}
}
2018-12-08 05:20:09 +00:00
break;
}
}
FindClose(dir);
2018-12-08 05:20:09 +00:00
#else
2018-12-08 05:20:09 +00:00
DIR* dir = opendir(path.data());
if (!dir)
return;
const dirent* d;
switch (mode) {
case Mode::Native:
while ((d = readdir(dir))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (noHidden && d->d_name[0] == '.')
continue;
2021-06-30 18:15:40 +00:00
std::string fp(path);
2018-12-08 05:20:09 +00:00
fp += '/';
fp += d->d_name;
Sstat st;
if (Stat(fp.c_str(), &st))
continue;
2018-12-08 05:20:09 +00:00
size_t sz = 0;
bool isDir = false;
if (S_ISDIR(st.st_mode))
isDir = true;
else if (S_ISREG(st.st_mode))
sz = st.st_size;
else
continue;
2018-12-08 05:20:09 +00:00
m_entries.emplace_back(fp, d->d_name, sz, isDir);
}
break;
case Mode::DirsThenFilesSorted:
case Mode::DirsSorted: {
2021-06-30 18:15:40 +00:00
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:20:09 +00:00
while ((d = readdir(dir))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (noHidden && d->d_name[0] == '.')
continue;
2021-06-30 18:15:40 +00:00
std::string fp(path);
2018-12-08 05:20:09 +00:00
fp += '/';
fp += d->d_name;
Sstat st;
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode))
continue;
sort.emplace(std::make_pair(d->d_name, Entry(fp, d->d_name, 0, true)));
}
2018-12-08 05:20:09 +00:00
m_entries.reserve(sort.size());
if (reverse)
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
m_entries.push_back(std::move(it->second));
else
for (auto& e : sort)
m_entries.push_back(std::move(e.second));
2018-12-08 05:20:09 +00:00
if (mode == Mode::DirsSorted)
break;
rewinddir(dir);
2019-02-18 05:45:24 +00:00
[[fallthrough]];
2018-12-08 05:20:09 +00:00
}
case Mode::FilesSorted: {
if (mode == Mode::FilesSorted)
m_entries.clear();
2018-12-08 05:20:09 +00:00
if (sizeSort) {
std::multimap<size_t, Entry> sort;
while ((d = readdir(dir))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (noHidden && d->d_name[0] == '.')
continue;
2021-06-30 18:15:40 +00:00
std::string fp(path);
2018-12-08 05:20:09 +00:00
fp += '/';
fp += d->d_name;
Sstat st;
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
continue;
sort.emplace(std::make_pair(st.st_size, Entry(fp, d->d_name, st.st_size, false)));
}
2018-12-08 05:20:09 +00:00
m_entries.reserve(sort.size());
if (reverse)
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
m_entries.push_back(std::move(it->second));
else
for (auto& e : sort)
m_entries.push_back(std::move(e.second));
} else {
2021-06-30 18:15:40 +00:00
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:20:09 +00:00
while ((d = readdir(dir))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (noHidden && d->d_name[0] == '.')
continue;
2021-06-30 18:15:40 +00:00
std::string fp(path);
2018-12-08 05:20:09 +00:00
fp += '/';
fp += d->d_name;
Sstat st;
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
continue;
sort.emplace(std::make_pair(d->d_name, Entry(fp, d->d_name, st.st_size, false)));
}
m_entries.reserve(sort.size());
if (reverse)
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
m_entries.push_back(std::move(it->second));
else
for (auto& e : sort)
m_entries.push_back(std::move(e.second));
}
2018-12-08 05:20:09 +00:00
break;
}
}
closedir(dir);
#endif
}
2018-12-08 05:20:09 +00:00
} // namespace amuse