File indexing completed on 2025-12-15 10:24:13
0001
0002
0003
0004
0005 #ifndef QDIRLISTING_H
0006 #define QDIRLISTING_H
0007
0008 #include <QtCore/qfiledevice.h>
0009 #include <QtCore/qflags.h>
0010 #include <QtCore/qtclasshelpermacros.h>
0011 #include <QtCore/qtcoreexports.h>
0012 #include <QtCore/qdatetime.h>
0013
0014 #include <iterator>
0015 #include <utility>
0016
0017 QT_BEGIN_NAMESPACE
0018
0019 class QDirListingPrivate;
0020 class QFileInfo;
0021 class QDir;
0022 class QTimeZone;
0023
0024 class QDirListing
0025 {
0026 public:
0027 enum class IteratorFlag {
0028 Default = 0x000000,
0029 ExcludeFiles = 0x000004,
0030 ExcludeDirs = 0x000008,
0031 ExcludeSpecial = 0x000010,
0032 ResolveSymlinks = 0x000020,
0033 FilesOnly = ExcludeDirs | ExcludeSpecial,
0034 DirsOnly = ExcludeFiles | ExcludeSpecial,
0035 IncludeHidden = 0x000040,
0036 IncludeDotAndDotDot = 0x000080,
0037 CaseSensitive = 0x000100,
0038 Recursive = 0x000400,
0039 FollowDirSymlinks = 0x000800,
0040 };
0041 Q_DECLARE_FLAGS(IteratorFlags, IteratorFlag)
0042
0043 Q_CORE_EXPORT explicit QDirListing(const QString &path,
0044 IteratorFlags flags = IteratorFlag::Default);
0045 Q_CORE_EXPORT explicit QDirListing(const QString &path, const QStringList &nameFilters,
0046 IteratorFlags flags = IteratorFlag::Default);
0047
0048 QDirListing(QDirListing &&other) noexcept
0049 : d{std::exchange(other.d, nullptr)} {}
0050 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDirListing)
0051
0052 void swap(QDirListing &other) noexcept { qt_ptr_swap(d, other.d); }
0053
0054 Q_CORE_EXPORT ~QDirListing();
0055
0056 Q_CORE_EXPORT QString iteratorPath() const;
0057 Q_CORE_EXPORT IteratorFlags iteratorFlags() const;
0058 Q_CORE_EXPORT QStringList nameFilters() const;
0059
0060 class DirEntry
0061 {
0062 friend class QDirListing;
0063 QDirListingPrivate *dirListPtr = nullptr;
0064 public:
0065 Q_CORE_EXPORT QString fileName() const;
0066 Q_CORE_EXPORT QString baseName() const;
0067 Q_CORE_EXPORT QString completeBaseName() const;
0068 Q_CORE_EXPORT QString suffix() const;
0069 Q_CORE_EXPORT QString bundleName() const;
0070 Q_CORE_EXPORT QString completeSuffix() const;
0071 Q_CORE_EXPORT QString filePath() const;
0072 Q_CORE_EXPORT bool isDir() const;
0073 Q_CORE_EXPORT bool isFile() const;
0074 Q_CORE_EXPORT bool isSymLink() const;
0075 Q_CORE_EXPORT bool exists() const;
0076 Q_CORE_EXPORT bool isHidden() const;
0077 Q_CORE_EXPORT bool isReadable() const;
0078 Q_CORE_EXPORT bool isWritable() const;
0079 Q_CORE_EXPORT bool isExecutable() const;
0080 Q_CORE_EXPORT QFileInfo fileInfo() const;
0081 Q_CORE_EXPORT QString canonicalFilePath() const;
0082 Q_CORE_EXPORT QString absoluteFilePath() const;
0083 Q_CORE_EXPORT QString absolutePath() const;
0084 Q_CORE_EXPORT qint64 size() const;
0085
0086 QDateTime birthTime(const QTimeZone &tz) const
0087 { return fileTime(QFileDevice::FileBirthTime, tz); }
0088 QDateTime metadataChangeTime(const QTimeZone &tz) const
0089 { return fileTime(QFileDevice::FileMetadataChangeTime, tz); }
0090 QDateTime lastModified(const QTimeZone &tz) const
0091 { return fileTime(QFileDevice::FileModificationTime, tz); }
0092 QDateTime lastRead(const QTimeZone &tz) const
0093 { return fileTime(QFileDevice::FileAccessTime, tz); }
0094 Q_CORE_EXPORT QDateTime fileTime(QFileDevice::FileTime type, const QTimeZone &tz) const;
0095 };
0096
0097 class sentinel
0098 {
0099 friend constexpr bool operator==(sentinel, sentinel) noexcept { return true; }
0100 friend constexpr bool operator!=(sentinel, sentinel) noexcept { return false; }
0101 };
0102
0103 class const_iterator
0104 {
0105 Q_DISABLE_COPY(const_iterator)
0106 friend class QDirListing;
0107 explicit const_iterator(QDirListingPrivate *dp) { dirEntry.dirListPtr = dp; }
0108 DirEntry dirEntry;
0109 public:
0110 using iterator_category = std::input_iterator_tag;
0111 using value_type = DirEntry;
0112 using difference_type = qint64;
0113 using pointer = const value_type *;
0114 using reference = const value_type &;
0115
0116 const_iterator() = default;
0117 const_iterator(const_iterator &&) noexcept = default;
0118 const_iterator &operator=(const_iterator &&) noexcept = default;
0119
0120 reference operator*() const { return dirEntry; }
0121 pointer operator->() const { return &dirEntry; }
0122 const_iterator &operator++() { dirEntry = next(dirEntry); return *this; }
0123 void operator++(int) { ++*this; }
0124 private:
0125 bool atEnd() const noexcept { return dirEntry.dirListPtr == nullptr; }
0126 friend bool operator==(const const_iterator &lhs, sentinel) noexcept { return lhs.atEnd(); }
0127 #ifndef __cpp_impl_three_way_comparison
0128 friend bool operator!=(const const_iterator &lhs, sentinel) noexcept
0129 { return !operator==(lhs, sentinel{}); }
0130 friend bool operator==(sentinel, const const_iterator &rhs) noexcept
0131 { return operator==(rhs, sentinel{}); }
0132 friend bool operator!=(sentinel, const const_iterator &rhs) noexcept
0133 { return !operator==(sentinel{}, rhs); }
0134 #endif
0135 };
0136
0137 Q_CORE_EXPORT const_iterator begin() const;
0138 const_iterator cbegin() const { return begin(); }
0139 sentinel end() const { return {}; }
0140 sentinel cend() const { return end(); }
0141
0142
0143 const_iterator constBegin() const { return begin(); }
0144 sentinel constEnd() const { return end(); }
0145
0146 private:
0147 Q_DISABLE_COPY(QDirListing)
0148
0149 Q_CORE_EXPORT static DirEntry next(DirEntry);
0150
0151
0152
0153
0154
0155 Q_CORE_EXPORT QDirListing(const QString &path, const QStringList &nameFilters, uint dirFilters,
0156 uint qdirIteratorFlags = 0);
0157
0158 QDirListingPrivate *d;
0159 friend class QDir;
0160 friend class QDirPrivate;
0161 friend class QDirIteratorPrivate;
0162 friend class QAbstractFileEngine;
0163 friend class QFileInfoGatherer;
0164 };
0165
0166 Q_DECLARE_OPERATORS_FOR_FLAGS(QDirListing::IteratorFlags)
0167
0168 QT_END_NAMESPACE
0169
0170 #endif