浏览代码

[cs] FileSystem API

Caue Waneck 13 年之前
父节点
当前提交
0e0d79f06b

+ 5 - 0
std/cs/Lib.hx

@@ -74,4 +74,9 @@ class Lib
 	{
 		return untyped obj.GetType();
 	}
+	
+	public static function fromNativeArray<T>(native:cs.NativeArray<T>):Array<T>
+	{
+		return untyped Array.ofNative(native);
+	}
 }

+ 8 - 0
std/cs/_std/Date.hx

@@ -126,6 +126,7 @@ class Date
 		d.date = new DateTime(cast(t, Int64));
 		return d;
 	}
+	
 
 	/**
 		Returns a Date from a formated string of one of the following formats :
@@ -152,4 +153,11 @@ class Date
 				throw "Invalid date format : " + s;
 		}
 	}
+	
+	private static function fromNative( d : system.DateTime ) : Date
+	{
+		var date = new Date(0, 0, 0, 0, 0, 0);
+		date.date = d;
+		return date;
+	}
 }

+ 159 - 0
std/cs/_std/sys/FileSystem.hx

@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package sys;
+import system.io.DirectoryInfo;
+import system.io.File;
+import system.io.Directory;
+import system.io.FileInfo;
+
+/**
+	This class allows you to get informations about the files and directories.
+**/
+class FileSystem {
+
+	/**
+		Tells if the given file or directory exists.
+	**/
+	public static function exists( path : String ) : Bool
+	{
+		return (File.Exists(path) || Directory.Exists(path));
+	}
+
+	/**
+		Rename the corresponding file or directory, allow to move it accross directories as well.
+	**/
+	public static function rename( path : String, newpath : String ) : Void
+	{
+		Directory.Move(path, newpath);
+	}
+
+	/**
+		Returns informations for the given file/directory.
+	**/
+	public static function stat( path : String ) : FileStat
+	{
+		if (File.Exists(path))
+		{
+			var fi = new FileInfo(path);
+			return {
+				gid: 0, //C# doesn't let you get this info
+				uid: 0, //same
+				atime: untyped Date.fromNative(fi.LastAccessTime),
+				mtime: untyped Date.fromNative(fi.LastWriteTime),
+				ctime: untyped Date.fromNative(fi.CreationTime),
+				size: cast(fi.Length, Int), //TODO: maybe change to Int64 for Haxe 3?
+				dev: 0, //FIXME: not sure what that is
+				ino: 0, //FIXME: not sure what that is
+				nlink: 0, //FIXME: not sure what that is
+				rdev: 0, //FIXME: not sure what that is
+				mode: 0 //FIXME: not sure what that is
+			};
+		} else if (Directory.Exists(path)) {
+			var fi = new DirectoryInfo(path);
+			return {
+				gid: 0, //C# doesn't let you get this info
+				uid: 0, //same
+				atime: untyped Date.fromNative(fi.LastAccessTime),
+				mtime: untyped Date.fromNative(fi.LastWriteTime),
+				ctime: untyped Date.fromNative(fi.CreationTime),
+				size: 0, //TODO: maybe change to Int64 for Haxe 3?
+				dev: 0, //FIXME: not sure what that is
+				ino: 0, //FIXME: not sure what that is
+				nlink: 0, //FIXME: not sure what that is
+				rdev: 0, //FIXME: not sure what that is
+				mode: 0 //FIXME: not sure what that is
+			};
+		} else {
+			throw "Path '" + path + "' doesn't exist";
+		}
+		
+	}
+
+	/**
+		Returns the full path for the given path which is relative to the current working directory.
+	**/
+	public static function fullPath( relpath : String ) : String
+	{
+		return new FileInfo(relpath).FullName;
+	}
+
+	/**
+		Tells if the given path is a directory. Throw an exception if it does not exists or is not accesible.
+	**/
+	public static function isDirectory( path : String ) : Bool
+	{
+		var isdir = Directory.Exists(path);
+		if (isdir != File.Exists(path))
+			return isdir;
+		throw "Path '" + path + "' doesn't exist";
+	}
+
+	/**
+		Create the given directory. Not recursive : the parent directory must exists.
+	**/
+	public static function createDirectory( path : String ) : Void
+	{
+		Directory.CreateDirectory(path);
+	}
+
+	/**
+		Delete a given file.
+	**/
+	public static function deleteFile( path : String ) : Void
+	{
+		File.Delete(path);
+	}
+	
+	/**
+		Delete a given directory.
+	**/
+	public static function deleteDirectory( path : String ) : Void
+	{
+		Directory.Delete(path);
+	}
+
+	/**
+		Read all the files/directories stored into the given directory.
+	**/
+	public static function readDirectory( path : String ) : Array<String>
+	{
+		var ret = Directory.GetFileSystemEntries(path);
+		if (ret.Length > 0)
+		{
+			var fst = ret[0];
+			var sep = "/";
+			if (fst.lastIndexOf(sep) < fst.lastIndexOf("\\"))
+				sep = "\\";
+			for (i in 0...ret.Length)
+			{
+				var path = ret[i];
+				ret[i] = path.substr(path.lastIndexOf(sep) + 1);
+			}
+		}
+		
+		return cs.Lib.fromNativeArray( ret );
+	}
+
+}

