[ fromfile: iterators.xml id: qdiriterator ]
A directory, sometimes called a folder, is a container for files.
Because a directory can also contain other directories, it has a natural tree structure.
A directory can also contain symbolic links (called symlinks) which point to other files or directories.
A symlink is a reference that can be used instead of the name or path for most operations that handle files or directories.
Example 4.2. src/iteration/whyiterator/recurseadddir.cpp
[ . . . . ] void recurseAddDir(QDir d, bool recursive=true, bool symlinks=false ) { d.setSorting( QDir::Name ); QDir::Filters df = QDir::Files | QDir::NoDotAndDotDot; if (recursive) df |= QDir::Dirs; if (not symlinks) df |= QDir::NoSymLinks; QStringList qsl = d.entryList(df, QDir::Name | QDir::DirsFirst); foreach (const QString &entry, qsl) { QFileInfo finfo(d, entry); if ( finfo.isDir() ) { QDir sd(finfo.absoluteFilePath()); recurseAddDir(sd); } else { if (finfo.completeSuffix()=="mp3") addMp3File(finfo.absoluteFilePath()); } } } [ . . . . ]
The application listed in Example 4.3 reuses QDirIterator to accomplish the same thing with fewer lines of code.
Example 4.3. src/iteration/diriterator/diriterator.cpp
[ . . . . ] int main (int argc, char* argv[]) { QCoreApplication app(argc, argv); QDir dir = QDir::current(); if (app.arguments().size() > 1) { dir = app.arguments()[1]; } if (!dir.exists()) { cerr << dir.path() << " does not exist!" << endl; usage(); return -1; } QDirIterator qdi(dir.absolutePath(), QStringList() << "*.mp3", QDir::NoSymLinks | QDir::Files, QDirIterator::Subdirectories ); while (qdi.hasNext()) { addMp3File(qdi.next()); } } [ . . . . ]
In Example 4.2, the call to the project-specific function, addMp3File()
occurs inside the definition of the function, recurseAddDir()
, that manages the iteration, severely limiting the reusability of that function.
In Example 4.3, QDirIterator manages the iteration.
The call to addMp3File()
occurs in the client code for QDirIterator (main()
) and, thus, can have no effect on the reusability of that class.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |