2010-07-17 16:38:36 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-01-03 12:14:58 +01:00
|
|
|
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
|
2010-07-17 16:38:36 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-03-19 21:20:30 +01:00
|
|
|
#if defined(__GNUC__) && (defined(_WIN32) || defined(__CYGWIN__))
|
2014-01-17 22:29:53 +01:00
|
|
|
#undef __STRICT_ANSI__
|
|
|
|
#endif
|
2014-06-16 15:11:44 +02:00
|
|
|
#include "path.h"
|
2010-07-19 12:02:04 +02:00
|
|
|
#include <algorithm>
|
2010-10-29 21:21:27 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
2011-01-18 17:34:28 +01:00
|
|
|
#include <cstring>
|
2012-01-06 18:11:06 +01:00
|
|
|
#include <cctype>
|
2010-07-17 16:38:36 +02:00
|
|
|
|
2012-01-08 10:34:31 +01:00
|
|
|
/** Is the filesystem case insensitive? */
|
|
|
|
static bool caseInsensitiveFilesystem()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
// TODO: Non-windows filesystems might be case insensitive
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-26 21:02:04 +01:00
|
|
|
std::string Path::toNativeSeparators(std::string path)
|
2010-07-17 16:38:36 +02:00
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
char separ = '/';
|
|
|
|
char native = '\\';
|
|
|
|
#else
|
|
|
|
char separ = '\\';
|
|
|
|
char native = '/';
|
|
|
|
#endif
|
2011-11-26 21:02:04 +01:00
|
|
|
std::replace(path.begin(), path.end(), separ, native);
|
|
|
|
return path;
|
2010-07-17 16:38:36 +02:00
|
|
|
}
|
|
|
|
|
2011-11-26 21:02:04 +01:00
|
|
|
std::string Path::fromNativeSeparators(std::string path)
|
2010-07-17 16:38:36 +02:00
|
|
|
{
|
|
|
|
char nonnative = '\\';
|
2010-07-19 12:02:04 +02:00
|
|
|
char newsepar = '/';
|
2011-11-26 21:02:04 +01:00
|
|
|
std::replace(path.begin(), path.end(), nonnative, newsepar);
|
|
|
|
return path;
|
2010-07-17 16:38:36 +02:00
|
|
|
}
|
2010-07-19 13:27:05 +02:00
|
|
|
|
2014-04-02 13:56:34 +02:00
|
|
|
std::string Path::simplifyPath(std::string originalPath)
|
2010-10-29 21:21:27 +02:00
|
|
|
{
|
2015-02-13 16:32:45 +01:00
|
|
|
// Remove ./, .//, ./// etc. at the beginning
|
2014-04-02 13:56:34 +02:00
|
|
|
if (originalPath.size() > 2 && originalPath[0] == '.' &&
|
2011-10-13 20:53:06 +02:00
|
|
|
originalPath[1] == '/') {
|
2015-02-13 16:32:45 +01:00
|
|
|
size_t toErase = 2;
|
|
|
|
for (std::size_t i = 2; i < originalPath.size(); i++) {
|
|
|
|
if (originalPath[i] == '/')
|
|
|
|
toErase++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
originalPath = originalPath.erase(0, toErase);
|
2011-03-29 19:31:36 +02:00
|
|
|
}
|
|
|
|
|
2010-10-29 21:21:27 +02:00
|
|
|
std::string subPath = "";
|
|
|
|
std::vector<std::string> pathParts;
|
2014-04-02 13:56:34 +02:00
|
|
|
for (std::size_t i = 0; i < originalPath.size(); ++i) {
|
|
|
|
if (originalPath[i] == '/' || originalPath[i] == '\\') {
|
2011-10-13 20:53:06 +02:00
|
|
|
if (subPath.length() > 0) {
|
2010-10-29 21:21:27 +02:00
|
|
|
pathParts.push_back(subPath);
|
|
|
|
subPath = "";
|
|
|
|
}
|
|
|
|
|
2014-04-02 13:56:34 +02:00
|
|
|
pathParts.push_back(std::string(1 , originalPath[i]));
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2014-04-02 13:56:34 +02:00
|
|
|
subPath.append(1, originalPath[i]);
|
2010-10-29 21:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (subPath.length() > 0)
|
|
|
|
pathParts.push_back(subPath);
|
|
|
|
|
2012-02-18 11:55:05 +01:00
|
|
|
for (unsigned int i = 1; i < pathParts.size(); ++i) {
|
2011-10-13 20:53:06 +02:00
|
|
|
if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) {
|
2010-10-29 21:21:27 +02:00
|
|
|
pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
|
|
|
|
pathParts.erase(pathParts.begin() + static_cast<int>(i));
|
|
|
|
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
|
|
|
|
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 2);
|
|
|
|
i = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (i > 0 && pathParts[i] == ".") {
|
2010-10-29 21:21:27 +02:00
|
|
|
pathParts.erase(pathParts.begin() + static_cast<int>(i));
|
|
|
|
i = 0;
|
2015-02-13 16:32:45 +01:00
|
|
|
// Don't touch leading "//" which means a UNC path
|
|
|
|
} else if (i > 1 && pathParts[i] == "/" && pathParts[i-1] == "/") {
|
2010-10-29 21:21:27 +02:00
|
|
|
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) {
|
2010-10-29 21:21:27 +02:00
|
|
|
oss << pathParts[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return oss.str();
|
|
|
|
}
|
2011-01-18 17:34:28 +01:00
|
|
|
|
2012-12-28 10:48:12 +01:00
|
|
|
std::string Path::getPathFromFilename(const std::string &filename)
|
|
|
|
{
|
|
|
|
std::size_t pos = filename.find_last_of("\\/");
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
2014-10-17 17:16:34 +02:00
|
|
|
return filename.substr(0, 1 + pos);
|
2012-12-28 10:48:12 +01:00
|
|
|
|
2014-10-17 17:16:34 +02:00
|
|
|
return "";
|
2012-12-28 10:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 17:34:28 +01:00
|
|
|
bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
|
|
|
|
{
|
2011-10-20 09:01:58 +02:00
|
|
|
#if defined(__linux__) || defined(__sun) || defined(__hpux)
|
2011-01-18 17:34:28 +01:00
|
|
|
return bool(fname1 == fname2);
|
2014-01-17 22:29:53 +01:00
|
|
|
#elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32))
|
|
|
|
return bool(_stricmp(fname1.c_str(), fname2.c_str()) == 0);
|
2011-04-27 11:08:10 +02:00
|
|
|
#elif defined(__GNUC__)
|
2011-01-18 17:34:28 +01:00
|
|
|
return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
|
2011-04-27 11:08:10 +02:00
|
|
|
#elif defined(__BORLANDC__)
|
2011-01-18 17:34:28 +01:00
|
|
|
return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0);
|
2011-04-27 11:08:10 +02:00
|
|
|
#else
|
2011-04-27 18:30:37 +02:00
|
|
|
#error Platform filename compare function needed
|
2011-01-18 17:34:28 +01:00
|
|
|
#endif
|
|
|
|
}
|
2011-03-28 21:14:19 +02:00
|
|
|
|
2012-01-06 20:56:28 +01:00
|
|
|
// This wrapper exists because Sun's CC does not allow a static_cast
|
|
|
|
// from extern "C" int(*)(int) to int(*)(int).
|
|
|
|
static int tolowerWrapper(int c)
|
|
|
|
{
|
|
|
|
return std::tolower(c);
|
|
|
|
}
|
|
|
|
|
2011-11-26 21:02:04 +01:00
|
|
|
std::string Path::removeQuotationMarks(std::string path)
|
2011-03-28 21:14:19 +02:00
|
|
|
{
|
2011-11-26 21:02:04 +01:00
|
|
|
path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
|
|
|
|
return path;
|
2011-03-28 21:14:19 +02:00
|
|
|
}
|
2011-08-06 11:29:51 +02:00
|
|
|
|
|
|
|
std::string Path::getFilenameExtension(const std::string &path)
|
|
|
|
{
|
|
|
|
const std::string::size_type dotLocation = path.find_last_of('.');
|
|
|
|
if (dotLocation == std::string::npos)
|
|
|
|
return "";
|
|
|
|
|
2012-01-08 10:34:31 +01:00
|
|
|
std::string extension = path.substr(dotLocation);
|
|
|
|
if (caseInsensitiveFilesystem()) {
|
|
|
|
// on a case insensitive filesystem the case doesn't matter so
|
|
|
|
// let's return the extension in lowercase
|
|
|
|
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
|
|
|
|
}
|
2011-08-06 11:29:51 +02:00
|
|
|
return extension;
|
|
|
|
}
|
2012-01-06 17:31:10 +01:00
|
|
|
|
2012-01-06 20:56:28 +01:00
|
|
|
std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
|
2012-01-06 17:31:10 +01:00
|
|
|
{
|
2012-01-06 20:56:28 +01:00
|
|
|
std::string extension = getFilenameExtension(path);
|
|
|
|
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
|
|
|
|
return extension;
|
2012-01-06 17:31:10 +01:00
|
|
|
}
|
|
|
|
|
2012-04-06 10:49:21 +02:00
|
|
|
std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
|
|
|
|
{
|
|
|
|
for (std::vector<std::string>::const_iterator i = basePaths.begin(); i != basePaths.end(); ++i) {
|
2012-04-14 14:44:15 +02:00
|
|
|
if (absolutePath == *i || i->empty()) // Seems to be a file, or path is empty
|
2012-04-06 10:49:21 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bool endsWithSep = (*i)[i->length()-1] == '/';
|
|
|
|
if (absolutePath.compare(0, i->length(), *i) == 0 && absolutePath[i->length() - (endsWithSep?1:0)] == '/') {
|
|
|
|
std::string rest = absolutePath.substr(i->length() + (endsWithSep?0:1));
|
|
|
|
return rest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return absolutePath;
|
|
|
|
}
|
|
|
|
|
2012-01-06 20:56:28 +01:00
|
|
|
bool Path::isC(const std::string &path)
|
2012-01-06 17:31:10 +01:00
|
|
|
{
|
2012-05-05 15:21:27 +02:00
|
|
|
// In unix, ".C" is considered C++ file
|
2012-01-07 09:47:15 +01:00
|
|
|
const std::string extension = getFilenameExtension(path);
|
2013-08-07 16:27:37 +02:00
|
|
|
return (extension == ".c");
|
2012-01-06 20:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Path::isCPP(const std::string &path)
|
|
|
|
{
|
|
|
|
const std::string extension = getFilenameExtensionInLowerCase(path);
|
2012-01-06 17:31:10 +01:00
|
|
|
if (extension == ".cpp" ||
|
|
|
|
extension == ".cxx" ||
|
|
|
|
extension == ".cc" ||
|
|
|
|
extension == ".c++" ||
|
2012-11-29 10:32:34 +01:00
|
|
|
extension == ".hpp" ||
|
2012-01-06 17:31:10 +01:00
|
|
|
extension == ".tpp" ||
|
|
|
|
extension == ".txx") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-05 15:21:27 +02:00
|
|
|
// In unix, ".C" is considered C++ file
|
2013-08-07 16:27:37 +02:00
|
|
|
return (getFilenameExtension(path) == ".C");
|
2012-01-06 17:31:10 +01:00
|
|
|
}
|
|
|
|
|
2013-10-31 19:09:01 +01:00
|
|
|
bool Path::acceptFile(const std::string &path, const std::set<std::string> &extra)
|
2012-01-06 20:56:28 +01:00
|
|
|
{
|
2013-10-31 19:09:01 +01:00
|
|
|
return !Path::isHeader(path) && (Path::isCPP(path) || Path::isC(path) || extra.find(getFilenameExtension(path)) != extra.end());
|
2013-03-01 16:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Path::isHeader(const std::string &path)
|
|
|
|
{
|
|
|
|
const std::string extension = getFilenameExtensionInLowerCase(path);
|
|
|
|
return (extension.compare(0, 2, ".h") == 0);
|
2012-01-06 20:56:28 +01:00
|
|
|
}
|
2014-10-19 07:34:40 +02:00
|
|
|
|
|
|
|
std::string Path::getAbsoluteFilePath(const std::string& filePath)
|
|
|
|
{
|
|
|
|
std::string absolute_path;
|
2014-10-21 18:46:09 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
char absolute[_MAX_PATH];
|
|
|
|
if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
|
|
|
|
absolute_path = absolute;
|
|
|
|
#elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__)
|
2014-10-19 07:34:40 +02:00
|
|
|
char * absolute = realpath(filePath.c_str(), NULL);
|
|
|
|
if (absolute)
|
|
|
|
absolute_path = absolute;
|
|
|
|
free(absolute);
|
|
|
|
#else
|
|
|
|
#error Platform absolute path function needed
|
|
|
|
#endif
|
|
|
|
return absolute_path;
|
|
|
|
}
|