GUI: Implement FileList class to handle list of files to check.

Create a list of files to check. Currently we only read this list
once. But later on we can refer to this list to for example
determine which files were checked and which not in aborted
checking.
This commit is contained in:
Kimmo Varis 2010-08-09 22:15:05 +03:00
parent 988b43d419
commit 7dbe5b547a
5 changed files with 195 additions and 64 deletions

105
gui/FileList.cpp Normal file
View File

@ -0,0 +1,105 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include "filelist.h"
QStringList FileList::GetDefaultFilters()
{
QStringList extensions;
extensions << "*.cpp" << "*.cxx" << "*.cc" << "*.c" << "*.c++" << "*.txx";
return extensions;
}
bool FileList::FilterMatches(const QFileInfo &inf)
{
if (inf.isFile())
{
const QStringList filters = FileList::GetDefaultFilters();
QString ext("*.");
ext += inf.suffix();
if (filters.contains(ext))
return true;
}
return false;
}
void FileList::AddFile(const QString &filepath)
{
QFileInfo inf(filepath);
if (FilterMatches(inf))
mFileList << inf;
}
void FileList::AddDirectory(const QString &directory, bool recursive)
{
QDir dir(directory);
dir.setSorting(QDir::Name);
const QStringList filters = FileList::GetDefaultFilters();
const QStringList origNameFilters = dir.nameFilters();
dir.setNameFilters(filters);
if (!recursive)
{
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList items = dir.entryInfoList();
mFileList += items;
}
else
{
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList items = dir.entryInfoList();
mFileList += items;
dir.setNameFilters(origNameFilters);
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList list = dir.entryInfoList();
QFileInfo item;
foreach(item, list)
{
const QString path = item.canonicalFilePath();
AddDirectory(path, recursive);
}
}
}
void FileList::AddPathList(const QStringList &paths)
{
QString path;
foreach(path, paths)
{
QFileInfo inf(path);
if (inf.isFile())
AddFile(path);
else
AddDirectory(path, true);
}
}
QStringList FileList::GetFileList() const
{
QStringList names;
QFileInfo item;
foreach(item, mFileList)
{
QString name = QDir::fromNativeSeparators(item.canonicalFilePath());
names << name;
}
return names;
}

81
gui/FileList.h Normal file
View File

@ -0,0 +1,81 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILELIST_H
#define FILELIST_H
#include <QList>
#include <QFileInfoList>
#include <QStringList>
/**
* @brief A class for listing files and directories to check.
* This class creates a list of files to check. If directory name is given then
* all files in the directory matching the filter will be added. The directory
* can be also added recursively when all files in subdirectories are added too.
* The filenames are matched against the filter and only those files whose
* filename extension is included in the filter list are added.
*/
class FileList
{
public:
/**
* @brief Add filename to the list.
* @param filepath Full path to the file.
*/
void AddFile(const QString &filepath);
/**
* @brief Add files in the directory to the list.
* @param directory Full pathname to directory to add.
* @param recursive If true also files in subdirectories are added.
*/
void AddDirectory(const QString &directory, bool recursive = false);
/**
* @brief Add list of filenames and directories to the list.
* @param paths List of paths to add.
*/
void AddPathList(const QStringList &paths);
/**
* @brief Return list of filenames (to check).
* @return list of filenames to check.
*/
QStringList GetFileList() const;
protected:
/**
* @brief Return list of default filename extensions included.
* @return list of default filename extensions included.
*/
static QStringList GetDefaultFilters();
/**
* @brief Test if filename matches the filename extensions filtering.
* @param true if filename matches filterin.
*/
bool FilterMatches(const QFileInfo &inf);
private:
QFileInfoList mFileList;
};
#endif // FILELIST_H

View File

