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 "utils.h"
24 
25 #include <algorithm>
26 #include <cctype>
27 #include <cstdlib>
28 #include <cstring>
29 #include <sstream>
30 
31 #ifndef _WIN32
32 #include <unistd.h>
33 #else
34 #include <direct.h>
35 #endif
36 #if defined(__CYGWIN__)
37 #include <strings.h>
38 #endif
39 
40 #include <simplecpp.h>
41 
42 /** Is the filesystem case insensitive? */
44 {
45 #ifdef _WIN32
46  return true;
47 #else
48  // TODO: Non-windows filesystems might be case insensitive
49  return false;
50 #endif
51 }
52 
53 std::string Path::toNativeSeparators(std::string path)
54 {
55 #if defined(_WIN32)
56  const char separ = '/';
57  const char native = '\\';
58 #else
59  const char separ = '\\';
60  const char native = '/';
61 #endif
62  std::replace(path.begin(), path.end(), separ, native);
63  return path;
64 }
65 
66 std::string Path::fromNativeSeparators(std::string path)
67 {
68  const char nonnative = '\\';
69  const char newsepar = '/';
70  std::replace(path.begin(), path.end(), nonnative, newsepar);
71  return path;
72 }
73 
74 std::string Path::simplifyPath(std::string originalPath)
75 {
76  return simplecpp::simplifyPath(originalPath);
77 }
78 
79 std::string Path::getPathFromFilename(const std::string &filename)
80 {
81  const std::size_t pos = filename.find_last_of("\\/");
82 
83  if (pos != std::string::npos)
84  return filename.substr(0, 1 + pos);
85 
86  return "";
87 }
88 
89 
90 bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
91 {
92 #if defined(__linux__) || defined(__sun) || defined(__hpux)
93  return (fname1 == fname2);
94 #elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32))
95  return (_stricmp(fname1.c_str(), fname2.c_str()) == 0);
96 #elif defined(__GNUC__)
97  return (strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
98 #elif defined(__BORLANDC__)
99  return (stricmp(fname1.c_str(), fname2.c_str()) == 0);
100 #else
101 #error Platform filename compare function needed
102 #endif
103 }
104 
105 // This wrapper exists because Sun's CC does not allow a static_cast
106 // from extern "C" int(*)(int) to int(*)(int).
107 static int tolowerWrapper(int c)
108 {
109  return std::tolower(c);
110 }
111 
112 std::string Path::removeQuotationMarks(std::string path)
113 {
114  path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
115  return path;
116 }
117 
118 std::string Path::getFilenameExtension(const std::string &path)
119 {
120  const std::string::size_type dotLocation = path.find_last_of('.');
121  if (dotLocation == std::string::npos)
122  return "";
123 
124  std::string extension = path.substr(dotLocation);
126  // on a case insensitive filesystem the case doesn't matter so
127  // let's return the extension in lowercase
128  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
129  }
130  return extension;
131 }
132 
133 std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
134 {
135  std::string extension = getFilenameExtension(path);
136  std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
137  return extension;
138 }
139 
140 const std::string Path::getCurrentPath()
141 {
142  char currentPath[4096];
143 
144 #ifndef _WIN32
145  if (getcwd(currentPath, 4096) != nullptr)
146 #else
147  if (_getcwd(currentPath, 4096) != 0)
148 #endif
149  return std::string(currentPath);
150 
151  return emptyString;
152 }
153 
154 bool Path::isAbsolute(const std::string& path)
155 {
156  const std::string& nativePath = toNativeSeparators(path);
157 
158 #ifdef _WIN32
159  if (path.length() < 2)
160  return false;
161 
162  // On Windows, 'C:\foo\bar' is an absolute path, while 'C:foo\bar' is not
163  if (nativePath.compare(0, 2, "\\\\") == 0 || (std::isalpha(nativePath[0]) != 0 && nativePath.compare(1, 2, ":\\") == 0))
164  return true;
165 #else
166  if (!nativePath.empty() && nativePath[0] == '/')
167  return true;
168 #endif
169 
170  return false;
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 = endsWith(*i,'/');
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  extension == ".cl";
194 }
195 
196 bool Path::isCPP(const std::string &path)
197 {
198  const std::string extension = getFilenameExtensionInLowerCase(path);
199  return extension == ".cpp" ||
200  extension == ".cxx" ||
201  extension == ".cc" ||
202  extension == ".c++" ||
203  extension == ".hpp" ||
204  extension == ".hxx" ||
205  extension == ".hh" ||
206  extension == ".tpp" ||
207  extension == ".txx" ||
208  getFilenameExtension(path) == ".C"; // In unix, ".C" is considered C++ file
209 }
210 
211 bool Path::acceptFile(const std::string &path, const std::set<std::string> &extra)
212 {
213  return !Path::isHeader(path) && (Path::isCPP(path) || Path::isC(path) || extra.find(getFilenameExtension(path)) != extra.end());
214 }
215 
216 bool Path::isHeader(const std::string &path)
217 {
218  const std::string extension = getFilenameExtensionInLowerCase(path);
219  return (extension.compare(0, 2, ".h") == 0);
220 }
221 
222 std::string Path::getAbsoluteFilePath(const std::string& filePath)
223 {
224  std::string absolute_path;
225 #ifdef _WIN32
226  char absolute[_MAX_PATH];
227  if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
228  absolute_path = absolute;
229 #elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__)
230  char * absolute = realpath(filePath.c_str(), nullptr);
231  if (absolute)
232  absolute_path = absolute;
233  free(absolute);
234 #else
235 #error Platform absolute path function needed
236 #endif
237  return absolute_path;
238 }
static bool sameFileName(const std::string &fname1, const std::string &fname2)
Compare filenames to see if they are the same.
Definition: path.cpp:90
static const std::string getCurrentPath()
Returns the absolute path of current working directory.
Definition: path.cpp:140
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:79
static bool isAbsolute(const std::string &path)
Check if given path is absolute.
Definition: path.cpp:154
static std::string fromNativeSeparators(std::string path)
Convert path to use internal path separators.
Definition: path.cpp:66
static bool acceptFile(const std::string &filename)
Check if the file extension indicates that it's a C/C++ source file.
Definition: path.h:134
static std::string getAbsoluteFilePath(const std::string &filePath)
Get an absolute file path from a relative one.
Definition: path.cpp:222
static std::string simplifyPath(std::string originalPath)
Simplify path "foo/bar/.." => "foo".
Definition: path.cpp:74
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:216
static bool isC(const std::string &path)
Identify language based on file extension.
Definition: path.cpp:188
static int tolowerWrapper(int c)
Definition: path.cpp:107
bool endsWith(const std::string &str, char c)
Definition: utils.h:60
static bool caseInsensitiveFilesystem()
Is the filesystem case insensitive?
Definition: path.cpp:43
static std::string getFilenameExtensionInLowerCase(const std::string &path)
Get an extension of the filename in lower case.
Definition: path.cpp:133
static std::string getFilenameExtension(const std::string &path)
Get an extension of the filename.
Definition: path.cpp:118
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:173
static bool isCPP(const std::string &path)
Identify language based on file extension.
Definition: path.cpp:196
static std::string removeQuotationMarks(std::string path)
Remove quotation marks (") from the path.
Definition: path.cpp:112
static std::string toNativeSeparators(std::string path)
Convert path to use native separators.
Definition: path.cpp:53