Cppcheck
filelist.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "filelist.h"
20 
21 #include "pathmatch.h"
22 
23 #include <algorithm>
24 #include <iterator>
25 #include <string>
26 #include <vector>
27 
28 #include <QDir>
29 #include <Qt>
30 
32 {
33  QStringList extensions;
34  extensions << "*.cpp" << "*.cxx" << "*.cc" << "*.c" << "*.c++" << "*.txx" << "*.tpp" << "*.ipp" << "*.ixx";
35  return extensions;
36 }
37 
38 bool FileList::filterMatches(const QFileInfo &inf)
39 {
40  if (inf.isFile()) {
41  const QStringList filters = FileList::getDefaultFilters();
42  QString ext("*.");
43  ext += inf.suffix();
44  if (filters.contains(ext, Qt::CaseInsensitive))
45  return true;
46  }
47  return false;
48 }
49 
50 void FileList::addFile(const QString &filepath)
51 {
52  QFileInfo inf(filepath);
53  if (filterMatches(inf))
54  mFileList << inf;
55 }
56 
57 void FileList::addDirectory(const QString &directory, bool recursive)
58 {
59  QDir dir(directory);
60  dir.setSorting(QDir::Name);
61  const QStringList filters = FileList::getDefaultFilters();
62  const QStringList origNameFilters = dir.nameFilters();
63  dir.setNameFilters(filters);
64  if (!recursive) {
65  dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
66  QFileInfoList items = dir.entryInfoList();
67  mFileList += items;
68  } else {
69  dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
70  QFileInfoList items = dir.entryInfoList();
71  mFileList += items;
72 
73  dir.setNameFilters(origNameFilters);
74  dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
75  for (const QFileInfo& item : dir.entryInfoList()) {
76  const QString path = item.canonicalFilePath();
77  addDirectory(path, recursive);
78  }
79  }
80 }
81 
82 void FileList::addPathList(const QStringList &paths)
83 {
84  for (const QString& path : paths) {
85  QFileInfo inf(path);
86  if (inf.isFile())
87  addFile(path);
88  else
89  addDirectory(path, true);
90  }
91 }
92 
93 QStringList FileList::getFileList() const
94 {
95  if (mExcludedPaths.empty()) {
96  QStringList names;
97  for (const QFileInfo& item : mFileList) {
98  QString name = QDir::fromNativeSeparators(item.filePath());
99  names << name;
100  }
101  return names;
102  }
103  return applyExcludeList();
104 }
105 
106 void FileList::addExcludeList(const QStringList &paths)
107 {
108  mExcludedPaths = paths;
109 }
110 
111 static std::vector<std::string> toStdStringList(const QStringList &stringList)
112 {
113  std::vector<std::string> ret;
114  std::transform(stringList.cbegin(), stringList.cend(), std::back_inserter(ret), [](const QString& s) {
115  return s.toStdString();
116  });
117  return ret;
118 }
119 
120 QStringList FileList::applyExcludeList() const
121 {
122 #ifdef _WIN32
123  const PathMatch pathMatch(toStdStringList(mExcludedPaths), true);
124 #else
125  const PathMatch pathMatch(toStdStringList(mExcludedPaths), false);
126 #endif
127 
128  QStringList paths;
129  for (const QFileInfo& item : mFileList) {
130  if (pathMatch.match(QDir::fromNativeSeparators(item.filePath()).toStdString()))
131  continue;
132  QString canonical = QDir::fromNativeSeparators(item.canonicalFilePath());
133  if (!pathMatch.match(canonical.toStdString()))
134  paths << canonical;
135  }
136  return paths;
137 }
static bool filterMatches(const QFileInfo &inf)
Test if filename matches the filename extensions filtering.
Definition: filelist.cpp:38
static QStringList getDefaultFilters()
Return list of default filename extensions included.
Definition: filelist.cpp:31
void addFile(const QString &filepath)
Add filename to the list.
Definition: filelist.cpp:50
QFileInfoList mFileList
Definition: filelist.h:97
void addDirectory(const QString &directory, bool recursive=false)
Add files in the directory to the list.
Definition: filelist.cpp:57
QStringList getFileList() const
Return list of filenames (to check).
Definition: filelist.cpp:93
void addPathList(const QStringList &paths)
Add list of filenames and directories to the list.
Definition: filelist.cpp:82
QStringList applyExcludeList() const
Get filtered list of paths.
Definition: filelist.cpp:120
void addExcludeList(const QStringList &paths)
Add list of paths to exclusion list.
Definition: filelist.cpp:106
QStringList mExcludedPaths
Definition: filelist.h:98
Simple path matching for ignoring paths in CLI.
Definition: pathmatch.h:33
bool match(const std::string &path) const
Match path against list of masks.
Definition: pathmatch.cpp:36
static std::vector< std::string > toStdStringList(const QStringList &stringList)
Definition: filelist.cpp:111
static constexpr char Name[]