summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2021-07-11 19:28:35 +0200
committerMarc Mutz <marc.mutz@kdab.com>2021-07-28 15:16:42 +0200
commit25fff849e8f34af6d41ff36f2891bb4099b89360 (patch)
treed34bb46cd97d25bd2abeee68c9060a1a9b40ff22 /src
parent8ccd5d5af295ae36440157fe1d00a176fdf1c6bf (diff)
QDirIterator: add nextFileInfo()
Before this change, next() was the only way to advance the iterator, whether the caller was ultimately interested in just the filePath() (good) or not (bad luck, had to call .fileInfo()). Add a new function, nextFileInfo(), with returns fileInfo() instead. Incidentally, the returned object has already been constructed as part of advance()ing the iterator, so the new function is faster than next() even if the result is ignored, because we're not calculating a QString result the caller may not be interested in. Use the new function around the code. Fix a couple of cases of next(); fileInfo().filePath() (just use next()'s return value) as a drive-by. [ChangeLog][QtCore][QDirIterator] Added nextFileInfo(), which is like next(), but returns fileInfo() instead of filePath(). Change-Id: I601220575961169b44139fc55b9eae6c3197afb4 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/io/qdir.cpp21
-rw-r--r--src/corelib/io/qdiriterator.cpp38
-rw-r--r--src/corelib/io/qdiriterator.h1
-rw-r--r--src/corelib/io/qfileinfo.cpp4
-rw-r--r--src/corelib/io/qstorageinfo_unix.cpp3
-rw-r--r--src/gui/itemmodels/qfileinfogatherer.cpp3
-rw-r--r--src/network/access/qnetworkdiskcache.cpp4
-rw-r--r--src/plugins/tls/openssl/qtlsbackend_openssl.cpp3
-rw-r--r--src/testlib/qtestcase.cpp4
-rw-r--r--src/tools/androiddeployqt/main.cpp3
10 files changed, 46 insertions, 38 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp
index e92d2f9ca78..76336ef3846 100644
--- a/src/corelib/io/qdir.cpp
+++ b/src/corelib/io/qdir.cpp
@@ -355,10 +355,8 @@ inline void QDirPrivate::initFileLists(const QDir &dir) const
if (!fileListsInitialized) {
QFileInfoList l;
QDirIterator it(dir);
- while (it.hasNext()) {
- it.next();
- l.append(it.fileInfo());
- }
+ while (it.hasNext())
+ l.append(it.nextFileInfo());
sortFileList(sort, l, &files, &fileInfos);
fileListsInitialized = true;
}
@@ -1397,10 +1395,8 @@ QStringList QDir::entryList(const QStringList &nameFilters, Filters filters,
QFileInfoList l;
QDirIterator it(d->dirEntry.filePath(), nameFilters, filters);
- while (it.hasNext()) {
- it.next();
- l.append(it.fileInfo());
- }
+ while (it.hasNext())
+ l.append(it.nextFileInfo());
QStringList ret;
d->sortFileList(sort, l, &ret, nullptr);
return ret;
@@ -1439,10 +1435,8 @@ QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, Filters filter
QFileInfoList l;
QDirIterator it(d->dirEntry.filePath(), nameFilters, filters);
- while (it.hasNext()) {
- it.next();
- l.append(it.fileInfo());
- }
+ while (it.hasNext())
+ l.append(it.nextFileInfo());
QFileInfoList ret;
d->sortFileList(sort, l, nullptr, &ret);
return ret;
@@ -1580,8 +1574,7 @@ bool QDir::removeRecursively()
// not empty -- we must empty it first
QDirIterator di(dirPath, QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
while (di.hasNext()) {
- di.next();
- const QFileInfo& fi = di.fileInfo();
+ const QFileInfo fi = di.nextFileInfo();
const QString &filePath = di.filePath();
bool ok;
if (fi.isDir() && !fi.isSymLink()) {
diff --git a/src/corelib/io/qdiriterator.cpp b/src/corelib/io/qdiriterator.cpp
index 792f2a863d4..7e86aacdb11 100644
--- a/src/corelib/io/qdiriterator.cpp
+++ b/src/corelib/io/qdiriterator.cpp
@@ -60,11 +60,11 @@
\snippet code/src_corelib_io_qdiriterator.cpp 1
- The next() function returns the path to the next directory entry and
- advances the iterator. You can also call filePath() to get the current
- file path without advancing the iterator. The fileName() function returns
- only the name of the file, similar to how QDir::entryList() works. You can
- also call fileInfo() to get a QFileInfo for the current entry.
+ The next() and nextFileInfo() functions advance the iterator and return
+ the path or the QFileInfo of the next directory entry. You can also call
+ filePath() or fileInfo() to get the current file path or QFileInfo without
+ first advancing the iterator. The fileName() function returns only the
+ name of the file, similar to how QDir::entryList() works.
Unlike Qt's container iterators, QDirIterator is uni-directional (i.e.,
you cannot iterate directories in reverse order) and does not allow random
@@ -490,10 +490,12 @@ QDirIterator::~QDirIterator()
new entry. If hasNext() returns \c false, this function does nothing, and
returns an empty QString.
- You can call fileName() or filePath() to get the current entry file name
+ You can call fileName() or filePath() to get the current entry's file name
or path, or fileInfo() to get a QFileInfo for the current entry.
- \sa hasNext(), fileName(), filePath(), fileInfo()
+ Call nextFileInfo() instead of next() if you're interested in the QFileInfo.
+
+ \sa hasNext(), nextFileInfo(), fileName(), filePath(), fileInfo()
*/
QString QDirIterator::next()
{
@@ -502,10 +504,30 @@ QString QDirIterator::next()
}
/*!
+ \since 6.3
+
+ Advances the iterator to the next entry, and returns the file info of this
+ new entry. If hasNext() returns \c false, this function does nothing, and
+ returns an empty QFileInfo.
+
+ You can call fileName() or filePath() to get the current entry's file name
+ or path, or fileInfo() to get a QFileInfo for the current entry.
+
+ Call next() instead of nextFileInfo() when all you need is the filePath().
+
+ \sa hasNext(), fileName(), filePath(), fileInfo()
+*/
+QFileInfo QDirIterator::nextFileInfo()
+{
+ d->advance();
+ return fileInfo();
+}
+
+/*!
Returns \c true if there is at least one more entry in the directory;
otherwise, false is returned.
- \sa next(), fileName(), filePath(), fileInfo()
+ \sa next(), nextFileInfo(), fileName(), filePath(), fileInfo()
*/
bool QDirIterator::hasNext() const
{
diff --git a/src/corelib/io/qdiriterator.h b/src/corelib/io/qdiriterator.h
index acfe040a262..f3a4b979acb 100644
--- a/src/corelib/io/qdiriterator.h
+++ b/src/corelib/io/qdiriterator.h
@@ -69,6 +69,7 @@ public:
~QDirIterator();
QString next();
+ QFileInfo nextFileInfo();
bool hasNext() const;
QString fileName() const;
diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp
index 8d471c3e1c0..8f2eecc22ae 100644
--- a/src/corelib/io/qfileinfo.cpp
+++ b/src/corelib/io/qfileinfo.cpp
@@ -1694,10 +1694,8 @@ QDebug operator<<(QDebug dbg, const QFileInfo &fi)
QDirIterator it(dir);
while (it.hasNext()) {
- it.next();
-
// Extract the QFileInfo from the iterator directly:
- QFileInfo fi = it.fileInfo();
+ QFileInfo fi = it.nextFileInfo();
~~~
}
diff --git a/src/corelib/io/qstorageinfo_unix.cpp b/src/corelib/io/qstorageinfo_unix.cpp
index bdb798e89ad..6845a24838d 100644
--- a/src/corelib/io/qstorageinfo_unix.cpp
+++ b/src/corelib/io/qstorageinfo_unix.cpp
@@ -793,8 +793,7 @@ static inline QString retrieveLabel(const QByteArray &device)
QDirIterator it(QLatin1String(pathDiskByLabel), QDir::NoDotAndDotDot);
while (it.hasNext()) {
- it.next();
- QFileInfo fileInfo(it.fileInfo());
+ QFileInfo fileInfo = it.nextFileInfo();
if (fileInfo.isSymLink() && fileInfo.symLinkTarget() == devicePath)
return decodeFsEncString(fileInfo.fileName());
}
diff --git a/src/gui/itemmodels/qfileinfogatherer.cpp b/src/gui/itemmodels/qfileinfogatherer.cpp
index bd368e945c5..ec4ae2269bd 100644
--- a/src/gui/itemmodels/qfileinfogatherer.cpp
+++ b/src/gui/itemmodels/qfileinfogatherer.cpp
@@ -401,8 +401,7 @@ void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &fil
if (files.isEmpty()) {
QDirIterator dirIt(path, QDir::AllEntries | QDir::System | QDir::Hidden);
while (!abort.loadRelaxed() && dirIt.hasNext()) {
- dirIt.next();
- fileInfo = dirIt.fileInfo();
+ fileInfo = dirIt.nextFileInfo();
fileInfo.stat();
allFiles.append(fileInfo.fileName());
fetch(fileInfo, base, firstTime, updatedFiles, path);
diff --git a/src/network/access/qnetworkdiskcache.cpp b/src/network/access/qnetworkdiskcache.cpp
index 7b175404176..c6c20b742c6 100644
--- a/src/network/access/qnetworkdiskcache.cpp
+++ b/src/network/access/qnetworkdiskcache.cpp
@@ -522,8 +522,8 @@ qint64 QNetworkDiskCache::expire()
QMultiMap<QDateTime, QString> cacheItems;
qint64 totalSize = 0;
while (it.hasNext()) {
- QString path = it.next();
- QFileInfo info = it.fileInfo();
+ QFileInfo info = it.nextFileInfo();
+ QString path = info.filePath();
QString fileName = info.fileName();
if (fileName.endsWith(CACHE_POSTFIX)) {
const QDateTime birthTime = info.fileTime(QFile::FileBirthTime);
diff --git a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
index 2374f79ed6e..5d3b3ea582a 100644
--- a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
+++ b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
@@ -413,9 +413,8 @@ QList<QSslCertificate> systemCaCertificates()
currentDir.setPath(QLatin1String(directory));
QDirIterator it(currentDir);
while (it.hasNext()) {
- it.next();
// use canonical path here to not load the same certificate twice if symlinked
- certFiles.insert(it.fileInfo().canonicalFilePath());
+ certFiles.insert(it.nextFileInfo().canonicalFilePath());
}
}
for (const QString& file : qAsConst(certFiles))
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index 8d43815cc8c..631ca9afb60 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -2158,9 +2158,7 @@ QSharedPointer<QTemporaryDir> QTest::qExtractTestData(const QString &dirName)
}
while (it.hasNext()) {
- it.next();
-
- QFileInfo fileInfo = it.fileInfo();
+ QFileInfo fileInfo = it.nextFileInfo();
if (!fileInfo.isDir()) {
const QString destination = dataPath + QLatin1Char('/') + QStringView{fileInfo.filePath()}.mid(resourcePath.length());
diff --git a/src/tools/androiddeployqt/main.cpp b/src/tools/androiddeployqt/main.cpp
index 0bd9f4e16e5..25e5ea3d44e 100644
--- a/src/tools/androiddeployqt/main.cpp
+++ b/src/tools/androiddeployqt/main.cpp
@@ -1080,8 +1080,7 @@ bool readInputFile(Options *options)
if (QFileInfo(path).isDir()) {
QDirIterator iterator(path, QDirIterator::Subdirectories);
while (iterator.hasNext()) {
- iterator.next();
- if (iterator.fileInfo().isFile()) {
+ if (iterator.nextFileInfo().isFile()) {
QString subPath = iterator.filePath();
auto arch = fileArchitecture(*options, subPath);
if (!arch.isEmpty()) {