Quellcode durchsuchen

Add path::has_trailing_separator()

Daniele Bartolini vor 9 Jahren
Ursprung
Commit
194f254c3e

+ 1 - 1
src/core/filesystem/file_monitor_linux.cpp

@@ -57,7 +57,7 @@ struct FileMonitorImpl
 	void add_watch(const char* path, bool recursive)
 	{
 		CE_ENSURE(path != NULL);
-		CE_ASSERT(path[strlen32(path)-1] != PATH_SEPARATOR, "Malformed path");
+		CE_ASSERT(!path::has_trailing_separator(path), "Malformed path");
 
 		int wd = inotify_add_watch(_fd
 			, path

+ 12 - 0
src/core/filesystem/path.cpp

@@ -10,6 +10,12 @@
 
 namespace crown
 {
+#if CROWN_PLATFORM_POSIX
+const char PATH_SEPARATOR = '/';
+#elif CROWN_PLATFORM_WINDOWS
+const char PATH_SEPARATOR = '\\';
+#endif // CROWN_PLATFORM_POSIX
+
 namespace path
 {
 	bool is_absolute(const char* path)
@@ -69,5 +75,11 @@ namespace path
 		const char* ld = strrchr(path, '.');
 		return ld == NULL ? NULL : ld + 1;
 	}
+
+	bool has_trailing_separator(const char* path)
+	{
+		CE_ENSURE(NULL != path);
+		return path[strlen32(path) - 1] == PATH_SEPARATOR;
+	}
 } // namespace path
 } // namespace crown

+ 3 - 6
src/core/filesystem/path.h

@@ -10,12 +10,6 @@
 
 namespace crown
 {
-#if CROWN_PLATFORM_POSIX
-	const char PATH_SEPARATOR = '/';
-#elif CROWN_PLATFORM_WINDOWS
-	const char PATH_SEPARATOR = '\\';
-#endif // CROWN_PLATFORM_POSIX
-
 /// Functions for operating on strings as file paths.
 ///
 /// @ingroup Filesystem
@@ -45,6 +39,9 @@ namespace path
 	/// "/home/texture.tga" -> "tga"
 	/// "/home/texture" -> NULL
 	const char* extension(const char* path);
+
+	/// Returns whether the @a path has a trailing separator.
+	bool has_trailing_separator(const char* path);
 } // namespace path
 
 } // namespace crown

+ 8 - 0
src/core/unit_tests.cpp

@@ -1162,6 +1162,10 @@ static void test_path()
 		path::join(path, "/home", "foo");
 		ENSURE(path == "/home/foo");
 	}
+	{
+		ENSURE(path::has_trailing_separator("/home/foo/"));
+		ENSURE(!path::has_trailing_separator("/home/foo"));
+	}
 #else
 	{
 		const bool a = path::is_absolute("C:\\Users\\foo");
@@ -1187,6 +1191,10 @@ static void test_path()
 		path::join(path, "C:\\Users", "foo");
 		ENSURE(path == "C:\\Users\\foo");
 	}
+	{
+		ENSURE(path::has_trailing_separator("C:\\Users\\foo\\"));
+		ENSURE(!path::has_trailing_separator("C:\\Users\\foo"));
+	}
 #endif // CROWN_PLATFORM_POSIX
 	{
 		const char* p = path::basename("");