diff options
author | Elvis Angelaccio <elvis.angelaccio@kde.org> | 2016-09-12 18:48:17 +0200 |
---|---|---|
committer | Elvis Angelaccio <elvis.angelaccio@kde.org> | 2017-01-08 16:11:34 +0000 |
commit | 3f455a4b53254b3ebbafae91600a63caebee3e0e (patch) | |
tree | 5e63f750d03de84307369c66c0bfac144b238adf /src/corelib/io/qdir.cpp | |
parent | 5311d481c2159c0076afc31c1276e1bd5b674fc4 (diff) |
Introduce QDir::isEmpty()
A directory is empty when it doesn't contain files or folders.
We can exploit QDirIterator::hasNext() to check whether this is the
case. This is efficient since it doesn't list the whole folder (in the
non-empty case).
Test cases are added for both the empty and non-empty cases.
Change-Id: I0f7e26782c0f97f9c16f928dab6cae37927875d8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qdir.cpp')
-rw-r--r-- | src/corelib/io/qdir.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 437f774547..37795ae5c4 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -1843,6 +1843,26 @@ bool QDir::exists(const QString &name) const } /*! + Returns whether the directory is empty. + + Equivalent to \c{count() == 0} with filters + \c{QDir::AllEntries | QDir::NoDotAndDotDot}, but faster as it just checks + whether the directory contains at least one entry. + + \note Unless you set the \p filters flags to include \c{QDir::NoDotAndDotDot} + (as the default value does), no directory is empty. + + \sa count(), entryList(), setFilter() + \since 5.9 +*/ +bool QDir::isEmpty(Filters filters) const +{ + const auto d = d_ptr.constData(); + QDirIterator it(d->dirEntry.filePath(), d->nameFilters, filters); + return !it.hasNext(); +} + +/*! Returns a list of the root directories on this system. On Windows this returns a list of QFileInfo objects containing "C:/", |