Now adds directory contents to list of files to check.

Only adds proper (.cpp,.c,.cpp,...) files to the list of files to check.
Modified checkthread to clear results after each file.
This commit is contained in:
Vesa Pikki 2009-03-02 19:56:51 +00:00
parent 20390ce655
commit e629db6818
12 changed files with 182 additions and 107 deletions

View File

@ -27,6 +27,9 @@
#include <QFileDialog> #include <QFileDialog>
#include <QDirIterator> #include <QDirIterator>
#include <QDebug> #include <QDebug>
#include <QFileInfo>
#include <QDirIterator>
#include "../src/filelister.h"
CheckDialog::CheckDialog(QSettings &programSettings) : CheckDialog::CheckDialog(QSettings &programSettings) :
mSettings(programSettings) mSettings(programSettings)
@ -111,8 +114,54 @@ CheckDialog::~CheckDialog()
} }
QStringList CheckDialog::RemoveUnacceptedFiles(const QStringList &list)
{
QStringList result;
QString str;
foreach(str, list)
{
if (FileLister::AcceptFile(str.toStdString()))
{
result << str;
}
}
return result;
}
QStringList CheckDialog::GetFiles(QModelIndex index)
{
QFileInfo info(mModel.filePath(index));
QStringList list;
if (info.isDir())
{
QDirIterator it(mModel.filePath(index), QDirIterator::Subdirectories);
while (it.hasNext())
{
list << it.next();
}
}
else
{
list << mModel.filePath(index);
}
return list;
}
QStringList CheckDialog::RemoveDuplicates(const QStringList &list)
{
QHash<QString, int> hash;
QString str;
foreach(str, list)
{
hash[str] = 0;
}
return QStringList(hash.uniqueKeys());
}
QStringList CheckDialog::GetSelectedFiles() QStringList CheckDialog::GetSelectedFiles()
{ {
@ -124,11 +173,13 @@ QStringList CheckDialog::GetSelectedFiles()
{ {
if (!mModel.filePath(index).isEmpty()) if (!mModel.filePath(index).isEmpty())
{ {
list << mModel.filePath(index); list << GetFiles(index);
} }
} }
return list; QString str;
return RemoveUnacceptedFiles(RemoveDuplicates(list));
} }

View File

