Explorar el Código

fixed windows problem (saveContent), small refactoring

frabbit hace 11 años
padre
commit
4c4d125f80

+ 7 - 7
std/python/_std/sys/io/File.hx

@@ -34,14 +34,14 @@ class File {
 
 	public static function getContent( path : String ) : String
 	{
-		var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "r", -1, "utf-8");
+		var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "r", -1, "utf-8", null, "");
 		var content = f.read(-1);
 		f.close();
 		return content;
 	}
 
 	public static function saveContent( path : String, content : String ) : Void {
-		var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "w", -1, "utf-8");
+		var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "w", -1, "utf-8", null, "");
 		f.write(content);
 		f.close();
 	}
@@ -63,21 +63,21 @@ class File {
 	public static function read( path : String, binary : Bool = true ) : FileInput {
 		var mode = if (binary) "rb" else "r";
 
-		var f = python.lib.Builtin.open(path, mode, -1);
+		var f = python.lib.Builtin.open(path, mode, -1, null, null, binary ? null : "");
 
 		return if (binary) IoTools.createFileInputFromBytes(cast f) else IoTools.createFileInputFromText(cast f);
 	}
 
 	public static function write( path : String, binary : Bool = true ) : FileOutput {
-		var mode = "wb";
-		var f = python.lib.Builtin.open(path, mode, -1);
+		var mode = if (binary) "wb" else "w";
+		var f = python.lib.Builtin.open(path, mode, -1, null, null, binary ? null : "");
 
 		return if (binary) IoTools.createFileOutputFromBytes(cast f) else IoTools.createFileOutputFromText(cast f);
 	}
 
 	public static function append( path : String, binary : Bool = true ) : FileOutput {
-		var mode = "ab";
-		var f = python.lib.Builtin.open(path, mode, -1);
+		var mode = if (binary) "ab" else "a";
+		var f = python.lib.Builtin.open(path, mode, -1, null, null, binary ? null : "");
 
 		return if (binary) IoTools.createFileOutputFromBytes(cast f) else IoTools.createFileOutputFromText(cast f);
 	}

+ 30 - 0
std/python/io/IoTools.hx

@@ -10,6 +10,9 @@ import python.lib.io.TextIOBase;
 import sys.io.FileInput;
 import sys.io.FileOutput;
 
+import python.lib.io.IOBase.SeekSet;
+
+
 class IoTools {
 
 	public static function createFileInputFromText (t:TextIOBase) {
@@ -28,4 +31,31 @@ class IoTools {
 		return new FileOutput(new FileBytesOutput(t));
 	}
 
+	public static function seekInTextMode (stream:TextIOBase, tell:Void->Int , p : Int, pos : sys.io.FileSeek)
+	{
+ 		var pos = switch (pos) {
+ 			case SeekBegin:
+ 				SeekSet.SeekSet;
+ 			case SeekCur:
+ 				p = tell() + p;
+ 				SeekSet.SeekSet;
+ 			case SeekEnd :
+ 				stream.seek(0, SeekSet.SeekEnd);
+ 				p = tell() + p;
+ 				SeekSet.SeekSet;
+ 		}
+ 		stream.seek(p, pos);
+	}
+
+	public static function seekInBinaryMode (stream:RawIOBase, p : Int, pos : sys.io.FileSeek)
+	{
+ 		var pos = switch(pos)
+		{
+			case SeekBegin: SeekSet.SeekSet;
+			case SeekCur: SeekSet.SeekCur;
+			case SeekEnd: SeekSet.SeekEnd;
+		};
+		stream.seek(p, pos);
+	}
+
 }

+ 2 - 7
std/python/io/NativeBytesInput.hx

@@ -5,6 +5,7 @@ import haxe.io.Eof;
 import haxe.io.Input;
 
 import python.io.IInput;
+import python.io.IoTools;
 import python.lib.Builtin;
 import python.lib.io.RawIOBase;
 import python.lib.io.IOBase.SeekSet;
@@ -30,14 +31,8 @@ class NativeBytesInput extends NativeInput<RawIOBase> implements IInput {
 
 	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
 	{
-		var pos = switch(pos)
-		{
-			case SeekBegin: SeekSet.SeekSet;
-			case SeekCur: SeekSet.SeekCur;
-			case SeekEnd: SeekSet.SeekEnd;
-		};
 		wasEof = false;
-		stream.seek(p, pos);
+		return IoTools.seekInBinaryMode(stream, p, pos);
 	}
 
 

+ 1 - 7
std/python/io/NativeBytesOutput.hx

@@ -15,13 +15,7 @@ class NativeBytesOutput extends NativeOutput<RawIOBase>{
 
 	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
 	{
-		var pos = switch(pos)
-		{
-			case SeekBegin: SeekSet.SeekSet;
-			case SeekCur: SeekSet.SeekCur;
-			case SeekEnd: SeekSet.SeekEnd;
-		};
-		stream.seek(p, pos);
+		return IoTools.seekInBinaryMode(stream, p, pos);
 	}
 
 	override public function prepare(nbytes:Int):Void

+ 2 - 15
std/python/io/NativeTextInput.hx

@@ -5,6 +5,7 @@ import haxe.io.Eof;
 import haxe.io.Input;
 
 import python.io.IInput;
+import python.io.IoTools;
 import python.io.NativeInput;
 import python.lib.Builtin;
 import python.lib.io.RawIOBase;
@@ -30,21 +31,7 @@ class NativeTextInput extends NativeInput<TextIOBase> implements IInput {
 	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
 	{
 		wasEof = false;
-
-
- 		var pos = switch (pos) {
- 			case SeekBegin:
- 				SeekSet.SeekSet;
- 			case SeekCur:
- 				p = tell() + p;
- 				SeekSet.SeekSet;
- 			case SeekEnd :
- 				stream.seek(0, SeekSet.SeekEnd);
- 				p = tell() + p;
- 				SeekSet.SeekSet;
- 		}
- 		stream.seek(p, pos);
-
+		IoTools.seekInTextMode(stream, tell, p, pos);
 	}
 
 

+ 2 - 7
std/python/io/NativeTextOutput.hx

@@ -3,6 +3,7 @@ package python.io;
 
 import haxe.io.Output;
 
+import python.io.IoTools;
 import python.lib.Builtin;
 import python.lib.io.IOBase;
 import python.lib.io.RawIOBase;
@@ -17,13 +18,7 @@ class NativeTextOutput extends NativeOutput<TextIOBase> {
 
 	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
 	{
-		var pos = switch(pos)
-		{
-			case SeekBegin: SeekSet.SeekSet;
-			case SeekCur: SeekSet.SeekCur;
-			case SeekEnd: SeekSet.SeekEnd;
-		};
-		stream.seek(p, pos);
+		IoTools.seekInTextMode(stream, tell, p, pos);
 	}
 
 	override public function writeByte(c:Int):Void