diff --git a/src/common-tests/CMakeLists.txt b/src/common-tests/CMakeLists.txt index a571640ca..6502fe81d 100644 --- a/src/common-tests/CMakeLists.txt +++ b/src/common-tests/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(common-tests bitutils_tests.cpp event_tests.cpp + file_system_tests.cpp rectangle_tests.cpp ) diff --git a/src/common-tests/common-tests.vcxproj b/src/common-tests/common-tests.vcxproj index 9f69e06f7..a55c1536f 100644 --- a/src/common-tests/common-tests.vcxproj +++ b/src/common-tests/common-tests.vcxproj @@ -46,6 +46,7 @@ + diff --git a/src/common-tests/common-tests.vcxproj.filters b/src/common-tests/common-tests.vcxproj.filters index bf5fc0c76..225e130ff 100644 --- a/src/common-tests/common-tests.vcxproj.filters +++ b/src/common-tests/common-tests.vcxproj.filters @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/src/common-tests/file_system_tests.cpp b/src/common-tests/file_system_tests.cpp new file mode 100644 index 000000000..2456fefb8 --- /dev/null +++ b/src/common-tests/file_system_tests.cpp @@ -0,0 +1,25 @@ +#include "common/file_system.h" +#include + +TEST(FileSystem, IsAbsolutePath) +{ +#ifdef WIN32 + ASSERT_TRUE(FileSystem::IsAbsolutePath("C:\\")); + ASSERT_TRUE(FileSystem::IsAbsolutePath("C:\\Path")); + ASSERT_TRUE(FileSystem::IsAbsolutePath("C:\\Path\\Subdirectory")); + ASSERT_TRUE(FileSystem::IsAbsolutePath("C:/")); + ASSERT_TRUE(FileSystem::IsAbsolutePath("C:/Path")); + ASSERT_TRUE(FileSystem::IsAbsolutePath("C:/Path/Subdirectory")); + ASSERT_FALSE(FileSystem::IsAbsolutePath("")); + ASSERT_FALSE(FileSystem::IsAbsolutePath("C:")); + ASSERT_FALSE(FileSystem::IsAbsolutePath("Path")); + ASSERT_FALSE(FileSystem::IsAbsolutePath("Path/Subdirectory")); +#else + ASSERT_TRUE(FileSystem::IsAbsolutePath("/")); + ASSERT_TRUE(FileSystem::IsAbsolutePath("/path")); + ASSERT_TRUE(FileSystem::IsAbsolutePath("/path/subdirectory")); + ASSERT_FALSE(FileSystem::IsAbsolutePath("")); + ASSERT_FALSE(FileSystem::IsAbsolutePath("path")); + ASSERT_FALSE(FileSystem::IsAbsolutePath("path/subdirectory")); +#endif +} diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 70b7619cb..50f85a123 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -230,6 +230,16 @@ void SanitizeFileName(String& Destination, bool StripSlashes /* = true */) return SanitizeFileName(Destination, Destination, StripSlashes); } +bool IsAbsolutePath(const std::string_view& path) +{ +#ifdef WIN32 + return (path.length() >= 3 && ((path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z')) && + path[1] == ':' && (path[2] == '/' || path[2] == '\\')); +#else + return (path.length() >= 1 && path[0] == '/'); +#endif +} + std::string ReplaceExtension(std::string_view path, std::string_view new_extension) { std::string_view::size_type pos = path.rfind('.'); diff --git a/src/common/file_system.h b/src/common/file_system.h index a2202f290..733467fee 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -137,6 +137,9 @@ void SanitizeFileName(char* Destination, u32 cbDestination, const char* FileName void SanitizeFileName(String& Destination, const char* FileName, bool StripSlashes = true); void SanitizeFileName(String& Destination, bool StripSlashes = true); +/// Returns true if the specified path is an absolute path (C:\Path on Windows or /path on Unix). +bool IsAbsolutePath(const std::string_view& path); + /// Replaces the extension of a filename with another. std::string ReplaceExtension(std::string_view path, std::string_view new_extension);