@ -71,6 +71,9 @@ public:
*/ */
void SaveCheckboxValues(); void SaveCheckboxValues();
protected: protected:
QStringList RemoveUnacceptedFiles(const QStringList &list);
QStringList RemoveDuplicates(const QStringList &list);
QStringList GetFiles(QModelIndex index);
/** /**
* @brief Load saved values * @brief Load saved values

View File

@ -46,9 +46,12 @@ void CheckThread::run()
while (!file.isEmpty()) while (!file.isEmpty())
{ {
qDebug() << tr("Checking file") << file; qDebug() << "Checking file" << file;
mCppCheck.addFile(file.toStdString()); mCppCheck.addFile(file.toStdString());
mCppCheck.check(); mCppCheck.check();
mCppCheck.clearFiles();
emit FileChecked(file);
file = mResult.GetNextFile(); file = mResult.GetNextFile();
} }

View File

@ -57,6 +57,8 @@ signals:
* *
*/ */
void Done(); void Done();
void FileChecked(const QString &file);
protected: protected:
ThreadResult &mResult; ThreadResult &mResult;
/** /**

View File

@ -19,6 +19,7 @@
#include "resultstree.h" #include "resultstree.h"
#include <QDebug>
ResultsTree::ResultsTree(QSettings &settings) : ResultsTree::ResultsTree(QSettings &settings) :
mSettings(settings) mSettings(settings)
@ -61,6 +62,8 @@ void ResultsTree::AddErrorItem(const QString &file,
fileitem = CreateItem(file); fileitem = CreateItem(file);
} }
qDebug() << "Adding error for file" << file << ". Message is" << message;
QList<QStandardItem*> list; QList<QStandardItem*> list;
list << CreateItem(severity); list << CreateItem(severity);
list << CreateItem(message); list << CreateItem(message);

View File

@ -51,7 +51,7 @@ void ResultsView::Clear()
void ResultsView::Progress(int value, int max) void ResultsView::Progress(int value, int max)
{ {
qDebug() << tr("Progress:") << value << tr("/") << max; qDebug() << "Progress:" << value << "/" << max;
mProgress->setMaximum(max); mProgress->setMaximum(max);
mProgress->setValue(value); mProgress->setValue(value);
} }

View File

@ -21,21 +21,14 @@
#include "threadhandler.h" #include "threadhandler.h"
#include <QDebug> #include <QDebug>
ThreadHandler::ThreadHandler() : mThreadCount(1), mRunningThreadCount(0) ThreadHandler::ThreadHandler() : mRunningThreadCount(0)
{ {
SetThreadCount(1); SetThreadCount(1);
} }
ThreadHandler::~ThreadHandler() ThreadHandler::~ThreadHandler()
{ {
Stop(); RemoveThreads();
for (int i = 0;i < mThreads.size();i++)
{
delete mThreads[i];
}
mThreads.clear();
} }
void ThreadHandler::ClearFiles() void ThreadHandler::ClearFiles()
@ -50,23 +43,23 @@ void ThreadHandler::SetFiles(const QStringList &files)
void ThreadHandler::Check(Settings settings) void ThreadHandler::Check(Settings settings)
{ {
if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0) if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0 || settings._jobs <= 0)
{ {
qDebug() << tr("Can't start checking if there's no files to check or if check is in progress."); qDebug() << "Can't start checking if there's no files to check or if check is in progress.";
return; return;
} }
SetThreadCount(settings._jobs); SetThreadCount(settings._jobs);
mRunningThreadCount = mThreads.size();
mRunningThreadCount = mThreadCount;
if (mResults.GetFileCount() < mRunningThreadCount) if (mResults.GetFileCount() < mRunningThreadCount)
{ {
mRunningThreadCount = mResults.GetFileCount(); mRunningThreadCount = mResults.GetFileCount();
} }
qDebug() << tr("Starting") << mRunningThreadCount << tr("threads"); qDebug() << "Starting" << mRunningThreadCount << "threads";
qDebug() << mThreads.size();
for (int i = 0;i < mRunningThreadCount;i++) for (int i = 0;i < mRunningThreadCount;i++)
{ {
mThreads[i]->Check(settings); mThreads[i]->Check(settings);
@ -76,41 +69,51 @@ void ThreadHandler::Check(Settings settings)
void ThreadHandler::SetThreadCount(const int count) void ThreadHandler::SetThreadCount(const int count)
{ {
if (mRunningThreadCount > 0 || if (mRunningThreadCount > 0 ||
count == mThreadCount || count == mThreads.size() ||
count <= 0) count <= 0)
{ {
return; return;
} }
qDebug() << tr("Setting thead count to") << count; qDebug() << "Setting thead count to" << count;
mThreadCount = count;
//Remove unused old threads //Remove unused old threads
if (mThreads.size() > count) RemoveThreads();
//Create new threads
for (int i = mThreads.size();i < count;i++)
{ {
for (int i = count;i < mThreads.size();i++) mThreads << new CheckThread(mResults);
{ connect(mThreads.last(), SIGNAL(Done()),
disconnect(mThreads.last(), SIGNAL(Done()), this, SLOT(ThreadDone()));
this, SLOT(ThreadDone())); connect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
delete mThreads.takeLast(); &mResults, SLOT(FileChecked(const QString &)));
}
} }
else
}
void ThreadHandler::RemoveThreads()
{
for (int i = 0;i < mThreads.size();i++)
{ {
//Create new threads mThreads[i]->terminate();
for (int i = mThreads.size();i < count;i++) disconnect(mThreads.last(), SIGNAL(Done()),
{ this, SLOT(ThreadDone()));
mThreads << new CheckThread(mResults); disconnect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
connect(mThreads.last(), SIGNAL(Done()), &mResults, SLOT(FileChecked(const QString &)));
this, SLOT(ThreadDone()));
} delete mThreads[i];
} }
mThreads.clear();
} }
void ThreadHandler::ThreadDone() void ThreadHandler::ThreadDone()
{ {
mRunningThreadCount--; mRunningThreadCount--;
qDebug() << "Thread done" << mRunningThreadCount << "threads left";
if (mRunningThreadCount == 0) if (mRunningThreadCount == 0)
{ {
emit Done(); emit Done();
@ -153,6 +156,6 @@ void ThreadHandler::LoadSettings(QSettings &settings)
void ThreadHandler::SaveSettings(QSettings &settings) void ThreadHandler::SaveSettings(QSettings &settings)
{ {
settings.setValue(tr("Check threads"), mThreadCount); settings.setValue(tr("Check threads"), mThreads.size());
} }

View File

@ -65,8 +65,8 @@ protected slots:
void Stop(); void Stop();
void ThreadDone(); void ThreadDone();
protected: protected:
void RemoveThreads();
ThreadResult mResults; ThreadResult mResults;
int mThreadCount;
QList<CheckThread *> mThreads; QList<CheckThread *> mThreads;
int mRunningThreadCount; int mRunningThreadCount;
private: private:

View File

@ -19,6 +19,7 @@
#include "threadresult.h" #include "threadresult.h"
#include <QDebug>
ThreadResult::ThreadResult() : mMaxProgress(0), mProgress(0) ThreadResult::ThreadResult() : mMaxProgress(0), mProgress(0)
{ {
@ -32,10 +33,16 @@ ThreadResult::~ThreadResult()
void ThreadResult::reportOut(const std::string &outmsg) void ThreadResult::reportOut(const std::string &outmsg)
{ {
//emit CurrentFile(QString(outmsg.c_str()));
Q_UNUSED(outmsg); Q_UNUSED(outmsg);
} }
void ThreadResult::FileChecked(const QString &file)
{
Q_UNUSED(file); //For later use maybe?
mProgress++;
emit Progress(mProgress, mMaxProgress);
}
void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg) void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
{ {
QMutexLocker locker(&mutex); QMutexLocker locker(&mutex);
@ -50,6 +57,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
files << QString((*tok).file.c_str()); files << QString((*tok).file.c_str());
lines << (*tok).line; lines << (*tok).line;
} }
qDebug() << "Got error for file" << QString(callStackToString(msg._callStack).c_str()) << QString(msg._msg.c_str());
emit Error(QString(callStackToString(msg._callStack).c_str()), emit Error(QString(callStackToString(msg._callStack).c_str()),
QString(msg._severity.c_str()), QString(msg._severity.c_str()),
@ -57,9 +65,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
files, files,
lines); lines);
mProgress++;
emit Progress(mProgress, mMaxProgress);
} }
@ -83,8 +89,7 @@ void ThreadResult::reportStatus(unsigned int index, unsigned int max)
void ThreadResult::SetFiles(const QStringList &files) void ThreadResult::SetFiles(const QStringList &files)
{ {
//TODO we should check which of the strings in files is actually a path QMutexLocker locker(&mutex);
//and add the path's contents
mFiles = files; mFiles = files;
mProgress = 0; mProgress = 0;
mMaxProgress = files.size(); mMaxProgress = files.size();
@ -92,11 +97,13 @@ void ThreadResult::SetFiles(const QStringList &files)
void ThreadResult::ClearFiles() void ThreadResult::ClearFiles()
{ {
QMutexLocker locker(&mutex);
mFiles.clear(); mFiles.clear();
} }
int ThreadResult::GetFileCount() int ThreadResult::GetFileCount()
{ {
QMutexLocker locker(&mutex);
return mFiles.size(); return mFiles.size();
} }

View File

@ -47,6 +47,8 @@ public:
void reportOut(const std::string &outmsg); void reportOut(const std::string &outmsg);
void reportErr(const ErrorLogger::ErrorMessage &msg); void reportErr(const ErrorLogger::ErrorMessage &msg);
void reportStatus(unsigned int index, unsigned int max); void reportStatus(unsigned int index, unsigned int max);
public slots:
void FileChecked(const QString &file);
signals: signals:
void Progress(int value, int max); void Progress(int value, int max);
void Error(const QString &file, void Error(const QString &file,

View File

@ -42,8 +42,9 @@ public:
static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive); static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
static std::string simplifyPath(const char *originalPath); static std::string simplifyPath(const char *originalPath);
static bool SameFileName(const char fname1[], const char fname2[]); static bool SameFileName(const char fname1[], const char fname2[]);
private:
static bool AcceptFile(const std::string &filename); static bool AcceptFile(const std::string &filename);
private:
}; };
#endif // #ifndef FILELISTER_H #endif // #ifndef FILELISTER_H