summaryrefslogtreecommitdiffstats
path: root/src/corelib/io
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/corelib/io
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/corelib/io')
-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
5 files changed, 40 insertions, 27 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp
index e92d2f9ca7..76336ef384 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 792f2a863d..7e86aacdb1 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 acfe040a26..f3a4b979ac 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 8d471c3e1c..8f2eecc22a 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 bdb798e89a..6845a24838 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());
}