QtBase  v6.3.1
qdir.h
Go to the documentation of this file.
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 
50 
51 class QDirIterator;
52 class QDirPrivate;
53 
54 class Q_CORE_EXPORT QDir
55 {
56 public:
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
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 &);
125 
126  void swap(QDir &other) noexcept
127  { d_ptr.swap(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 mkdir(const QString &dirName, QFile::Permissions permissions) const;
200  bool rmdir(const QString &dirName) const;
201  bool mkpath(const QString &dirPath) const;
202  bool rmpath(const QString &dirPath) const;
203 
204  bool removeRecursively();
205 
206  bool isReadable() const;
207  bool exists() const;
208  bool isRoot() const;
209 
210  static bool isRelativePath(const QString &path);
211  inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); }
212  bool isRelative() const;
213  inline bool isAbsolute() const { return !isRelative(); }
214  bool makeAbsolute();
215 
216  bool operator==(const QDir &dir) const;
217  inline bool operator!=(const QDir &dir) const { return !operator==(dir); }
218 
219  bool remove(const QString &fileName);
220  bool rename(const QString &oldName, const QString &newName);
221  bool exists(const QString &name) const;
222 
223  static QFileInfoList drives();
224 
225  constexpr static inline QChar listSeparator() noexcept
226  {
227 #if defined(Q_OS_WIN)
228  return QLatin1Char(';');
229 #else
230  return QLatin1Char(':');
231 #endif
232  }
233 
234  static QChar separator()
235  {
236 #if defined(Q_OS_WIN)
237  return QLatin1Char('\\');
238 #else
239  return QLatin1Char('/');
240 #endif
241  }
242 
243  static bool setCurrent(const QString &path);
244  static inline QDir current() { return QDir(currentPath()); }
245  static QString currentPath();
246 
247  static inline QDir home() { return QDir(homePath()); }
248  static QString homePath();
249  static inline QDir root() { return QDir(rootPath()); }
250  static QString rootPath();
251  static inline QDir temp() { return QDir(tempPath()); }
252  static QString tempPath();
253 
254 #if QT_CONFIG(regularexpression)
255  static bool match(const QStringList &filters, const QString &fileName);
256  static bool match(const QString &filter, const QString &fileName);
257 #endif
258 
259  static QString cleanPath(const QString &path);
260  void refresh() const;
261 
262 protected:
263  explicit QDir(QDirPrivate &d);
264 
266 
267 private:
268  friend class QDirIterator;
269  // Q_DECLARE_PRIVATE equivalent for shared data pointers
270  QDirPrivate *d_func();
271  const QDirPrivate *d_func() const { return d_ptr.constData(); }
272 };
273 
275 Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::Filters)
276 Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::SortFlags)
277 
278 #ifndef QT_NO_DEBUG_STREAM
279 class QDebug;
280 Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters);
281 Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QDir &dir);
282 #endif
283 
285 
286 #endif // QDIR_H
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:84
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:55
bool operator!=(const QDir &dir) const
Definition: qdir.h:217
bool isAbsolute() const
Definition: qdir.h:213
static bool isAbsolutePath(const QString &path)
Definition: qdir.h:211
static QDir root()
Definition: qdir.h:249
static void setSearchPaths(const QString &prefix, const QStringList &searchPaths)
static QDir current()
Definition: qdir.h:244
static QDir temp()
Definition: qdir.h:251
static QStringList searchPaths(const QString &prefix)
QSharedDataPointer< QDirPrivate > d_ptr
Definition: qdir.h:265
static QChar separator()
Definition: qdir.h:234
static void addSearchPath(const QString &prefix, const QString &path)
static QDir home()
Definition: qdir.h:247
SortFlag
Definition: qdir.h:85
Filter
Definition: qdir.h:57
constexpr static QChar listSeparator() noexcept
Definition: qdir.h:225
The QDirIterator class provides an iterator for directory entrylists.
Definition: qdiriterator.h:49
const T * constData() const noexcept
Definition: qshareddata.h:87
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringList class provides a list of strings.
@ CaseSensitive
Definition: qnamespace.h:1284
#define QString()
Definition: parse-defines.h:51
void
Definition: png.h:1080
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition: qflags.h:210
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition: qflags.h:227
unsigned int uint
Definition: qglobal.h:334
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Class)
Definition: qglobal.h:563
bool operator==(const QMakeBaseKey &one, const QMakeBaseKey &two)
@ AccessMask
Definition: qmetaobject_p.h:90
@ Readable
Definition: qmetaobject_p.h:69
@ Writable
Definition: qmetaobject_p.h:70
GLenum GLenum GLsizei count
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLuint name
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
#define Q_DECLARE_SHARED(TYPE)
Definition: qtypeinfo.h:197
@ Time
Definition: sctpchannels.h:56
settings remove("monkey")
QSharedPointer< T > other(t)
[5]
this swap(other)
QString dir
[11]
dialog setNameFilters(filters)
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:53
Definition: main.cpp:38
Definition: moc.h:48