Prechádzať zdrojové kódy

[cpp] Convert FileSystem, File and Random over to use new Native externs

Hugh 9 rokov pred
rodič
commit
1e1eccaa1c

+ 10 - 2
std/cpp/NativeFile.hx

@@ -18,6 +18,10 @@ extern class NativeFile
    public static function file_write_char(handle:Dynamic,c:Int) : Void { }
 
 
+   @:extern @:native("_hx_std_file_read")
+   public static function file_read(handle:Dynamic,s:haxe.io.BytesData,p:Int,n:Int) : Int return 0;
+
+
    @:extern @:native("_hx_std_file_read_char")
    public static function file_read_char(handle:Dynamic) : Int return 0;
 
@@ -38,8 +42,12 @@ extern class NativeFile
    public static function file_flush(handle:Dynamic) : Void return null;
 
 
-   @:extern @:native("_hx_std_file_contents")
-   public static function file_contents(name:String) : String return null;
+   @:extern @:native("_hx_std_file_contents_string")
+   public static function file_contents_string(name:String) : String return null;
+
+
+   @:extern @:native("_hx_std_file_contents_bytes")
+   public static function file_contents_bytes(name:String) : haxe.io.BytesData return null;
 
 
    @:extern @:native("_hx_std_file_stdin")

+ 1 - 1
std/cpp/NativeSys.hx

@@ -53,7 +53,7 @@ extern class NativeSys
 
 
    @:extern @:native("_hx_std_sys_rename")
-   public static function sys_rename(path:String,newname:String) : Void { }
+   public static function sys_rename(path:String,newname:String) : Bool return false;
 
 
    @:extern @:native("_hx_std_sys_stat")

+ 4 - 9
std/cpp/Random.hx

@@ -26,24 +26,19 @@ class Random {
 	var r : Dynamic;
 
 	public function new() {
-		r = random_new();
+		r = cpp.NativeRandom.random_new();
 	}
 
 	public function setSeed( s : Int ) {
-		random_set_seed(r,s);
+		cpp.NativeRandom.random_set_seed(r,s);
 	}
 
 	public function int( max : Int ) : Int {
-		return random_int(r,max);
+		return cpp.NativeRandom.random_int(r,max);
 	}
 
 	public function float() : Float {
-		return random_float(r);
+		return cpp.NativeRandom.random_float(r);
 	}
 
-	static var random_new = Lib.load("std","random_new",0);
-	static var random_set_seed = Lib.load("std","random_set_seed",2);
-	static var random_int = Lib.load("std","random_int",2);
-	static var random_float = Lib.load("std","random_float",1);
-
 }

+ 11 - 23
std/cpp/_std/sys/FileSystem.hx

@@ -21,6 +21,8 @@
  */
 package sys;
 
+import cpp.NativeSys;
+
 private enum FileKind {
 	kdir;
 	kfile;
@@ -32,16 +34,15 @@ private enum FileKind {
 class FileSystem {
 
 	public static function exists( path : String ) : Bool {
-		return sys_exists(makeCompatiblePath(path));
+		return NativeSys.sys_exists(makeCompatiblePath(path));
 	}
 
 	public static function rename( path : String, newPath : String ) : Void {
-		if (sys_rename(path,newPath)==null)
-         throw "Could not rename:" + path + " to " + newPath;
+		NativeSys.sys_rename(path,newPath);
 	}
 
 	public static function stat( path : String ) : FileStat {
-		var s : FileStat = sys_stat(makeCompatiblePath(path));
+		var s : FileStat = NativeSys.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));
@@ -51,7 +52,7 @@ class FileSystem {
 	}
 
 	public static function fullPath( relPath : String ) : String {
-		return new String(file_full_path(relPath));
+		return NativeSys.file_full_path(relPath);
 	}
 
 	public static function absolutePath ( relPath : String ) : String {
@@ -60,7 +61,7 @@ class FileSystem {
 	}
 
 	static function kind( path : String ) : FileKind {
-		var k:String = sys_file_type(makeCompatiblePath(path));
+		var k:String = NativeSys.sys_file_type(makeCompatiblePath(path));
 		return switch(k) {
 		case "file": kfile;
 		case "dir": kdir;
@@ -81,23 +82,21 @@ class FileSystem {
 			path = _p;
 		}
 		for (part in parts) {
-			if (part.charCodeAt(part.length - 1) != ":".code && !exists(part) && sys_create_dir( part, 493 )==null)
+			if (part.charCodeAt(part.length - 1) != ":".code && !exists(part) && !NativeSys.sys_create_dir( part, 493 ))
 				throw "Could not create directory:" + part;
 		}
 	}
 
 	public static function deleteFile( path : String ) : Void {
-		if (file_delete(path)==null)
-         throw "Could not delete file:" + path;
+		NativeSys.file_delete(path);
 	}
 
 	public static function deleteDirectory( path : String ) : Void {
-		if (sys_remove_dir(path)==null)
-         throw "Could not delete directory:" + path;
+		NativeSys.sys_remove_dir(path);
 	}
 
 	public static function readDirectory( path : String ) : Array<String> {
-		return sys_read_dir(path);
+		return NativeSys.sys_read_dir(path);
 	}
 
 	private static inline function makeCompatiblePath(path:String):String {
@@ -107,15 +106,4 @@ class FileSystem {
 			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);
-	private static var sys_stat = cpp.Lib.load("std","sys_stat",1);
-	private static var sys_file_type = cpp.Lib.load("std","sys_file_type",1);
-	private static var sys_create_dir = cpp.Lib.load("std","sys_create_dir",2);
-	private static var sys_remove_dir = cpp.Lib.load("std","sys_remove_dir",1);
-	private static var sys_read_dir = cpp.Lib.load("std","sys_read_dir",1);
-	private static var file_full_path = cpp.Lib.load("std","file_full_path",1);
-
 }

+ 7 - 10
std/cpp/_std/sys/io/File.hx

@@ -21,16 +21,17 @@
  */
 package sys.io;
 
+import cpp.NativeFile;
+
 @:coreApi
 class File {
 
 	public static function getContent( path : String ) : String {
-		var b = getBytes(path);
-		return b.toString();
+		return NativeFile.file_contents_string(path);
 	}
 
 	public static function getBytes( path : String ) : haxe.io.Bytes {
-		var data:haxe.io.BytesData = file_contents(path);
+		var data = NativeFile.file_contents_bytes(path);
 		return haxe.io.Bytes.ofData(data);
 	}
 
@@ -47,15 +48,15 @@ class File {
 	}
 
 	public static function read( path : String, binary : Bool = true ) : FileInput {
-		return untyped new FileInput(file_open(path,(if( binary ) "rb" else "r")));
+		return untyped new FileInput(NativeFile.file_open(path,(if( binary ) "rb" else "r")));
 	}
 
 	public static function write( path : String, binary : Bool = true ) : FileOutput {
-		return untyped new FileOutput(file_open(path,(if( binary ) "wb" else "w")));
+		return untyped new FileOutput(NativeFile.file_open(path,(if( binary ) "wb" else "w")));
 	}
 
 	public static function append( path : String, binary : Bool = true ) : FileOutput {
-		return untyped new FileOutput(file_open(path,(if( binary ) "ab" else "a")));
+		return untyped new FileOutput(NativeFile.file_open(path,(if( binary ) "ab" else "a")));
 	}
 
 	public static function copy( srcPath : String, dstPath : String ) : Void {
@@ -66,8 +67,4 @@ class File {
 		d.close();
 	}
 
-	private static var file_contents = cpp.Lib.load("std","file_contents",1);
-	private static var file_open = cpp.Lib.load("std","file_open",2);
-
-
 }

+ 7 - 15
std/cpp/_std/sys/io/FileInput.hx

@@ -21,6 +21,7 @@
  */
 package sys.io;
 import sys.io.FileSeek;
+import cpp.NativeFile;
 
 @:coreApi
 class FileInput extends haxe.io.Input {
@@ -33,7 +34,7 @@ class FileInput extends haxe.io.Input {
 
 	public override function readByte() : Int {
 		return try {
-			file_read_char(__f);
+			NativeFile.file_read_char(__f);
 		} catch( e : Dynamic ) {
 			if( untyped e.__IsArray() )
 				throw new haxe.io.Eof();
@@ -44,7 +45,7 @@ class FileInput extends haxe.io.Input {
 
 	public override function readBytes( s : haxe.io.Bytes, p : Int, l : Int ) : Int {
 		return try {
-			file_read(__f,s.getData(),p,l);
+			NativeFile.file_read(__f,s.getData(),p,l);
 		} catch( e : Dynamic ) {
 			if( untyped e.__IsArray() )
 				throw new haxe.io.Eof();
@@ -55,29 +56,20 @@ class FileInput extends haxe.io.Input {
 
 	public override function close() : Void {
 		super.close();
-		file_close(__f);
+		NativeFile.file_close(__f);
 	}
 
 	public function seek( p : Int, pos : FileSeek ) : Void {
-		file_seek(__f,p,pos==SeekBegin ? 0 : pos==SeekCur ? 1 :  2 );
+		NativeFile.file_seek(__f,p,pos==SeekBegin ? 0 : pos==SeekCur ? 1 :  2 );
 	}
 
 	public function tell() : Int {
-		return file_tell(__f);
+		return NativeFile.file_tell(__f);
 	}
 
 
 	public function eof() : Bool {
-		return file_eof(__f);
+		return NativeFile.file_eof(__f);
 	}
 
-	private static var file_eof = cpp.Lib.load("std","file_eof",1);
-
-	private static var file_read = cpp.Lib.load("std","file_read",4);
-	private static var file_read_char = cpp.Lib.load("std","file_read_char",1);
-
-	private static var file_close = cpp.Lib.load("std","file_close",1);
-	private static var file_seek = cpp.Lib.load("std","file_seek",3);
-	private static var file_tell = cpp.Lib.load("std","file_tell",1);
-
 }

+ 7 - 14
std/cpp/_std/sys/io/FileOutput.hx

@@ -21,6 +21,7 @@
  */
 package sys.io;
 import sys.io.FileSeek;
+import cpp.NativeFile;
 
 @:coreApi
 class FileOutput extends haxe.io.Output {
@@ -32,36 +33,28 @@ class FileOutput extends haxe.io.Output {
 	}
 
 	public override function writeByte( c : Int ) : Void {
-		try file_write_char(__f,c) catch( e : Dynamic ) throw haxe.io.Error.Custom(e);
+		try NativeFile.file_write_char(__f,c) catch( e : Dynamic ) throw haxe.io.Error.Custom(e);
 	}
 
 	public override function writeBytes( s : haxe.io.Bytes, p : Int, l : Int ) : Int {
-		return try file_write(__f,s.getData(),p,l) catch( e : Dynamic ) throw haxe.io.Error.Custom(e);
+		return try NativeFile.file_write(__f,s.getData(),p,l) catch( e : Dynamic ) throw haxe.io.Error.Custom(e);
 	}
 
 	public override function flush() : Void {
-		file_flush(__f);
+		NativeFile.file_flush(__f);
 	}
 
 	public override function close() : Void {
 		super.close();
-		file_close(__f);
+		NativeFile.file_close(__f);
 	}
 
 	public function seek( p : Int, pos : FileSeek ) : Void {
-		file_seek(__f,p, pos == SeekBegin ? 0 : pos ==  SeekCur ? 1 : 2);
+		NativeFile.file_seek(__f,p, pos == SeekBegin ? 0 : pos ==  SeekCur ? 1 : 2);
 	}
 
 	public function tell() : Int {
-		return file_tell(__f);
+		return NativeFile.file_tell(__f);
 	}
 
-	private static var file_close = cpp.Lib.load("std","file_close",1);
-	private static var file_seek = cpp.Lib.load("std","file_seek",3);
-	private static var file_tell = cpp.Lib.load("std","file_tell",1);
-
-	private static var file_flush = cpp.Lib.load("std","file_flush",1);
-	private static var file_write = cpp.Lib.load("std","file_write",4);
-	private static var file_write_char = cpp.Lib.load("std","file_write_char",2);
-
 }