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-2014 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  // Skip ./ at the beginning
64  if (originalPath.size() > 2 && originalPath[0] == '.' &&
65  originalPath[1] == '/') {
66  originalPath = originalPath.erase(0, 2);
67  }
68 
69  std::string subPath = "";
70  std::vector<std::string> pathParts;
71  for (std::size_t i = 0; i < originalPath.size(); ++i) {
72  if (originalPath[i] == '/' || originalPath[i] == '\\') {
73  if (subPath.length() > 0) {
74  pathParts.push_back(subPath);
75  subPath = "";
76  }
77 
78  pathParts.push_back(std::string(1 , originalPath[i]));
79  } else
80  subPath.append(1, originalPath[i]);
81  }
82 
83  if (subPath.length() > 0)
84  pathParts.push_back(subPath);
85 
86  for (unsigned int i = 1; i < pathParts.size(); ++i) {
87  if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) {
88  pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
89  pathParts.erase(pathParts.begin() + static_cast<int>(i));
90  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
91  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 2);
92  i = 0;
93  } else if (i > 0 && pathParts[i] == ".") {
94  pathParts.erase(pathParts.begin() + static_cast<int>(i));
95  i = 0;
96  } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
97  pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
98  i = 0;
99  }
100  }
101 
102  std::ostringstream oss;
103  for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) {
104  oss << pathParts[i];
105  }
106 
107  return oss.str();
108 }
109 
110 std::string Path::getPathFromFilename(const std::string &filename)
111 {
112  std::string path = "";
113 
114  std::size_t pos = filename.find_last_of("\\/");
115 
116  if (pos != std::string::npos)
117  path = filename.substr(0, 1 + pos);
118 
119  return path;
120 }
121 
122 
123 bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
124 {
125 #if defined(__linux__) || defined(__sun) || defined(__hpux)
126  return bool(fname1 == fname2);
127 #elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32))
128  return bool(_stricmp(fname1.c_str(), fname2.c_str()) == 0);
129 #elif defined(__GNUC__)
130  return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
131 #elif defined(__BORLANDC__)
132  return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0);
133 #else
134 #error Platform filename compare function needed
135 #endif
136 }
137 
138 // This wrapper exists because Sun's CC does not allow a static_cast
139 // from extern "C" int(*)(int) to int(*)(int).
140 static int tolowerWrapper(int c)
141 {
142  return std::tolower(c);
143 }
144 
145 std::string Path::removeQuotationMarks(std::string path)
146 {
147  path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
148  return path;
149 }
150 
151 std::string Path::getFilenameExtension(const std::string &path)
152 {
153  const std::string::size_type dotLocation = path.find_last_of('.');
154  if (dotLocation == std::string::npos)
155  return "";
156 
157  std::string extension = path.substr(dotLocation);
159  // on a case insensitive filesystem the case doesn't matter so
160  // let's return the extension in lowercase
161  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
162  }
163  return extension;
164 }
165 
166 std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
167 {
168  std::string extension = getFilenameExtension(path);
169  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
170  return extension;
171 }
172 
173 std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
174 {
175  for (std::vector<std::string>::const_iterator i = basePaths.begin(); i != basePaths.end(); ++i) {
176  if (absolutePath == *i || i->empty()) // Seems to be a file, or path is empty
177  continue;
178 
179  bool endsWithSep = (*i)[i->length()-1] == '/';
180  if (absolutePath.compare(0, i->length(), *i) == 0 && absolutePath[i->length() - (endsWithSep?1:0)] == '/') {
181  std::string rest = absolutePath.substr(i->length() + (endsWithSep?0:1));
182  return rest;
183  }
184  }
185  return absolutePath;
186 }
187 
188 bool Path::isC(const std::string &path)
189 {
190  // In unix, ".C" is considered C++ file
191  const std::string extension = getFilenameExtension(path);
192  return (extension == ".c");
193 }
194 
195 bool Path::isCPP(const std::string &path)
196 {
197  const std::string extension = getFilenameExtensionInLowerCase(path);
198  if (extension == ".cpp" ||
199  extension == ".cxx" ||
200  extension == ".cc" ||
201  extension == ".c++" ||
202  extension == ".hpp" ||
203  extension == ".tpp" ||
204  extension == ".txx") {
205  return true;
206  }
207 
208  // In unix, ".C" is considered C++ file
209  return (getFilenameExtension(path) == ".C");
210 }
211 
212 bool Path::acceptFile(const std::string &path, const std::set<std::string> &extra)
213 {
214  return !Path::isHeader(path) && (Path::isCPP(path) || Path::isC(path) || extra.find(getFilenameExtension(path)) != extra.end());
215 }
216 
217 bool Path::isHeader(const std::string &path)
218 {
219  const std::string extension = getFilenameExtensionInLowerCase(path);
220  return (extension.compare(0, 2, ".h") == 0);
221 }