1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QDIR_H
41#define QDIR_H
42
43#include <QtCore/qstring.h>
44#include <QtCore/qfile.h>
45#include <QtCore/qfileinfo.h>
46#include <QtCore/qstringlist.h>
47#include <QtCore/qshareddata.h>
48
49QT_BEGIN_NAMESPACE
50
51class QDirIterator;
52class QDirPrivate;
53
54class Q_CORE_EXPORT QDir
55{
56public:
57 enum Filter { Dirs = 0x001,
58 Files = 0x002,
59 Drives = 0x004,
60 NoSymLinks = 0x008,
61 AllEntries = Dirs | Files | Drives,
62 TypeMask = 0x00f,
63
64 Readable = 0x010,
65 Writable = 0x020,
66 Executable = 0x040,
67 PermissionMask = 0x070,
68
69 Modified = 0x080,
70 Hidden = 0x100,
71 System = 0x200,
72
73 AccessMask = 0x3F0,
74
75 AllDirs = 0x400,
76 CaseSensitive = 0x800,
77 NoDot = 0x2000,
78 NoDotDot = 0x4000,
79 NoDotAndDotDot = NoDot | NoDotDot,
80
81 NoFilter = -1
82 };
83 Q_DECLARE_FLAGS(Filters, Filter)
84
85 enum SortFlag { Name = 0x00,
86 Time = 0x01,
87 Size = 0x02,
88 Unsorted = 0x03,
89 SortByMask = 0x03,
90
91 DirsFirst = 0x04,
92 Reversed = 0x08,
93 IgnoreCase = 0x10,
94 DirsLast = 0x20,
95 LocaleAware = 0x40,
96 Type = 0x80,
97 NoSort = -1
98 };
99 Q_DECLARE_FLAGS(SortFlags, SortFlag)
100
101 QDir(const QDir &);
102 QDir(const QString &path = QString());
103 QDir(const QString &path, const QString &nameFilter,
104 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
105#ifdef Q_CLANG_QDOC
106 QDir(const std::filesystem::path &path);
107 QDir(const std::filesystem::path &path, const QString &nameFilter,
108 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
109#elif QT_CONFIG(cxx17_filesystem)
110 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
111 QDir(const T &path) : QDir(QtPrivate::fromFilesystemPath(path))
112 {
113 }
114 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
115 QDir(const T &path, const QString &nameFilter,
116 SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries)
117 : QDir(QtPrivate::fromFilesystemPath(path), nameFilter, sort, filter)
118 {
119 }
120#endif // QT_CONFIG(cxx17_filesystem)
121 ~QDir();
122
123 QDir &operator=(const QDir &);
124 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDir)
125
126 void swap(QDir &other) noexcept
127 { qSwap(d_ptr, other.d_ptr); }
128
129 void setPath(const QString &path);
130#ifdef Q_CLANG_QDOC
131 void setPath(const std::filesystem::path &path);
132#elif QT_CONFIG(cxx17_filesystem)
133 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
134 void setPath(const T &path)
135 {
136 setPath(QtPrivate::fromFilesystemPath(path));
137 }
138#endif // QT_CONFIG(cxx17_filesystem)
139 QString path() const;
140 QString absolutePath() const;
141 QString canonicalPath() const;
142#if QT_CONFIG(cxx17_filesystem) || defined(Q_CLANG_QDOC)
143 std::filesystem::path filesystemPath() const
144 { return QtPrivate::toFilesystemPath(path()); }
145 std::filesystem::path filesystemAbsolutePath() const
146 { return QtPrivate::toFilesystemPath(absolutePath()); }
147 std::filesystem::path filesystemCanonicalPath() const
148 { return QtPrivate::toFilesystemPath(canonicalPath()); }
149#endif // QT_CONFIG(cxx17_filesystem)
150
151 static void setSearchPaths(const QString &prefix, const QStringList &searchPaths);
152 static void addSearchPath(const QString &prefix, const QString &path);
153#ifdef Q_CLANG_QDOC
154 static void addSearchPath(const QString &prefix, const std::filesystem::path &path);
155#elif QT_CONFIG(cxx17_filesystem)
156 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
157 static void addSearchPath(const QString &prefix, const T &path)
158 {
159 addSearchPath(prefix, QtPrivate::fromFilesystemPath(path));
160 }
161#endif // QT_CONFIG(cxx17_filesystem)
162 static QStringList searchPaths(const QString &prefix);
163
164 QString dirName() const;
165 QString filePath(const QString &fileName) const;
166 QString absoluteFilePath(const QString &fileName) const;
167 QString relativeFilePath(const QString &fileName) const;
168
169 static QString toNativeSeparators(const QString &pathName);
170 static QString fromNativeSeparators(const QString &pathName);
171
172 bool cd(const QString &dirName);
173 bool cdUp();
174
175 QStringList nameFilters() const;
176 void setNameFilters(const QStringList &nameFilters);
177
178 Filters filter() const;
179 void setFilter(Filters filter);
180 SortFlags sorting() const;
181 void setSorting(SortFlags sort);
182
183 uint count() const;
184 bool isEmpty(Filters filters = Filters(AllEntries | NoDotAndDotDot)) const;
185
186 QString operator[](int) const;
187
188 static QStringList nameFiltersFromString(const QString &nameFilter);
189
190 QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
191 QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter,
192 SortFlags sort = NoSort) const;
193
194 QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
195 QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter,
196 SortFlags sort = NoSort) const;
197
198 bool mkdir(const QString &dirName) const;
199 bool rmdir(const QString &dirName) const;
200 bool mkpath(const QString &dirPath) const;
201 bool rmpath(const QString &dirPath) const;
202
203 bool removeRecursively();
204
205 bool isReadable() const;
206 bool exists() const;
207 bool isRoot() const;
208
209 static bool isRelativePath(const QString &path);
210 inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); }
211 bool isRelative() const;
212 inline bool isAbsolute() const { return !isRelative(); }
213 bool makeAbsolute();
214
215 bool operator==(const QDir &dir) const;
216 inline bool operator!=(const QDir &dir) const { return !operator==(dir); }
217
218 bool remove(const QString &fileName);
219 bool rename(const QString &oldName, const QString &newName);
220 bool exists(const QString &name) const;
221
222 static QFileInfoList drives();
223
224 constexpr static inline QChar listSeparator() noexcept
225 {
226#if defined(Q_OS_WIN)
227 return QLatin1Char(';');
228#else
229 return QLatin1Char(':');
230#endif
231 }
232
233 static QChar separator()
234 {
235#if defined(Q_OS_WIN)
236 return QLatin1Char('\\');
237#else
238 return QLatin1Char('/');
239#endif
240 }
241
242 static bool setCurrent(const QString &path);
243 static inline QDir current() { return QDir(currentPath()); }
244 static QString currentPath();
245
246 static inline QDir home() { return QDir(homePath()); }
247 static QString homePath();
248 static inline QDir root() { return QDir(rootPath()); }
249 static QString rootPath();
250 static inline QDir temp() { return QDir(tempPath()); }
251 static QString tempPath();
252
253#if QT_CONFIG(regularexpression)
254 static bool match(const QStringList &filters, const QString &fileName);
255 static bool match(const QString &filter, const QString &fileName);
256#endif
257
258 static QString cleanPath(const QString &path);
259 void refresh() const;
260
261protected:
262 explicit QDir(QDirPrivate &d);
263
264 QSharedDataPointer<QDirPrivate> d_ptr;
265
266private:
267 friend class QDirIterator;
268 // Q_DECLARE_PRIVATE equivalent for shared data pointers
269 QDirPrivate *d_func();
270 const QDirPrivate *d_func() const { return d_ptr.constData(); }
271};
272
273Q_DECLARE_SHARED(QDir)
274Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::Filters)
275Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::SortFlags)
276
277#ifndef QT_NO_DEBUG_STREAM
278class QDebug;
279Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters);
280Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QDir &dir);
281#endif
282
283QT_END_NAMESPACE
284
285#endif // QDIR_H
286

source code of qtbase/src/corelib/io/qdir.h