From 1deea835de5026f151bc9bc75c60c9a84ab75bcb Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 10 Sep 2025 10:49:55 +0200 Subject: [PATCH 1/5] exposed `isAbsolutePath()` and merged implementations --- simplecpp.cpp | 21 +++++++++------------ simplecpp.h | 3 +++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 84e4b54b..6623caa0 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -2413,21 +2413,18 @@ namespace simplecpp { return windowsPath; } #endif -} + bool isAbsolutePath(const std::string &path) + { #ifdef SIMPLECPP_WINDOWS -static bool isAbsolutePath(const std::string &path) -{ - if (path.length() >= 3 && path[0] > 0 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/')) - return true; - return path.length() > 1U && (path[0] == '/' || path[0] == '\\'); -} + if (path.length() >= 3 && path[0] > 0 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/')) + return true; + return path.length() > 1U && (path[0] == '/' || path[0] == '\\'); #else -static bool isAbsolutePath(const std::string &path) -{ - return path.length() > 1U && path[0] == '/'; -} + return path.length() > 1U && path[0] == '/'; #endif + } +} namespace simplecpp { /** @@ -2988,7 +2985,7 @@ static std::string openHeaderDirect(std::ifstream &f, const std::string &path) static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader) { - if (isAbsolutePath(header)) + if (simplecpp::isAbsolutePath(header)) return openHeaderDirect(f, simplecpp::simplifyPath(header)); // prefer first to search the header relatively to source file if found, when not a system header diff --git a/simplecpp.h b/simplecpp.h index ac367154..95ea4f68 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -559,6 +559,9 @@ namespace simplecpp { /** Returns the __cplusplus value for a given standard */ SIMPLECPP_LIB std::string getCppStdString(const std::string &std); SIMPLECPP_LIB std::string getCppStdString(cppstd_t std); + + /** Checks if given path is absolute */ + SIMPLECPP_LIB bool isAbsolutePath(const std::string &path); } #undef SIMPLECPP_TOKENLIST_ALLOW_PTR From 1efe7df1261a55be319b5cae0343d7efa8723820 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 10 Sep 2025 10:55:17 +0200 Subject: [PATCH 2/5] test.cpp: added filename to assertion failure message allows some IDEs to show clickable links to location --- test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.cpp b/test.cpp index 0ecaa3b1..0257feda 100644 --- a/test.cpp +++ b/test.cpp @@ -45,7 +45,7 @@ static int assertEquals(const std::string &expected, const std::string &actual, if (expected != actual) { numberOfFailedAssertions++; std::cerr << "------ assertion failed ---------" << std::endl; - std::cerr << "line " << line << std::endl; + std::cerr << "line test.cpp:" << line << std::endl; std::cerr << "expected:" << pprint(expected) << std::endl; std::cerr << "actual:" << pprint(actual) << std::endl; } From 0f68cadf7168c36238aef7092be1394b7f9cf3c2 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 10 Sep 2025 10:55:49 +0200 Subject: [PATCH 3/5] test.cpp: added `isAbsolutePath()` tests from Cppcheck --- test.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test.cpp b/test.cpp index 0257feda..20f63251 100644 --- a/test.cpp +++ b/test.cpp @@ -3231,6 +3231,26 @@ static void fuzz_crash() } } +static void isAbsolutePath() { +#ifdef _WIN32 + ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar")); + ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:/foo/bar")); + ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\\\foo\\bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo\\bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo.cpp")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:foo.cpp")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:foo\\bar.cpp")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("bar.cpp")); + //ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\")); // TODO +#else + ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/foo/bar")); + //ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/")); // TODO + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo.cpp")); +#endif +} + int main(int argc, char **argv) { TEST_CASE(backslash); @@ -3487,5 +3507,7 @@ int main(int argc, char **argv) TEST_CASE(fuzz_crash); + TEST_CASE(isAbsolutePath); + return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS; } From 28bf747c5d1cf56302e4039fa0c9ef15a1067eea Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 10 Sep 2025 11:00:42 +0200 Subject: [PATCH 4/5] test.cpp: added more `isAbsolutePath()` tests --- test.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test.cpp b/test.cpp index 20f63251..729c7a19 100644 --- a/test.cpp +++ b/test.cpp @@ -3236,6 +3236,7 @@ static void isAbsolutePath() { ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar")); ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:/foo/bar")); ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\\\foo\\bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo\\bar")); ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar")); ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo.cpp")); @@ -3243,11 +3244,23 @@ static void isAbsolutePath() { ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:foo\\bar.cpp")); ASSERT_EQUALS(false, simplecpp::isAbsolutePath("bar.cpp")); //ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\")); // TODO + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:\\foo\\bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:/foo/bar")); + //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\foo\\bar")); // TODO + //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\\\")); // TODO + //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("//")); // TODO + //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/foo/bar")); // TODO + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/")); #else ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/foo/bar")); //ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/")); // TODO + ASSERT_EQUALS(true, simplecpp::isAbsolutePath("//host/foo/bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar")); ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo.cpp")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:\\foo\\bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("C:/foo/bar")); + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\\\foo\\bar")); #endif } From f10842962760a05039289cd140b62a3340b82c56 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 10 Sep 2025 11:06:57 +0200 Subject: [PATCH 5/5] improved `isAbsolutePath()` implementation --- simplecpp.cpp | 14 +++++++++++--- test.cpp | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 6623caa0..9b60d69d 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -2417,11 +2417,19 @@ namespace simplecpp { bool isAbsolutePath(const std::string &path) { #ifdef SIMPLECPP_WINDOWS - if (path.length() >= 3 && path[0] > 0 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/')) + // C:\\path\\file + // C:/path/file + if (path.length() >= 3 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/')) return true; - return path.length() > 1U && (path[0] == '/' || path[0] == '\\'); + + // \\host\path\file + // //host/path/file + if (path.length() >= 2 && (path[0] == '\\' || path[0] == '/') && (path[1] == '\\' || path[1] == '/')) + return true; + + return false; #else - return path.length() > 1U && path[0] == '/'; + return !path.empty() && path[0] == '/'; #endif } } diff --git a/test.cpp b/test.cpp index 729c7a19..b23243af 100644 --- a/test.cpp +++ b/test.cpp @@ -3246,14 +3246,14 @@ static void isAbsolutePath() { //ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\")); // TODO ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:\\foo\\bar")); ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:/foo/bar")); - //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\foo\\bar")); // TODO + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\foo\\bar")); //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\\\")); // TODO //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("//")); // TODO - //ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/foo/bar")); // TODO + ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/foo/bar")); ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/")); #else ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/foo/bar")); - //ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/")); // TODO + ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/")); ASSERT_EQUALS(true, simplecpp::isAbsolutePath("//host/foo/bar")); ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar"));