123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 | // Copyright (C) 2016 The Qt Company Ltd.// Copyright (C) 2024 Ahmad Samir <a.samirh78@gmail.com>// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#ifndef QDIRLISTING_H#define QDIRLISTING_H#include <QtCore/qfiledevice.h>#include <QtCore/qflags.h>#include <QtCore/qtclasshelpermacros.h>#include <QtCore/qtcoreexports.h>#include <QtCore/qdatetime.h>#include <iterator>#include <utility> QT_BEGIN_NAMESPACE class QDirListingPrivate;class QFileInfo;class QDir;class QTimeZone;class QDirListing {public:enumclass IteratorFlag { Default =0x000000, ExcludeFiles =0x000004, ExcludeDirs =0x000008,QT6_ONLY(ExcludeSpecial =0x000010,) ExcludeOther =0x000010, ResolveSymlinks =0x000020, FilesOnly = ExcludeDirs | ExcludeOther, DirsOnly = ExcludeFiles | ExcludeOther, IncludeHidden =0x000040, IncludeDotAndDotDot =0x000080, CaseSensitive =0x000100, Recursive =0x000400, FollowDirSymlinks =0x000800,};Q_DECLARE_FLAGS(IteratorFlags, IteratorFlag) Q_CORE_EXPORT explicitQDirListing(const QString &path, IteratorFlags flags =IteratorFlag::Default); Q_CORE_EXPORT explicitQDirListing(const QString &path,const QStringList &nameFilters, IteratorFlags flags =IteratorFlag::Default);QDirListing(QDirListing &&other) noexcept : d{std::exchange(other.d,nullptr)} {}QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QDirListing)voidswap(QDirListing &other) noexcept {qt_ptr_swap(d, other.d); } Q_CORE_EXPORT ~QDirListing(); Q_CORE_EXPORT QString iteratorPath()const; Q_CORE_EXPORT IteratorFlags iteratorFlags()const; Q_CORE_EXPORT QStringList nameFilters()const;class DirEntry {friend class QDirListing; QDirListingPrivate *dirListPtr =nullptr;public: Q_CORE_EXPORT QString fileName()const; Q_CORE_EXPORT QString baseName()const; Q_CORE_EXPORT QString completeBaseName()const; Q_CORE_EXPORT QString suffix()const; Q_CORE_EXPORT QString bundleName()const; Q_CORE_EXPORT QString completeSuffix()const; Q_CORE_EXPORT QString filePath()const; Q_CORE_EXPORT boolisDir()const; Q_CORE_EXPORT boolisFile()const; Q_CORE_EXPORT boolisSymLink()const; Q_CORE_EXPORT boolexists()const; Q_CORE_EXPORT boolisHidden()const; Q_CORE_EXPORT boolisReadable()const; Q_CORE_EXPORT boolisWritable()const; Q_CORE_EXPORT boolisExecutable()const; Q_CORE_EXPORT QFileInfo fileInfo()const; Q_CORE_EXPORT QString canonicalFilePath()const; Q_CORE_EXPORT QString absoluteFilePath()const; Q_CORE_EXPORT QString absolutePath()const; Q_CORE_EXPORT qint64 size()const; QDateTime birthTime(const QTimeZone &tz)const{returnfileTime(QFileDevice::FileBirthTime, tz); } QDateTime metadataChangeTime(const QTimeZone &tz)const{returnfileTime(QFileDevice::FileMetadataChangeTime, tz); } QDateTime lastModified(const QTimeZone &tz)const{returnfileTime(QFileDevice::FileModificationTime, tz); } QDateTime lastRead(const QTimeZone &tz)const{returnfileTime(QFileDevice::FileAccessTime, tz); } Q_CORE_EXPORT QDateTime fileTime(QFileDevice::FileTime type,const QTimeZone &tz)const;};class sentinel {friend constexprbooloperator==(sentinel, sentinel) noexcept {return true; }friend constexprbooloperator!=(sentinel, sentinel) noexcept {return false; }};class const_iterator {Q_DISABLE_COPY(const_iterator)friend class QDirListing;explicitconst_iterator(QDirListingPrivate *dp) { dirEntry.dirListPtr = dp; } DirEntry dirEntry;public:using iterator_category =std::input_iterator_tag;using value_type = DirEntry;using difference_type = qint64;using pointer =const value_type *;using reference =const value_type &;const_iterator() =default;const_iterator(const_iterator &&) noexcept =default; const_iterator &operator=(const_iterator &&) noexcept =default; reference operator*()const{return dirEntry; } pointer operator->()const{return&dirEntry; } const_iterator &operator++() { dirEntry =next(dirEntry);return*this; }voidoperator++(int) { ++*this; }// [iterator.concept.winc]/14 not required to return sthprivate:boolatEnd()const noexcept {return dirEntry.dirListPtr ==nullptr; }friendbooloperator==(const const_iterator &lhs, sentinel) noexcept {return lhs.atEnd(); }#ifndef __cpp_impl_three_way_comparisonfriendbooloperator!=(const const_iterator &lhs, sentinel) noexcept {return!operator==(lhs, sentinel{}); }friendbooloperator==(sentinel,const const_iterator &rhs) noexcept {returnoperator==(rhs, sentinel{}); }friendbooloperator!=(sentinel,const const_iterator &rhs) noexcept {return!operator==(sentinel{}, rhs); }#endif// __cpp_impl_three_way_comparison}; Q_CORE_EXPORT const_iterator begin()const; const_iterator cbegin()const{returnbegin(); } sentinel end()const{return{}; } sentinel cend()const{returnend(); }// Qt compatibility const_iterator constBegin()const{returnbegin(); } sentinel constEnd()const{returnend(); }private:Q_DISABLE_COPY(QDirListing) Q_CORE_EXPORT static DirEntry next(DirEntry);// Private constructor that is used in deprecated code paths.// `uint` instead of QDir::Filters and QDirIterator::IteratorFlags// because qdir.h can't be included here; qdiriterator.h can't included// either, because it includes qdir.h Q_CORE_EXPORT QDirListing(const QString &path,const QStringList &nameFilters, uint dirFilters, uint qdirIteratorFlags =0);// QDirIterator::NoIteratorFlags == 0x0 QDirListingPrivate *d;friend class QDir;friend class QDirPrivate;friend class QDirIteratorPrivate;friend class QAbstractFileEngine;friend class QFileInfoGatherer;};Q_DECLARE_OPERATORS_FOR_FLAGS(QDirListing::IteratorFlags) QT_END_NAMESPACE #endif// QDIRLISTING_H
|