Cppcheck
Static Public Member Functions | List of all members
Path Class Reference

Path handling routines. More...

#include <path.h>

Static Public Member Functions

static std::string toNativeSeparators (std::string path)
 Convert path to use native separators. More...
 
static std::string fromNativeSeparators (std::string path)
 Convert path to use internal path separators. More...
 
static std::string simplifyPath (std::string originalPath)
 Simplify path "foo/bar/.." => "foo". More...
 
static std::string getPathFromFilename (const std::string &filename)
 Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '') More...
 
static bool sameFileName (const std::string &fname1, const std::string &fname2)
 Compare filenames to see if they are the same. More...
 
static std::string removeQuotationMarks (std::string path)
 Remove quotation marks (") from the path. More...
 
static std::string getFilenameExtension (const std::string &path, bool lowercase=false)
 Get an extension of the filename. More...
 
static std::string getFilenameExtensionInLowerCase (const std::string &path)
 Get an extension of the filename in lower case. More...
 
static std::string getCurrentPath ()
 Returns the absolute path of current working directory. More...
 
static std::string getCurrentExecutablePath (const char *fallback)
 Returns the absolute path to the current executable. More...
 
static bool isAbsolute (const std::string &path)
 Check if given path is absolute. More...
 
static std::string getRelativePath (const std::string &absolutePath, const std::vector< std::string > &basePaths)
 Create a relative path from an absolute one, if absolute path is inside the basePaths. More...
 
static std::string getAbsoluteFilePath (const std::string &filePath)
 Get an absolute file path from a relative one. More...
 
static bool acceptFile (const std::string &filename)
 Check if the file extension indicates that it's a C/C++ source file. More...
 
static bool acceptFile (const std::string &path, const std::set< std::string > &extra)
 Check if the file extension indicates that it's a C/C++ source file. More...
 
static DEPRECATED bool isC (const std::string &path)
 Identify language based on file extension. More...
 
static DEPRECATED bool isCPP (const std::string &path)
 Identify language based on file extension. More...
 
static DEPRECATED bool isHeader (const std::string &path)
 Is filename a header based on file extension. More...
 
static bool isHeader2 (const std::string &path)
 Is filename a header based on file extension. More...
 
static Standards::Language identify (const std::string &path, bool *header=nullptr)
 Identify the language based on the file extension. More...
 
static std::string stripDirectoryPart (const std::string &file)
 Get filename without a directory path part. More...
 
static bool isFile (const std::string &path)
 Checks if given path is a file. More...
 
static bool isDirectory (const std::string &path)
 Checks if a given path is a directory. More...
 
static std::string join (const std::string &path1, const std::string &path2)
 join 2 paths with '/' separators More...
 

Detailed Description

Path handling routines.

Internally cppcheck wants to store paths with / separator which is also native separator for Unix-derived systems. When giving path to user or for other functions we convert path separators back to native type.

Definition at line 41 of file path.h.

Member Function Documentation

◆ acceptFile() [1/2]

static bool Path::acceptFile ( const std::string &  filename)
inlinestatic

Check if the file extension indicates that it's a C/C++ source file.

Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx

Parameters
filenamefilename to check. path info is optional
Returns
true if the file extension indicates it should be checked

Definition at line 142 of file path.h.

Referenced by addFiles2(), ImportProject::importBcb6Prj(), ImportProject::importCompileCommands(), and ImportProject::importVcxproj().

◆ acceptFile() [2/2]

bool Path::acceptFile ( const std::string &  path,
const std::set< std::string > &  extra 
)
static

Check if the file extension indicates that it's a C/C++ source file.

Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx

Parameters
pathfilename to check. path info is optional
extraextra file extensions
Returns
true if the file extension indicates it should be checked

Definition at line 235 of file path.cpp.

References getFilenameExtension(), and identify().

◆ fromNativeSeparators()

std::string Path::fromNativeSeparators ( std::string  path)
static

◆ getAbsoluteFilePath()

std::string Path::getAbsoluteFilePath ( const std::string &  filePath)
static

Get an absolute file path from a relative one.

Parameters
filePathFile path to be made absolute.
Returns
absolute path, if possible. Otherwise an empty path is returned

Definition at line 284 of file path.cpp.

Referenced by Library::load(), and loadVisualStudioProperties().

◆ getCurrentExecutablePath()

std::string Path::getCurrentExecutablePath ( const char *  fallback)
static

Returns the absolute path to the current executable.

Returns
absolute path to the current executable

Definition at line 143 of file path.cpp.

Referenced by Library::load(), and CmdLineParser::parseFromArgs().

◆ getCurrentPath()

std::string Path::getCurrentPath ( )
static

Returns the absolute path of current working directory.

Returns
absolute path of current working directory

Definition at line 129 of file path.cpp.

Referenced by PathMatch::PathMatch(), and ImportProject::setRelativePaths().

◆ getFilenameExtension()

std::string Path::getFilenameExtension ( const std::string &  path,
bool  lowercase = false 
)
static

Get an extension of the filename.

Parameters
pathPath containing filename.
lowercaseReturn the extension in lower case
Returns
Filename extension (containing the dot, e.g. ".h" or ".CPP").

Definition at line 109 of file path.cpp.

References caseInsensitiveFilesystem(), and strTolower().

Referenced by acceptFile(), getFilenameExtensionInLowerCase(), identify(), isC(), isCPP(), and Library::load().

◆ getFilenameExtensionInLowerCase()

std::string Path::getFilenameExtensionInLowerCase ( const std::string &  path)
static

Get an extension of the filename in lower case.

Parameters
pathPath containing filename.
Returns
Filename extension (containing the dot, e.g. ".h").

Definition at line 124 of file path.cpp.

References getFilenameExtension().

Referenced by Library::blockend(), Library::blockstart(), Library::blockstartoffset(), ImportProject::importBcb6Prj(), isCPP(), Library::isexecutableblock(), isHeader(), Library::isimporter(), Library::iskeyword(), Library::markupFile(), Library::processMarkupAfterCode(), and Library::reportErrors().

◆ getPathFromFilename()

std::string Path::getPathFromFilename ( const std::string &  filename)
static

Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')

Parameters
filenamefilename to lookup, must have / -separators.
Returns
path part of the filename

Definition at line 88 of file path.cpp.

Referenced by CppCheck::executeAddons(), getFullPath(), ImportProject::import(), ImportProject::importBcb6Prj(), ImportProject::importVcxproj(), Library::load(), Settings::loadCppcheckCfg(), Platform::loadFromFile(), CmdLineParser::loadLibraries(), loadVisualStudioProperties(), and LibraryDialog::saveCfgAs().

◆ getRelativePath()

std::string Path::getRelativePath ( const std::string &  absolutePath,
const std::vector< std::string > &  basePaths 
)
static

Create a relative path from an absolute one, if absolute path is inside the basePaths.

Parameters
absolutePathPath to be made relative.
basePathsPaths to which it may be made relative.
Returns
relative path, if possible. Otherwise absolutePath is returned unchanged

Definition at line 181 of file path.cpp.

References endsWith().

Referenced by CppCheck::checkFile(), TokenList::createTokens(), Tokenizer::dump(), Preprocessor::error(), CppCheck::getDumpFileContentsRawTokens(), PathMatch::match(), and ImportProject::setRelativePaths().

◆ identify()

Standards::Language Path::identify ( const std::string &  path,
bool *  header = nullptr 
)
static

Identify the language based on the file extension.

Parameters
pathfilename to check. path info is optional
headerif provided indicates if the file is a header
Returns
the language type

Definition at line 248 of file path.cpp.

References c_src_exts, caseInsensitiveFilesystem(), cpp_src_exts, getFilenameExtension(), header_exts, and strTolower().

Referenced by acceptFile(), CppCheck::checkClang(), CppCheck::checkFile(), createDUI(), createDumpFile(), TokenList::determineCppC(), and isHeader2().

◆ isAbsolute()

bool Path::isAbsolute ( const std::string &  path)
static

◆ isC()

bool Path::isC ( const std::string &  path)
static

Identify language based on file extension.

Parameters
pathfilename to check. path info is optional
Returns
true if extension is meant for C files
Deprecated:
does not account for headers - use @identify() instead

Definition at line 210 of file path.cpp.

References getFilenameExtension().

◆ isCPP()

bool Path::isCPP ( const std::string &  path)
static

Identify language based on file extension.

Parameters
pathfilename to check. path info is optional
Returns
true if extension is meant for C++ files
Deprecated:
returns true for some header extensions - use @identify() instead

Definition at line 218 of file path.cpp.

References getFilenameExtension(), and getFilenameExtensionInLowerCase().

◆ isDirectory()

bool Path::isDirectory ( const std::string &  path)
static

Checks if a given path is a directory.

Parameters
pathPath to be checked
Returns
true if given path is a directory

Definition at line 334 of file path.cpp.

References file_type().

Referenced by addFiles2(), CmdLineParser::fillSettingsFromArgs(), and CmdLineParser::parseFromArgs().

◆ isFile()

bool Path::isFile ( const std::string &  path)
static

Checks if given path is a file.

Parameters
pathPath to be checked
Returns
true if given path is a file

Definition at line 329 of file path.cpp.

References file_type().

Referenced by getFullPath(), Settings::loadCppcheckCfg(), and ImportProject::sourceFileExists().

◆ isHeader()

bool Path::isHeader ( const std::string &  path)
static

Is filename a header based on file extension.

Parameters
pathfilename to check. path info is optional
Returns
true if filename extension is meant for headers
Deprecated:
returns only heuristic result - use @identify() or @isHeader2() instead

Definition at line 242 of file path.cpp.

References getFilenameExtensionInLowerCase(), and startsWith().

◆ isHeader2()

bool Path::isHeader2 ( const std::string &  path)
static

Is filename a header based on file extension.

Parameters
pathfilename to check. path info is optional
Returns
true if filename extension is meant for headers

Definition at line 277 of file path.cpp.

References identify().

Referenced by ResultsTree::recheckSelectedFiles(), and ResultsView::updateDetails().

◆ join()

std::string Path::join ( const std::string &  path1,
const std::string &  path2 
)
static

join 2 paths with '/' separators

Definition at line 339 of file path.cpp.

Referenced by AnalyzerInformation::getAnalyzerInfoFile(), and Settings::loadCppcheckCfg().

◆ removeQuotationMarks()

std::string Path::removeQuotationMarks ( std::string  path)
static

Remove quotation marks (") from the path.

Parameters
pathpath to be cleaned.
Returns
Cleaned path without quotation marks.

Definition at line 103 of file path.cpp.

Referenced by addIncludePathsToList(), and CmdLineParser::parseFromArgs().

◆ sameFileName()

bool Path::sameFileName ( const std::string &  fname1,
const std::string &  fname2 
)
static

Compare filenames to see if they are the same.

On Linux the comparison is case-sensitive. On Windows it is case-insensitive.

Parameters
fname1one filename
fname2other filename
Returns
true if the filenames match on the current platform

Definition at line 98 of file path.cpp.

References caseInsensitiveFilesystem(), and caseInsensitiveStringCompare().

Referenced by TokenList::appendFileIfNew().

◆ simplifyPath()

std::string Path::simplifyPath ( std::string  originalPath)
static

◆ stripDirectoryPart()

std::string Path::stripDirectoryPart ( const std::string &  file)
static

Get filename without a directory path part.

Parameters
filefilename to be stripped. path info is optional
Returns
filename without directory path part.

Definition at line 302 of file path.cpp.

Referenced by bailoutInternal(), ValueType::setDebugPath(), and setSourceLocation().

◆ toNativeSeparators()

std::string Path::toNativeSeparators ( std::string  path)
static

The documentation for this class was generated from the following files: