1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:07:35 +00:00

LibCore: Simplify some of DirIterator's code

The main changes are in advance_next() where we flatten some of the
nesting to improve readability
This commit is contained in:
Shannon Booth 2020-02-15 13:04:09 +13:00 committed by Andreas Kling
parent 9920d17342
commit 3879d75219

View file

@ -33,14 +33,14 @@ DirIterator::DirIterator(const StringView& path, Flags flags)
: m_flags(flags) : m_flags(flags)
{ {
m_dir = opendir(String(path).characters()); m_dir = opendir(String(path).characters());
if (m_dir == nullptr) { if (!m_dir) {
m_error = errno; m_error = errno;
} }
} }
DirIterator::~DirIterator() DirIterator::~DirIterator()
{ {
if (m_dir != nullptr) { if (m_dir) {
closedir(m_dir); closedir(m_dir);
m_dir = nullptr; m_dir = nullptr;
} }
@ -48,32 +48,27 @@ DirIterator::~DirIterator()
bool DirIterator::advance_next() bool DirIterator::advance_next()
{ {
if (m_dir == nullptr) if (!m_dir)
return false; return false;
bool keep_advancing = true; while (true) {
while (keep_advancing) {
errno = 0; errno = 0;
auto* de = readdir(m_dir); auto* de = readdir(m_dir);
if (de) { if (!de) {
m_next = de->d_name;
} else {
m_error = errno; m_error = errno;
m_next = String(); m_next = String();
return false;
} }
if (m_next.is_null()) { m_next = de->d_name;
keep_advancing = false; if (m_next.is_null())
} else if (m_flags & Flags::SkipDots) { return false;
if (m_next.length() < 1 || m_next[0] != '.') {
keep_advancing = false; if (m_flags & Flags::SkipDots && m_next.starts_with('.'))
} continue;
} else {
keep_advancing = false; return !m_next.is_empty();
}
} }
return m_next.length() > 0;
} }
bool DirIterator::has_next() bool DirIterator::has_next()