Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-2015 Daniel Marjam√§ki and 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  if (originalPath.size() > 2 && originalPath[0] == '.' &&
67  originalPath[1] == '/') {
68  size_t toErase = 2;
69  for (std::size_t i = 2; i < originalPath.size(); i++) {
70  if (originalPath[i] == '/')
71  toErase++;
72  else
73  break;
74  }
75  originalPath = originalPath.erase(0, toErase);
76  }
77 
78  std::string subPath = "";
79  std::vector<std::string> pathParts;
80  for (std::size_t i = 0; i < originalPath.size(); ++i) {
81  if (originalPath[i] == '/' || originalPath[i] == '\\') {
82  if (subPath.length() > 0) {
83  pathParts.push_back(subPath);
84  subPath = "";
85  }
86 
87  pathParts.push_back(std::string(1 , originalPath[i]));
88  } else
89  subPath.append(1, originalPath[i]);
90  }
91 
92  if (subPath.length() > 0)
93  pathParts.push_back(subPath);
94 
95  // First filter out all double slashes
96  for (unsigned int i = 1; i < pathParts.size(); ++i) {
97  if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
98  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
99  --i;
100  }
101  }
102 
103  for (unsigned int i = 1; i < pathParts.size(); ++i) {
104  if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) {
105  pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
106  pathParts.erase(pathParts.begin() + static_cast<int>(i));
107  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
108  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 2);
109  i = 0;
110  } else if (i > 0 && pathParts[i] == ".") {
111  pathParts.erase(pathParts.begin() + static_cast<int>(i));
112  i = 0;
113  } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
114  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
115  i = 0;
116  }
117  }
118 
119  if (isUnc) {
120  // Restore the leading double slash
121  pathParts.insert(pathParts.begin(), "/");
122  }
123 
124  std::ostringstream oss;
125  for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) {
126  oss << pathParts[i];
127  }
128 
129  return oss.str();
130 }
131 
132 std::string Path::getPathFromFilename(const std::string &filename)
133 {
134  std::size_t pos = filename.find_last_of("\\/");
135 
136  if (pos != std::string::npos)
137  return filename.substr(0, 1 + pos);
138 
139  return "";
140 }
141 
142 
143 bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
144 {
145 #if defined(__linux__) || defined(__sun) || defined(__hpux)
146  return bool(fname1 == fname2);
147 #elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32))
148  return bool(_stricmp(fname1.c_str(), fname2.c_str()) == 0);
149 #elif defined(__GNUC__)
150  return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
151 #elif defined(__BORLANDC__)
152  return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0);
153 #else
154 #error Platform filename compare function needed
155 #endif
156 }
157 
158 // This wrapper exists because Sun's CC does not allow a static_cast
159 // from extern "C" int(*)(int) to int(*)(int).
160 static int tolowerWrapper(int c)
161 {
162  return std::tolower(c);
163 }
164 
165 std::string Path::removeQuotationMarks(std::string path)
166 {
167  path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
168  return path;
169 }
170 
171 std::string Path::getFilenameExtension(const std::string &path)
172 {
173  const std::string::size_type dotLocation = path.find_last_of('.');
174  if (dotLocation == std::string::npos)
175  return "";
176 
177  std::string extension = path.substr(dotLocation);
179  // on a case insensitive filesystem the case doesn't matter so
180  // let's return the extension in lowercase
181  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
182  }
183  return extension;
184 }
185 
186 std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
187 {
188  std::string extension = getFilenameExtension(path);
189  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
190  return extension;
191 }
192 
193 std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
194 {
195  for (std::vector<std::string>::const_iterator i = basePaths.begin(); i != basePaths.end(); ++i) {
196  if (absolutePath == *i || i->empty()) // Seems to be a file, or path is empty
197  continue;
198 
199  bool endsWithSep = (*i)[i->length()-1] == '/';
200  if (absolutePath.compare(0, i->length(), *i) == 0 && absolutePath[i->length() - (endsWithSep?1:0)] == '/') {
201  std::string rest = absolutePath.substr(i->length() + (endsWithSep?0:1));
202  return rest;
203  }
204  }
205  return absolutePath;
206 }
207 
208 bool Path::isC(const std::string &path)
209 {
210  // In unix, ".C" is considered C++ file
211  const std::string extension = getFilenameExtension(path);
212  return (extension == ".c");
213 }
214 
215 bool Path::isCPP(const std::string &path)
216 {
217  const std::string extension = getFilenameExtensionInLowerCase(path);
218  if (extension == ".cpp" ||
219  extension == ".cxx" ||
220  extension == ".cc" ||
221  extension == ".c++" ||
222  extension == ".hpp" ||
223  extension == ".tpp" ||
224  extension == ".txx") {
225  return true;
226  }
227 
228  // In unix, ".C" is considered C++ file
229  return (getFilenameExtension(path) == ".C");
230 }
231 
232 bool Path::acceptFile(const std::string &path, const std::set<std::string> &extra)
233 {
234  return !Path::isHeader(path) && (Path::isCPP(path) || Path::isC(path) || extra.find(getFilenameExtension(path)) != extra.end());
235 }
236 
237 bool Path::isHeader(const std::string &path)
238 {
239  const std::string extension = getFilenameExtensionInLowerCase(path);
240  return (extension.compare(0, 2, ".h") == 0);
241 }
242 
243 std::string Path::getAbsoluteFilePath(const std::string& filePath)
244 {
245  std::string absolute_path;
246 #ifdef _WIN32
247  char absolute[_MAX_PATH];
248  if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
249  absolute_path = absolute;
250 #elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__)
251  char * absolute = realpath(filePath.c_str(), NULL);
252  if (absolute)
253  absolute_path = absolute;
254  free(absolute);
255 #else
256 #error Platform absolute path function needed
257 #endif
258  return absolute_path;
259 }