Blame lib/path.h

Packit 2035a7
/*
Packit 2035a7
 * Cppcheck - A tool for static C/C++ code analysis
Packit 2035a7
 * Copyright (C) 2007-2017 Cppcheck team.
Packit 2035a7
 *
Packit 2035a7
 * This program is free software: you can redistribute it and/or modify
Packit 2035a7
 * it under the terms of the GNU General Public License as published by
Packit 2035a7
 * the Free Software Foundation, either version 3 of the License, or
Packit 2035a7
 * (at your option) any later version.
Packit 2035a7
 *
Packit 2035a7
 * This program is distributed in the hope that it will be useful,
Packit 2035a7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2035a7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 2035a7
 * GNU General Public License for more details.
Packit 2035a7
 *
Packit 2035a7
 * You should have received a copy of the GNU General Public License
Packit 2035a7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 2035a7
 */
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#ifndef pathH
Packit 2035a7
#define pathH
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
Packit 2035a7
#include "config.h"
Packit 2035a7
Packit 2035a7
#include <set>
Packit 2035a7
#include <string>
Packit 2035a7
#include <vector>
Packit 2035a7
Packit 2035a7
/// @addtogroup Core
Packit 2035a7
/// @{
Packit 2035a7
Packit 2035a7
Packit 2035a7
/**
Packit 2035a7
 * @brief Path handling routines.
Packit 2035a7
 * Internally cppcheck wants to store paths with / separator which is also
Packit 2035a7
 * native separator for Unix-derived systems. When giving path to user
Packit 2035a7
 * or for other functions we convert path separators back to native type.
Packit 2035a7
 */
Packit 2035a7
class CPPCHECKLIB Path {
Packit 2035a7
public:
Packit 2035a7
    /**
Packit 2035a7
     * Convert path to use native separators.
Packit 2035a7
     * @param path Path string to convert.
Packit 2035a7
     * @return converted path.
Packit 2035a7
     */
Packit 2035a7
    static std::string toNativeSeparators(std::string path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
      * Convert path to use internal path separators.
Packit 2035a7
      * @param path Path string to convert.
Packit 2035a7
      * @return converted path.
Packit 2035a7
      */
Packit 2035a7
    static std::string fromNativeSeparators(std::string path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Simplify path "foo/bar/.." => "foo"
Packit 2035a7
     * @param originalPath path to be simplified, must have / -separators.
Packit 2035a7
     * @return simplified path
Packit 2035a7
     */
Packit 2035a7
    static std::string simplifyPath(std::string originalPath);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
Packit 2035a7
     * @param filename filename to lookup, must have / -separators.
Packit 2035a7
     * @return path part of the filename
Packit 2035a7
     */
Packit 2035a7
    static std::string getPathFromFilename(const std::string &filename);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Compare filenames to see if they are the same.
Packit 2035a7
     * On Linux the comparison is case-sensitive. On Windows it is case-insensitive.
Packit 2035a7
     * @param fname1 one filename
Packit 2035a7
     * @param fname2 other filename
Packit 2035a7
     * @return true if the filenames match on the current platform
Packit 2035a7
     */
Packit 2035a7
    static bool sameFileName(const std::string &fname1, const std::string &fname2);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Remove quotation marks (") from the path.
Packit 2035a7
     * @param path path to be cleaned.
Packit 2035a7
     * @return Cleaned path without quotation marks.
Packit 2035a7
     */
Packit 2035a7
    static std::string removeQuotationMarks(std::string path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
      * @brief Get an extension of the filename.
Packit 2035a7
      * @param path Path containing filename.
Packit 2035a7
      * @return Filename extension (containing the dot, e.g. ".h" or ".CPP").
Packit 2035a7
      */
Packit 2035a7
    static std::string getFilenameExtension(const std::string &path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
      * @brief Get an extension of the filename in lower case.
Packit 2035a7
      * @param path Path containing filename.
Packit 2035a7
      * @return Filename extension (containing the dot, e.g. ".h").
Packit 2035a7
      */
Packit 2035a7
    static std::string getFilenameExtensionInLowerCase(const std::string &path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Returns the absolute path of current working directory
Packit 2035a7
     * @return absolute path of current working directory
Packit 2035a7
     */
Packit 2035a7
    static std::string getCurrentPath();
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Check if given path is absolute
Packit 2035a7
     * @param path Path to check
Packit 2035a7
     * @return true if given path is absolute
Packit 2035a7
     */
Packit 2035a7
    static bool isAbsolute(const std::string& path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
      * @brief Create a relative path from an absolute one, if absolute path is inside the basePaths.
Packit 2035a7
      * @param absolutePath Path to be made relative.
Packit 2035a7
      * @param basePaths Paths to which it may be made relative.
Packit 2035a7
      * @return relative path, if possible. Otherwise absolutePath is returned unchanged
Packit 2035a7
      */
Packit 2035a7
    static std::string getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
      * @brief Get an absolute file path from a relative one.
Packit 2035a7
      * @param filePath File path to be made absolute.
Packit 2035a7
      * @return absolute path, if possible. Otherwise an empty path is returned
Packit 2035a7
      */
Packit 2035a7
    static std::string getAbsoluteFilePath(const std::string& filePath);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Check if the file extension indicates that it's a C/C++ source file.
Packit 2035a7
     * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
Packit 2035a7
     * @param filename filename to check. path info is optional
Packit 2035a7
     * @return true if the file extension indicates it should be checked
Packit 2035a7
     */
Packit 2035a7
    static bool acceptFile(const std::string &filename) {
Packit 2035a7
        const std::set<std::string> extra;
Packit 2035a7
        return acceptFile(filename, extra);
Packit 2035a7
    }
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Check if the file extension indicates that it's a C/C++ source file.
Packit 2035a7
     * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
Packit 2035a7
     * @param path filename to check. path info is optional
Packit 2035a7
     * @param extra    extra file extensions
Packit 2035a7
     * @return true if the file extension indicates it should be checked
Packit 2035a7
     */
Packit 2035a7
    static bool acceptFile(const std::string &path, const std::set<std::string> &extra);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Identify language based on file extension.
Packit 2035a7
     * @param path filename to check. path info is optional
Packit 2035a7
     * @return true if extension is meant for C files
Packit 2035a7
     */
Packit 2035a7
    static bool isC(const std::string &path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Identify language based on file extension.
Packit 2035a7
     * @param path filename to check. path info is optional
Packit 2035a7
     * @return true if extension is meant for C++ files
Packit 2035a7
     */
Packit 2035a7
    static bool isCPP(const std::string &path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Is filename a header based on file extension
Packit 2035a7
     * @param path filename to check. path info is optional
Packit 2035a7
     * @return true if filename extension is meant for headers
Packit 2035a7
     */
Packit 2035a7
    static bool isHeader(const std::string &path);
Packit 2035a7
Packit 2035a7
    /**
Packit 2035a7
     * @brief Get filename without a directory path part.
Packit 2035a7
     * @param file filename to be stripped. path info is optional
Packit 2035a7
     * @return filename without directory path part.
Packit 2035a7
     */
Packit 2035a7
    static std::string stripDirectoryPart(const std::string &file;;
Packit 2035a7
};
Packit 2035a7
Packit 2035a7
/// @}
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
#endif // pathH