Cppcheck
filelist.h
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 #ifndef FILELIST_H
20 #define FILELIST_H
21 
22 #include <QFileInfo>
23 #include <QFileInfoList>
24 #include <QString>
25 #include <QStringList>
26 
27 /**
28  * @brief A class for listing files and directories to check.
29  * This class creates a list of files to check. If directory name is given then
30  * all files in the directory matching the filter will be added. The directory
31  * can be also added recursively when all files in subdirectories are added too.
32  * The filenames are matched against the filter and only those files whose
33  * filename extension is included in the filter list are added.
34  *
35  * This class also handles filtering of paths against ignore filters given. If
36  * there is ignore filters then only paths not matching those filters are
37  * returned.
38  */
39 class FileList {
40 public:
41 
42  /**
43  * @brief Add filename to the list.
44  * @param filepath Full path to the file.
45  */
46  void addFile(const QString &filepath);
47 
48  /**
49  * @brief Add files in the directory to the list.
50  * @param directory Full pathname to directory to add.
51  * @param recursive If true also files in subdirectories are added.
52  */
53  void addDirectory(const QString &directory, bool recursive = false);
54 
55  /**
56  * @brief Add list of filenames and directories to the list.
57  * @param paths List of paths to add.
58  */
59  void addPathList(const QStringList &paths);
60 
61  /**
62  * @brief Return list of filenames (to check).
63  * @return list of filenames to check.
64  */
65  QStringList getFileList() const;
66 
67  /**
68  * @brief Add list of paths to exclusion list.
69  * @param paths Paths to exclude.
70  */
71  void addExcludeList(const QStringList &paths);
72 
73  /**
74  * @brief Return list of default filename extensions included.
75  * @return list of default filename extensions included.
76  */
77  static QStringList getDefaultFilters();
78 
79 protected:
80 
81  /**
82  * @brief Test if filename matches the filename extensions filtering.
83  * @return true if filename matches filtering.
84  */
85  static bool filterMatches(const QFileInfo &inf);
86 
87  /**
88  * @brief Get filtered list of paths.
89  * This method takes the list of paths and applies the exclude lists to
90  * it. And then returns the list of paths that did not match the
91  * exclude filters.
92  * @return Filtered list of paths.
93  */
94  QStringList applyExcludeList() const;
95 
96 private:
97  QFileInfoList mFileList;
98  QStringList mExcludedPaths;
99 };
100 
101 #endif // FILELIST_H
A class for listing files and directories to check.
Definition: filelist.h:39
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