Nicolas Cannasse 19 лет назад
Родитель
Сommit
44ca51d376
4 измененных файлов с 357 добавлено и 2 удалено
  1. 2 0
      std/haxe/ImportAll.hx
  2. 131 2
      std/neko/File.hx
  3. 113 0
      std/neko/FileSystem.hx
  4. 111 0
      std/neko/Sys.hx

+ 2 - 0
std/haxe/ImportAll.hx

@@ -114,6 +114,8 @@ import neko.Boot;
 import neko.Lib;
 import neko.Web;
 import neko.File;
+import neko.FileSystem;
+import neko.Sys;
 
 import neko.db.Mysql;
 import neko.db.Connection;

+ 131 - 2
std/neko/File.hx

@@ -1,11 +1,140 @@
+/*
+ * 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 neko;
 
+/**
+	API for reading and writing to files.
+**/
 class File {
 
+	private var __f : Void;
+
+	private function new(f) {
+		__f = f;
+	}
+
+	public function name() : String {
+		return new String(file_name(__f));
+	}
+
+	public function read( nbytes : Int ) : String {
+		var s = untyped __dollar__smake(nbytes);
+		var p = 0;
+		while( nbytes > 0 ) {
+			var k = file_read(__f,s,p,nbytes);
+			if( k == 0 ) throw "Blocked";
+			p += k;
+			nbytes -= k;
+		}
+		return new String(s);
+	}
+
+	public function readBuf( s : String, p : Int, l : Int ) : Int {
+		return file_read(__f,untyped s.__s,p,l);
+	}
+
+	public function readChar() : Int {
+		return file_read_char(__f);
+	}
+
+	public function write( str : String ) {
+		var l = str.length;
+		var p = 0;
+		while( l > 0 ) {
+			var k = file_write(__f,untyped str.__s,p,l);
+			if( k == 0 ) throw "Blocked";
+			p += k;
+			l -= k;
+		}
+	}
+
+	public function writeSub( s : String, p : Int, l : Int ) : Int {
+		return file_write(__f,untyped s.__s,p,l);
+	}
+
+	public function writeChar( c : Int ) {
+		file_write_char(__f,c);
+	}
+
+	public function close() {
+		file_close(__f);
+	}
+
+	public function seekBegin( p : Int ) : Int {
+		return file_seek(__f,p,0);
+	}
+
+	public function seekEnd( p : Int ) : Int {
+		return file_seek(__f,p,2);
+	}
+
+	public function seek( p : Int ) : Int {
+		return file_seek(__f,p,1);
+	}
+
+	public function tell() : Int {
+		return file_tell(__f);
+	}
+
+	public function eof() : Bool {
+		return file_eof(__f);
+	}
+
+	public function flush() {
+		file_flush(__f);
+	}
+
+	// --- Statics ----
+
 	public static function getContent( filename : String ) {
-		return new String(_content(untyped filename.__s));
+		return new String(file_contents(untyped filename.__s));
+	}
+
+	public static function read( path : String, binary : Bool ) {
+		return new File(untyped file_open(path.__s,(if( binary ) "r" else "rb").__s));
+	}
+
+	public static function write( path : String, binary : Bool ) {
+		return new File(untyped file_open(path.__s,(if( binary ) "w" else "wb").__s));
+	}
+
+	public static function append( path : String, binary : Bool ) {
+		return new File(untyped file_open(path.__s,(if( binary ) "a" else "ab").__s));
 	}
 
-	private static var _content = Lib.load("std","file_contents",1);
+	private static var file_contents = Lib.load("std","file_contents",1);
+	private static var file_open = Lib.load("std","file_open",2);
+	private static var file_close = Lib.load("std","file_close",1);
+	private static var file_name = Lib.load("std","file_name",1);
+	private static var file_write = Lib.load("std","file_write",4);
+	private static var file_read = Lib.load("std","file_read",4);
+	private static var file_read_char = Lib.load("std","file_read_char",1);
+	private static var file_write_char = Lib.load("std","file_write_char",2);
+	private static var file_seek = Lib.load("std","file_seek",3);
+	private static var file_tell = Lib.load("std","file_tell",1);
+	private static var file_eof = Lib.load("std","file_eof",1);
+	private static var file_flush = Lib.load("std","file_flush",1);
 
 }

+ 113 - 0
std/neko/FileSystem.hx

