src/corelib/io/qdir.cpp
changeset 30 5dc02b23752f
parent 19 fcece45ef507
--- a/src/corelib/io/qdir.cpp	Wed Jun 23 19:07:03 2010 +0300
+++ b/src/corelib/io/qdir.cpp	Tue Jul 06 15:10:48 2010 +0300
@@ -52,13 +52,13 @@
 #include "qregexp.h"
 #include "qvector.h"
 #include "qalgorithms.h"
+#include "qvarlengtharray.h"
+
 #ifdef QT_BUILD_CORE_LIB
-# include "qresource.h"
+#  include "qresource.h"
+#  include "private/qcoreglobaldata_p.h"
 #endif
 
-#include "qvarlengtharray.h"
-
-#include "private/qcoreglobaldata_p.h"
 #include <stdlib.h>
 
 QT_BEGIN_NAMESPACE
@@ -83,20 +83,15 @@
 //************* QDirPrivate
 class QDirPrivate
 {
-    QDir *q_ptr;
-    Q_DECLARE_PUBLIC(QDir)
-
     friend struct QScopedPointerDeleter<QDirPrivate>;
-protected:
-    QDirPrivate(QDir*, const QDir *copy=0);
+
+public:
+    QDirPrivate(const QDir *copy = 0);
     ~QDirPrivate();
 
-    QString initFileEngine(const QString &file);
-
     void updateFileLists() const;
     void sortFileList(QDir::SortFlags, QFileInfoList &, QStringList *, QFileInfoList *) const;
 
-private:
 #ifdef QT3_SUPPORT
     QChar filterSepChar;
     bool matchAllDirs;
@@ -109,28 +104,30 @@
             sep = QChar(QLatin1Char(' '));
         return sep;
     }
-    static inline QStringList splitFilters(const QString &nameFilter, QChar sep=0) {
-        if(sep == 0)
+    static inline QStringList splitFilters(const QString &nameFilter, QChar sep = 0) {
+        if (sep == 0)
             sep = getFilterSepChar(nameFilter);
         QStringList ret = nameFilter.split(sep);
-        for(int i = 0; i < ret.count(); i++)
+        for (int i = 0; i < ret.count(); ++i)
             ret[i] = ret[i].trimmed();
         return ret;
     }
 
     struct Data {
         inline Data()
-            : ref(1), fileEngine(0)
-        { clear(); }
+            : ref(1), fileEngine(0), listsDirty(1)
+        {}
         inline Data(const Data &copy)
             : ref(1), path(copy.path), nameFilters(copy.nameFilters), sort(copy.sort),
-              filters(copy.filters), fileEngine(0)
-        { clear(); }
+              filters(copy.filters), fileEngine(0), listsDirty(1)
+        {}
         inline ~Data()
         { delete fileEngine; }
 
         inline void clear() {
             listsDirty = 1;
+            files.clear();
+            fileInfos.clear();
         }
         mutable QAtomicInt ref;
 
@@ -156,9 +153,10 @@
 #endif
                 path.truncate(path.length() - 1);
         }
-        if(!data->fileEngine || !QDir::isRelativePath(path))
-            path = initFileEngine(path);
-        data->fileEngine->setFileName(path);
+
+        delete data->fileEngine;
+        data->fileEngine = QAbstractFileEngine::create(path);
+
         // set the path to be the qt friendly version so then we can operate on it using just /
         data->path = data->fileEngine->fileName(QAbstractFileEngine::DefaultName);
         data->clear();
@@ -170,18 +168,16 @@
     void detach(bool createFileEngine = true);
 };
 
-QDirPrivate::QDirPrivate(QDir *qq, const QDir *copy) : q_ptr(qq)
+QDirPrivate::QDirPrivate(const QDir *copy)
 #ifdef QT3_SUPPORT
-                                                     , filterSepChar(0)
-                                                     , matchAllDirs(false)
+    : filterSepChar(0), matchAllDirs(false)
 #endif
 {
-    if(copy) {
+    if (copy) {
         copy->d_func()->data->ref.ref();
         data = copy->d_func()->data;
     } else {
         data = new QDirPrivate::Data;
-        data->clear();
     }
 }
 
@@ -190,18 +186,19 @@
     if (!data->ref.deref())
         delete data;
     data = 0;
-    q_ptr = 0;
 }
 
 /* For sorting */
