"Fossies" - the Fresh Open Source Software Archive

Member "cppcheck-1.89/lib/pathmatch.cpp" (1 Sep 2019, 3254 Bytes) of package /windows/misc/cppcheck-1.89.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pathmatch.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.84_vs_1.85.

    1 /*
    2  * Cppcheck - A tool for static C/C++ code analysis
    3  * Copyright (C) 2007-2018 Cppcheck team.
    4  *
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 
   19 #include "pathmatch.h"
   20 
   21 #include "path.h"
   22 #include "utils.h"
   23 
   24 #include <algorithm>
   25 #include <cctype>
   26 #include <cstddef>
   27 
   28 PathMatch::PathMatch(const std::vector<std::string> &excludedPaths, bool caseSensitive)
   29     : mExcludedPaths(excludedPaths), mCaseSensitive(caseSensitive)
   30 {
   31     if (!mCaseSensitive)
   32         for (std::vector<std::string>::iterator i = mExcludedPaths.begin(); i != mExcludedPaths.end(); ++i)
   33             std::transform(i->begin(), i->end(), i->begin(), ::tolower);
   34     mWorkingDirectory.push_back(Path::getCurrentPath());
   35 }
   36 
   37 bool PathMatch::match(const std::string &path) const
   38 {
   39     if (path.empty())
   40         return false;
   41 
   42     for (std::vector<std::string>::const_iterator i = mExcludedPaths.begin(); i != mExcludedPaths.end(); ++i) {
   43         const std::string excludedPath((!Path::isAbsolute(path) && Path::isAbsolute(*i)) ? Path::getRelativePath(*i, mWorkingDirectory) : *i);
   44 
   45         std::string findpath = Path::fromNativeSeparators(path);
   46         if (!mCaseSensitive)
   47             std::transform(findpath.begin(), findpath.end(), findpath.begin(), ::tolower);
   48 
   49         // Filtering directory name
   50         if (endsWith(excludedPath,'/')) {
   51             if (!endsWith(findpath,'/'))
   52                 findpath = removeFilename(findpath);
   53 
   54             if (excludedPath.length() > findpath.length())
   55                 continue;
   56             // Match relative paths starting with mask
   57             // -isrc matches src/foo.cpp
   58             if (findpath.compare(0, excludedPath.size(), excludedPath) == 0)
   59                 return true;
   60             // Match only full directory name in middle or end of the path
   61             // -isrc matches myproject/src/ but does not match
   62             // myproject/srcfiles/ or myproject/mysrc/
   63             if (findpath.find("/" + excludedPath) != std::string::npos)
   64                 return true;
   65         }
   66         // Filtering filename
   67         else {
   68             if (excludedPath.length() > findpath.length())
   69                 continue;
   70             // Check if path ends with mask
   71             // -ifoo.cpp matches (./)foo.c, src/foo.cpp and proj/src/foo.cpp
   72             // -isrc/file.cpp matches src/foo.cpp and proj/src/foo.cpp
   73             if (findpath.compare(findpath.size() - excludedPath.size(), findpath.size(), excludedPath) == 0)
   74                 return true;
   75 
   76         }
   77     }
   78     return false;
   79 }
   80 
   81 std::string PathMatch::removeFilename(const std::string &path)
   82 {
   83     const std::size_t ind = path.find_last_of('/');
   84     return path.substr(0, ind + 1);
   85 }