X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Fapps%2FFileFinder%2FEngine%2Fengine.cpp;h=1cded9037cebbf7ead028bfdcbeca54586122413;hb=HEAD;hp=b9fae0eddd917263fc16288bc4f2042406f5cd4a;hpb=e73fe47c7bba8066feeb68b0f1568f3ac493f50c;p=evaf diff --git a/src/apps/FileFinder/Engine/engine.cpp b/src/apps/FileFinder/Engine/engine.cpp index b9fae0e..1cded90 100644 --- a/src/apps/FileFinder/Engine/engine.cpp +++ b/src/apps/FileFinder/Engine/engine.cpp @@ -3,7 +3,7 @@ * @brief Module for the FileFinder application that searches for files * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2019 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -19,7 +19,6 @@ #include "engine.h" -#include "version.h" #include #include @@ -27,7 +26,6 @@ #include VER_EXPORT_VERSION_INFO() -Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, eVaf::FileFinder::Engine::Module) using namespace eVaf; using namespace eVaf::FileFinder; @@ -81,7 +79,7 @@ void Module::done() Internal::Engine::Engine() : iFileFinder() - , mWorker(0) + , mWorker(nullptr) { setObjectName(QString("%1.Engine").arg(VER_MODULE_NAME_STR)); @@ -110,10 +108,10 @@ bool Internal::Engine::init() void Internal::Engine::done() { - if (mWorker) { + if (mWorker != nullptr) { mWorker->stop(); delete mWorker; - mWorker = 0; + mWorker = nullptr; } EVAF_INFO("%s finalized", qPrintable(objectName())); @@ -121,20 +119,20 @@ void Internal::Engine::done() void Internal::Engine::search(QString const & dir, bool recursive, Filter const & filter) { - if (mWorker) + if (mWorker != nullptr) mWorker->search(dir, recursive, filter); } bool Internal::Engine::busy() const { - if (mWorker) + if (mWorker != nullptr) return mWorker->busy(); return false; } void Internal::Engine::cancel() { - if (mWorker) + if (mWorker != nullptr) mWorker->cancel(); } @@ -196,7 +194,7 @@ QStringList Internal::RegExpChain::split(const QString & pattern) bool e = false; while (offset < sz) { - QChar ch = pattern.at(offset++); + QChar const ch = pattern.at(offset++); if (e) { e = false; if (ch == '*' || ch == '?' || ch == '[' || ch == ']') @@ -235,6 +233,13 @@ Internal::Worker::Worker(QObject * parent) , mBusy(false) { setObjectName(QString("%1.Worker").arg(VER_MODULE_NAME_STR)); + + EVAF_INFO("%s created", qPrintable(objectName())); +} + +Internal::Worker::~Worker() +{ + EVAF_INFO("%s destroyed", qPrintable(objectName())); } void Internal::Worker::cancel() @@ -278,6 +283,9 @@ void Internal::Worker::run() forever { QMutexLocker lock(&mLock); + if (mDoTerminate) + break; + mSomethingToDo.wait(&mLock); if (mDoTerminate) @@ -304,8 +312,10 @@ void Internal::Worker::run() lock.relock(); mBusy = false; + bool c = mDoCancel; + lock.unlock(); - emit finished(mDoCancel); + emit finished(c); } } } @@ -318,14 +328,14 @@ void Internal::Worker::recursiveSearch(QString const & path) QDir dir(l); // Get the list of files in this directory - QStringList files = dir.entryList(QDir::Files | QDir::NoSymLinks); + QStringList const files = dir.entryList(QDir::Files | QDir::NoSymLinks); foreach (QString const & file, files) { // Check for the cancel flag { QMutexLocker l(&mLock); if (mDoCancel) - break; + return; } // Check for the file name to match the include filter and not the exclude filter @@ -355,12 +365,19 @@ void Internal::Worker::recursiveSearch(QString const & path) QByteArray buf; while (!f.atEnd() && (includeFilterMatched <= 0 || excludeFilterMatched <= 0)) { + // Check for the cancel flag + { + QMutexLocker l(&mLock); + if (mDoCancel) + return; + } + /* We read ReadBufferSize bytes from the file and append to the buffer. * We keep max 2 x ReadBufferSize bytes in the buffer and throw away the oldest * ReadBufferSize bytes of data. Every block is checked twice, but we make sure that * also strings that stretch from one block to another are checked. */ - QByteArray b = f.read(ReadBufferSize); + QByteArray const b = f.read(ReadBufferSize); buf.append(b); if (buf.size() > (2 * ReadBufferSize)) buf.remove(0, ReadBufferSize); @@ -381,16 +398,19 @@ void Internal::Worker::recursiveSearch(QString const & path) // Process sub-directories if (mRecursive) { - QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks); + QStringList const dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks); foreach (QString const & directory, dirs) { // Check for the cancel flag { QMutexLocker l(&mLock); if (mDoCancel) - break; + return; } + if (mRxExcludeNames.isValid() && !mRxExcludeNames.isEmpty() && mRxExcludeNames.exactMatch(directory)) + continue; + recursiveSearch(l + directory); } }