2010-08-09 21:15:05 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 Cppcheck team.
|
2010-08-09 21:15:05 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-11 16:22:39 +02:00
|
|
|
#include "filelist.h"
|
2021-04-03 21:30:50 +02:00
|
|
|
|
2016-10-02 13:02:29 +02:00
|
|
|
#include "pathmatch.h"
|
2010-08-09 21:15:05 +02:00
|
|
|
|
2022-04-13 12:24:00 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-02-02 16:17:28 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
QStringList FileList::getDefaultFilters()
|
2010-08-09 21:15:05 +02:00
|
|
|
{
|
|
|
|
QStringList extensions;
|
2021-08-09 10:46:56 +02:00
|
|
|
extensions << "*.cpp" << "*.cxx" << "*.cc" << "*.c" << "*.c++" << "*.txx" << "*.tpp" << "*.ipp" << "*.ixx";
|
2010-08-09 21:15:05 +02:00
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
bool FileList::filterMatches(const QFileInfo &inf)
|
2010-08-09 21:15:05 +02:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (inf.isFile()) {
|
2017-07-28 10:55:51 +02:00
|
|
|
const QStringList filters = FileList::getDefaultFilters();
|
2010-08-09 21:15:05 +02:00
|
|
|
QString ext("*.");
|
|
|
|
ext += inf.suffix();
|
2015-10-18 17:19:05 +02:00
|
|
|
if (filters.contains(ext, Qt::CaseInsensitive))
|
2010-08-09 21:15:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
void FileList::addFile(const QString &filepath)
|
2010-08-09 21:15:05 +02:00
|
|
|
{
|
|
|
|
QFileInfo inf(filepath);
|
2017-07-28 10:55:51 +02:00
|
|
|
if (filterMatches(inf))
|
2010-08-09 21:15:05 +02:00
|
|
|
mFileList << inf;
|
|
|
|
}
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
void FileList::addDirectory(const QString &directory, bool recursive)
|
2010-08-09 21:15:05 +02:00
|
|
|
{
|
|
|
|
QDir dir(directory);
|
|
|
|
dir.setSorting(QDir::Name);
|
2017-07-28 10:55:51 +02:00
|
|
|
const QStringList filters = FileList::getDefaultFilters();
|
2010-08-09 21:15:05 +02:00
|
|
|
const QStringList origNameFilters = dir.nameFilters();
|
|
|
|
dir.setNameFilters(filters);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!recursive) {
|
2010-08-09 21:15:05 +02:00
|
|
|
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
QFileInfoList items = dir.entryInfoList();
|
|
|
|
mFileList += items;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2010-08-09 21:15:05 +02:00
|
|
|
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
QFileInfoList items = dir.entryInfoList();
|
|
|
|
mFileList += items;
|
|
|
|
|
|
|
|
dir.setNameFilters(origNameFilters);
|
|
|
|
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
2022-03-21 17:14:26 +01:00
|
|
|
for (const QFileInfo& item : dir.entryInfoList()) {
|
2010-08-09 21:15:05 +02:00
|
|
|
const QString path = item.canonicalFilePath();
|
2017-07-28 10:55:51 +02:00
|
|
|
addDirectory(path, recursive);
|
2010-08-09 21:15:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
void FileList::addPathList(const QStringList &paths)
|
2010-08-09 21:15:05 +02:00
|
|
|
{
|
2022-03-21 17:14:26 +01:00
|
|
|
for (const QString& path : paths) {
|
2010-08-09 21:15:05 +02:00
|
|
|
QFileInfo inf(path);
|
|
|
|
if (inf.isFile())
|
2017-07-28 10:55:51 +02:00
|
|
|
addFile(path);
|
2010-08-09 21:15:05 +02:00
|
|
|
else
|
2017-07-28 10:55:51 +02:00
|
|
|
addDirectory(path, true);
|
2010-08-09 21:15:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
QStringList FileList::getFileList() const
|
2010-08-09 21:15:05 +02:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mExcludedPaths.empty()) {
|
2011-02-27 23:13:28 +01:00
|
|
|
QStringList names;
|
2022-03-21 17:14:26 +01:00
|
|
|
for (const QFileInfo& item : mFileList) {
|
2018-07-01 08:00:05 +02:00
|
|
|
QString name = QDir::fromNativeSeparators(item.filePath());
|
2011-02-27 23:13:28 +01:00
|
|
|
names << name;
|
|
|
|
}
|
|
|
|
return names;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2017-07-28 10:55:51 +02:00
|
|
|
return applyExcludeList();
|
2011-02-27 23:13:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
void FileList::addExcludeList(const QStringList &paths)
|
2011-02-27 23:13:28 +01:00
|
|
|
{
|
2011-08-23 18:54:53 +02:00
|
|
|
mExcludedPaths = paths;
|
2011-02-27 23:13:28 +01:00
|
|
|
}
|
|
|
|
|
2016-10-02 13:02:29 +02:00
|
|
|
static std::vector<std::string> toStdStringList(const QStringList &stringList)
|
|
|
|
{
|
|
|
|
std::vector<std::string> ret;
|
2022-12-30 15:13:47 +01:00
|
|
|
std::transform(stringList.cbegin(), stringList.cend(), std::back_inserter(ret), [](const QString& s) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return s.toStdString();
|
|
|
|
});
|
2016-10-02 13:02:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-28 10:55:51 +02:00
|
|
|
QStringList FileList::applyExcludeList() const
|
2011-02-27 23:13:28 +01:00
|
|
|
{
|
2016-10-02 13:02:29 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
const PathMatch pathMatch(toStdStringList(mExcludedPaths), true);
|
|
|
|
#else
|
|
|
|
const PathMatch pathMatch(toStdStringList(mExcludedPaths), false);
|
|
|
|
#endif
|
|
|
|
|
2011-02-27 23:13:28 +01:00
|
|
|
QStringList paths;
|
2022-03-21 17:14:26 +01:00
|
|
|
for (const QFileInfo& item : mFileList) {
|
2022-11-19 14:11:40 +01:00
|
|
|
if (pathMatch.match(QDir::fromNativeSeparators(item.filePath()).toStdString()))
|
|
|
|
continue;
|
|
|
|
QString canonical = QDir::fromNativeSeparators(item.canonicalFilePath());
|
|
|
|
if (!pathMatch.match(canonical.toStdString()))
|
|
|
|
paths << canonical;
|
2011-02-27 23:13:28 +01:00
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|