@ -64,7 +64,8 @@ HEADERS += mainwindow.h \
xmlreport.h \ xmlreport.h \
translationhandler.h \ translationhandler.h \
csvreport.h \ csvreport.h \
logview.h logview.h \
filelist.h
SOURCES += main.cpp \ SOURCES += main.cpp \
mainwindow.cpp\ mainwindow.cpp\
@ -87,7 +88,8 @@ SOURCES += main.cpp \
xmlreport.cpp \ xmlreport.cpp \
translationhandler.cpp \ translationhandler.cpp \
csvreport.cpp \ csvreport.cpp \
logview.cpp logview.cpp \
filelist.cpp
win32 { win32 {
DEFINES += _CRT_SECURE_NO_WARNINGS DEFINES += _CRT_SECURE_NO_WARNINGS

View File

@ -34,7 +34,7 @@
#include "project.h" #include "project.h"
#include "report.h" #include "report.h"
#include "logview.h" #include "logview.h"
#include "../lib/filelister.h" #include "filelist.h"
// HTMLHelp is only available in Windows // HTMLHelp is only available in Windows
#ifdef WIN32 #ifdef WIN32
@ -210,13 +210,9 @@ void MainWindow::DoCheckFiles(const QStringList &files)
} }
ClearResults(); ClearResults();
QStringList fileNames; FileList pathList;
QString selection; pathList.AddPathList(files);
QStringList fileNames = pathList.GetFileList();
foreach(selection, files)
{
fileNames << RemoveUnacceptedFiles(GetFilesRecursively(selection));
}
mUI.mResults->Clear(); mUI.mResults->Clear();
mThread->ClearFiles(); mThread->ClearFiles();
@ -234,7 +230,7 @@ void MainWindow::DoCheckFiles(const QStringList &files)
mUI.mResults->CheckingStarted(fileNames.count()); mUI.mResults->CheckingStarted(fileNames.count());
mThread->SetFiles(RemoveUnacceptedFiles(fileNames)); mThread->SetFiles(fileNames);
QDir inf(mCurrentDirectory); QDir inf(mCurrentDirectory);
const QString absDirectory = inf.absolutePath(); const QString absDirectory = inf.absolutePath();
mSettings->setValue(SETTINGS_CHECK_PATH, absDirectory); mSettings->setValue(SETTINGS_CHECK_PATH, absDirectory);
@ -362,43 +358,6 @@ Settings MainWindow::GetCppcheckSettings()
return result; return result;
} }
QStringList MainWindow::GetFilesRecursively(const QString &path)
{
QFileInfo info(path);
QStringList list;
if (info.isDir())
{
QDirIterator it(path, QDirIterator::Subdirectories);
while (it.hasNext())
{
list << it.next();
}
}
else
{
list << path;
}
return list;
}
QStringList MainWindow::RemoveUnacceptedFiles(const QStringList &list)
{
QStringList result;
QString str;
foreach(str, list)
{
if (getFileLister()->acceptFile(str.toStdString()))
{
result << str;
}
}
return result;
}
void MainWindow::CheckDone() void MainWindow::CheckDone()
{ {
if (mExiting) if (mExiting)

View File

@ -251,14 +251,6 @@ protected:
*/ */
void DoCheckFiles(const QStringList &files); void DoCheckFiles(const QStringList &files);
/**
* @brief Get all files recursively from given path
*
* @param path Path to get files from
* @return List of file paths
*/
QStringList GetFilesRecursively(const QString &path);
/** /**
* @brief Get our default cppcheck settings and read project file. * @brief Get our default cppcheck settings and read project file.
* *
@ -266,14 +258,6 @@ protected:
*/ */
Settings GetCppcheckSettings(); Settings GetCppcheckSettings();
/**
* @brief Removes all unaccepted (by cppcheck core) files from the list
*
* @param list List to remove unaccepted files from
* @return List of files that are all accepted by cppcheck core
*/
QStringList RemoveUnacceptedFiles(const QStringList &list);
/** /**
* @brief Load program settings * @brief Load program settings
* *