⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qdiriterator.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************//*!    \since 4.3    \class QDirIterator    \brief The QDirIterator class provides an iterator for directory entrylists.    You can use QDirIterator to navigate entries of a directory one at a time.    It is similar to QDir::entryList() and QDir::entryInfoList(), but because    it lists entries one at a time instead of all at once, it scales better    and is more suitable for large directories. It also supports listing    directory contents recursively, and following symbolic links. Unlike    QDir::entryList(), QDirIterator does not support sorting.    The QDirIterator constructor takes a QDir or a directory as    argument. After construction, the iterator is located before the first    directory entry. Here's how to iterate over all the entries sequentially:    \code        QDirIterator it("/etc", QDirIterator::Subdirectories);        while (it.hasNext()) {            qDebug() << it.next();            // /etc/.            // /etc/..            // /etc/X11            // /etc/X11/fs            // ...        }    \endcode    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.    Unlike Qt's container iterators, QDirIterator is uni-directional (i.e.,    you cannot iterate directories in reverse order) and does not allow random    access.    QDirIterator works with all supported file engines, and is implemented    using QAbstractFileEngineIterator.    \sa QDir, QDir::entryList(), QAbstractFileEngineIterator*//*! \enum QDirIterator::IteratorFlag    This enum describes flags that you can combine to configure the behavior    of QDirIterator.    \value NoIteratorFlags The default value, representing no flags. The    iterator will return entries for the assigned path.    \value Subdirectories List entries inside all subdirectories as well.    \value FollowSymlinks When combined with Subdirectories, this flag    enables iterating through all subdirectories of the assigned path,    following all symbolic links. Symbolic link loops (e.g., "link" => "." or    "link" => "..") are automatically detected and ignored.*/#include "qdiriterator.h"#include "qabstractfileengine.h"#include <QtCore/qset.h>#include <QtCore/qstack.h>#include <QtCore/qvariant.h>class QDirIteratorPrivate{public:    QDirIteratorPrivate(const QString &path, const QStringList &nameFilters,                        QDir::Filters filters, QDirIterator::IteratorFlags flags);    ~QDirIteratorPrivate();    void pushSubDirectory(const QString &path, const QStringList &nameFilters,                          QDir::Filters filters);    void advance();    bool matchesFilters(const QAbstractFileEngineIterator *it) const;    QSet<QString> visitedLinks;    QAbstractFileEngine *engine;    QStack<QAbstractFileEngineIterator *> fileEngineIterators;    QString path;    QFileInfo fileInfo;    QString currentFilePath;    QDirIterator::IteratorFlags iteratorFlags;    QDir::Filters filters;    QStringList nameFilters;    bool followNextDir;    bool first;    bool done;    QDirIterator *q;};/*!    \internal*/QDirIteratorPrivate::QDirIteratorPrivate(const QString &path, const QStringList &nameFilters,                                         QDir::Filters filters, QDirIterator::IteratorFlags flags)    : engine(0), path(path), iteratorFlags(flags), followNextDir(false), first(true), done(false){    if (filters == QDir::NoFilter)        filters = QDir::AllEntries;    this->filters = filters;    this->nameFilters = nameFilters;    fileInfo.setFile(path);    pushSubDirectory(fileInfo.isSymLink() ? fileInfo.canonicalFilePath() : path,                     nameFilters, filters);}/*!    \internal*/QDirIteratorPrivate::~QDirIteratorPrivate(){    delete engine;}/*!    \internal*/void QDirIteratorPrivate::pushSubDirectory(const QString &path, const QStringList &nameFilters,                                           QDir::Filters filters){    if (iteratorFlags & QDirIterator::FollowSymlinks) {        if (fileInfo.filePath() != path)            fileInfo.setFile(path);        if (fileInfo.isSymLink()) {            visitedLinks << fileInfo.canonicalFilePath();        } else {            visitedLinks << fileInfo.absoluteFilePath();        }    }        if (engine || (engine = QAbstractFileEngine::create(this->path))) {        engine->setFileName(path);        QAbstractFileEngineIterator *it = engine->beginEntryList(filters, nameFilters);        if (it) {            it->setPath(path);            fileEngineIterators << it;        } else {            // No iterator; no entry list.        }    }}/*!    \internal*/void QDirIteratorPrivate::advance(){    // Store the current entry    if (!fileEngineIterators.isEmpty())        currentFilePath = fileEngineIterators.top()->currentFilePath();    // Advance to the next entry    if (followNextDir) {        // Start by navigating into the current directory.        followNextDir = false;        QAbstractFileEngineIterator *it = fileEngineIterators.top();        QString subDir = it->currentFilePath();#ifdef Q_OS_WIN        if (fileInfo.isSymLink())            subDir = fileInfo.canonicalFilePath();#endif        pushSubDirectory(subDir, it->nameFilters(), it->filters());    }    if (fileEngineIterators.isEmpty())        done = true;    bool foundValidEntry = false;    while (!fileEngineIterators.isEmpty()) {        QAbstractFileEngineIterator *it = fileEngineIterators.top();        // Find the next valid iterator that matches the filters.        foundValidEntry = false;        while (it->hasNext()) {            it->next();            if (matchesFilters(it)) {                foundValidEntry = true;                break;            }        }        if (!foundValidEntry) {            // If this iterator is done, pop and delete it, and continue            // iteration on the parent. Otherwise break, we're done.            if (!fileEngineIterators.isEmpty()) {                delete it;                it = fileEngineIterators.pop();                continue;            }            break;        }        fileInfo = it->currentFileInfo();        // If we're doing flat iteration, we're done.        if (!(iteratorFlags & QDirIterator::Subdirectories))            break;        // Subdirectory iteration.        QString filePath = fileInfo.filePath();        // Never follow . and ..        if (fileInfo.fileName() == QLatin1String(".") || fileInfo.fileName() == QLatin1String(".."))            break;        // Never follow non-directory entries        if (!fileInfo.isDir())            break;              // Check symlinks        if (fileInfo.isSymLink() && !(iteratorFlags & QDirIterator::FollowSymlinks)) {            // Follow symlinks only if FollowSymlinks was passed            break;        }        // Stop link loops        if (visitedLinks.contains(fileInfo.canonicalFilePath()))            break;        // Signal that we want to follow this entry.        followNextDir = true;        break;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -