DirIterator.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/DeprecatedString.h>
  9. #include <LibCore/DirectoryEntry.h>
  10. #include <dirent.h>
  11. #include <string.h>
  12. namespace Core {
  13. class DirIterator {
  14. public:
  15. enum Flags {
  16. NoFlags = 0x0,
  17. SkipDots = 0x1,
  18. SkipParentAndBaseDir = 0x2,
  19. };
  20. explicit DirIterator(DeprecatedString path, Flags = Flags::NoFlags);
  21. ~DirIterator();
  22. DirIterator(DirIterator&&);
  23. DirIterator(DirIterator const&) = delete;
  24. bool has_error() const { return m_error.has_value(); }
  25. Error error() const { return Error::copy(m_error.value()); }
  26. bool has_next();
  27. Optional<DirectoryEntry> next();
  28. DeprecatedString next_path();
  29. DeprecatedString next_full_path();
  30. int fd() const;
  31. private:
  32. DIR* m_dir = nullptr;
  33. Optional<Error> m_error;
  34. Optional<DirectoryEntry> m_next;
  35. DeprecatedString m_path;
  36. int m_flags;
  37. bool advance_next();
  38. };
  39. }