-struct QDirSortItem {
+struct QDirSortItem
+{
     mutable QString filename_cache;
     mutable QString suffix_cache;
     QFileInfo item;
 };
 
 
-class QDirSortItemComparator {
+class QDirSortItemComparator
+{
     int qt_cmp_si_sort_flags;
 public:
     QDirSortItemComparator(int flags) : qt_cmp_si_sort_flags(flags) {}
@@ -240,7 +237,7 @@
             f2->suffix_cache = ic ? f2->item.suffix().toLower()
                                : f2->item.suffix();
 
-	r = qt_cmp_si_sort_flags & QDir::LocaleAware
+        r = qt_cmp_si_sort_flags & QDir::LocaleAware
             ? f1->suffix_cache.localeAwareCompare(f2->suffix_cache)
             : f1->suffix_cache.compare(f2->suffix_cache);
       }
@@ -260,7 +257,7 @@
             f2->filename_cache = ic ? f2->item.fileName().toLower()
                                     : f2->item.fileName();
 
-	r = qt_cmp_si_sort_flags & QDir::LocaleAware
+        r = qt_cmp_si_sort_flags & QDir::LocaleAware
             ? f1->filename_cache.localeAwareCompare(f2->filename_cache)
             : f1->filename_cache.compare(f2->filename_cache);
     }
@@ -274,16 +271,13 @@
 inline void QDirPrivate::sortFileList(QDir::SortFlags sort, QFileInfoList &l,
                                       QStringList *names, QFileInfoList *infos) const
 {
-    if(names)
-        names->clear();
-    if(infos)
-        infos->clear();
+    // names and infos are always empty lists or 0 here
     int n = l.size();
-    if(n > 0) {
+    if (n > 0) {
         if (n == 1 || (sort & QDir::SortByMask) == QDir::Unsorted) {
-            if(infos)
+            if (infos)
                 *infos = l;
-            if(names) {
+            if (names) {
                 for (int i = 0; i < n; ++i)
                     names->append(l.at(i).fileName());
             }
@@ -291,13 +285,13 @@
             QScopedArrayPointer<QDirSortItem> si(new QDirSortItem[n]);
             for (int i = 0; i < n; ++i)
                 si[i].item = l.at(i);
-            qSort(si.data(), si.data()+n, QDirSortItemComparator(sort));
+            qSort(si.data(), si.data() + n, QDirSortItemComparator(sort));
             // put them back in the list(s)
-            if(infos) {
+            if (infos) {
                 for (int i = 0; i < n; ++i)
                     infos->append(si[i].item);
             }
-            if(names) {
+            if (names) {
                 for (int i = 0; i < n; ++i)
                     names->append(si[i].item.fileName());
             }
@@ -307,7 +301,7 @@
 
 inline void QDirPrivate::updateFileLists() const
 {
-    if(data->listsDirty) {
+    if (data->listsDirty) {
         QFileInfoList l;
         QDirIterator it(data->path, data->nameFilters, data->filters);
         while (it.hasNext()) {
@@ -319,16 +313,6 @@
     }
 }
 
-QString QDirPrivate::initFileEngine(const QString &path)
-{
-    detach(false);
-    delete data->fileEngine;
-    data->fileEngine = 0;
-    data->clear();
-    data->fileEngine = QAbstractFileEngine::create(path);
-    return data->fileEngine->fileName(QAbstractFileEngine::DefaultName);
-}
-
 void QDirPrivate::detach(bool createFileEngine)
 {
     qAtomicDetach(data);
@@ -520,8 +504,7 @@
 
     \sa currentPath()
 */
-
-QDir::QDir(const QString &path) : d_ptr(new QDirPrivate(this))
+QDir::QDir(const QString &path) : d_ptr(new QDirPrivate)
 {
     Q_D(QDir);
     d->setPath(path.isEmpty() ? QString::fromLatin1(".") : path);
@@ -548,18 +531,17 @@
 
     \sa exists(), setPath(), setNameFilter(), setFilter(), setSorting()
 */
-
 QDir::QDir(const QString &path, const QString &nameFilter,
-           SortFlags sort, Filters filters)  : d_ptr(new QDirPrivate(this))
+           SortFlags sort, Filters filters) : d_ptr(new QDirPrivate)
 {
     Q_D(QDir);
     d->setPath(path.isEmpty() ? QString::fromLatin1(".") : path);
     d->data->nameFilters = QDir::nameFiltersFromString(nameFilter);
     bool empty = d->data->nameFilters.isEmpty();
-    if(!empty) {
+    if (!empty) {
         empty = true;
-        for(int i = 0; i < d->data->nameFilters.size(); ++i) {
-            if(!d->data->nameFilters.at(i).isEmpty()) {
+        for (int i = 0; i < d->data->nameFilters.size(); ++i) {
+            if (!d->data->nameFilters.at(i).isEmpty()) {
                 empty = false;
                 break;
             }
@@ -577,8 +559,7 @@
 
     \sa operator=()
 */
-
-QDir::QDir(const QDir &dir)  : d_ptr(new QDirPrivate(this, &dir))
+QDir::QDir(const QDir &dir) : d_ptr(new QDirPrivate(&dir))
 {
 }
 
@@ -586,7 +567,6 @@
     Destroys the QDir object frees up its resources. This has no
     effect on the underlying directory in the file system.
 */
-
 QDir::~QDir()
 {
 }
@@ -607,7 +587,6 @@
     \sa path(), absolutePath(), exists(), cleanPath(), dirName(),
       absoluteFilePath(), isRelative(), makeAbsolute()
 */
-
 void QDir::setPath(const QString &path)
 {
     Q_D(QDir);
@@ -624,7 +603,6 @@
     \sa setPath(), absolutePath(), exists(), cleanPath(), dirName(),
     absoluteFilePath(), toNativeSeparators(), makeAbsolute()
 */
-
 QString QDir::path() const
 {
     Q_D(const QDir);
@@ -639,7 +617,6 @@
     \sa setPath(), canonicalPath(), exists(), cleanPath(),
     dirName(), absoluteFilePath()
 */
-
 QString QDir::absolutePath() const
 {
     Q_D(const QDir);
@@ -649,7 +626,6 @@
     return cleanPath(ret);
 }
 
-
 /*!
     Returns the canonical path, i.e. a path without symbolic links or
     redundant "." or ".." elements.
@@ -666,12 +642,11 @@
     \sa path(), absolutePath(), exists(), cleanPath(), dirName(),
         absoluteFilePath()
 */
-
 QString QDir::canonicalPath() const
 {
     Q_D(const QDir);
 
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return QLatin1String("");
     return cleanPath(d->data->fileEngine->fileName(QAbstractFileEngine::CanonicalName));
 }
@@ -687,7 +662,6 @@
 
     \sa path(), filePath(), absolutePath(), absoluteFilePath()
 */
-
 QString QDir::dirName() const
 {
     Q_D(const QDir);
@@ -706,7 +680,6 @@
 
     \sa dirName() absoluteFilePath(), isRelative(), canonicalPath()
 */
-
 QString QDir::filePath(const QString &fileName) const
 {
     Q_D(const QDir);
@@ -714,7 +687,7 @@
         return QString(fileName);
 
     QString ret = d->data->path;
-    if(!fileName.isEmpty()) {
+    if (!fileName.isEmpty()) {
         if (!ret.isEmpty() && ret[(int)ret.length()-1] != QLatin1Char('/') && fileName[0] != QLatin1Char('/'))
             ret += QLatin1Char('/');
         ret += fileName;
@@ -730,13 +703,12 @@
 
     \sa relativeFilePath() filePath() canonicalPath()
 */
-
 QString QDir::absoluteFilePath(const QString &fileName) const
 {
     Q_D(const QDir);
     if (isAbsolutePath(fileName))
         return fileName;
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return fileName;
 
     QString ret;
@@ -744,7 +716,7 @@
     if (isRelativePath(d->data->path)) //get pwd
         ret = QFSFileEngine::currentPath(fileName);
 #endif
-    if(!d->data->path.isEmpty() && d->data->path != QLatin1String(".")) {
+    if (!d->data->path.isEmpty() && d->data->path != QLatin1String(".")) {
         if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/')))
             ret += QLatin1Char('/');
         ret += d->data->path;
@@ -764,7 +736,6 @@
 
     \sa absoluteFilePath() filePath() canonicalPath()
 */
-
 QString QDir::relativeFilePath(const QString &fileName) const
 {
     QString dir = absolutePath();
@@ -851,7 +822,7 @@
 {
     QString n(pathName);
 #if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN)
-    for (int i=0; i<(int)n.length(); i++) {
+    for (int i = 0; i < (int)n.length(); ++i) {
         if (n[i] == QLatin1Char('/'))
             n[i] = QLatin1Char('\\');
     }
@@ -875,7 +846,7 @@
 {
     QString n(pathName);
 #if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN)
-    for (int i=0; i<(int)n.length(); i++) {
+    for (int i = 0; i < (int)n.length(); ++i) {
         if (n[i] == QLatin1Char('\\'))
             n[i] = QLatin1Char('/');
     }
@@ -894,7 +865,6 @@
 
     \sa cdUp(), isReadable(), exists(), path()
 */
-
 bool QDir::cd(const QString &dirName)
 {
     Q_D(QDir);
@@ -930,14 +900,13 @@
             }
         }
     }
-    {
-        QFileInfo fi(newPath);
-        if (!(fi.exists() && fi.isDir()))
-            return false;
-    }
-
-    d->setPath(newPath);
-    refresh();
+
+    QDir dir(*this);
+    dir.setPath(newPath);
+    if (!dir.exists())
+        return false;
+
+    *this = dir;
     return true;
 }
 
@@ -951,7 +920,6 @@
 
     \sa cd(), isReadable(), exists(), path()
 */
-
 bool QDir::cdUp()
 {
     return cd(QString::fromLatin1(".."));
@@ -960,7 +928,6 @@
 /*!
     Returns the string list set by setNameFilters()
 */
-
 QStringList QDir::nameFilters() const
 {
     Q_D(const QDir);
@@ -983,11 +950,11 @@
 
     \sa nameFilters(), setFilter()
 */
-
 void QDir::setNameFilters(const QStringList &nameFilters)
 {
     Q_D(QDir);
-    d->detach();
+
+    d->reset();
     d->data->nameFilters = nameFilters;
 }
 
@@ -1039,7 +1006,7 @@
         return;
     }
 
-    for (int i = 0; i < prefix.count(); i++) {
+    for (int i = 0; i < prefix.count(); ++i) {
         if (!prefix.at(i).isLetterOrNumber()) {
             qWarning("QDir::setSearchPaths: Prefix can only contain letters or numbers");
             return;
@@ -1089,7 +1056,6 @@
 /*!
     Returns the value set by setFilter()
 */
-
 QDir::Filters QDir::filter() const
 {
     Q_D(const QDir);
@@ -1113,6 +1079,8 @@
     \value NoSymLinks  Do not list symbolic links (ignored by operating
                        systems that don't support symbolic links).
     \value NoDotAndDotDot Do not list the special entries "." and "..".
+    \value NoDot       Do not list the special entry ".".
+    \value NoDotDot    Do not list the special entry "..".
     \value AllEntries  List directories, files, drives and symlinks (this does not list
                 broken symlinks unless you specify System).
     \value Readable    List files for which the application has read
@@ -1171,12 +1139,11 @@
 
     \sa filter(), setNameFilters()
 */
-
 void QDir::setFilter(Filters filters)
 {
     Q_D(QDir);
 
-    d->detach();
+    d->reset();
     d->data->filters = filters;
 }
 
@@ -1185,7 +1152,6 @@
 
     \sa setSorting() SortFlag
 */
-
 QDir::SortFlags QDir::sorting() const
 {
     Q_D(const QDir);
@@ -1231,16 +1197,14 @@
 
     \sa sorting() SortFlag
 */
-
 void QDir::setSorting(SortFlags sort)
 {
     Q_D(QDir);
 
-    d->detach();
+    d->reset();
     d->data->sort = sort;
 }
 
-
 /*!
     Returns the total number of directories and files in the directory.
 
@@ -1248,7 +1212,6 @@
 
     \sa operator[](), entryList()
 */
-
 uint QDir::count() const
 {
     Q_D(const QDir);
@@ -1260,13 +1223,10 @@
 /*!
     Returns the file name at position \a pos in the list of file
     names. Equivalent to entryList().at(index).
-
-    Returns an empty string if \a pos is out of range or if the
-    entryList() function failed.
+    \a pos must be a valid index position in the list (i.e., 0 <= pos < count()).
 
     \sa count(), entryList()
 */
-
 QString QDir::operator[](int pos) const
 {
     Q_D(const QDir);
@@ -1294,7 +1254,6 @@
 
     \sa entryInfoList(), setNameFilters(), setSorting(), setFilter()
 */
-
 QStringList QDir::entryList(Filters filters, SortFlags sort) const
 {
     Q_D(const QDir);
@@ -1342,7 +1301,6 @@
 
     \sa entryInfoList(), setNameFilters(), setSorting(), setFilter()
 */
-
 QStringList QDir::entryList(const QStringList &nameFilters, Filters filters,
                             SortFlags sort) const
 {
@@ -1356,10 +1314,12 @@
 #endif
     if (sort == NoSort)
         sort = d->data->sort;
-    if (filters == NoFilter && sort == NoSort && nameFilters == d->data->nameFilters) {
+
+    if (filters == d->data->filters && sort == d->data->sort && nameFilters == d->data->nameFilters) {
         d->updateFileLists();
         return d->data->files;
     }
+
     QFileInfoList l;
     QDirIterator it(d->data->path, nameFilters, filters);
     while (it.hasNext()) {
@@ -1387,7 +1347,6 @@
 
     \sa entryList(), setNameFilters(), setSorting(), setFilter(), isReadable(), exists()
 */
-
 QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, Filters filters,
                                   SortFlags sort) const
 {
@@ -1401,10 +1360,12 @@
 #endif
     if (sort == NoSort)
         sort = d->data->sort;
-    if (filters == NoFilter && sort == NoSort && nameFilters == d->data->nameFilters) {
+
+    if (filters == d->data->filters && sort == d->data->sort && nameFilters == d->data->nameFilters) {
         d->updateFileLists();
         return d->data->fileInfos;
     }
+
     QFileInfoList l;
     QDirIterator it(d->data->path, nameFilters, filters);
     while (it.hasNext()) {
@@ -1423,7 +1384,6 @@
 
     \sa rmdir()
 */
-
 bool QDir::mkdir(const QString &dirName) const
 {
     Q_D(const QDir);
@@ -1432,7 +1392,7 @@
         qWarning("QDir::mkdir: Empty or null file name(s)");
         return false;
     }
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
 
     QString fn = filePath(dirName);
@@ -1448,7 +1408,6 @@
 
     \sa mkdir()
 */
-
 bool QDir::rmdir(const QString &dirName) const
 {
     Q_D(const QDir);
@@ -1457,7 +1416,7 @@
         qWarning("QDir::rmdir: Empty or null file name(s)");
         return false;
     }
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
 
     QString fn = filePath(dirName);
@@ -1474,7 +1433,6 @@
 
     \sa rmpath()
 */
-
 bool QDir::mkpath(const QString &dirPath) const
 {
     Q_D(const QDir);
@@ -1483,7 +1441,7 @@
         qWarning("QDir::mkpath: Empty or null file name(s)");
         return false;
     }
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
 
     QString fn = filePath(dirPath);
@@ -1509,7 +1467,7 @@
         qWarning("QDir::rmpath: Empty or null file name(s)");
         return false;
     }
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
 
     QString fn = filePath(dirPath);
@@ -1525,17 +1483,16 @@
 
     \sa QFileInfo::isReadable()
 */
-
-
 bool QDir::isReadable() const
 {
     Q_D(const QDir);
 
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
-    const QAbstractFileEngine::FileFlags info = d->data->fileEngine->fileFlags(QAbstractFileEngine::DirectoryType
-                                                                       |QAbstractFileEngine::PermsMask);
-    if(!(info & QAbstractFileEngine::DirectoryType))
+    const QAbstractFileEngine::FileFlags info =
+        d->data->fileEngine->fileFlags(QAbstractFileEngine::DirectoryType
+                                       | QAbstractFileEngine::PermsMask);
+    if (!(info & QAbstractFileEngine::DirectoryType))
         return false;
     return info & QAbstractFileEngine::ReadUserPerm;
 }
@@ -1551,19 +1508,17 @@
 
     \sa QFileInfo::exists(), QFile::exists()
 */
-
 bool QDir::exists() const
 {
     Q_D(const QDir);
 
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
     const QAbstractFileEngine::FileFlags info =
-        d->data->fileEngine->fileFlags(
-            QAbstractFileEngine::DirectoryType
-            | QAbstractFileEngine::ExistsFlag
-            | QAbstractFileEngine::Refresh);
-    if(!(info & QAbstractFileEngine::DirectoryType))
+        d->data->fileEngine->fileFlags(QAbstractFileEngine::DirectoryType
+                                       | QAbstractFileEngine::ExistsFlag
+                                       | QAbstractFileEngine::Refresh);
+    if (!(info & QAbstractFileEngine::DirectoryType))
         return false;
     return info & QAbstractFileEngine::ExistsFlag;
 }
@@ -1580,12 +1535,11 @@
 
     \sa root(), rootPath()
 */
-
 bool QDir::isRoot() const
 {
     Q_D(const QDir);
 
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return true;
     return d->data->fileEngine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::RootFlag;
 }
@@ -1615,12 +1569,11 @@
 
     \sa makeAbsolute() isAbsolute() isAbsolutePath() cleanPath()
 */
-
 bool QDir::isRelative() const
 {
     Q_D(const QDir);
 
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
     return d->data->fileEngine->isRelativePath();
 }
@@ -1633,20 +1586,19 @@
 
     \sa isAbsolute() isAbsolutePath() isRelative() cleanPath()
 */
-
 bool QDir::makeAbsolute() // ### What do the return values signify?
 {
     Q_D(QDir);
 
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
     QString absolutePath = d->data->fileEngine->fileName(QAbstractFileEngine::AbsoluteName);
-    if(QDir::isRelativePath(absolutePath))
+    if (QDir::isRelativePath(absolutePath))
         return false;
     d->detach();
     d->data->path = absolutePath;
     d->data->fileEngine->setFileName(absolutePath);
-    if(!(d->data->fileEngine->fileFlags(QAbstractFileEngine::TypesMask) & QAbstractFileEngine::DirectoryType))
+    if (!(d->data->fileEngine->fileFlags(QAbstractFileEngine::TypesMask) & QAbstractFileEngine::DirectoryType))
         return false;
     return true;
 }
@@ -1660,22 +1612,21 @@
 
     \snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 10
 */
-
 bool QDir::operator==(const QDir &dir) const
 {
     const QDirPrivate *d = d_func();
     const QDirPrivate *other = dir.d_func();
 
-    if(d->data == other->data)
+    if (d->data == other->data)
         return true;
     Q_ASSERT(d->data->fileEngine && other->data->fileEngine);
-    if(d->data->fileEngine->caseSensitive() != other->data->fileEngine->caseSensitive())
+    if (d->data->fileEngine->caseSensitive() != other->data->fileEngine->caseSensitive())
         return false;
-    if(d->data->filters == other->data->filters
+    if (d->data->filters == other->data->filters
        && d->data->sort == other->data->sort
        && d->data->nameFilters == other->data->nameFilters) {
         QString dir1 = absolutePath(), dir2 = dir.absolutePath();
-        if(!other->data->fileEngine->caseSensitive())
+        if (!other->data->fileEngine->caseSensitive())
             return (dir1.toLower() == dir2.toLower());
 
         return (dir1 == dir2);
@@ -1688,7 +1639,6 @@
     Makes a copy of the \a dir object and assigns it to this QDir
     object.
 */
-
 QDir &QDir::operator=(const QDir &dir)
 {
     if (this == &dir)
@@ -1707,7 +1657,6 @@
 
     Use setPath() instead.
 */
-
 QDir &QDir::operator=(const QString &path)
 {
     Q_D(QDir);
@@ -1728,22 +1677,19 @@
     \snippet doc/src/snippets/code/src_corelib_io_qdir.cpp 11
 */
 
-
 /*!
     Removes the file, \a fileName.
 
     Returns true if the file is removed successfully; otherwise
     returns false.
 */
-
 bool QDir::remove(const QString &fileName)
 {
     if (fileName.isEmpty()) {
         qWarning("QDir::remove: Empty or null file name");
         return false;
     }
-    QString p = filePath(fileName);
-    return QFile::remove(p);
+    return QFile::remove(filePath(fileName));
 }
 
 /*!
@@ -1757,7 +1703,6 @@
     fail. For example, on at least one file system rename() fails if
     \a newName points to an open file.
 */
-
 bool QDir::rename(const QString &oldName, const QString &newName)
 {
     Q_D(QDir);
@@ -1766,11 +1711,11 @@
         qWarning("QDir::rename: Empty or null file name(s)");
         return false;
     }
-    if(!d->data->fileEngine)
+    if (!d->data->fileEngine)
         return false;
 
     QFile file(filePath(oldName));
-    if(!file.exists())
+    if (!file.exists())
         return false;
     return file.rename(filePath(newName));
 }
@@ -1785,15 +1730,13 @@
 
     \sa QFileInfo::exists(), QFile::exists()
 */
-
 bool QDir::exists(const QString &name) const
 {
     if (name.isEmpty()) {
         qWarning("QDir::exists: Empty or null file name");
         return false;
     }
-    QString tmp = filePath(name);
-    return QFile::exists(tmp);
+    return QFile::exists(filePath(name));
 }
 
 /*!
@@ -1805,7 +1748,6 @@
 
     \sa root(), rootPath()
 */
-
 QFileInfoList QDir::drives()
 {
 #ifdef QT_NO_FSFILEENGINE
@@ -1825,7 +1767,6 @@
     user using their operating system's separator use
     toNativeSeparators().
 */
-
 QChar QDir::separator()
 {
 #if defined (Q_FS_FAT) || defined(Q_WS_WIN) || defined(Q_OS_SYMBIAN)
@@ -1844,9 +1785,8 @@
     Returns true if the directory was successfully changed; otherwise
     returns false.
 
-    \sa current() currentPath() home() root() temp()
+    \sa current(), currentPath(), home(), root(), temp()
 */
-
 bool QDir::setCurrent(const QString &path)
 {
 #ifdef QT_NO_FSFILEENGINE
@@ -1865,13 +1805,13 @@
     The directory is constructed using the absolute path of the current directory,
     ensuring that its path() will be the same as its absolutePath().
 
-    \sa currentPath(), home(), root(), temp()
+    \sa currentPath(), setCurrent(), home(), root(), temp()
 */
 
 /*!
     Returns the absolute path of the application's current directory.
 
-    \sa current(), homePath(), rootPath(), tempPath()
+    \sa current(), setCurrent(), homePath(), rootPath(), tempPath()
 */
 QString QDir::currentPath()
 {
@@ -1888,7 +1828,7 @@
 
     Use currentPath() instead.
 
-    \sa currentPath()
+    \sa currentPath(), setCurrent()
 */
 
 /*!
@@ -1945,14 +1885,14 @@
 }
 
 /*!
-  \fn QString QDir::homeDirPath()
-
-  Returns the absolute path of the user's home directory.
-
-  Use homePath() instead.
-
-  \sa homePath()
- */
+    \fn QString QDir::homeDirPath()
+
+    Returns the absolute path of the user's home directory.
+
+    Use homePath() instead.
+
+    \sa homePath()
+*/
 
 /*!
     \fn QDir QDir::temp()
@@ -2018,13 +1958,13 @@
 }
 
 /*!
-  \fn QString QDir::rootDirPath()
-
-  Returns the absolute path of the root directory.
-
-  Use rootPath() instead.
-
-  \sa rootPath()
+    \fn QString QDir::rootDirPath()
+
+    Returns the absolute path of the root directory.
+
+    Use rootPath() instead.
+
+    \sa rootPath()
 */
 
 #ifndef QT_NO_REGEXP
@@ -2037,11 +1977,9 @@
 
     \sa {QRegExp wildcard matching}, QRegExp::exactMatch() entryList() entryInfoList()
 */
-
-
 bool QDir::match(const QStringList &filters, const QString &fileName)
 {
-    for(QStringList::ConstIterator sit = filters.begin(); sit != filters.end(); ++sit) {
+    for (QStringList::ConstIterator sit = filters.constBegin(); sit != filters.constEnd(); ++sit) {
         QRegExp rx(*sit, Qt::CaseInsensitive, QRegExp::Wildcard);
         if (rx.exactMatch(fileName))
             return true;
@@ -2057,12 +1995,11 @@
 
     \sa {QRegExp wildcard matching}, QRegExp::exactMatch() entryList() entryInfoList()
 */
-
 bool QDir::match(const QString &filter, const QString &fileName)
 {
     return match(nameFiltersFromString(filter), fileName);
 }
-#endif
+#endif // QT_NO_REGEXP
 
 /*!
     Removes all multiple directory separators "/" and resolves any
@@ -2075,15 +2012,14 @@
 
     \sa absolutePath() canonicalPath()
 */
-
 QString QDir::cleanPath(const QString &path)
 {
     if (path.isEmpty())
         return path;
     QString name = path;
     QChar dir_separator = separator();
-    if(dir_separator != QLatin1Char('/'))
- 	name.replace(dir_separator, QLatin1Char('/'));
+    if (dir_separator != QLatin1Char('/'))
+       name.replace(dir_separator, QLatin1Char('/'));
 
     int used = 0, levels = 0;
     const int len = name.length();
@@ -2091,27 +2027,27 @@
     QChar *out = outVector.data();
 
     const QChar *p = name.unicode();
-    for(int i = 0, last = -1, iwrite = 0; i < len; i++) {
-        if(p[i] == QLatin1Char('/')) {
-            while(i < len-1 && p[i+1] == QLatin1Char('/')) {
+    for (int i = 0, last = -1, iwrite = 0; i < len; ++i) {
+        if (p[i] == QLatin1Char('/')) {
+            while (i < len-1 && p[i+1] == QLatin1Char('/')) {
 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) //allow unc paths
-                if(!i)
+                if (!i)
                     break;
 #endif
                 i++;
             }
             bool eaten = false;
-            if(i < len - 1 && p[i+1] == QLatin1Char('.')) {
+            if (i < len - 1 && p[i+1] == QLatin1Char('.')) {
                 int dotcount = 1;
-                if(i < len - 2 && p[i+2] == QLatin1Char('.'))
+                if (i < len - 2 && p[i+2] == QLatin1Char('.'))
                     dotcount++;
-                if(i == len - dotcount - 1) {
-                    if(dotcount == 1) {
+                if (i == len - dotcount - 1) {
+                    if (dotcount == 1) {
                         break;
-                    } else if(levels) {
-                        if(last == -1) {
-                            for(int i2 = iwrite-1; i2 >= 0; i2--) {
-                                if(out[i2] == QLatin1Char('/')) {
+                    } else if (levels) {
+                        if (last == -1) {
+                            for (int i2 = iwrite-1; i2 >= 0; i2--) {
+                                if (out[i2] == QLatin1Char('/')) {
                                     last = i2;
                                     break;
                                 }
@@ -2120,11 +2056,11 @@
                         used -= iwrite - last - 1;
                         break;
                     }
-                } else if(p[i+dotcount+1] == QLatin1Char('/')) {
-                    if(dotcount == 2 && levels) {
-                        if(last == -1 || iwrite - last == 1) {
-                            for(int i2 = (last == -1) ? (iwrite-1) : (last-1); i2 >= 0; i2--) {
-                                if(out[i2] == QLatin1Char('/')) {
+                } else if (p[i+dotcount+1] == QLatin1Char('/')) {
+                    if (dotcount == 2 && levels) {
+                        if (last == -1 || iwrite - last == 1) {
+                            for (int i2 = (last == -1) ? (iwrite-1) : (last-1); i2 >= 0; i2--) {
+                                if (out[i2] == QLatin1Char('/')) {
                                     eaten = true;
                                     last = i2;
                                     break;
@@ -2133,7 +2069,7 @@
                         } else {
                             eaten = true;
                         }
-                        if(eaten) {
+                        if (eaten) {
                             levels--;
                             used -= iwrite - last;
                             iwrite = last;
@@ -2145,38 +2081,38 @@
                         iwrite = qMax(0, last);
                         last = -1;
                         ++i;
-                    } else if(dotcount == 1) {
+                    } else if (dotcount == 1) {
                         eaten = true;
                     }
-                    if(eaten)
+                    if (eaten)
                         i += dotcount;
                 } else {
                     levels++;
                 }
-            } else if(last != -1 && iwrite - last == 1) {
+            } else if (last != -1 && iwrite - last == 1) {
 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
                 eaten = (iwrite > 2);
 #else
                 eaten = true;
 #endif
                 last = -1;
-            } else if(last != -1 && i == len-1) {
+            } else if (last != -1 && i == len-1) {
                 eaten = true;
             } else {
                 levels++;
             }
-            if(!eaten)
+            if (!eaten)
                 last = i - (i - iwrite);
             else
                 continue;
-        } else if(!i && p[i] == QLatin1Char('.')) {
+        } else if (!i && p[i] == QLatin1Char('.')) {
             int dotcount = 1;
-            if(len >= 1 && p[1] == QLatin1Char('.'))
+            if (len >= 1 && p[1] == QLatin1Char('.'))
                 dotcount++;
-            if(len >= dotcount && p[dotcount] == QLatin1Char('/')) {
-                if(dotcount == 1) {
+            if (len >= dotcount && p[dotcount] == QLatin1Char('/')) {
+                if (dotcount == 1) {
                     i++;
-                    while(i+1 < len-1 && p[i+1] == QLatin1Char('/'))
+                    while (i+1 < len-1 && p[i+1] == QLatin1Char('/'))
                         i++;
                     continue;
                 }
@@ -2185,16 +2121,15 @@
         out[iwrite++] = p[i];
         used++;
     }
-    QString ret;
-    if(used == len)
-        ret = name;
-    else
-	ret = QString(out, used);
-
+
+    QString ret = (used == len ? name : QString(out, used));
     // Strip away last slash except for root directories
-    if (ret.endsWith(QLatin1Char('/'))
-        && !(ret.size() == 1 || (ret.size() == 3 && ret.at(1) == QLatin1Char(':'))))
-        ret = ret.left(ret.length() - 1);
+    if (ret.length() > 1 && ret.endsWith(QLatin1Char('/'))) {
+#if defined (Q_OS_WIN) || defined (Q_OS_SYMBIAN)
+        if (!(ret.length() == 3 && ret.at(1) == QLatin1Char(':')))
+#endif
+            ret.chop(1);
+    }
 
     return ret;
 }
@@ -2205,7 +2140,6 @@
 
     \sa isRelative() isAbsolutePath() makeAbsolute()
 */
-
 bool QDir::isRelativePath(const QString &path)
 {
     return QFileInfo(path).isRelative();
@@ -2214,12 +2148,11 @@
 /*!
     Refreshes the directory information.
 */
-
 void QDir::refresh() const
 {
     Q_D(const QDir);
 
-    d->data->clear();
+    const_cast<QDirPrivate *>(d)->reset();
 }
 
 /*!
@@ -2229,7 +2162,6 @@
     there is more than one filter, each pair of filters is separated
     by a space or by a semicolon.)
 */
-
 QStringList QDir::nameFiltersFromString(const QString &nameFilter)
 {
     return QDirPrivate::splitFilters(nameFilter);
@@ -2309,6 +2241,8 @@
 void QDir::setMatchAllDirs(bool on)
 {
     Q_D(QDir);
+
+    d->reset();
     d->matchAllDirs = on;
 }
 
@@ -2421,7 +2355,8 @@
 
     Use QDir::SortFlags instead.
 */
-#endif
+#endif // QT3_SUPPORT
+
 #ifndef QT_NO_DEBUG_STREAM
 QDebug operator<<(QDebug debug, QDir::Filters filters)
 {
@@ -2434,7 +2369,9 @@
         if (filters & QDir::Files) flags << QLatin1String("Files");
         if (filters & QDir::Drives) flags << QLatin1String("Drives");
         if (filters & QDir::NoSymLinks) flags << QLatin1String("NoSymLinks");
-        if (filters & QDir::NoDotAndDotDot) flags << QLatin1String("NoDotAndDotDot");
+        if (filters & QDir::NoDotAndDotDot) flags << QLatin1String("NoDotAndDotDot"); // ### Qt5: remove (because NoDotAndDotDot=NoDot|NoDotDot)
+        if (filters & QDir::NoDot) flags << QLatin1String("NoDot");
+        if (filters & QDir::NoDotDot) flags << QLatin1String("NoDotDot");
         if ((filters & QDir::AllEntries) == QDir::AllEntries) flags << QLatin1String("AllEntries");
         if (filters & QDir::Readable) flags << QLatin1String("Readable");
         if (filters & QDir::Writable) flags << QLatin1String("Writable");
@@ -2484,9 +2421,6 @@
                        << ')';
     return debug.space();
 }
-
-
-
-#endif
+#endif // QT_NO_DEBUG_STREAM
 
 QT_END_NAMESPACE