| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QDIR_H |
| 5 | #define QDIR_H |
| 6 | |
| 7 | #include <QtCore/qcompare.h> |
| 8 | #include <QtCore/qstring.h> |
| 9 | #include <QtCore/qdirlisting.h> |
| 10 | #include <QtCore/qfile.h> |
| 11 | #include <QtCore/qfileinfo.h> |
| 12 | #include <QtCore/qstringlist.h> |
| 13 | #include <QtCore/qshareddata.h> |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | class QDirIterator; |
| 18 | class QDirPrivate; |
| 19 | |
| 20 | class Q_CORE_EXPORT QDir |
| 21 | { |
| 22 | public: |
| 23 | enum Filter { Dirs = 0x001, |
| 24 | Files = 0x002, |
| 25 | Drives = 0x004, |
| 26 | NoSymLinks = 0x008, |
| 27 | AllEntries = Dirs | Files | Drives, |
| 28 | TypeMask = 0x00f, |
| 29 | |
| 30 | Readable = 0x010, |
| 31 | Writable = 0x020, |
| 32 | Executable = 0x040, |
| 33 | PermissionMask = 0x070, |
| 34 | |
| 35 | Modified = 0x080, |
| 36 | Hidden = 0x100, |
| 37 | System = 0x200, |
| 38 | |
| 39 | AccessMask = 0x3F0, |
| 40 | |
| 41 | AllDirs = 0x400, |
| 42 | CaseSensitive = 0x800, |
| 43 | NoDot = 0x2000, |
| 44 | NoDotDot = 0x4000, |
| 45 | NoDotAndDotDot = NoDot | NoDotDot, |
| 46 | |
| 47 | NoFilter = -1 |
| 48 | }; |
| 49 | Q_DECLARE_FLAGS(Filters, Filter) |
| 50 | |
| 51 | enum SortFlag { Name = 0x00, |
| 52 | Time = 0x01, |
| 53 | Size = 0x02, |
| 54 | Unsorted = 0x03, |
| 55 | SortByMask = 0x03, |
| 56 | |
| 57 | DirsFirst = 0x04, |
| 58 | Reversed = 0x08, |
| 59 | IgnoreCase = 0x10, |
| 60 | DirsLast = 0x20, |
| 61 | LocaleAware = 0x40, |
| 62 | Type = 0x80, |
| 63 | NoSort = -1 |
| 64 | }; |
| 65 | Q_DECLARE_FLAGS(SortFlags, SortFlag) |
| 66 | |
| 67 | QDir(const QDir &); |
| 68 | QDir(const QString &path = QString()); |
| 69 | QDir(const QString &path, const QString &nameFilter, |
| 70 | SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries); |
| 71 | #ifdef Q_QDOC |
| 72 | QDir(const std::filesystem::path &path); |
| 73 | QDir(const std::filesystem::path &path, const QString &nameFilter, |
| 74 | SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries); |
| 75 | #elif QT_CONFIG(cxx17_filesystem) |
| 76 | template<typename T, QtPrivate::ForceFilesystemPath<T> = 0> |
| 77 | QDir(const T &path) : QDir(QtPrivate::fromFilesystemPath(path)) |
| 78 | { |
| 79 | } |
| 80 | template<typename T, QtPrivate::ForceFilesystemPath<T> = 0> |
| 81 | QDir(const T &path, const QString &nameFilter, |
| 82 | SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries) |
| 83 | : QDir(QtPrivate::fromFilesystemPath(path), nameFilter, sort, filter) |
| 84 | { |
| 85 | } |
| 86 | #endif // QT_CONFIG(cxx17_filesystem) |
| 87 | ~QDir(); |
| 88 | |
| 89 | QDir &operator=(const QDir &); |
| 90 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDir) |
| 91 | |
| 92 | void swap(QDir &other) noexcept |
| 93 | { d_ptr.swap(other&: other.d_ptr); } |
| 94 | |
| 95 | void setPath(const QString &path); |
| 96 | #ifdef Q_QDOC |
| 97 | void setPath(const std::filesystem::path &path); |
| 98 | #elif QT_CONFIG(cxx17_filesystem) |
| 99 | template<typename T, QtPrivate::ForceFilesystemPath<T> = 0> |
| 100 | void setPath(const T &path) |
| 101 | { |
| 102 | setPath(QtPrivate::fromFilesystemPath(path)); |
| 103 | } |
| 104 | #endif // QT_CONFIG(cxx17_filesystem) |
| 105 | QString path() const; |
| 106 | QString absolutePath() const; |
| 107 | QString canonicalPath() const; |
| 108 | #if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC) |
| 109 | std::filesystem::path filesystemPath() const |
| 110 | { return QtPrivate::toFilesystemPath(path: path()); } |
| 111 | std::filesystem::path filesystemAbsolutePath() const |
| 112 | { return QtPrivate::toFilesystemPath(path: absolutePath()); } |
| 113 | std::filesystem::path filesystemCanonicalPath() const |
| 114 | { return QtPrivate::toFilesystemPath(path: canonicalPath()); } |
| 115 | #endif // QT_CONFIG(cxx17_filesystem) |
| 116 | |
| 117 | #ifndef QT_BOOTSTRAPPED |
| 118 | static void setSearchPaths(const QString &prefix, const QStringList &searchPaths); |
| 119 | static void addSearchPath(const QString &prefix, const QString &path); |
| 120 | #ifdef Q_QDOC |
| 121 | static void addSearchPath(const QString &prefix, const std::filesystem::path &path); |
| 122 | #elif QT_CONFIG(cxx17_filesystem) |
| 123 | template<typename T, QtPrivate::ForceFilesystemPath<T> = 0> |
| 124 | static void addSearchPath(const QString &prefix, const T &path) |
| 125 | { |
| 126 | addSearchPath(prefix, QtPrivate::fromFilesystemPath(path)); |
| 127 | } |
| 128 | #endif // QT_CONFIG(cxx17_filesystem) |
| 129 | static QStringList searchPaths(const QString &prefix); |
| 130 | #endif // QT_BOOTSTRAPPED |
| 131 | |
| 132 | QString dirName() const; |
| 133 | QString filePath(const QString &fileName) const; |
| 134 | QString absoluteFilePath(const QString &fileName) const; |
| 135 | QString relativeFilePath(const QString &fileName) const; |
| 136 | |
| 137 | static QString toNativeSeparators(const QString &pathName); |
| 138 | static QString fromNativeSeparators(const QString &pathName); |
| 139 | |
| 140 | bool cd(const QString &dirName); |
| 141 | bool cdUp(); |
| 142 | |
| 143 | QStringList nameFilters() const; |
| 144 | void setNameFilters(const QStringList &nameFilters); |
| 145 | |
| 146 | Filters filter() const; |
| 147 | void setFilter(Filters filter); |
| 148 | SortFlags sorting() const; |
| 149 | void setSorting(SortFlags sort); |
| 150 | |
| 151 | #if QT_CORE_REMOVED_SINCE(6, 5) |
| 152 | uint count() const; |
| 153 | #endif |
| 154 | qsizetype count(QT6_DECL_NEW_OVERLOAD) const; |
| 155 | bool isEmpty(Filters filters = Filters(AllEntries | NoDotAndDotDot)) const; |
| 156 | |
| 157 | #if QT_CORE_REMOVED_SINCE(6, 5) && QT_POINTER_SIZE != 4 |
| 158 | QString operator[](int) const; |
| 159 | #endif |
| 160 | QString operator[](qsizetype) const; |
| 161 | |
| 162 | static QStringList nameFiltersFromString(const QString &nameFilter); |
| 163 | |
| 164 | QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const; |
| 165 | QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter, |
| 166 | SortFlags sort = NoSort) const; |
| 167 | |
| 168 | QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const; |
| 169 | QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter, |
| 170 | SortFlags sort = NoSort) const; |
| 171 | |
| 172 | bool mkdir(const QString &dirName) const; |
| 173 | bool mkdir(const QString &dirName, QFile::Permissions permissions) const; |
| 174 | bool rmdir(const QString &dirName) const; |
| 175 | bool mkpath(const QString &dirPath) const; |
| 176 | bool rmpath(const QString &dirPath) const; |
| 177 | |
| 178 | bool removeRecursively(); |
| 179 | |
| 180 | bool isReadable() const; |
| 181 | bool exists() const; |
| 182 | bool isRoot() const; |
| 183 | |
| 184 | static bool isRelativePath(const QString &path); |
| 185 | inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); } |
| 186 | bool isRelative() const; |
| 187 | inline bool isAbsolute() const { return !isRelative(); } |
| 188 | bool makeAbsolute(); |
| 189 | |
| 190 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 191 | bool operator==(const QDir &dir) const; |
| 192 | inline bool operator!=(const QDir &dir) const { return !operator==(dir); } |
| 193 | #endif |
| 194 | |
| 195 | bool remove(const QString &fileName); |
| 196 | bool rename(const QString &oldName, const QString &newName); |
| 197 | bool exists(const QString &name) const; |
| 198 | |
| 199 | static QFileInfoList drives(); |
| 200 | |
| 201 | constexpr static inline QChar listSeparator() noexcept |
| 202 | { |
| 203 | #if defined(Q_OS_WIN) |
| 204 | return u';'; |
| 205 | #else |
| 206 | return u':'; |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | static QChar separator() |
| 211 | { |
| 212 | #if defined(Q_OS_WIN) |
| 213 | return u'\\'; |
| 214 | #else |
| 215 | return u'/'; |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | static bool setCurrent(const QString &path); |
| 220 | static inline QDir current() { return QDir(currentPath()); } |
| 221 | static QString currentPath(); |
| 222 | |
| 223 | static inline QDir home() { return QDir(homePath()); } |
| 224 | static QString homePath(); |
| 225 | static inline QDir root() { return QDir(rootPath()); } |
| 226 | static QString rootPath(); |
| 227 | static inline QDir temp() { return QDir(tempPath()); } |
| 228 | static QString tempPath(); |
| 229 | |
| 230 | #if QT_CONFIG(regularexpression) |
| 231 | static bool match(const QStringList &filters, const QString &fileName); |
| 232 | static bool match(const QString &filter, const QString &fileName); |
| 233 | #endif |
| 234 | |
| 235 | static QString cleanPath(const QString &path); |
| 236 | void refresh() const; |
| 237 | |
| 238 | protected: |
| 239 | explicit QDir(QDirPrivate &d); |
| 240 | |
| 241 | QSharedDataPointer<QDirPrivate> d_ptr; |
| 242 | |
| 243 | private: |
| 244 | friend Q_CORE_EXPORT bool comparesEqual(const QDir &lhs, const QDir &rhs); |
| 245 | Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QDir) |
| 246 | friend class QDirIterator; |
| 247 | friend class QDirListing; |
| 248 | friend class QDirListingPrivate; |
| 249 | // Q_DECLARE_PRIVATE equivalent for shared data pointers |
| 250 | QDirPrivate *d_func(); |
| 251 | const QDirPrivate *d_func() const { return d_ptr.constData(); } |
| 252 | }; |
| 253 | |
| 254 | Q_DECLARE_SHARED(QDir) |
| 255 | Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::Filters) |
| 256 | Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::SortFlags) |
| 257 | |
| 258 | #ifndef QT_NO_DEBUG_STREAM |
| 259 | class QDebug; |
| 260 | Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters); |
| 261 | Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QDir &dir); |
| 262 | #endif |
| 263 | |
| 264 | QT_END_NAMESPACE |
| 265 | |
| 266 | #endif // QDIR_H |
| 267 | |