2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-03-02 15:39:07 +00:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-05-27 09:26:54 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2023-03-02 15:39:07 +00:00
|
|
|
#include <LibCore/DirectoryEntry.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <dirent.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <string.h>
|
2019-05-27 09:26:54 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class DirIterator {
|
2019-05-27 09:26:54 +02:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Flags {
|
2019-05-27 09:26:54 +02:00
|
|
|
NoFlags = 0x0,
|
|
|
|
SkipDots = 0x1,
|
2020-02-15 13:06:08 +13:00
|
|
|
SkipParentAndBaseDir = 0x2,
|
2019-05-27 09:26:54 +02:00
|
|
|
};
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
explicit DirIterator(DeprecatedString path, Flags = Flags::NoFlags);
|
2020-02-02 12:34:39 +01:00
|
|
|
~DirIterator();
|
2019-05-27 09:26:54 +02:00
|
|
|
|
2021-11-23 11:37:14 -05:00
|
|
|
DirIterator(DirIterator&&);
|
|
|
|
DirIterator(DirIterator const&) = delete;
|
|
|
|
|
2019-05-27 09:26:54 +02:00
|
|
|
bool has_error() const { return m_error != 0; }
|
|
|
|
int error() const { return m_error; }
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* error_string() const { return strerror(m_error); }
|
2019-05-27 09:26:54 +02:00
|
|
|
bool has_next();
|
2023-03-02 15:39:07 +00:00
|
|
|
Optional<DirectoryEntry> next();
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString next_path();
|
|
|
|
DeprecatedString next_full_path();
|
2021-05-14 21:05:18 +02:00
|
|
|
int fd() const;
|
2019-05-27 09:26:54 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
DIR* m_dir = nullptr;
|
|
|
|
int m_error = 0;
|
2023-03-02 15:39:07 +00:00
|
|
|
Optional<DirectoryEntry> m_next;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_path;
|
2019-05-27 09:26:54 +02:00
|
|
|
int m_flags;
|
|
|
|
|
|
|
|
bool advance_next();
|
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
}
|