19 #if defined(__GNUC__) && (defined(_WIN32) || defined(__CYGWIN__))
20 #undef __STRICT_ANSI__
29 #include <unordered_set>
32 #include <simplecpp.h>
35 #include <sys/types.h>
41 #if defined(__CYGWIN__)
44 #if defined(__APPLE__)
45 #include <mach-o/dyld.h>
52 #if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
65 constexpr
char separ =
'/';
66 constexpr
char native =
'\\';
68 constexpr
char separ =
'\\';
69 constexpr
char native =
'/';
77 constexpr
char nonnative =
'\\';
78 constexpr
char newsepar =
'/';
79 std::replace(path.begin(), path.end(), nonnative, newsepar);
85 return simplecpp::simplifyPath(std::move(originalPath));
90 const std::size_t pos = filename.find_last_of(
"\\/");
92 if (pos != std::string::npos)
93 return filename.substr(0, 1 + pos);
105 path.erase(std::remove(path.begin(), path.end(),
'\"'), path.end());
111 const std::string::size_type dotLocation = path.find_last_of(
'.');
112 if (dotLocation == std::string::npos)
115 std::string extension = path.substr(dotLocation);
131 char currentPath[4096];
134 if (getcwd(currentPath, 4096) !=
nullptr)
136 if (_getcwd(currentPath, 4096) !=
nullptr)
138 return std::string(currentPath);
148 success = (GetModuleFileNameA(
nullptr, buf,
sizeof(buf)) <
sizeof(buf));
149 #elif defined(__APPLE__)
150 uint32_t size =
sizeof(buf);
151 success = (_NSGetExecutablePath(buf, &size) == 0);
153 const char* procPath =
155 "/proc/self/path/a.out";
156 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
157 "/proc/curproc/file";
161 success = (readlink(procPath, buf,
sizeof(buf)) != -1);
163 return success ? std::string(buf) : std::string(fallback);
171 if (path.length() < 2)
175 return startsWith(nativePath,
"\\\\") || (std::isalpha(nativePath[0]) != 0 && nativePath.compare(1, 2,
":\\") == 0);
177 return !nativePath.empty() && nativePath[0] ==
'/';
183 for (
const std::string &bp : basePaths) {
184 if (absolutePath == bp || bp.empty())
187 if (absolutePath.compare(0, bp.length(), bp) != 0)
191 return absolutePath.substr(bp.length());
192 if (absolutePath.size() > bp.size() && absolutePath[bp.length()] ==
'/')
193 return absolutePath.substr(bp.length() + 1);
199 ".cpp",
".cxx",
".cc",
".c++",
".tpp",
".txx",
".ipp",
".ixx"
207 ".h",
".hpp",
".h++",
".hxx",
".hh"
214 return extension ==
".c" ||
221 return extension ==
".cpp" ||
222 extension ==
".cxx" ||
223 extension ==
".cc" ||
224 extension ==
".c++" ||
225 extension ==
".hpp" ||
226 extension ==
".hxx" ||
227 extension ==
".hh" ||
228 extension ==
".tpp" ||
229 extension ==
".txx" ||
230 extension ==
".ipp" ||
231 extension ==
".ixx" ||
256 return Standards::Language::CPP;
258 return Standards::Language::C;
265 return Standards::Language::C;
268 return Standards::Language::CPP;
272 return Standards::Language::CPP;
274 return Standards::Language::None;
286 std::string absolute_path;
288 char absolute[_MAX_PATH];
289 if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
290 absolute_path = absolute;
291 #elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__) || defined(__CPPCHECK__)
292 char * absolute = realpath(filePath.c_str(),
nullptr);
294 absolute_path = absolute;
297 #error Platform absolute path function needed
299 return absolute_path;
304 #if defined(_WIN32) && !defined(__MINGW32__)
305 constexpr
char native =
'\\';
307 constexpr
char native =
'/';
310 const std::string::size_type p = file.rfind(native);
311 if (p != std::string::npos) {
312 return file.substr(p + 1);
318 using mode_t =
unsigned short;
323 struct stat file_stat;
324 if (stat(path.c_str(), &file_stat) == -1)
326 return file_stat.st_mode & S_IFMT;
339 std::string
Path::join(
const std::string& path1,
const std::string& path2) {
340 if (path1.empty() || path2.empty())
341 return path1 + path2;
342 if (path2.front() ==
'/')
344 return ((path1.back() ==
'/') ? path1 : (path1 +
"/")) + path2;
static bool isHeader2(const std::string &path)
Is filename a header based on file extension.
static std::string simplifyPath(std::string originalPath)
Simplify path "foo/bar/.." => "foo".
static std::string getFilenameExtension(const std::string &path, bool lowercase=false)
Get an extension of the filename.
static std::string join(const std::string &path1, const std::string &path2)
join 2 paths with '/' separators
static bool isFile(const std::string &path)
Checks if given path is a file.
static std::string fromNativeSeparators(std::string path)
Convert path to use internal path separators.
static std::string getCurrentPath()
Returns the absolute path of current working directory.
static std::string removeQuotationMarks(std::string path)
Remove quotation marks (") from the path.
static DEPRECATED bool isCPP(const std::string &path)
Identify language based on file extension.
static DEPRECATED bool isHeader(const std::string &path)
Is filename a header based on file extension.
static std::string getCurrentExecutablePath(const char *fallback)
Returns the absolute path to the current executable.
static std::string toNativeSeparators(std::string path)
Convert path to use native separators.
static Standards::Language identify(const std::string &path, bool *header=nullptr)
Identify the language based on the file extension.
static std::string getPathFromFilename(const std::string &filename)
Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
static bool sameFileName(const std::string &fname1, const std::string &fname2)
Compare filenames to see if they are the same.
static std::string getAbsoluteFilePath(const std::string &filePath)
Get an absolute file path from a relative one.
static bool acceptFile(const std::string &filename)
Check if the file extension indicates that it's a C/C++ source file.
static DEPRECATED bool isC(const std::string &path)
Identify language based on file extension.
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.
static bool isDirectory(const std::string &path)
Checks if a given path is a directory.
static std::string stripDirectoryPart(const std::string &file)
Get filename without a directory path part.
static std::string getFilenameExtensionInLowerCase(const std::string &path)
Get an extension of the filename in lower case.
static bool isAbsolute(const std::string &path)
Check if given path is absolute.
static void replace(std::string &source, const std::unordered_map< std::string, std::string > &substitutionMap)
static const std::unordered_set< std::string > header_exts
static mode_t file_type(const std::string &path)
static constexpr bool caseInsensitiveFilesystem()
Is the filesystem case insensitive?
static const std::unordered_set< std::string > c_src_exts
static const std::unordered_set< std::string > cpp_src_exts
int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs)
void strTolower(std::string &str)
bool startsWith(const std::string &str, const char start[], std::size_t startlen)
bool endsWith(const std::string &str, char c)