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