Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
path.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2016 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 #if defined(__GNUC__) && (defined(_WIN32) || defined(__CYGWIN__))
20 #undef __STRICT_ANSI__
21 #endif
22 #include "path.h"
23 #include <algorithm>
24 #include <vector>
25 #include <sstream>
26 #include <cstring>
27 #include <cctype>
28 #ifndef _WIN32
29 #include <unistd.h>
30 #else
31 #include <direct.h>
32 #endif
33 
34 /** Is the filesystem case insensitive? */
36 {
37 #ifdef _WIN32
38  return true;
39 #else
40  // TODO: Non-windows filesystems might be case insensitive
41  return false;
42 #endif
43 }
44 
45 std::string Path::toNativeSeparators(std::string path)
46 {
47 #if defined(_WIN32)
48  char separ = '/';
49  char native = '\\';
50 #else
51  char separ = '\\';
52  char native = '/';
53 #endif
54  std::replace(path.begin(), path.end(), separ, native);
55  return path;
56 }
57 
58 std::string Path::fromNativeSeparators(std::string path)
59 {
60  char nonnative = '\\';
61  char newsepar = '/';
62  std::replace(path.begin(), path.end(), nonnative, newsepar);
63  return path;
64 }
65 
66 std::string Path::simplifyPath(std::string originalPath)
67 {
68  const bool isUnc = originalPath.compare(0,2,"//") == 0;
69 
70  // Remove ./, .//, ./// etc. at the beginning
71  while (originalPath.compare(0,2,"./") == 0) { // remove "./././"
72  const size_t toErase = originalPath.find_first_not_of('/', 2);
73  originalPath = originalPath.erase(0, toErase);
74  }
75 
76  std::string subPath;
77  std::vector<std::string> pathParts;
78  for (std::size_t i = 0; i < originalPath.size(); ++i) {
79  if (originalPath[i] == '/' || originalPath[i] == '\\') {
80  if (subPath.length() > 0) {
81  pathParts.push_back(subPath);
82  subPath = "";
83  }
84 
85  pathParts.push_back(std::string(1 , originalPath[i]));
86  } else
87  subPath.append(1, originalPath[i]);
88  }
89 
90  if (subPath.length() > 0)
91  pathParts.push_back(subPath);
92 
93  // First filter out all double slashes
94  for (unsigned int i = 1; i < pathParts.size(); ++i) {
95  if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
96  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
97  --i;
98  }
99  }
100 
101  for (unsigned int i = 1; i < pathParts.size(); ++i) {
102  if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) {
103  pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
104  pathParts.erase(pathParts.begin() + static_cast<int>(i));
105  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
106  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 2);
107  i = 0;
108  } else if (i > 0 && pathParts[i] == ".") {
109  pathParts.erase(pathParts.begin() + static_cast<int>(i));
110  i = 0;
111  } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
112  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
113  i = 0;
114  }
115  }
116 
117  if (isUnc) {
118  // Restore the leading double slash
119  pathParts.insert(pathParts.begin(), "/");
120  }
121 
122  std::ostringstream oss;
123  for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) {
124  oss << pathParts[i];
125  }
126 
127  return oss.str();
128 }
129 
130 std::string Path::getPathFromFilename(const std::string &filename)
131 {
132  std::size_t pos = filename.find_last_of("\\/");
133 
134  if (pos != std::string::npos)
135  return filename.substr(0, 1 + pos);
136 
137  return "";
138 }
139 
140 
141 bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
142 {
143 #if defined(__linux__) || defined(__sun) || defined(__hpux)
144  return bool(fname1 == fname2);
145 #elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32))
146  return bool(_stricmp(fname1.c_str(), fname2.c_str()) == 0);
147 #elif defined(__GNUC__)
148  return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
149 #elif defined(__BORLANDC__)
150  return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0);
151 #else
152 #error Platform filename compare function needed
153 #endif
154 }
155 
156 // This wrapper exists because Sun's CC does not allow a static_cast
157 // from extern "C" int(*)(int) to int(*)(int).
158 static int tolowerWrapper(int c)
159 {
160  return std::tolower(c);
161 }
162 
163 std::string Path::removeQuotationMarks(std::string path)
164 {
165  path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
166  return path;
167 }
168 
169 std::string Path::getFilenameExtension(const std::string &path)
170 {
171  const std::string::size_type dotLocation = path.find_last_of('.');
172  if (dotLocation == std::string::npos)
173  return "";
174 
175  std::string extension = path.substr(dotLocation);
177  // on a case insensitive filesystem the case doesn't matter so
178  // let's return the extension in lowercase
179  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
180  }
181  return extension;
182 }
183 
184 std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
185 {
186  std::string extension = getFilenameExtension(path);
187  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
188  return extension;
189 }
190 
191 const std::string Path::getCurrentPath()
192 {
193  char currentPath[4096];
194 
195 #ifndef _WIN32
196  if (getcwd(currentPath, 4096) != 0)
197 #else
198  if (_getcwd(currentPath, 4096) != 0)
199 #endif
200  return std::string(currentPath);
201 
202  return emptyString;
203 }
204 
205 bool Path::isAbsolute(const std::string& path)
206 {
207  const std::string& nativePath = toNativeSeparators(path);
208 
209 #ifdef _WIN32
210  if (path.length() < 2)
211  return false;
212 
213  // On Windows, 'C:\foo\bar' is an absolute path, while 'C:foo\bar' is not
214  if (nativePath.compare(0, 2, "\\\\") == 0 || (std::isalpha(nativePath[0]) != 0 && nativePath.compare(1, 2, ":\\") == 0))
215  return true;
216 #else
217  if (!nativePath.empty() && nativePath[0] == '/')
218  return true;
219 #endif
220 
221  return false;
222 }
223 
224 std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
225 {
226  for (std::vector<std::string>::const_iterator i = basePaths.begin(); i != basePaths.end(); ++i) {
227  if (absolutePath == *i || i->empty()) // Seems to be a file, or path is empty
228  continue;
229 
230  bool endsWithSep = (*i)[i->length()-1] == '/';
231  if (absolutePath.compare(0, i->length(), *i) == 0 && absolutePath[i->length() - (endsWithSep?1:0)] == '/') {
232  std::string rest = absolutePath.substr(i->length() + (endsWithSep?0:1));
233  return rest;
234  }
235  }
236  return absolutePath;
237 }
238 
239 bool Path::isC(const std::string &path)
240 {
241  // In unix, ".C" is considered C++ file
242  const std::string extension = getFilenameExtension(path);
243  return (extension == ".c");
244 }
245 
246 bool Path::isCPP(const std::string &path)
247 {
248  const std::string extension = getFilenameExtensionInLowerCase(path);
249  if (extension == ".cpp" ||
250  extension == ".cxx" ||
251  extension == ".cc" ||
252  extension == ".c++" ||
253  extension == ".hpp" ||
254  extension == ".hxx" ||
255  extension == ".hh" ||
256  extension == ".tpp" ||
257  extension == ".txx") {
258  return true;
259  }
260 
261  // In unix, ".C" is considered C++ file
262  return (getFilenameExtension(path) == ".C");
263 }
264 
265 bool Path::acceptFile(const std::string &path, const std::set<std::string> &extra)
266 {
267  return !Path::isHeader(path) && (Path::isCPP(path) || Path::isC(path) || extra.find(getFilenameExtension(path)) != extra.end());
268 }
269 
270 bool Path::isHeader(const std::string &path)
271 {
272  const std::string extension = getFilenameExtensionInLowerCase(path);
273  return (extension.compare(0, 2, ".h") == 0);
274 }
275 
276 std::string Path::getAbsoluteFilePath(const std::string& filePath)
277 {
278  std::string absolute_path;
279 #ifdef _WIN32
280  char absolute[_MAX_PATH];
281  if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
282  absolute_path = absolute;
283 #elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__)
284  char * absolute = realpath(filePath.c_str(), nullptr);
285  if (absolute)
286  absolute_path = absolute;
287  free(absolute);
288 #else
289 #error Platform absolute path function needed
290 #endif
291  return absolute_path;
292 }
static bool sameFileName(const std::string &fname1, const std::string &fname2)
Compare filenames to see if they are the same.
Definition: path.cpp:141
static const std::string getCurrentPath()
Returns the absolute path of current working directory.
Definition: path.cpp:191
static std::string getPathFromFilename(const std::string &filename)
Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
Definition: path.cpp:130
static bool isAbsolute(const std::string &path)
Check if given path is absolute.
Definition: path.cpp:205
static std::string fromNativeSeparators(std::string path)
Convert path to use internal path separators.
Definition: path.cpp:58
static bool acceptFile(const std::string &filename)
Check if the file extension indicates that it's a C/C++ source file.
Definition: path.h:133
static std::string getAbsoluteFilePath(const std::string &filePath)
Get an absolute file path from a relative one.
Definition: path.cpp:276
static std::string simplifyPath(std::string originalPath)
Simplify path "foo/bar/.." => "foo".
Definition: path.cpp:66
static const std::string emptyString
Definition: config.h:23
static bool isHeader(const std::string &path)
Is filename a header based on file extension.
Definition: path.cpp:270
static bool isC(const std::string &path)
Identify language based on file extension.
Definition: path.cpp:239
static int tolowerWrapper(int c)
Definition: path.cpp:158
static bool caseInsensitiveFilesystem()
Is the filesystem case insensitive?
Definition: path.cpp:35
static bool isCPP(const std::string &extensionInLowerCase)
Identify language based on file extension.
Definition: path.cpp:246
static std::string getFilenameExtensionInLowerCase(const std::string &path)
Get an extension of the filename in lower case.
Definition: path.cpp:184
static std::string getFilenameExtension(const std::string &path)
Get an extension of the filename.
Definition: path.cpp:169
static std::string getRelativePath(const std::string &absolutePath, const std::vector< std::string > &basePaths)
Create a relative path from an absolute one, if absolute path is inside the basePaths.
Definition: path.cpp:224
static std::string removeQuotationMarks(std::string path)
Remove quotation marks (") from the path.
Definition: path.cpp:163
static std::string toNativeSeparators(std::string path)
Convert path to use native separators.
Definition: path.cpp:45