2016-06-21 06:40:13 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
2017-12-29 07:57:22 +00:00
|
|
|
#include <cstdio>
|
2016-06-21 06:40:13 +00:00
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#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
|
|
|
|
|
2016-06-21 06:40:13 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "amuse/DirectoryEnumerator.hpp"
|
|
|
|
|
|
|
|
namespace amuse
|
|
|
|
{
|
|
|
|
|
2017-11-13 06:15:33 +00:00
|
|
|
DirectoryEnumerator::DirectoryEnumerator(SystemStringView path, Mode mode, bool sizeSort, bool reverse, bool noHidden)
|
2016-06-21 06:40:13 +00:00
|
|
|
{
|
|
|
|
Sstat theStat;
|
2017-11-13 06:15:33 +00:00
|
|
|
if (Stat(path.data(), &theStat) || !S_ISDIR(theStat.st_mode))
|
2016-06-21 06:40:13 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
#if _WIN32
|
|
|
|
SystemString wc(path);
|
|
|
|
wc += _S("/*");
|
|
|
|
WIN32_FIND_DATAW d;
|
|
|
|
HANDLE dir = FindFirstFileW(wc.c_str(), &d);
|
|
|
|
if (dir == INVALID_HANDLE_VALUE)
|
|
|
|
return;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case Mode::Native:
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
|
|
|
continue;
|
|
|
|
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
|
|
|
continue;
|
|
|
|
SystemString fp(path);
|
|
|
|
fp += _S('/');
|
|
|
|
fp += d.cFileName;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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-05-25 06:37:27 +00:00
|
|
|
m_entries.emplace_back(fp, d.cFileName, sz, isDir);
|
2016-06-21 06:40:13 +00:00
|
|
|
} while (FindNextFileW(dir, &d));
|
|
|
|
break;
|
|
|
|
case Mode::DirsThenFilesSorted:
|
|
|
|
case Mode::DirsSorted:
|
|
|
|
{
|
|
|
|
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
|
|
|
continue;
|
|
|
|
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
|
|
|
continue;
|
|
|
|
SystemString fp(path);
|
2016-07-14 04:54:46 +00:00
|
|
|
fp += _S('/');
|
2016-06-21 06:40:13 +00:00
|
|
|
fp += d.cFileName;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode))
|
|
|
|
continue;
|
2018-05-25 06:37:27 +00:00
|
|
|
sort.emplace(std::make_pair(d.cFileName, Entry(fp, d.cFileName, 0, true)));
|
2016-06-21 06:40:13 +00:00
|
|
|
} while (FindNextFileW(dir, &d));
|
|
|
|
|
2016-12-30 06:36:17 +00:00
|
|
|
m_entries.reserve(sort.size());
|
2016-06-21 06:40:13 +00:00
|
|
|
if (reverse)
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
2016-06-21 06:40:13 +00:00
|
|
|
m_entries.push_back(std::move(it->second));
|
|
|
|
else
|
|
|
|
for (auto& e : sort)
|
|
|
|
m_entries.push_back(std::move(e.second));
|
|
|
|
|
|
|
|
if (mode == Mode::DirsSorted)
|
|
|
|
break;
|
|
|
|
FindClose(dir);
|
|
|
|
dir = FindFirstFileW(wc.c_str(), &d);
|
|
|
|
}
|
|
|
|
case Mode::FilesSorted:
|
|
|
|
{
|
|
|
|
if (mode == Mode::FilesSorted)
|
|
|
|
m_entries.clear();
|
|
|
|
|
|
|
|
if (sizeSort)
|
|
|
|
{
|
|
|
|
std::multimap<size_t, Entry> sort;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
|
|
|
continue;
|
|
|
|
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
|
|
|
continue;
|
|
|
|
SystemString fp(path);
|
|
|
|
fp += _S('/');
|
|
|
|
fp += d.cFileName;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
|
|
|
continue;
|
2018-05-25 06:37:27 +00:00
|
|
|
sort.emplace(std::make_pair(st.st_size, Entry(fp, d.cFileName, st.st_size, false)));
|
2016-06-21 06:40:13 +00:00
|
|
|
} while (FindNextFileW(dir, &d));
|
|
|
|
|
2016-12-30 06:36:17 +00:00
|
|
|
m_entries.reserve(sort.size());
|
2016-06-21 06:40:13 +00:00
|
|
|
if (reverse)
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
2016-06-21 06:40:13 +00:00
|
|
|
m_entries.push_back(std::move(it->second));
|
|
|
|
else
|
|
|
|
for (auto& e : sort)
|
|
|
|
m_entries.push_back(std::move(e.second));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!wcscmp(d.cFileName, _S(".")) || !wcscmp(d.cFileName, _S("..")))
|
|
|
|
continue;
|
|
|
|
if (noHidden && (d.cFileName[0] == L'.' || (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
|
|
|
|
continue;
|
|
|
|
SystemString fp(path);
|
|
|
|
fp += _S('/');
|
|
|
|
fp += d.cFileName;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
|
|
|
continue;
|
2018-05-25 06:37:27 +00:00
|
|
|
sort.emplace(std::make_pair(d.cFileName, Entry(fp, d.cFileName, st.st_size, false)));
|
2016-06-21 06:40:13 +00:00
|
|
|
} while (FindNextFileW(dir, &d));
|
|
|
|
|
2016-12-30 06:36:17 +00:00
|
|
|
m_entries.reserve(sort.size());
|
2016-06-21 06:40:13 +00:00
|
|
|
if (reverse)
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
2016-06-21 06:40:13 +00:00
|
|
|
m_entries.push_back(std::move(it->second));
|
|
|
|
else
|
|
|
|
for (auto& e : sort)
|
|
|
|
m_entries.push_back(std::move(e.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FindClose(dir);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-11-13 06:15:33 +00:00
|
|
|
DIR* dir = opendir(path.data());
|
2016-06-21 06:40:13 +00:00
|
|
|
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;
|
|
|
|
SystemString fp(path);
|
|
|
|
fp += '/';
|
|
|
|
fp += d->d_name;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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-05-25 06:37:27 +00:00
|
|
|
m_entries.emplace_back(fp, d->d_name, sz, isDir);
|
2016-06-21 06:40:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Mode::DirsThenFilesSorted:
|
|
|
|
case Mode::DirsSorted:
|
|
|
|
{
|
|
|
|
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
|
|
|
while ((d = readdir(dir)))
|
|
|
|
{
|
|
|
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
if (noHidden && d->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
SystemString fp(path);
|
|
|
|
fp += '/';
|
|
|
|
fp += d->d_name;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st) || !S_ISDIR(st.st_mode))
|
|
|
|
continue;
|
2018-05-25 06:37:27 +00:00
|
|
|
sort.emplace(std::make_pair(d->d_name, Entry(fp, d->d_name, 0, true)));
|
2016-06-21 06:40:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 06:36:17 +00:00
|
|
|
m_entries.reserve(sort.size());
|
2016-06-21 06:40:13 +00:00
|
|
|
if (reverse)
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
2016-06-21 06:40:13 +00:00
|
|
|
m_entries.push_back(std::move(it->second));
|
|
|
|
else
|
|
|
|
for (auto& e : sort)
|
|
|
|
m_entries.push_back(std::move(e.second));
|
|
|
|
|
|
|
|
if (mode == Mode::DirsSorted)
|
|
|
|
break;
|
|
|
|
rewinddir(dir);
|
|
|
|
}
|
|
|
|
case Mode::FilesSorted:
|
|
|
|
{
|
|
|
|
if (mode == Mode::FilesSorted)
|
|
|
|
m_entries.clear();
|
|
|
|
|
|
|
|
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;
|
|
|
|
SystemString fp(path);
|
|
|
|
fp += '/';
|
|
|
|
fp += d->d_name;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
|
|
|
continue;
|
2018-05-25 06:37:27 +00:00
|
|
|
sort.emplace(std::make_pair(st.st_size, Entry(fp, d->d_name, st.st_size, false)));
|
2016-06-21 06:40:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 06:36:17 +00:00
|
|
|
m_entries.reserve(sort.size());
|
2016-06-21 06:40:13 +00:00
|
|
|
if (reverse)
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
2016-06-21 06:40:13 +00:00
|
|
|
m_entries.push_back(std::move(it->second));
|
|
|
|
else
|
|
|
|
for (auto& e : sort)
|
|
|
|
m_entries.push_back(std::move(e.second));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::map<SystemString, Entry, CaseInsensitiveCompare> sort;
|
|
|
|
while ((d = readdir(dir)))
|
|
|
|
{
|
|
|
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
if (noHidden && d->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
SystemString fp(path);
|
|
|
|
fp += '/';
|
|
|
|
fp += d->d_name;
|
|
|
|
Sstat st;
|
|
|
|
if (Stat(fp.c_str(), &st) || !S_ISREG(st.st_mode))
|
|
|
|
continue;
|
2018-05-25 06:37:27 +00:00
|
|
|
sort.emplace(std::make_pair(d->d_name, Entry(fp, d->d_name, st.st_size, false)));
|
2016-06-21 06:40:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 06:36:17 +00:00
|
|
|
m_entries.reserve(sort.size());
|
2016-06-21 06:40:13 +00:00
|
|
|
if (reverse)
|
2016-07-14 04:54:46 +00:00
|
|
|
for (auto it = sort.crbegin(); it != sort.crend(); ++it)
|
2016-06-21 06:40:13 +00:00
|
|
|
m_entries.push_back(std::move(it->second));
|
|
|
|
else
|
|
|
|
for (auto& e : sort)
|
|
|
|
m_entries.push_back(std::move(e.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|