amuse/lib/DirectoryEnumerator.cpp

301 lines
8.1 KiB
C++
Raw Permalink Normal View History

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