DirIterator.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/DirIterator.h>
  27. #include <errno.h>
  28. namespace Core {
  29. DirIterator::DirIterator(const StringView& path, Flags flags)
  30. : m_path(path)
  31. , m_flags(flags)
  32. {
  33. m_dir = opendir(String(path).characters());
  34. if (!m_dir) {
  35. m_error = errno;
  36. }
  37. }
  38. DirIterator::~DirIterator()
  39. {
  40. if (m_dir) {
  41. closedir(m_dir);
  42. m_dir = nullptr;
  43. }
  44. }
  45. bool DirIterator::advance_next()
  46. {
  47. if (!m_dir)
  48. return false;
  49. while (true) {
  50. errno = 0;
  51. auto* de = readdir(m_dir);
  52. if (!de) {
  53. m_error = errno;
  54. m_next = String();
  55. return false;
  56. }
  57. m_next = de->d_name;
  58. if (m_next.is_null())
  59. return false;
  60. if (m_flags & Flags::SkipDots && m_next.starts_with('.'))
  61. continue;
  62. if (m_flags & Flags::SkipParentAndBaseDir && (m_next == "." || m_next == ".."))
  63. continue;
  64. return !m_next.is_empty();
  65. }
  66. }
  67. bool DirIterator::has_next()
  68. {
  69. if (!m_next.is_null())
  70. return true;
  71. return advance_next();
  72. }
  73. String DirIterator::next_path()
  74. {
  75. if (m_next.is_null())
  76. advance_next();
  77. auto tmp = m_next;
  78. m_next = String();
  79. return tmp;
  80. }
  81. String DirIterator::next_full_path()
  82. {
  83. return String::format("%s/%s", m_path.characters(), next_path().characters());
  84. }
  85. }