DirIterator.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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/ByteString.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. NoStat = 0x4,
  20. };
  21. explicit DirIterator(ByteString path, Flags = Flags::NoFlags);
  22. ~DirIterator();
  23. DirIterator(DirIterator&&);
  24. DirIterator(DirIterator const&) = delete;
  25. bool has_error() const { return m_error.has_value(); }
  26. Error error() const { return Error::copy(m_error.value()); }
  27. bool has_next();
  28. Optional<DirectoryEntry> next();
  29. ByteString next_path();
  30. ByteString next_full_path();
  31. int fd() const;
  32. private:
  33. DIR* m_dir = nullptr;
  34. Optional<Error> m_error;
  35. Optional<DirectoryEntry> m_next;
  36. ByteString m_path;
  37. int m_flags;
  38. bool advance_next();
  39. };
  40. }