2010-07-17 16:38:36 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 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/>.
|
|
|
|
*/
|
|
|
|
|
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>
|
2010-07-17 16:38:36 +02:00
|
|
|
#include "path.h"
|
|
|
|
|
|
|
|
std::string Path::toNativeSeparators(const std::string &path)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
char separ = '/';
|
|
|
|
char native = '\\';
|
|
|
|
#else
|
|
|
|
char separ = '\\';
|
|
|
|
char native = '/';
|
|
|
|
#endif
|
2010-07-19 12:02:04 +02:00
|
|
|
std::string modified(path);
|
|
|
|
std::replace(modified.begin(), modified.end(), separ, native);
|
|
|
|
return modified;
|
2010-07-17 16:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Path::fromNativeSeparators(const std::string &path)
|
|
|
|
{
|
|
|
|
char nonnative = '\\';
|
2010-07-19 12:02:04 +02:00
|
|
|
char newsepar = '/';
|
|
|
|
std::string modified(path);
|
|
|
|
std::replace(modified.begin(), modified.end(), nonnative, newsepar);
|
|
|
|
return modified;
|
2010-07-17 16:38:36 +02:00
|
|
|
}
|
2010-07-19 13:27:05 +02:00
|
|
|
|
2010-10-29 21:21:27 +02:00
|
|
|
std::string Path::simplifyPath(const char *originalPath)
|
|
|
|
{
|
2011-03-29 19:31:36 +02:00
|
|
|
// Skip ./ at the beginning
|
|
|
|
if (strlen(originalPath) > 2 && originalPath[0] == '.' &&
|
2011-10-13 20:53:06 +02:00
|
|
|
originalPath[1] == '/') {
|
2011-03-29 19:31:36 +02:00
|
|
|
originalPath += 2;
|
|
|
|
}
|
|
|
|
|
2010-10-29 21:21:27 +02:00
|
|
|
std::string subPath = "";
|
|
|
|
std::vector<std::string> pathParts;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (; *originalPath; ++originalPath) {
|
|
|
|
if (*originalPath == '/' || *originalPath == '\\') {
|
|
|
|
if (subPath.length() > 0) {
|
2010-10-29 21:21:27 +02:00
|
|
|
pathParts.push_back(subPath);
|
|
|
|
subPath = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
pathParts.push_back(std::string(1 , *originalPath));
|
2011-10-13 20:53:06 +02:00
|
|
|
} else
|
2010-10-29 21:21:27 +02:00
|
|
|
subPath.append(1, *originalPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subPath.length() > 0)
|
|
|
|
pathParts.push_back(subPath);
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (unsigned int i = 0; i < pathParts.size(); ++i) {
|
|
|
|
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;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (pathParts[i] == "/" && i > 0 && 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
|
|
|
|
|
|
|
bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
|
|
|
|
{
|
|
|
|
#if defined(__linux__) || defined(__sun)
|
|
|
|
return bool(fname1 == fname2);
|
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
|
|
|
#elif defined(_MSC_VER)
|
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
|
|
|
|
|
|
|
std::string Path::removeQuotationMarks(const std::string &path)
|
|
|
|
{
|
|
|
|
std::string editPath(path);
|
2011-03-30 20:46:41 +02:00
|
|
|
editPath.erase(std::remove(editPath.begin(), editPath.end(), '\"'), editPath.end());
|
2011-03-28 21:14:19 +02:00
|
|
|
return editPath;
|
|
|
|
}
|
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 "";
|
|
|
|
|
|
|
|
const std::string extension = path.substr(dotLocation);
|
|
|
|
return extension;
|
|
|
|
}
|