2010-07-17 16:38:36 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 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
|
2022-09-16 07:15:49 +02:00
|
|
|
|
2014-06-16 15:11:44 +02:00
|
|
|
#include "path.h"
|
2017-04-01 18:14:18 +02:00
|
|
|
#include "utils.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2010-07-19 12:02:04 +02:00
|
|
|
#include <algorithm>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cstdlib>
|
2023-08-04 17:38:43 +02:00
|
|
|
#include <sys/stat.h>
|
2022-09-16 07:15:49 +02:00
|
|
|
#include <utility>
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2023-05-26 06:55:36 +02:00
|
|
|
#include <simplecpp.h>
|
|
|
|
|
2016-10-02 12:06:55 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
2016-10-02 15:48:03 +02:00
|
|
|
#else
|
|
|
|
#include <direct.h>
|
2023-05-26 06:55:36 +02:00
|
|
|
#include <windows.h>
|
2016-10-02 12:06:55 +02:00
|
|
|
#endif
|
2017-05-27 10:23:06 +02:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2023-05-26 06:55:36 +02:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
2010-07-17 16:38:36 +02:00
|
|
|
|
2017-06-21 14:27:46 +02:00
|
|
|
|
2012-01-08 10:34:31 +01:00
|
|
|
/** Is the filesystem case insensitive? */
|
2023-09-08 17:33:37 +02:00
|
|
|
static constexpr bool caseInsensitiveFilesystem()
|
2012-01-08 10:34:31 +01:00
|
|
|
{
|
2023-09-08 17:33:37 +02:00
|
|
|
#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
// Windows is case insensitive
|
|
|
|
// MacOS is case insensitive by default (also supports case sensitivity)
|
2012-01-08 10:34:31 +01:00
|
|
|
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)
|
2017-05-12 16:20:47 +02:00
|
|
|
const char separ = '/';
|
|
|
|
const char native = '\\';
|
2010-07-17 16:38:36 +02:00
|
|
|
#else
|
2017-05-12 16:20:47 +02:00
|
|
|
const char separ = '\\';
|
|
|
|
const char native = '/';
|
2010-07-17 16:38:36 +02:00
|
|
|
#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
|
|
|
{
|
2017-05-12 16:20:47 +02:00
|
|
|
const char nonnative = '\\';
|
|
|
|
const 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
|
|
|
{
|
2022-05-08 20:42:06 +02:00
|
|
|
return simplecpp::simplifyPath(std::move(originalPath));
|
2010-10-29 21:21:27 +02:00
|
|
|
}
|
2011-01-18 17:34:28 +01:00
|
|
|
|
2012-12-28 10:48:12 +01:00
|
|
|
std::string Path::getPathFromFilename(const std::string &filename)
|
|
|
|
{
|
2017-05-12 16:20:47 +02:00
|
|
|
const std::size_t pos = filename.find_last_of("\\/");
|
2012-12-28 10:48:12 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-09-30 11:25:46 +02:00
|
|
|
return caseInsensitiveFilesystem() ? (caseInsensitiveStringCompare(fname1, fname2) == 0) : (fname1 == fname2);
|
2011-01-18 17:34:28 +01:00
|
|
|
}
|
2011-03-28 21:14:19 +02:00
|
|
|
|
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
|
|
|
|
2022-10-06 20:12:07 +02:00
|
|
|
std::string Path::getFilenameExtension(const std::string &path, bool lowercase)
|
2011-08-06 11:29:51 +02:00
|
|
|
{
|
|
|
|
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);
|
2022-10-06 20:12:07 +02:00
|
|
|
if (lowercase || caseInsensitiveFilesystem()) {
|
2012-01-08 10:34:31 +01:00
|
|
|
// on a case insensitive filesystem the case doesn't matter so
|
|
|
|
// let's return the extension in lowercase
|
2022-04-15 16:17:36 +02:00
|
|
|
strTolower(extension);
|
2012-01-08 10:34:31 +01:00
|
|
|
}
|
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
|
|
|
{
|
2022-10-06 20:12:07 +02:00
|
|
|
return getFilenameExtension(path, true);
|
2012-01-06 17:31:10 +01:00
|
|
|
}
|
|
|
|
|
2017-10-03 18:24:18 +02:00
|
|
|
std::string Path::getCurrentPath()
|
2016-10-02 12:06:55 +02:00
|
|
|
{
|
|
|
|
char currentPath[4096];
|
|
|
|
|
2016-10-02 15:48:03 +02:00
|
|
|
#ifndef _WIN32
|
2017-08-09 20:00:26 +02:00
|
|
|
if (getcwd(currentPath, 4096) != nullptr)
|
2016-10-02 15:48:03 +02:00
|
|
|
#else
|
2019-09-25 15:25:19 +02:00
|
|
|
if (_getcwd(currentPath, 4096) != nullptr)
|
2016-10-02 15:48:03 +02:00
|
|
|
#endif
|
2016-10-02 12:06:55 +02:00
|
|
|
return std::string(currentPath);
|
|
|
|
|
2022-07-10 10:57:29 +02:00
|
|
|
return "";
|
2016-10-02 12:06:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 06:55:36 +02:00
|
|
|
std::string Path::getCurrentExecutablePath(const char* fallback)
|
|
|
|
{
|
|
|
|
char buf[4096] = {};
|
|
|
|
bool success{};
|
|
|
|
#ifdef _WIN32
|
|
|
|
success = (GetModuleFileNameA(nullptr, buf, sizeof(buf)) < sizeof(buf));
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
uint32_t size = sizeof(buf);
|
|
|
|
success = (_NSGetExecutablePath(buf, &size) == 0);
|
|
|
|
#else
|
|
|
|
const char* procPath =
|
|
|
|
#ifdef __SVR4 // Solaris
|
|
|
|
"/proc/self/path/a.out";
|
|
|
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
|
|
|
"/proc/curproc/file";
|
|
|
|
#else // Linux
|
|
|
|
"/proc/self/exe";
|
|
|
|
#endif
|
|
|
|
success = (readlink(procPath, buf, sizeof(buf)) != -1);
|
|
|
|
#endif
|
|
|
|
return success ? std::string(buf) : std::string(fallback);
|
|
|
|
}
|
|
|
|
|
2016-10-02 12:06:55 +02:00
|
|
|
bool Path::isAbsolute(const std::string& path)
|
|
|
|
{
|
|
|
|
const std::string& nativePath = toNativeSeparators(path);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (path.length() < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// On Windows, 'C:\foo\bar' is an absolute path, while 'C:foo\bar' is not
|
2023-09-08 19:30:25 +02:00
|
|
|
return startsWith(nativePath, "\\\\") || (std::isalpha(nativePath[0]) != 0 && nativePath.compare(1, 2, ":\\") == 0);
|
2016-10-02 12:06:55 +02:00
|
|
|
#else
|
2023-03-17 13:51:55 +01:00
|
|
|
return !nativePath.empty() && nativePath[0] == '/';
|
2016-10-02 12:06:55 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-02-24 06:21:02 +01:00
|
|
|
std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
|
2012-04-06 10:49:21 +02:00
|
|
|
{
|
2023-02-24 06:21:02 +01:00
|
|
|
for (const std::string &bp : basePaths) {
|
2018-04-18 22:23:38 +02:00
|
|
|
if (absolutePath == bp || bp.empty()) // Seems to be a file, or path is empty
|
2012-04-06 10:49:21 +02:00
|
|
|
continue;
|
|
|
|
|
2018-04-18 22:23:38 +02:00
|
|
|
if (absolutePath.compare(0, bp.length(), bp) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (endsWith(bp,'/'))
|
|
|
|
return absolutePath.substr(bp.length());
|
2023-06-20 18:43:21 +02:00
|
|
|
if (absolutePath.size() > bp.size() && absolutePath[bp.length()] == '/')
|
2018-04-18 22:23:38 +02:00
|
|
|
return absolutePath.substr(bp.length() + 1);
|
2012-04-06 10:49:21 +02:00
|
|
|
}
|
|
|
|
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);
|
2017-06-23 02:34:07 +02:00
|
|
|
return extension == ".c" ||
|
|
|
|
extension == ".cl";
|
2012-01-06 20:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Path::isCPP(const std::string &path)
|
|
|
|
{
|
|
|
|
const std::string extension = getFilenameExtensionInLowerCase(path);
|
2017-06-01 01:02:12 +02:00
|
|
|
return extension == ".cpp" ||
|
2017-06-03 15:30:36 +02:00
|
|
|
extension == ".cxx" ||
|
|
|
|
extension == ".cc" ||
|
|
|
|
extension == ".c++" ||
|
|
|
|
extension == ".hpp" ||
|
|
|
|
extension == ".hxx" ||
|
|
|
|
extension == ".hh" ||
|
|
|
|
extension == ".tpp" ||
|
|
|
|
extension == ".txx" ||
|
2021-08-09 10:46:56 +02:00
|
|
|
extension == ".ipp" ||
|
|
|
|
extension == ".ixx" ||
|
2017-06-03 15:30:36 +02:00
|
|
|
getFilenameExtension(path) == ".C"; // In unix, ".C" is considered C++ file
|
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);
|
2023-09-08 19:30:25 +02:00
|
|
|
return startsWith(extension, ".h");
|
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;
|
2019-04-30 20:31:46 +02:00
|
|
|
#elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__) || defined(__CPPCHECK__)
|
2015-11-30 22:13:49 +01:00
|
|
|
char * absolute = realpath(filePath.c_str(), nullptr);
|
2023-02-24 06:21:02 +01:00
|
|
|
if (absolute)
|
|
|
|
absolute_path = absolute;
|
2014-10-19 07:34:40 +02:00
|
|
|
free(absolute);
|
|
|
|
#else
|
|
|
|
#error Platform absolute path function needed
|
|
|
|
#endif
|
|
|
|
return absolute_path;
|
|
|
|
}
|
2017-10-05 23:03:13 +02:00
|
|
|
|
2023-02-24 06:21:02 +01:00
|
|
|
std::string Path::stripDirectoryPart(const std::string &file)
|
2017-10-05 23:03:13 +02:00
|
|
|
{
|
2023-02-24 06:21:02 +01:00
|
|
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
|
|
|
const char native = '\\';
|
|
|
|
#else
|
|
|
|
const char native = '/';
|
|
|
|
#endif
|
2017-10-05 23:03:13 +02:00
|
|
|
|
2023-02-24 06:21:02 +01:00
|
|
|
const std::string::size_type p = file.rfind(native);
|
2017-10-05 23:03:13 +02:00
|
|
|
if (p != std::string::npos) {
|
2017-10-06 22:45:37 +02:00
|
|
|
return file.substr(p + 1);
|
2017-10-05 23:03:13 +02:00
|
|
|
}
|
2017-10-06 22:45:37 +02:00
|
|
|
return file;
|
2017-10-05 23:03:13 +02:00
|
|
|
}
|
2019-01-05 23:11:43 +01:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
using mode_t = unsigned short;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static mode_t file_type(const std::string &path)
|
2019-01-05 23:11:43 +01:00
|
|
|
{
|
2023-08-23 11:22:41 +02:00
|
|
|
struct stat file_stat;
|
|
|
|
if (stat(path.c_str(), &file_stat) == -1)
|
|
|
|
return 0;
|
|
|
|
return file_stat.st_mode & S_IFMT;
|
2019-01-05 23:11:43 +01:00
|
|
|
}
|
2022-07-12 22:58:52 +02:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
bool Path::isFile(const std::string &path)
|
|
|
|
{
|
|
|
|
return file_type(path) == S_IFREG;
|
|
|
|
}
|
2023-08-04 17:38:43 +02:00
|
|
|
|
2023-08-23 11:22:41 +02:00
|
|
|
bool Path::isDirectory(const std::string &path)
|
2023-08-04 17:38:43 +02:00
|
|
|
{
|
2023-08-23 11:22:41 +02:00
|
|
|
return file_type(path) == S_IFDIR;
|
2023-08-04 17:38:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 22:58:52 +02:00
|
|
|
std::string Path::join(std::string path1, std::string path2) {
|
|
|
|
if (path1.empty() || path2.empty())
|
|
|
|
return path1 + path2;
|
|
|
|
if (path2.front() == '/')
|
|
|
|
return path2;
|
|
|
|
return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2;
|
|
|
|
}
|