"Fossies" - the Fresh Open Source Software Archive

Member "cppcheck-1.89/lib/path.h" (1 Sep 2019, 6600 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 "path.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.86_vs_1.87.

    1 /*
    2  * Cppcheck - A tool for static C/C++ code analysis
    3  * Copyright (C) 2007-2019 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 //---------------------------------------------------------------------------
   20 #ifndef pathH
   21 #define pathH
   22 //---------------------------------------------------------------------------
   23 
   24 #include "config.h"
   25 
   26 #include <set>
   27 #include <string>
   28 #include <vector>
   29 
   30 /// @addtogroup Core
   31 /// @{
   32 
   33 
   34 /**
   35  * @brief Path handling routines.
   36  * Internally cppcheck wants to store paths with / separator which is also
   37  * native separator for Unix-derived systems. When giving path to user
   38  * or for other functions we convert path separators back to native type.
   39  */
   40 class CPPCHECKLIB Path {
   41 public:
   42     /**
   43      * Convert path to use native separators.
   44      * @param path Path string to convert.
   45      * @return converted path.
   46      */
   47     static std::string toNativeSeparators(std::string path);
   48 
   49     /**
   50       * Convert path to use internal path separators.
   51       * @param path Path string to convert.
   52       * @return converted path.
   53       */
   54     static std::string fromNativeSeparators(std::string path);
   55 
   56     /**
   57      * @brief Simplify path "foo/bar/.." => "foo"
   58      * @param originalPath path to be simplified, must have / -separators.
   59      * @return simplified path
   60      */
   61     static std::string simplifyPath(std::string originalPath);
   62 
   63     /**
   64      * @brief Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
   65      * @param filename filename to lookup, must have / -separators.
   66      * @return path part of the filename
   67      */
   68     static std::string getPathFromFilename(const std::string &filename);
   69 
   70     /**
   71      * @brief Compare filenames to see if they are the same.
   72      * On Linux the comparison is case-sensitive. On Windows it is case-insensitive.
   73      * @param fname1 one filename
   74      * @param fname2 other filename
   75      * @return true if the filenames match on the current platform
   76      */
   77     static bool sameFileName(const std::string &fname1, const std::string &fname2);
   78 
   79     /**
   80      * @brief Remove quotation marks (") from the path.
   81      * @param path path to be cleaned.
   82      * @return Cleaned path without quotation marks.
   83      */
   84     static std::string removeQuotationMarks(std::string path);
   85 
   86     /**
   87       * @brief Get an extension of the filename.
   88       * @param path Path containing filename.
   89       * @return Filename extension (containing the dot, e.g. ".h" or ".CPP").
   90       */
   91     static std::string getFilenameExtension(const std::string &path);
   92 
   93     /**
   94       * @brief Get an extension of the filename in lower case.
   95       * @param path Path containing filename.
   96       * @return Filename extension (containing the dot, e.g. ".h").
   97       */
   98     static std::string getFilenameExtensionInLowerCase(const std::string &path);
   99 
  100     /**
  101      * @brief Returns the absolute path of current working directory
  102      * @return absolute path of current working directory
  103      */
  104     static std::string getCurrentPath();
  105 
  106     /**
  107      * @brief Check if given path is absolute
  108      * @param path Path to check
  109      * @return true if given path is absolute
  110      */
  111     static bool isAbsolute(const std::string& path);
  112 
  113     /**
  114       * @brief Create a relative path from an absolute one, if absolute path is inside the basePaths.
  115       * @param absolutePath Path to be made relative.
  116       * @param basePaths Paths to which it may be made relative.
  117       * @return relative path, if possible. Otherwise absolutePath is returned unchanged
  118       */
  119     static std::string getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths);
  120 
  121     /**
  122       * @brief Get an absolute file path from a relative one.
  123       * @param filePath File path to be made absolute.
  124       * @return absolute path, if possible. Otherwise an empty path is returned
  125       */
  126     static std::string getAbsoluteFilePath(const std::string& filePath);
  127 
  128     /**
  129      * @brief Check if the file extension indicates that it's a C/C++ source file.
  130      * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
  131      * @param filename filename to check. path info is optional
  132      * @return true if the file extension indicates it should be checked
  133      */
  134     static bool acceptFile(const std::string &filename) {
  135         const std::set<std::string> extra;
  136         return acceptFile(filename, extra);
  137     }
  138 
  139     /**
  140      * @brief Check if the file extension indicates that it's a C/C++ source file.
  141      * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
  142      * @param path filename to check. path info is optional
  143      * @param extra    extra file extensions
  144      * @return true if the file extension indicates it should be checked
  145      */
  146     static bool acceptFile(const std::string &path, const std::set<std::string> &extra);
  147 
  148     /**
  149      * @brief Identify language based on file extension.
  150      * @param path filename to check. path info is optional
  151      * @return true if extension is meant for C files
  152      */
  153     static bool isC(const std::string &path);
  154 
  155     /**
  156      * @brief Identify language based on file extension.
  157      * @param path filename to check. path info is optional
  158      * @return true if extension is meant for C++ files
  159      */
  160     static bool isCPP(const std::string &path);
  161 
  162     /**
  163      * @brief Is filename a header based on file extension
  164      * @param path filename to check. path info is optional
  165      * @return true if filename extension is meant for headers
  166      */
  167     static bool isHeader(const std::string &path);
  168 
  169     /**
  170      * @brief Get filename without a directory path part.
  171      * @param file filename to be stripped. path info is optional
  172      * @return filename without directory path part.
  173      */
  174     static std::string stripDirectoryPart(const std::string &file);
  175 
  176     static bool fileExists(const std::string &file);
  177 };
  178 
  179 /// @}
  180 //---------------------------------------------------------------------------
  181 #endif // pathH