Skip to content

Commit 5b736f2

Browse files
authored
improved and exposed isAbsolutePath() (#538)
1 parent 009ceca commit 5b736f2

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

simplecpp.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,21 +2438,26 @@ namespace simplecpp {
24382438
return windowsPath;
24392439
}
24402440
#endif
2441-
}
24422441

2442+
bool isAbsolutePath(const std::string &path)
2443+
{
24432444
#ifdef SIMPLECPP_WINDOWS
2444-
static bool isAbsolutePath(const std::string &path)
2445-
{
2446-
if (path.length() >= 3 && path[0] > 0 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/'))
2447-
return true;
2448-
return path.length() > 1U && (path[0] == '/' || path[0] == '\\');
2449-
}
2445+
// C:\\path\\file
2446+
// C:/path/file
2447+
if (path.length() >= 3 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/'))
2448+
return true;
2449+
2450+
// \\host\path\file
2451+
// //host/path/file
2452+
if (path.length() >= 2 && (path[0] == '\\' || path[0] == '/') && (path[1] == '\\' || path[1] == '/'))
2453+
return true;
2454+
2455+
return false;
24502456
#else
2451-
static bool isAbsolutePath(const std::string &path)
2452-
{
2453-
return path.length() > 1U && path[0] == '/';
2454-
}
2457+
return !path.empty() && path[0] == '/';
24552458
#endif
2459+
}
2460+
}
24562461

24572462
namespace simplecpp {
24582463
/**
@@ -3013,7 +3018,7 @@ static std::string openHeaderDirect(std::ifstream &f, const std::string &path)
30133018

30143019
static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader)
30153020
{
3016-
if (isAbsolutePath(header))
3021+
if (simplecpp::isAbsolutePath(header))
30173022
return openHeaderDirect(f, simplecpp::simplifyPath(header));
30183023

30193024
// prefer first to search the header relatively to source file if found, when not a system header

simplecpp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ namespace simplecpp {
556556
/** Returns the __cplusplus value for a given standard */
557557
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
558558
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);
559+
560+
/** Checks if given path is absolute */
561+
SIMPLECPP_LIB bool isAbsolutePath(const std::string &path);
559562
}
560563

561564
#undef SIMPLECPP_TOKENLIST_ALLOW_PTR

test.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static int assertEquals(const std::string &expected, const std::string &actual,
4545
if (expected != actual) {
4646
numberOfFailedAssertions++;
4747
std::cerr << "------ assertion failed ---------" << std::endl;
48-
std::cerr << "line " << line << std::endl;
48+
std::cerr << "line test.cpp:" << line << std::endl;
4949
std::cerr << "expected:" << pprint(expected) << std::endl;
5050
std::cerr << "actual:" << pprint(actual) << std::endl;
5151
}
@@ -3246,6 +3246,39 @@ static void safe_api()
32463246
#endif
32473247
}
32483248

3249+
static void isAbsolutePath() {
3250+
#ifdef _WIN32
3251+
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar"));
3252+
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:/foo/bar"));
3253+
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\\\foo\\bar"));
3254+
3255+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo\\bar"));
3256+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar"));
3257+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo.cpp"));
3258+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:foo.cpp"));
3259+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:foo\\bar.cpp"));
3260+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("bar.cpp"));
3261+
//ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\")); // TODO
3262+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:\\foo\\bar"));
3263+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:/foo/bar"));
3264+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\foo\\bar"));
3265+
//ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\\\")); // TODO
3266+
//ASSERT_EQUALS(false, simplecpp::isAbsolutePath("//")); // TODO
3267+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/foo/bar"));
3268+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/"));
3269+
#else
3270+
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/foo/bar"));
3271+
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/"));
3272+
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("//host/foo/bar"));
3273+
3274+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar"));
3275+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo.cpp"));
3276+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:\\foo\\bar"));
3277+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:/foo/bar"));
3278+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\\\foo\\bar"));
3279+
#endif
3280+
}
3281+
32493282
// crashes detected by fuzzer
32503283
static void fuzz_crash()
32513284
{
@@ -3531,6 +3564,8 @@ int main(int argc, char **argv)
35313564

35323565
TEST_CASE(safe_api);
35333566

3567+
TEST_CASE(isAbsolutePath);
3568+
35343569
TEST_CASE(fuzz_crash);
35353570

35363571
TEST_CASE(leak);

0 commit comments

Comments
 (0)