فهرست منبع

added read/write float/double.

Nicolas Cannasse 19 سال پیش
والد
کامیت
f29fbb2c73
2فایلهای تغییر یافته به همراه44 افزوده شده و 0 حذف شده
  1. 19 0
      std/neko/io/Input.hx
  2. 25 0
      std/neko/io/Output.hx

+ 19 - 0
std/neko/io/Input.hx

@@ -104,6 +104,22 @@ class Input {
 		readBytes(nbytes);
 	}
 
+	public function readFloat() {
+		return _float_of_bytes(untyped readBytes(4).__s,false);
+	}
+
+	public function readFloatB() {
+		return _float_of_bytes(untyped readBytes(4).__s,true);
+	}
+
+	public function readDouble() {
+		return _double_of_bytes(untyped readBytes(8).__s,false);
+	}
+
+	public function readDoubleB() {
+		return _double_of_bytes(untyped readBytes(8).__s,true);
+	}
+
 	public function readInt8() {
 		var n = readChar();
 		if( n >= 128 )
@@ -188,4 +204,7 @@ class Input {
 		return ch4 | (ch3 << 8) | (ch2 << 16) | (ch1 << 24);
 	}
 
+	static var _float_of_bytes = neko.Lib.load("std","float_of_bytes",2);
+	static var _double_of_bytes = neko.Lib.load("std","double_of_bytes",2);
+
 }

+ 25 - 0
std/neko/io/Output.hx

@@ -69,6 +69,28 @@ class Output {
 		}
 	}
 
+	public function writeFloat( c : Float ) {
+		writeBytes(new String(_float_bytes(c,false)));
+	}
+
+	public function writeFloatB( c : Float ) {
+		writeBytes(new String(_float_bytes(c,true)));
+	}
+
+	public function writeDouble( c : Float ) {
+		writeBytes(new String(_double_bytes(c,false)));
+	}
+
+	public function writeDoubleB( c : Float ) {
+		writeBytes(new String(_double_bytes(c,true)));
+	}
+
+	public function writeInt8( c : Int ) {
+		if( c >= 128 || c < -128 )
+			throw Error.Overflow;
+		writeChar(c & 0xFF);
+	}
+
 	public function writeInt32( x : Int ) {
 		writeChar(x & 0xFF);
 		writeChar((x >> 8) & 0xFF);
@@ -99,4 +121,7 @@ class Output {
 	public function prepare( nbytes : Int ) {
 	}
 
+	static var _float_bytes = neko.Lib.load("std","float_bytes",2);
+	static var _double_bytes = neko.Lib.load("std","double_bytes",2);
+
 }