forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibCore: Add CDirIterator, and use it in everything rather than readdir
- Loading branch information
1 parent
f352a50
commit 9d2b08e
Showing
9 changed files
with
152 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "CDirIterator.h" | ||
#include <cerrno> | ||
|
||
CDirIterator::CDirIterator(const String& path, Flags flags) | ||
: m_flags(flags) | ||
{ | ||
m_dir = opendir(path.characters()); | ||
if (m_dir == nullptr) { | ||
m_error = errno; | ||
} | ||
} | ||
|
||
CDirIterator::~CDirIterator() | ||
{ | ||
if (m_dir != nullptr) { | ||
closedir(m_dir); | ||
m_dir = nullptr; | ||
} | ||
} | ||
|
||
bool CDirIterator::advance_next() | ||
{ | ||
if (m_dir == nullptr) | ||
return false; | ||
|
||
bool keep_advancing = true; | ||
while (keep_advancing) { | ||
errno = 0; | ||
auto* de = readdir(m_dir); | ||
if (de) { | ||
m_next = de->d_name; | ||
} else { | ||
m_error = errno; | ||
m_next = String(); | ||
} | ||
|
||
if (m_next.is_null()) { | ||
keep_advancing = false; | ||
} else if (m_flags & Flags::SkipDots) { | ||
if (m_next.length() < 1 || m_next[0] != '.') { | ||
keep_advancing = false; | ||
} | ||
} else { | ||
keep_advancing = false; | ||
} | ||
} | ||
|
||
return m_next.length() > 0; | ||
} | ||
|
||
bool CDirIterator::has_next() | ||
{ | ||
if (!m_next.is_null()) | ||
return true; | ||
|
||
return advance_next(); | ||
} | ||
|
||
String CDirIterator::next_path() | ||
{ | ||
if (m_next.is_null()) | ||
advance_next(); | ||
|
||
auto tmp = m_next; | ||
m_next = String(); | ||
return tmp; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <dirent.h> | ||
#include <AK/AKString.h> | ||
|
||
class CDirIterator { | ||
public: | ||
enum Flags { | ||
NoFlags = 0x0, | ||
SkipDots = 0x1, | ||
}; | ||
|
||
CDirIterator(const String& path, Flags = Flags::NoFlags); | ||
~CDirIterator(); | ||
|
||
bool has_error() const { return m_error != 0; } | ||
int error() const { return m_error; } | ||
const char* error_string() const { return strerror(m_error); } | ||
bool has_next(); | ||
String next_path(); | ||
|
||
private: | ||
DIR* m_dir = nullptr; | ||
int m_error = 0; | ||
String m_next; | ||
int m_flags; | ||
|
||
bool advance_next(); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters