Procházet zdrojové kódy

tame neko's FileSystem API (closes #3266)

Simon Krajewski před 10 roky
rodič
revize
d99f1d421c

+ 16 - 4
std/cpp/_std/sys/FileSystem.hx

@@ -31,7 +31,7 @@ private enum FileKind {
 class FileSystem {
 
 	public static function exists( path : String ) : Bool {
-		return sys_exists(haxe.io.Path.removeTrailingSlashes(path));
+		return sys_exists(makeCompatiblePath(path));
 	}
 
 	public static function rename( path : String, newPath : String ) : Void {
@@ -40,7 +40,7 @@ class FileSystem {
 	}
 
 	public static function stat( path : String ) : FileStat {
-		var s : FileStat = sys_stat(path);
+		var s : FileStat = sys_stat(makeCompatiblePath(path));
 		if (s==null)
 			return { gid:0, uid:0, atime:Date.fromTime(0), mtime:Date.fromTime(0), ctime:Date.fromTime(0), dev:0, ino:0, nlink:0, rdev:0, size:0, mode:0 };
 		s.atime = Date.fromTime(1000.0*(untyped s.atime));
@@ -59,7 +59,7 @@ class FileSystem {
 	}
 
 	static function kind( path : String ) : FileKind {
-		var k:String = sys_file_type(haxe.io.Path.removeTrailingSlashes(path));
+		var k:String = sys_file_type(makeCompatiblePath(path));
 		return switch(k) {
 		case "file": kfile;
 		case "dir": kdir;
@@ -68,7 +68,11 @@ class FileSystem {
 	}
 
 	public static function isDirectory( path : String ) : Bool {
-		return kind(path) == kdir;
+		return try {
+			kind(path) == kdir;
+		} catch(e:Dynamic) {
+			false;
+		}
 	}
 
 	public static function createDirectory( path : String ) : Void {
@@ -99,6 +103,14 @@ class FileSystem {
 		return sys_read_dir(path);
 	}
 
+	private static inline function makeCompatiblePath(path:String):String {
+		return if (path.charCodeAt(1) == ":".code) {
+			haxe.io.Path.addTrailingSlash(path);
+		} else {
+			haxe.io.Path.removeTrailingSlashes(path);
+		}
+	}
+
 	private static var sys_exists = cpp.Lib.load("std","sys_exists",1);
 	private static var file_delete = cpp.Lib.load("std","file_delete",1);
 	private static var sys_rename = cpp.Lib.load("std","sys_rename",2);

+ 16 - 4
std/neko/_std/sys/FileSystem.hx

@@ -31,7 +31,7 @@ private enum FileKind {
 class FileSystem {
 
 	public static function exists( path : String ) : Bool {
-		return sys_exists(untyped (haxe.io.Path.removeTrailingSlashes(path)).__s);
+		return sys_exists(untyped (makeCompatiblePath(path)).__s);
 	}
 
 	public static function rename( path : String, newPath : String ) : Void {
@@ -39,7 +39,7 @@ class FileSystem {
 	}
 
 	public static function stat( path : String ) : FileStat {
-		var s : FileStat = sys_stat(untyped path.__s);
+		var s : FileStat = sys_stat(untyped (makeCompatiblePath(path)).__s);
 		s.atime = untyped Date.new1(s.atime);
 		s.mtime = untyped Date.new1(s.mtime);
 		s.ctime = untyped Date.new1(s.ctime);
@@ -56,7 +56,7 @@ class FileSystem {
 	}
 
 	static function kind( path : String ) : FileKind {
-		var k = new String(sys_file_type(untyped (haxe.io.Path.removeTrailingSlashes(path)).__s));
+		var k = new String(sys_file_type(untyped (makeCompatiblePath(path)).__s));
 		return switch(k) {
 		case "file": kfile;
 		case "dir": kdir;
@@ -65,7 +65,11 @@ class FileSystem {
 	}
 
 	public static function isDirectory( path : String ) : Bool {
-		return kind(path) == kdir;
+		return try {
+			kind(path) == kdir;
+		} catch(e:Dynamic) {
+			false;
+		}
 	}
 
 	public static function createDirectory( path : String ) : Void {
@@ -100,6 +104,14 @@ class FileSystem {
 		return a;
 	}
 
+	private static inline function makeCompatiblePath(path:String):String {
+		return if (path.charCodeAt(1) == ":".code) {
+			haxe.io.Path.addTrailingSlash(path);
+		} else {
+			haxe.io.Path.removeTrailingSlashes(path);
+		}
+	}
+
 	private static var sys_exists = neko.Lib.load("std","sys_exists",1);
 	private static var file_delete = neko.Lib.load("std","file_delete",1);
 	private static var sys_rename = neko.Lib.load("std","sys_rename",2);

+ 13 - 0
tests/sys/src/TestFileSystem.hx

@@ -91,4 +91,17 @@ class TestFileSystem extends haxe.unit.TestCase {
 			FileSystem.deleteDirectory(dir + "complex");
 		}
 	}
+
+	function testWindowsSpecialCases() {
+		var withSlash = "C:/";
+		var withBackslash = "C:\\";
+		var without = "C:";
+
+		for (path in [withSlash, withBackslash, without]) {
+			assertTrue(FileSystem.exists(path));
+			assertTrue(FileSystem.isDirectory(path));
+			assertTrue(FileSystem.stat(path) != null);
+			assertTrue(FileSystem.readDirectory(path) != null);
+		}
+	}
 }