Cppcheck
path.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 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 //---------------------------------------------------------------------------
20 #ifndef pathH
21 #define pathH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 #include "standards.h"
26 
27 #include <set>
28 #include <string>
29 #include <vector>
30 
31 /// @addtogroup Core
32 /// @{
33 
34 
35 /**
36  * @brief Path handling routines.
37  * Internally cppcheck wants to store paths with / separator which is also
38  * native separator for Unix-derived systems. When giving path to user
39  * or for other functions we convert path separators back to native type.
40  */
42 public:
43  /**
44  * Convert path to use native separators.
45  * @param path Path string to convert.
46  * @return converted path.
47  */
48  static std::string toNativeSeparators(std::string path);
49 
50  /**
51  * Convert path to use internal path separators.
52  * @param path Path string to convert.
53  * @return converted path.
54  */
55  static std::string fromNativeSeparators(std::string path);
56 
57  /**
58  * @brief Simplify path "foo/bar/.." => "foo"
59  * @param originalPath path to be simplified, must have / -separators.
60  * @return simplified path
61  */
62  static std::string simplifyPath(std::string originalPath);
63 
64  /**
65  * @brief Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
66  * @param filename filename to lookup, must have / -separators.
67  * @return path part of the filename
68  */
69  static std::string getPathFromFilename(const std::string &filename);
70 
71  /**
72  * @brief Compare filenames to see if they are the same.
73  * On Linux the comparison is case-sensitive. On Windows it is case-insensitive.
74  * @param fname1 one filename
75  * @param fname2 other filename
76  * @return true if the filenames match on the current platform
77  */
78  static bool sameFileName(const std::string &fname1, const std::string &fname2);
79 
80  /**
81  * @brief Remove quotation marks (") from the path.
82  * @param path path to be cleaned.
83  * @return Cleaned path without quotation marks.
84  */
85  static std::string removeQuotationMarks(std::string path);
86 
87  /**
88  * @brief Get an extension of the filename.
89  * @param path Path containing filename.
90  * @param lowercase Return the extension in lower case
91  * @return Filename extension (containing the dot, e.g. ".h" or ".CPP").
92  */
93  static std::string getFilenameExtension(const std::string &path, bool lowercase = false);
94 
95  /**
96  * @brief Get an extension of the filename in lower case.
97  * @param path Path containing filename.
98  * @return Filename extension (containing the dot, e.g. ".h").
99  */
100  static std::string getFilenameExtensionInLowerCase(const std::string &path);
101 
102  /**
103  * @brief Returns the absolute path of current working directory
104  * @return absolute path of current working directory
105  */
106  static std::string getCurrentPath();
107 
108  /**
109  * @brief Returns the absolute path to the current executable
110  * @return absolute path to the current executable
111  */
112  static std::string getCurrentExecutablePath(const char* fallback);
113 
114  /**
115  * @brief Check if given path is absolute
116  * @param path Path to check
117  * @return true if given path is absolute
118  */
119  static bool isAbsolute(const std::string& path);
120 
121  /**
122  * @brief Create a relative path from an absolute one, if absolute path is inside the basePaths.
123  * @param absolutePath Path to be made relative.
124  * @param basePaths Paths to which it may be made relative.
125  * @return relative path, if possible. Otherwise absolutePath is returned unchanged
126  */
127  static std::string getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths);
128 
129  /**
130  * @brief Get an absolute file path from a relative one.
131  * @param filePath File path to be made absolute.
132  * @return absolute path, if possible. Otherwise an empty path is returned
133  */
134  static std::string getAbsoluteFilePath(const std::string& filePath);
135 
136  /**
137  * @brief Check if the file extension indicates that it's a C/C++ source file.
138  * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
139  * @param filename filename to check. path info is optional
140  * @return true if the file extension indicates it should be checked
141  */
142  static bool acceptFile(const std::string &filename) {
143  const std::set<std::string> extra;
144  return acceptFile(filename, extra);
145  }
146 
147  /**
148  * @brief Check if the file extension indicates that it's a C/C++ source file.
149  * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
150  * @param path filename to check. path info is optional
151  * @param extra extra file extensions
152  * @return true if the file extension indicates it should be checked
153  */
154  static bool acceptFile(const std::string &path, const std::set<std::string> &extra);
155 
156  /**
157  * @brief Identify language based on file extension.
158  * @param path filename to check. path info is optional
159  * @return true if extension is meant for C files
160  * @deprecated does not account for headers - use @identify() instead
161  */
162  static DEPRECATED bool isC(const std::string &path);
163 
164  /**
165  * @brief Identify language based on file extension.
166  * @param path filename to check. path info is optional
167  * @return true if extension is meant for C++ files
168  * @deprecated returns true for some header extensions - use @identify() instead
169  */
170  static DEPRECATED bool isCPP(const std::string &path);
171 
172  /**
173  * @brief Is filename a header based on file extension
174  * @param path filename to check. path info is optional
175  * @return true if filename extension is meant for headers
176  * @deprecated returns only heuristic result - use @identify() or @isHeader2() instead
177  */
178  static DEPRECATED bool isHeader(const std::string &path);
179 
180  /**
181  * @brief Is filename a header based on file extension
182  * @param path filename to check. path info is optional
183  * @return true if filename extension is meant for headers
184  */
185  static bool isHeader2(const std::string &path);
186 
187  /**
188  * @brief Identify the language based on the file extension
189  * @param path filename to check. path info is optional
190  * @param header if provided indicates if the file is a header
191  * @return the language type
192  */
193  static Standards::Language identify(const std::string &path, bool *header = nullptr);
194 
195  /**
196  * @brief Get filename without a directory path part.
197  * @param file filename to be stripped. path info is optional
198  * @return filename without directory path part.
199  */
200  static std::string stripDirectoryPart(const std::string &file);
201 
202  /**
203  * @brief Checks if given path is a file
204  * @param path Path to be checked
205  * @return true if given path is a file
206  */
207  static bool isFile(const std::string &path);
208 
209  /**
210  * @brief Checks if a given path is a directory
211  * @param path Path to be checked
212  * @return true if given path is a directory
213  */
214  static bool isDirectory(const std::string &path);
215 
216  /**
217  * join 2 paths with '/' separators
218  */
219  static std::string join(const std::string& path1, const std::string& path2);
220 };
221 
222 /// @}
223 //---------------------------------------------------------------------------
224 #endif // pathH
Path handling routines.
Definition: path.h:41
static bool acceptFile(const std::string &filename)
Check if the file extension indicates that it's a C/C++ source file.
Definition: path.h:142
#define CPPCHECKLIB
Definition: config.h:35
#define DEPRECATED
Definition: config.h:121
static std::string join(const std::list< std::string > &strlist, const char *sep)