+ 20 - 0
std/cs/_std/system/io/Directory.hx

@@ -0,0 +1,20 @@
+package system.io;
+import cs.NativeArray;
+import cs.NativeArray;
+
+@:native('System.IO.Directory') extern class Directory 
+{
+	static function CreateDirectory(path:String):Void;
+	static function Delete(path:String):Void;
+	static function Exists(path:String):Bool;
+	static function GetCurrentDirectory():String;
+	static function GetDirectories(path:String):NativeArray<String>;
+	static function GetDirectoryRoot(path:String):String;
+	static function GetFiles(path:String):NativeArray<String>;
+	static function GetFileSystemEntries(path:String):NativeArray<String>;
+	static function GetCreationTime(path:String):system.DateTime;
+	static function GetLastAccessTime(path:String):system.DateTime;
+	static function GetLastWriteTime(path:String):system.DateTime;
+	static function Move(path:String, newpath:String):Void;
+	static function SetCurrentDirectory(path:String):Void;
+}

+ 18 - 0
std/cs/_std/system/io/DirectoryInfo.hx

@@ -0,0 +1,18 @@
+package system.io;
+import haxe.Int64;
+
+@:final @:native('System.IO.DirectoryInfo') extern class DirectoryInfo 
+{
+	var Attributes(default, null):FileAttributes;
+	var CreationTime(default, null):system.DateTime;
+	var Exists(default, null):Bool;
+	var Extension(default, null):String;
+	var FullName(default, null):String;
+	var LastAccessTime(default, null):system.DateTime;
+	var LastWriteTime(default, null):system.DateTime;
+	var Name(default, null):String;
+	var Parent(default, null):DirectoryInfo;
+	var Root(default, null):DirectoryInfo;
+	
+	function new(path:String):Void;
+}

+ 20 - 0
std/cs/_std/system/io/File.hx

@@ -0,0 +1,20 @@
+package system.io;
+
+@:native('System.IO.File') extern class File 
+{
+	//not compatible with XNA
+	//static function AppendAllText(path:String, contents:String):Void;
+	static function Copy(source:String, dest:String):Void;
+	
+	@:overload(function(path:String, bufferSize:Int):FileStream {})
+	static function Create(path:String):FileStream;
+	
+	static function Delete(path:String):Void;
+	static function Exists(path:String):Bool;
+	static function GetCreationTime(path:String):system.DateTime;
+	static function GetLastAccessTime(path:String):system.DateTime;
+	static function GetLastWriteTime(path:String):system.DateTime;
+	static function Move(source:String, dest:String):Void;
+	
+	
+}