@@ -0,0 +1,113 @@
+/*
+ * 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 neko;
+
+interface FileStat {
+	var gid : Int;
+	var uid : Int;
+	var atime : Date;
+	var mtime : Date;
+	var ctime : Date;
+	var dev : Int;
+	var ino : Int;
+	var nlink : Int;
+	var rdev : Int;
+	var size : Int;
+}
+
+enum FileKind {
+	kdir;
+	kfile;
+	kother( k : String );
+}
+
+class FileSystem {
+
+	public static function exists( path : String ) : Bool {
+		return file_exists(untyped path.__s);
+	}
+
+	public static function rename( path : String, newpath : String ) {
+		untyped sys_rename(path.__s,newpath.__s);
+	}
+
+	public static function stat( path : String ) : FileStat {
+		var s : FileStat = sys_stat(untyped path.__s);
+		s.atime = untyped Date.new1(s.atime);
+		s.mtime = untyped Date.new1(s.mtime);
+		s.ctime = untyped Date.new1(s.ctime);
+		return s;
+	}
+
+	public static function fullPath( relpath : String ) : String {
+		return new String(file_full_path(untyped relpath.__s));
+	}
+
+	public static function kind( path : String ) : FileKind {
+		var k = new String(sys_file_type(untyped path.__s));
+		return switch(k) {
+		case "file": kfile;
+		case "dir": kdir;
+		default: kother(k);
+		}
+	}
+
+	public static function isDirectory( path : String ) : Bool {
+		return kind(path) == kdir;
+	}
+
+	public static function createDir( path : String ) {
+		sys_create_dir( untyped path.__s );
+	}
+
+	public static function deleteFile( path : String ) {
+		file_delete(untyped path.__s);
+	}
+
+	public static function deleteDir( path : String ) {
+		sys_remove_dir(untyped path.__s);
+	}
+
+	public static function readDir( path : String ) : Array<String> {
+		var l = sys_read_dir(untyped path.__s);
+		var a = new Array();
+		while( l != null ) {
+			a.push(new String(l[0]));
+			l = l[1];
+		}
+		return a;
+	}
+
+	private static var file_exists = Lib.load("std","file_exists",1);
+	private static var file_delete = Lib.load("std","file_delete",1);
+	private static var sys_rename = Lib.load("std","sys_rename",2);
+	private static var sys_stat = Lib.load("std","sys_stat",1);
+	private static var sys_file_type = Lib.load("std","sys_file_type",1);
+	private static var sys_create_dir = Lib.load("std","sys_create_dir",2);
+	private static var sys_remove_dir = Lib.load("std","sys_remove_dir",1);
+	private static var sys_read_dir = Lib.load("std","sys_read_dir",1);
+	private static var file_full_path = Lib.load("std","file_full_path",1);
+
+}

+ 111 - 0
std/neko/Sys.hx

@@ -0,0 +1,111 @@
+/*
+ * 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 neko;
+
+class Sys {
+
+	public static function getEnv( s : String ) {
+		return new String(get_env(untyped s.__s));
+	}
+
+	public static function putEnv( s : String, v : String ) {
+		untyped put_env(s.__s,if( v == null ) null else v.__s);
+	}
+
+	public static function sleep( seconds : Float ) {
+		_sleep(seconds);
+	}
+
+	public static function setTimeLocale( loc : String ) : Bool {
+		return set_time_locale(untyped loc.__s);
+	}
+
+	public static function getCwd() : String {
+		return new String(get_cwd());
+	}
+
+	public static function setCwd( s : String ) {
+		set_cwd(untyped s.__s);
+	}
+
+	public static function systemName() : String {
+		return new String(sys_string());
+	}
+
+	public static function command( cmd : String ) : Int {
+		return sys_command(untyped cmd.__s);
+	}
+
+	public static function exit( code : Int ) {
+		sys_exit(code);
+	}
+
+	public static function time() : Float {
+		return sys_time();
+	}
+
+	public static function executablePath() : String {
+		return new String(sys_exe_path());
+	}
+
+	public static function environment() : Hash<String> {
+		var l = sys_env();
+		var h = new Hash();
+		while( l != null ) {
+			h.set(new String(l[0]),new String(l[1]));
+			l = l[2];
+		}
+		return h;
+	}
+
+	public static function stdin() {
+		return untyped new File(file_stdin());
+	}
+
+	public static function stdout() {
+		return untyped new File(file_stdout());
+	}
+
+	public static function stderr() {
+		return untyped new File(file_stderr());
+	}
+
+	private static var get_env = Lib.load("std","get_env",1);
+	private static var put_env = Lib.load("std","put_env",2);
+	private static var _sleep = Lib.load("std","sys_sleep",1);
+	private static var set_time_locale = Lib.load("std","set_time_locale",1);
+	private static var get_cwd = Lib.load("std","get_cwd",0);
+	private static var set_cwd = Lib.load("std","set_cwd",1);
+	private static var sys_string = Lib.load("std","sys_string",0);
+	private static var sys_command = Lib.load("std","sys_command",1);
+	private static var sys_exit = Lib.load("std","sys_exit",1);
+	private static var sys_time = Lib.load("std","sys_time",0);
+	private static var sys_exe_path = Lib.load("std","sys_exe_path",0);
+	private static var sys_env = Lib.load("std","sys_env",0);
+	private static var file_stdin = Lib.load("std","file_stdin",0);
+	private static var file_stdout = Lib.load("std","file_stdout",0);
+	private static var file_stderr = Lib.load("std","file_stderr",0);
+
+}