Cppcheck
path.h
Go to the documentation of this file.
00001 /*
00002  * Cppcheck - A tool for static C/C++ code analysis
00003  * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #ifndef PATH_H_INCLUDED
00020 #define PATH_H_INCLUDED
00021 
00022 #include <string>
00023 #include <vector>
00024 #include "config.h"
00025 
00026 /// @addtogroup Core
00027 /// @{
00028 
00029 
00030 /**
00031  * @brief Path handling routines.
00032  * Internally cppcheck wants to store paths with / separator which is also
00033  * native separator for Unix-derived systems. When giving path to user
00034  * or for other functions we convert path separators back to native type.
00035  */
00036 class CPPCHECKLIB Path {
00037 public:
00038     /**
00039      * Convert path to use native separators.
00040      * @param path Path string to convert.
00041      * @return converted path.
00042      */
00043     static std::string toNativeSeparators(std::string path);
00044 
00045     /**
00046       * Convert path to use internal path separators.
00047       * @param path Path string to convert.
00048       * @return converted path.
00049       */
00050     static std::string fromNativeSeparators(std::string path);
00051 
00052     /**
00053      * @brief Simplify path "foo/bar/.." => "foo"
00054      * @param originalPath path to be simplified, must have / -separators.
00055      * @return simplified path
00056      */
00057     static std::string simplifyPath(const char *originalPath);
00058 
00059     /**
00060      * @brief Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
00061      * @param filename filename to lookup, must have / -separators.
00062      * @return path part of the filename
00063      */
00064     static std::string getPathFromFilename(const std::string &filename);
00065 
00066     /**
00067      * @brief Compare filenames to see if they are the same.
00068      * On Linux the comparison is case-sensitive. On Windows it is case-insensitive.
00069      * @param fname1 one filename
00070      * @param fname2 other filename
00071      * @return true if the filenames match on the current platform
00072      */
00073     static bool sameFileName(const std::string &fname1, const std::string &fname2);
00074 
00075     /**
00076      * @brief Remove quotation marks (") from the path.
00077      * @param path path to be cleaned.
00078      * @return Cleaned path without quotation marks.
00079      */
00080     static std::string removeQuotationMarks(std::string path);
00081 
00082     /**
00083       * @brief Get an extension of the filename.
00084       * @param path Path containing filename.
00085       * @return Filename extension (containing the dot, e.g. ".h" or ".CPP").
00086       */
00087     static std::string getFilenameExtension(const std::string &path);
00088 
00089     /**
00090       * @brief Get an extension of the filename in lower case.
00091       * @param path Path containing filename.
00092       * @return Filename extension (containing the dot, e.g. ".h").
00093       */
00094     static std::string getFilenameExtensionInLowerCase(const std::string &path);
00095 
00096     /**
00097       * @brief Create a relative path from an absolute one, if absolute path is inside the basePaths.
00098       * @param absolutePath Path to be made relative.
00099       * @param basePaths Paths to which it may be made relative.
00100       * @return relative path, if possible. Otherwise absolutePath is returned unchanged
00101       */
00102     static std::string getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths);
00103 
00104     /**
00105      * @brief Check if the file extension indicates that it's a C/C++ source file.
00106      * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
00107      * @param path filename to check. path info is optional
00108      * @return returns true if the file extension indicates it should be checked
00109      */
00110     static bool acceptFile(const std::string &filename);
00111 
00112     /**
00113      * @brief Identify language based on file extension.
00114      * @param path filename to check. path info is optional
00115      * @return true if extension is meant for C files
00116      */
00117     static bool isC(const std::string &path);
00118 
00119     /**
00120      * @brief Identify language based on file extension.
00121      * @param path filename to check. path info is optional
00122      * @return true if extension is meant for C++ files
00123      */
00124     static bool isCPP(const std::string &extensionInLowerCase);
00125 
00126 private:
00127     /**
00128      * @brief Is filename a header based on file extension
00129      * @param path filename to check. path info is optional
00130      * @return true if filename extension is meant for headers
00131      */
00132     static bool isHeader(const std::string &path);
00133 };
00134 
00135 /// @}
00136 
00137 #endif // PATH_H_INCLUDED