+ 8 - 0
std/cs/_std/system/io/FileAccess.hx

@@ -0,0 +1,8 @@
+package system.io;
+
+@:native('System.IO.FileAccess') extern enum FileAccess 
+{
+	Read;
+	Write;
+	ReadWrite;
+}

+ 19 - 0
std/cs/_std/system/io/FileAttributes.hx

@@ -0,0 +1,19 @@
+package system.io;
+
+@:native('System.IO.FileAttributes') extern enum FileAttributes 
+{
+	ReadOnly;
+	Hidden;
+	System;
+	Directory;
+	Archive;
+	Device;
+	Normal;
+	Temporary;
+	SparseFile;
+	ReparsePoint;
+	Compressed;
+	Offline;
+	NotContentIndexed;
+	Encrypted;
+}

+ 19 - 0
std/cs/_std/system/io/FileInfo.hx

@@ -0,0 +1,19 @@
+package system.io;
+import haxe.Int64;
+
+@:final @:native('System.IO.FileInfo') extern class FileInfo 
+{
+	var Attributes(default, null):FileAttributes;
+	var CreationTime(default, null):system.DateTime;
+	var Directory(default, null):DirectoryInfo;
+	var DirectoryName(default, null):String;
+	var Exists(default, null):Bool;
+	var Extension(default, null):String;
+	var FullName(default, null):String;
+	var LastAccessTime(default, null):system.DateTime;
+	var LastWriteTime(default, null):system.DateTime;
+	var Length(default, null):Int64;
+	var Name(default, null):String;
+	
+	function new(filename:String):Void;
+}

+ 11 - 0
std/cs/_std/system/io/FileMode.hx

@@ -0,0 +1,11 @@
+package system.io;
+
+@:native('System.IO.FileMode') extern enum FileMode 
+{
+	CreateNew;
+	Create;
+	Open;
+	OpenOrCreate;
+	Truncate;
+	Append;
+}

+ 9 - 0
std/cs/_std/system/io/FileShare.hx

@@ -0,0 +1,9 @@
+package system.io;
+
+@:native('System.IO.FileShare') extern enum FileShare 
+{
+	None;
+	Read;
+	Write;
+	ReadWrite;
+}

+ 12 - 0
std/cs/_std/system/io/FileStream.hx

@@ -0,0 +1,12 @@
+package system.io;
+
+@:native('System.IO.FileStream') extern class FileStream extends Stream
+{
+	@:overload(function (path:String, mode:FileMode, access:FileAccess, share:FileShare, bufferSize:Int, useAsync:Bool):Void {})
+	@:overload(function (path:String, mode:FileMode, access:FileAccess, share:FileShare, bufferSize:Int):Void {})
+	@:overload(function (path:String, mode:FileMode, access:FileAccess, share:FileShare):Void {})
+	@:overload(function (path:String, mode:FileMode, access:FileAccess):Void {})
+	function new(path:String, mode:FileMode):Void;
+	var IsAsync(default, null):Bool;
+	var Name(default, null):String;
+}

+ 8 - 0
std/cs/_std/system/io/SeekOrigin.hx

@@ -0,0 +1,8 @@
+package system.io;
+
+@:native('System.IO.SeekOrigin') extern enum SeekOrigin 
+{
+	Begin;
+	Current;
+	End;
+}

+ 1 - 1
std/cs/_std/system/io/Stream.hx

@@ -20,7 +20,7 @@ import haxe.io.BytesData;
 	function Flush():Void;
 	function Read(buf:BytesData, offset:Int, count:Int):Int;
 	function ReadByte():Int;
-	//function Seek(offset:Int64, 
+	function Seek(offset:Int64, origin:SeekOrigin):Int64;
 	function SetLength(value:Int64):Void;
 	function Write(buf:BytesData, offset:Int, count:Int):Void;
 	function WriteByte(value:UInt8):Void;