Move simplifyPath method from FileLister to Path class.

simplifyPath() "fits" better to Path class conceptually. It handles
paths, not lists them. Also this way we get rid of few unneeded
dependencies to FileLister class.
This commit is contained in:
Kimmo Varis 2010-10-29 22:21:27 +03:00
parent ca9f8a7036
commit 9627b667a1
9 changed files with 69 additions and 72 deletions

View File

@ -108,7 +108,7 @@ unsigned int CppCheck::check()
if (_settings._errorsOnly == false)
{
std::string fixedpath(fname);
fixedpath = getFileLister()->simplifyPath(fixedpath.c_str());
fixedpath = Path::simplifyPath(fixedpath.c_str());
fixedpath = Path::toNativeSeparators(fixedpath);
_errorLogger.reportOut(std::string("Checking ") + fixedpath + std::string("..."));
}
@ -166,7 +166,7 @@ unsigned int CppCheck::check()
// If only errors are printed, print filename after the check
if (_settings._errorsOnly == false && it != configurations.begin())
{
std::string fixedpath = getFileLister()->simplifyPath(fname.c_str());
std::string fixedpath = Path::simplifyPath(fname.c_str());
fixedpath = Path::toNativeSeparators(fixedpath);
_errorLogger.reportOut(std::string("Checking ") + fixedpath + ": " + cfg + std::string("..."));
}

View File

@ -17,7 +17,6 @@
*/
#include "errorlogger.h"
#include "filelister.h"
#include "path.h"
#include <sstream>
@ -220,7 +219,7 @@ std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMes
std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const
{
std::string f = getFileLister()->simplifyPath(_file.c_str());
std::string f = Path::simplifyPath(_file.c_str());
if (convert)
f = Path::toNativeSeparators(f);

View File

@ -16,8 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <vector>
#include <cstring>
#include <string>
#include <cctype>
@ -48,60 +46,6 @@ FileLister * getFileLister()
return fileLister;
}
std::string FileLister::simplifyPath(const char *originalPath)
{
std::string subPath = "";
std::vector<std::string> pathParts;
for (; *originalPath; ++originalPath)
{
if (*originalPath == '/' || *originalPath == '\\')
{
if (subPath.length() > 0)
{
pathParts.push_back(subPath);
subPath = "";
}
pathParts.push_back(std::string(1 , *originalPath));
}
else
subPath.append(1, *originalPath);
}
if (subPath.length() > 0)
pathParts.push_back(subPath);
for (unsigned int i = 0; i < pathParts.size(); ++i)
{
if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1)
{
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;
}
else if (i > 0 && pathParts[i] == ".")
{
pathParts.erase(pathParts.begin() + static_cast<int>(i));
i = 0;
}
else if (pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/")
{
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
i = 0;
}
}
std::ostringstream oss;
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i)
{
oss << pathParts[i];
}
return oss.str();
}
// 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)

View File

@ -50,13 +50,6 @@ public:
virtual void recursiveAddFiles(std::vector<std::string> &filenames,
const std::string &path, bool recursive) = 0;
/**
* @brief Simplify path "foo/bar/.." => "foo"
* @param originalPath path to be simplified
* @return simplified path
*/
virtual std::string simplifyPath(const char *originalPath);
/**
* @brief Compare filenames to see if they are the same.
* On Linux the comparison is case-sensitive. On Windows it is case-insensitive.

View File

@ -31,7 +31,6 @@ class FileListerUnix : public FileLister
{
public:
virtual void recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
// virtual static std::string simplifyPath(const char *originalPath);
virtual bool sameFileName(const std::string &fname1, const std::string &fname2);
// virtual static bool acceptFile(const std::string &filename);
private:

View File

@ -17,6 +17,8 @@
*/
#include <algorithm>
#include <vector>
#include <sstream>
#include "path.h"
std::string Path::toNativeSeparators(const std::string &path)
@ -42,3 +44,56 @@ std::string Path::fromNativeSeparators(const std::string &path)
return modified;
}
std::string Path::simplifyPath(const char *originalPath)
{
std::string subPath = "";
std::vector<std::string> pathParts;
for (; *originalPath; ++originalPath)
{
if (*originalPath == '/' || *originalPath == '\\')
{
if (subPath.length() > 0)
{
pathParts.push_back(subPath);
subPath = "";
}
pathParts.push_back(std::string(1 , *originalPath));
}
else
subPath.append(1, *originalPath);
}
if (subPath.length() > 0)
pathParts.push_back(subPath);
for (unsigned int i = 0; i < pathParts.size(); ++i)
{
if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1)
{
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;
}
else if (i > 0 && pathParts[i] == ".")
{
pathParts.erase(pathParts.begin() + static_cast<int>(i));
i = 0;
}
else if (pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/")
{
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
i = 0;
}
}
std::ostringstream oss;
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i)
{
oss << pathParts[i];
}
return oss.str();
}

View File

@ -47,6 +47,13 @@ public:
* @return converted path.
*/
static std::string fromNativeSeparators(const std::string &path);
/**
* @brief Simplify path "foo/bar/.." => "foo"
* @param originalPath path to be simplified
* @return simplified path
*/
static std::string simplifyPath(const char *originalPath);
};
/// @}

View File

@ -20,7 +20,6 @@
#include "preprocessor.h"
#include "tokenize.h"
#include "token.h"
#include "filelister.h"
#include "path.h"
#include "errorlogger.h"
#include "settings.h"
@ -1545,7 +1544,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
if (fileOpened)
{
filename = getFileLister()->simplifyPath(filename.c_str());
filename = Path::simplifyPath(filename.c_str());
std::string tempFile = filename;
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
if (handledFiles.find(tempFile) != handledFiles.end())

View File

@ -29,6 +29,7 @@
#include "settings.h"
#include "errorlogger.h"
#include "check.h"
#include "path.h"
#include <locale>
#include <fstream>
@ -306,7 +307,7 @@ void Tokenizer::createTokens(std::istream &code)
if (!foundOurfile)
{
// The "_files" vector remembers what files have been tokenized..
_files.push_back(getFileLister()->simplifyPath(line.c_str()));
_files.push_back(Path::simplifyPath(line.c_str()));
FileIndex = static_cast<unsigned int>(_files.size() - 1);
}
@ -1726,7 +1727,7 @@ bool Tokenizer::tokenize(std::istream &code,
_configuration = configuration;
// The "_files" vector remembers what files have been tokenized..
_files.push_back(getFileLister()->simplifyPath(FileName));
_files.push_back(Path::simplifyPath(FileName));
createTokens(code);