From 9627b667a167fb39e8b9db81dfc109f1c696a782 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Fri, 29 Oct 2010 22:21:27 +0300 Subject: [PATCH] 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. --- lib/cppcheck.cpp | 4 ++-- lib/errorlogger.cpp | 3 +-- lib/filelister.cpp | 56 ------------------------------------------- lib/filelister.h | 7 ------ lib/filelister_unix.h | 1 - lib/path.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++ lib/path.h | 7 ++++++ lib/preprocessor.cpp | 3 +-- lib/tokenize.cpp | 5 ++-- 9 files changed, 69 insertions(+), 72 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 77c702785..402c37a65 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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("...")); } diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index bf9d2adf8..99251da1d 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -17,7 +17,6 @@ */ #include "errorlogger.h" -#include "filelister.h" #include "path.h" #include @@ -220,7 +219,7 @@ std::string ErrorLogger::callStackToString(const std::listsimplifyPath(_file.c_str()); + std::string f = Path::simplifyPath(_file.c_str()); if (convert) f = Path::toNativeSeparators(f); diff --git a/lib/filelister.cpp b/lib/filelister.cpp index 7e34dec81..4e7212a96 100644 --- a/lib/filelister.cpp +++ b/lib/filelister.cpp @@ -16,8 +16,6 @@ * along with this program. If not, see . */ -#include -#include #include #include #include @@ -48,60 +46,6 @@ FileLister * getFileLister() return fileLister; } -std::string FileLister::simplifyPath(const char *originalPath) -{ - std::string subPath = ""; - std::vector 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(i) + 1); - pathParts.erase(pathParts.begin() + static_cast(i)); - pathParts.erase(pathParts.begin() + static_cast(i) - 1); - pathParts.erase(pathParts.begin() + static_cast(i) - 2); - i = 0; - } - else if (i > 0 && pathParts[i] == ".") - { - pathParts.erase(pathParts.begin() + static_cast(i)); - i = 0; - } - else if (pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/") - { - pathParts.erase(pathParts.begin() + static_cast(i) - 1); - i = 0; - } - } - - std::ostringstream oss; - for (std::vector::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) diff --git a/lib/filelister.h b/lib/filelister.h index db6cdfff5..86924b13f 100644 --- a/lib/filelister.h +++ b/lib/filelister.h @@ -50,13 +50,6 @@ public: virtual void recursiveAddFiles(std::vector &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. diff --git a/lib/filelister_unix.h b/lib/filelister_unix.h index 2bbe3a33c..3550d9f80 100644 --- a/lib/filelister_unix.h +++ b/lib/filelister_unix.h @@ -31,7 +31,6 @@ class FileListerUnix : public FileLister { public: virtual void recursiveAddFiles(std::vector &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: diff --git a/lib/path.cpp b/lib/path.cpp index 2f3374b3f..a0eceb852 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -17,6 +17,8 @@ */ #include +#include +#include #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 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(i) + 1); + pathParts.erase(pathParts.begin() + static_cast(i)); + pathParts.erase(pathParts.begin() + static_cast(i) - 1); + pathParts.erase(pathParts.begin() + static_cast(i) - 2); + i = 0; + } + else if (i > 0 && pathParts[i] == ".") + { + pathParts.erase(pathParts.begin() + static_cast(i)); + i = 0; + } + else if (pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/") + { + pathParts.erase(pathParts.begin() + static_cast(i) - 1); + i = 0; + } + } + + std::ostringstream oss; + for (std::vector::size_type i = 0; i < pathParts.size(); ++i) + { + oss << pathParts[i]; + } + + return oss.str(); +} diff --git a/lib/path.h b/lib/path.h index f38213112..0c0c6ae04 100644 --- a/lib/path.h +++ b/lib/path.h @@ -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); }; /// @} diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 9abf974b1..87bda0ce6 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -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()) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1ea17ccc9..f3de99082 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -29,6 +29,7 @@ #include "settings.h" #include "errorlogger.h" #include "check.h" +#include "path.h" #include #include @@ -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(_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);