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