Skip to content

Commit 91d97c5

Browse files
committed
Path: Add methods Path::isAbsolute() and Path::getCurrentPath()
1 parent 30b48cb commit 91d97c5

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

lib/path.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include <sstream>
2626
#include <cstring>
2727
#include <cctype>
28+
#ifndef _WIN32
29+
#include <unistd.h>
30+
#endif
2831

2932
/** Is the filesystem case insensitive? */
3033
static bool caseInsensitiveFilesystem()
@@ -183,6 +186,35 @@ std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
183186
return extension;
184187
}
185188

189+
const std::string Path::getCurrentPath()
190+
{
191+
char currentPath[4096];
192+
193+
if (getcwd(currentPath, 4096) != 0)
194+
return std::string(currentPath);
195+
196+
return emptyString;
197+
}
198+
199+
bool Path::isAbsolute(const std::string& path)
200+
{
201+
const std::string& nativePath = toNativeSeparators(path);
202+
203+
#ifdef _WIN32
204+
if (path.length() < 2)
205+
return false;
206+
207+
// On Windows, 'C:\foo\bar' is an absolute path, while 'C:foo\bar' is not
208+
if (nativePath.compare(0, 2, "\\\\") == 0 || (std::isalpha(nativePath[0]) != 0 && nativePath.compare(1, 2, ":\\") == 0))
209+
return true;
210+
#else
211+
if (!nativePath.empty() && nativePath[0] == '/')
212+
return true;
213+
#endif
214+
215+
return false;
216+
}
217+
186218
std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
187219
{
188220
for (std::vector<std::string>::const_iterator i = basePaths.begin(); i != basePaths.end(); ++i) {

lib/path.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ class CPPCHECKLIB Path {
9696
*/
9797
static std::string getFilenameExtensionInLowerCase(const std::string &path);
9898

99+
/**
100+
* @brief Returns the absolute path of current working directory
101+
* @return absolute path of current working directory
102+
*/
103+
static const std::string getCurrentPath();
104+
105+
/**
106+
* @brief Check if given path is absolute
107+
* @param path Path to check
108+
* @return true if given path is absolute
109+
*/
110+
static bool isAbsolute(const std::string& path);
111+
99112
/**
100113
* @brief Create a relative path from an absolute one, if absolute path is inside the basePaths.
101114
* @param absolutePath Path to be made relative.

test/testpath.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class TestPath : public TestFixture {
3030
void run() {
3131
TEST_CASE(simplify_path);
3232
TEST_CASE(accept_file);
33+
TEST_CASE(getCurrentPath);
34+
TEST_CASE(isAbsolute);
3335
TEST_CASE(getRelative);
3436
TEST_CASE(is_c);
3537
TEST_CASE(is_cpp);
@@ -110,6 +112,28 @@ class TestPath : public TestFixture {
110112
ASSERT_EQUALS(false, Path::acceptFile("index.hpp"));
111113
}
112114

115+
void getCurrentPath() const {
116+
ASSERT_EQUALS(true, Path::isAbsolute(Path::getCurrentPath()));
117+
}
118+
119+
void isAbsolute() const {
120+
#ifdef _WIN32
121+
ASSERT_EQUALS(true, Path::isAbsolute("C:\\foo\\bar"));
122+
ASSERT_EQUALS(true, Path::isAbsolute("C:/foo/bar"));
123+
ASSERT_EQUALS(true, Path::isAbsolute("\\\\foo\\bar"));
124+
ASSERT_EQUALS(false, Path::isAbsolute("foo\\bar"));
125+
ASSERT_EQUALS(false, Path::isAbsolute("foo/bar"));
126+
ASSERT_EQUALS(false, Path::isAbsolute("foo.cpp"));
127+
ASSERT_EQUALS(false, Path::isAbsolute("C:foo.cpp"));
128+
ASSERT_EQUALS(false, Path::isAbsolute("C:foo\\bar.cpp"));
129+
#else
130+
ASSERT_EQUALS(true, Path::isAbsolute("/foo/bar"));
131+
ASSERT_EQUALS(true, Path::isAbsolute("/"));
132+
ASSERT_EQUALS(false, Path::isAbsolute("foo/bar"));
133+
ASSERT_EQUALS(false, Path::isAbsolute("foo.cpp"));
134+
#endif
135+
}
136+
113137
void getRelative() const {
114138
std::vector<std::string> basePaths;
115139
basePaths.push_back(""); // Don't crash with empty paths

0 commit comments

Comments
 (0)