nod/lib/DirectoryEnumerator.cpp

295 lines
7.9 KiB
C++
Raw Normal View History

#include "nod/DirectoryEnumerator.hpp"
2016-01-24 05:06:54 +00:00
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
2016-01-21 06:30:37 +00:00
#include <windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
2016-01-21 06:30:37 +00:00
#endif
#include <cstring>
2016-01-21 06:30:37 +00:00
#include <map>
2018-12-08 05:21:47 +00:00
namespace nod {
2016-01-21 06:30:37 +00:00
DirectoryEnumerator::DirectoryEnumerator(std::string_view path, Mode mode, bool sizeSort, bool reverse, bool noHidden) {
2018-12-08 05:21:47 +00:00
Sstat theStat;
if (Stat(path.data(), &theStat) || !S_ISDIR(theStat.st_mode)) {
2018-12-08 05:21:47 +00:00
return;
}
2016-01-21 06:30:37 +00:00
#if _WIN32
std::wstring wc = nowide::widen(path);
wc += L"/*";
2018-12-08 05:21:47 +00:00
WIN32_FIND_DATAW d;
HANDLE dir = FindFirstFileW(wc.c_str(), &d);
if (dir == INVALID_HANDLE_VALUE) {
2018-12-08 05:21:47 +00:00
return;
}
2018-12-08 05:21:47 +00:00
switch (mode) {
case Mode::Native:
do {
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:21:47 +00:00
continue;
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:21:47 +00:00
continue;
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:21:47 +00:00
Sstat st;
if (Stat(fp.c_str(), &st))
continue;
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +00:00
size_t sz = 0;
bool isDir = false;
if (S_ISDIR(st.st_mode)) {
2018-12-08 05:21:47 +00:00
isDir = true;
} else if (S_ISREG(st.st_mode)) {
2018-12-08 05:21:47 +00:00
sz = st.st_size;
} else {
2018-12-08 05:21:47 +00:00
continue;
}
2016-01-21 06:30:37 +00:00
m_entries.emplace_back(fp, fileName, sz, isDir);
2018-12-08 05:21:47 +00:00
} while (FindNextFileW(dir, &d));
break;
case Mode::DirsThenFilesSorted:
case Mode::DirsSorted: {
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:21:47 +00:00
do {
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:21:47 +00:00
continue;
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:21:47 +00:00
continue;
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:21:47 +00:00
Sstat st;
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode)) {
2018-12-08 05:21:47 +00:00
continue;
}
sort.emplace(fileName, Entry{fp, fileName, 0, true});
2018-12-08 05:21:47 +00:00
} while (FindNextFileW(dir, &d));
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +00:00
m_entries.reserve(sort.size());
if (reverse) {
for (auto it = sort.crbegin(); it != sort.crend(); ++it) {
m_entries.emplace_back(std::move(it->second));
}
} else {
for (auto& e : sort) {
m_entries.emplace_back(std::move(e.second));
}
}
2016-01-21 06:30:37 +00:00
if (mode == Mode::DirsSorted) {
2018-12-08 05:21:47 +00:00
break;
}
2018-12-08 05:21:47 +00:00
FindClose(dir);
dir = FindFirstFileW(wc.c_str(), &d);
}
case Mode::FilesSorted: {
if (mode == Mode::FilesSorted) {
2018-12-08 05:21:47 +00:00
m_entries.clear();
}
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +00:00
if (sizeSort) {
std::multimap<size_t, Entry> sort;
do {
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:21:47 +00:00
continue;
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:21:47 +00:00
continue;
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:21:47 +00:00
Sstat st;
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode)) {
2018-12-08 05:21:47 +00:00
continue;
}
sort.emplace(st.st_size, Entry{fp, fileName, static_cast<size_t>(st.st_size), false});
2018-12-08 05:21:47 +00:00
} while (FindNextFileW(dir, &d));
2016-01-21 06:30:37 +00: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));
}
} else {
for (auto& e : sort) {
m_entries.emplace_back(std::move(e.second));
}
}
2018-12-08 05:21:47 +00:00
} else {
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:21:47 +00:00
do {
if (!wcscmp(d.cFileName, L".") || !wcscmp(d.cFileName, L"..")) {
2018-12-08 05:21:47 +00:00
continue;
}
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)) {
2018-12-08 05:21:47 +00:00
continue;
}
std::string fileName = nowide::narrow(d.cFileName);
std::string fp(path);
fp += '/';
fp += fileName;
2018-12-08 05:21:47 +00:00
Sstat st;
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode)) {
2018-12-08 05:21:47 +00:00
continue;
}
sort.emplace(fileName, Entry{fp, fileName, static_cast<size_t>(st.st_size), false});
2018-12-08 05:21:47 +00:00
} while (FindNextFileW(dir, &d));
2016-01-21 06:30:37 +00: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));
}
} else {
for (auto& e : sort) {
m_entries.emplace_back(std::move(e.second));
}
}
2016-01-21 06:30:37 +00:00
}
2018-12-08 05:21:47 +00:00
break;
}
}
FindClose(dir);
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +00:00
#else
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +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;
std::string fp(path);
2018-12-08 05:21:47 +00:00
fp += '/';
fp += d->d_name;
Sstat st;
if (Stat(fp.c_str(), &st))
continue;
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +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;
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +00:00
m_entries.push_back(Entry(fp, d->d_name, sz, isDir));
}
break;
case Mode::DirsThenFilesSorted:
case Mode::DirsSorted: {
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:21:47 +00:00
while ((d = readdir(dir))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (noHidden && d->d_name[0] == '.')
continue;
std::string fp(path);
2018-12-08 05:21:47 +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)));
2016-01-21 06:30:37 +00:00
}
2018-12-08 05:21:47 +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));
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +00:00
if (mode == Mode::DirsSorted)
break;
rewinddir(dir);
2019-02-18 05:46:42 +00:00
[[fallthrough]];
2018-12-08 05:21:47 +00:00
}
case Mode::FilesSorted: {
if (mode == Mode::FilesSorted)
m_entries.clear();
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +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;
std::string fp(path);
2018-12-08 05:21:47 +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)));
}
2016-01-21 06:30:37 +00:00
2018-12-08 05:21:47 +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 {
std::map<std::string, Entry, CaseInsensitiveCompare> sort;
2018-12-08 05:21:47 +00:00
while ((d = readdir(dir))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (noHidden && d->d_name[0] == '.')
continue;
std::string fp(path);
2018-12-08 05:21:47 +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));
2016-01-21 06:30:37 +00:00
}
2018-12-08 05:21:47 +00:00
break;
}
}
closedir(dir);
2016-01-21 06:30:37 +00:00
#endif
}
2018-12-08 05:21:47 +00:00
} // namespace nod