| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2024 Ahmad Samir <[email protected]> |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QDIRLISTING_H |
| 6 | #define QDIRLISTING_H |
| 7 | |
| 8 | #include <QtCore/qfiledevice.h> |
| 9 | #include <QtCore/qflags.h> |
| 10 | #include <QtCore/qtclasshelpermacros.h> |
| 11 | #include <QtCore/qtcoreexports.h> |
| 12 | #include <QtCore/qdatetime.h> |
| 13 | |
| 14 | #include <iterator> |
| 15 | #include <utility> |
| 16 | |
| 17 | QT_BEGIN_NAMESPACE |
| 18 | |
| 19 | class QDirListingPrivate; |
| 20 | class QFileInfo; |
| 21 | class QDir; |
| 22 | class QTimeZone; |
| 23 | |
| 24 | class QDirListing |
| 25 | { |
| 26 | public: |
| 27 | enum class IteratorFlag { |
| 28 | Default = 0x000000, |
| 29 | ExcludeFiles = 0x000004, |
| 30 | ExcludeDirs = 0x000008, |
| 31 | ExcludeSpecial = 0x000010, |
| 32 | ResolveSymlinks = 0x000020, |
| 33 | FilesOnly = ExcludeDirs | ExcludeSpecial, |
| 34 | DirsOnly = ExcludeFiles | ExcludeSpecial, |
| 35 | IncludeHidden = 0x000040, |
| 36 | IncludeDotAndDotDot = 0x000080, |
| 37 | CaseSensitive = 0x000100, |
| 38 | Recursive = 0x000400, |
| 39 | FollowDirSymlinks = 0x000800, |
| 40 | }; |
| 41 | Q_DECLARE_FLAGS(IteratorFlags, IteratorFlag) |
| 42 | |
| 43 | Q_CORE_EXPORT explicit QDirListing(const QString &path, |
| 44 | IteratorFlags flags = IteratorFlag::Default); |
| 45 | Q_CORE_EXPORT explicit QDirListing(const QString &path, const QStringList &nameFilters, |
| 46 | IteratorFlags flags = IteratorFlag::Default); |
| 47 | |
| 48 | QDirListing(QDirListing &&other) noexcept |
| 49 | : d{std::exchange(obj&: other.d, new_val: nullptr)} {} |
| 50 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDirListing) |
| 51 | |
| 52 | void swap(QDirListing &other) noexcept { qt_ptr_swap(lhs&: d, rhs&: other.d); } |
| 53 | |
| 54 | Q_CORE_EXPORT ~QDirListing(); |
| 55 | |
| 56 | Q_CORE_EXPORT QString iteratorPath() const; |
| 57 | Q_CORE_EXPORT IteratorFlags iteratorFlags() const; |
| 58 | Q_CORE_EXPORT QStringList nameFilters() const; |
| 59 | |
| 60 | class DirEntry |
| 61 | { |
| 62 | friend class QDirListing; |
| 63 | QDirListingPrivate *dirListPtr = nullptr; |
| 64 | public: |
| 65 | Q_CORE_EXPORT QString fileName() const; |
| 66 | Q_CORE_EXPORT QString baseName() const; |
| 67 | Q_CORE_EXPORT QString completeBaseName() const; |
| 68 | Q_CORE_EXPORT QString suffix() const; |
| 69 | Q_CORE_EXPORT QString bundleName() const; |
| 70 | Q_CORE_EXPORT QString completeSuffix() const; |
| 71 | Q_CORE_EXPORT QString filePath() const; |
| 72 | Q_CORE_EXPORT bool isDir() const; |
| 73 | Q_CORE_EXPORT bool isFile() const; |
| 74 | Q_CORE_EXPORT bool isSymLink() const; |
| 75 | Q_CORE_EXPORT bool exists() const; |
| 76 | Q_CORE_EXPORT bool isHidden() const; |
| 77 | Q_CORE_EXPORT bool isReadable() const; |
| 78 | Q_CORE_EXPORT bool isWritable() const; |
| 79 | Q_CORE_EXPORT bool isExecutable() const; |
| 80 | Q_CORE_EXPORT QFileInfo fileInfo() const; |
| 81 | Q_CORE_EXPORT QString canonicalFilePath() const; |
| 82 | Q_CORE_EXPORT QString absoluteFilePath() const; |
| 83 | Q_CORE_EXPORT QString absolutePath() const; |
| 84 | Q_CORE_EXPORT qint64 size() const; |
| 85 | |
| 86 | QDateTime birthTime(const QTimeZone &tz) const |
| 87 | { return fileTime(type: QFileDevice::FileBirthTime, tz); } |
| 88 | QDateTime metadataChangeTime(const QTimeZone &tz) const |
| 89 | { return fileTime(type: QFileDevice::FileMetadataChangeTime, tz); } |
| 90 | QDateTime lastModified(const QTimeZone &tz) const |
| 91 | { return fileTime(type: QFileDevice::FileModificationTime, tz); } |
| 92 | QDateTime lastRead(const QTimeZone &tz) const |
| 93 | { return fileTime(type: QFileDevice::FileAccessTime, tz); } |
| 94 | Q_CORE_EXPORT QDateTime fileTime(QFileDevice::FileTime type, const QTimeZone &tz) const; |
| 95 | }; |
| 96 | |
| 97 | class sentinel |
| 98 | { |
| 99 | friend constexpr bool operator==(sentinel, sentinel) noexcept { return true; } |
| 100 | friend constexpr bool operator!=(sentinel, sentinel) noexcept { return false; } |
| 101 | }; |
| 102 | |
| 103 | class const_iterator |
| 104 | { |
| 105 | Q_DISABLE_COPY(const_iterator) |
| 106 | friend class QDirListing; |
| 107 | explicit const_iterator(QDirListingPrivate *dp) { dirEntry.dirListPtr = dp; } |
| 108 | DirEntry dirEntry; |
| 109 | public: |
| 110 | using iterator_category = std::input_iterator_tag; |
| 111 | using value_type = DirEntry; |
| 112 | using difference_type = qint64; |
| 113 | using pointer = const value_type *; |
| 114 | using reference = const value_type &; |
| 115 | |
| 116 | const_iterator() = default; |
| 117 | const_iterator(const_iterator &&) noexcept = default; |
| 118 | const_iterator &operator=(const_iterator &&) noexcept = default; |
| 119 | |
| 120 | reference operator*() const { return dirEntry; } |
| 121 | pointer operator->() const { return &dirEntry; } |
| 122 | const_iterator &operator++() { dirEntry = next(dirEntry); return *this; } |
| 123 | void operator++(int) { ++*this; } // [iterator.concept.winc]/14 not required to return sth |
| 124 | private: |
| 125 | bool atEnd() const noexcept { return dirEntry.dirListPtr == nullptr; } |
| 126 | friend bool operator==(const const_iterator &lhs, sentinel) noexcept { return lhs.atEnd(); } |
| 127 | #ifndef __cpp_impl_three_way_comparison |
| 128 | friend bool operator!=(const const_iterator &lhs, sentinel) noexcept |
| 129 | { return !operator==(lhs, sentinel{}); } |
| 130 | friend bool operator==(sentinel, const const_iterator &rhs) noexcept |
| 131 | { return operator==(lhs: rhs, sentinel{}); } |
| 132 | friend bool operator!=(sentinel, const const_iterator &rhs) noexcept |
| 133 | { return !operator==(sentinel{}, rhs); } |
| 134 | #endif // __cpp_impl_three_way_comparison |
| 135 | }; |
| 136 | |
| 137 | Q_CORE_EXPORT const_iterator begin() const; |
| 138 | const_iterator cbegin() const { return begin(); } |
| 139 | sentinel end() const { return {}; } |
| 140 | sentinel cend() const { return end(); } |
| 141 | |
| 142 | // Qt compatibility |
| 143 | const_iterator constBegin() const { return begin(); } |
| 144 | sentinel constEnd() const { return end(); } |
| 145 | |
| 146 | private: |
| 147 | Q_DISABLE_COPY(QDirListing) |
| 148 | |
| 149 | Q_CORE_EXPORT static DirEntry next(DirEntry); |
| 150 | |
| 151 | // Private constructor that is used in deprecated code paths. |
| 152 | // `uint` instead of QDir::Filters and QDirIterator::IteratorFlags |
| 153 | // because qdir.h can't be included here; qdiriterator.h can't included |
| 154 | // either, because it includes qdir.h |
| 155 | Q_CORE_EXPORT QDirListing(const QString &path, const QStringList &nameFilters, uint dirFilters, |
| 156 | uint qdirIteratorFlags = 0); // QDirIterator::NoIteratorFlags == 0x0 |
| 157 | |
| 158 | QDirListingPrivate *d; |
| 159 | friend class QDir; |
| 160 | friend class QDirPrivate; |
| 161 | friend class QDirIteratorPrivate; |
| 162 | friend class QAbstractFileEngine; |
| 163 | friend class QFileInfoGatherer; |
| 164 | }; |
| 165 | |
| 166 | Q_DECLARE_OPERATORS_FOR_FLAGS(QDirListing::IteratorFlags) |
| 167 | |
| 168 | QT_END_NAMESPACE |
| 169 | |
| 170 | #endif // QDIRLISTING_H |
| 171 | |