changeset 145:90f38a5d0e0a

add Os.File git-svn-id: https://luan-java.googlecode.com/svn/trunk@146 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 15 Jun 2014 10:02:16 +0000
parents 2e92f0a6fcac
children 0517a4a7fcc5
files src/luan/lib/IoLib.java src/luan/lib/OsLib.java src/luan/lib/PickleServer.java src/luan/lib/init.luan src/luan/modules/Reactionary.luan
diffstat 5 files changed, 121 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/lib/IoLib.java	Sun Jun 15 06:42:58 2014 +0000
+++ b/src/luan/lib/IoLib.java	Sun Jun 15 10:02:16 2014 +0000
@@ -37,7 +37,7 @@
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
 			try {
-				add( module, "file", LuanState.class, String.class );
+				add( module, "File", LuanState.class, String.class );
 				add( module, "read_console_line", String.class );
 
 				LuanTable stdin = new LuanTable();
@@ -55,7 +55,7 @@
 				) );
 				module.put( "stdin", stdin );
 
-				add( module, "socket", String.class, Integer.TYPE );
+				add( module, "Socket", String.class, Integer.TYPE );
 				add( module, "socket_server", Integer.TYPE );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
@@ -330,7 +330,11 @@
 		private final File file;
 
 		public LuanFile(String name) {
-			this.file = new File(name);
+			this(new File(name));
+		}
+
+		public LuanFile(File file) {
+			this.file = file;
 		}
 
 		@Override InputStream inputStream() throws IOException {
@@ -344,6 +348,22 @@
 		@Override public String name() {
 			return file.toString();
 		}
+
+		public LuanTable os_file() {
+			return new OsLib.LuanFile(file).table();
+		}
+
+		@Override LuanTable table() {
+			LuanTable tbl = super.table();
+			try {
+				tbl.put( "os_file", new LuanJavaFunction(
+					LuanFile.class.getMethod( "os_file" ), this
+				) );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
+			}
+			return tbl;
+		}
 	}
 
 	public static LuanIn luanIo(LuanState luan,String name) throws LuanException {
@@ -359,7 +379,7 @@
 		}
 	}
 
-	public static LuanTable file(LuanState luan,String name) throws LuanException {
+	public static LuanTable File(LuanState luan,String name) throws LuanException {
 		return luanIo(luan,name).table();
 	}
 
@@ -386,7 +406,7 @@
 			return socket.toString();
 		}
 
-		public LuanTable pickle_client(LuanState luan) throws IOException {
+		public LuanTable Pickle_client(LuanState luan) throws IOException {
 			DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream()));
 			DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream()));
 			return new PickleClient(luan,in,out).table();
@@ -401,8 +421,8 @@
 		@Override LuanTable table() {
 			LuanTable tbl = super.table();
 			try {
-				tbl.put( "pickle_client", new LuanJavaFunction(
-					LuanSocket.class.getMethod( "pickle_client", LuanState.class ), this
+				tbl.put( "Pickle_client", new LuanJavaFunction(
+					LuanSocket.class.getMethod( "Pickle_client", LuanState.class ), this
 				) );
 				tbl.put( "run_pickle_server", new LuanJavaFunction(
 					LuanSocket.class.getMethod( "run_pickle_server", LuanState.class ), this
@@ -414,7 +434,7 @@
 		}
 	}
 
-	public static LuanTable socket(String host,int port) throws IOException {
+	public static LuanTable Socket(String host,int port) throws IOException {
 		return new LuanSocket(host,port).table();
 	}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/lib/OsLib.java	Sun Jun 15 10:02:16 2014 +0000
@@ -0,0 +1,89 @@
+package luan.lib;
+
+import java.io.File;
+import luan.LuanState;
+import luan.LuanTable;
+import luan.LuanFunction;
+import luan.LuanJavaFunction;
+import luan.LuanException;
+
+
+public final class OsLib {
+
+	public static final LuanFunction LOADER = new LuanFunction() {
+		@Override public Object call(LuanState luan,Object[] args) {
+			LuanTable module = new LuanTable();
+			try {
+				add( module, "File", String.class );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
+			}
+			return module;
+		}
+	};
+
+	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
+		t.put( method, new LuanJavaFunction(OsLib.class.getMethod(method,parameterTypes),null) );
+	}
+
+	public static class LuanFile {
+		private final File file;
+
+		public LuanFile(String name) {
+			this(new File(name));
+		}
+
+		public LuanFile(File file) {
+			this.file = file;
+		}
+
+		public LuanTable child(String name) {
+			return new LuanFile(new File(file,name)).table();
+		}
+
+		public LuanTable io_file() {
+			return new IoLib.LuanFile(file).table();
+		}
+
+		LuanTable table() {
+			LuanTable tbl = new LuanTable();
+			try {
+				tbl.put( "name", new LuanJavaFunction(
+					File.class.getMethod( "toString" ), file
+				) );
+				tbl.put( "exists", new LuanJavaFunction(
+					File.class.getMethod( "exists" ), file
+				) );
+				tbl.put( "is_directory", new LuanJavaFunction(
+					File.class.getMethod( "isDirectory" ), file
+				) );
+				tbl.put( "is_file", new LuanJavaFunction(
+					File.class.getMethod( "isFile" ), file
+				) );
+				tbl.put( "delete", new LuanJavaFunction(
+					File.class.getMethod( "delete" ), file
+				) );
+				tbl.put( "mkdir", new LuanJavaFunction(
+					File.class.getMethod( "mkdir" ), file
+				) );
+				tbl.put( "mkdirs", new LuanJavaFunction(
+					File.class.getMethod( "mkdirs" ), file
+				) );
+				tbl.put( "child", new LuanJavaFunction(
+					LuanFile.class.getMethod( "child", String.class ), this
+				) );
+				tbl.put( "io_file", new LuanJavaFunction(
+					LuanFile.class.getMethod( "io_file" ), this
+				) );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
+			}
+			return tbl;
+		}
+	}
+
+	public static LuanTable File(String name) throws LuanException {
+		return new LuanFile(name).table();
+	}
+
+}
--- a/src/luan/lib/PickleServer.java	Sun Jun 15 06:42:58 2014 +0000
+++ b/src/luan/lib/PickleServer.java	Sun Jun 15 10:02:16 2014 +0000
@@ -30,6 +30,7 @@
 					list.add( con.pickle(obj) );
 				}
 			} catch(LuanException e) {
+//e.printStackTrace();
 				list.add( "return false, " );
 				list.add( con.pickle(e.getMessage()) );
 				list.add( ", " );
--- a/src/luan/lib/init.luan	Sun Jun 15 06:42:58 2014 +0000
+++ b/src/luan/lib/init.luan	Sun Jun 15 10:02:16 2014 +0000
@@ -17,6 +17,7 @@
 Package.load_java("Html","luan.lib.HtmlLib.LOADER")
 Package.load_java("Thread","luan.lib.ThreadLib.LOADER")
 Package.load_java("Binary","luan.lib.BinaryLib.LOADER")
+Package.load_java("Os","luan.lib.OsLib.LOADER")
 Package.preload_java("Java","luan.lib.JavaLib.LOADER")
 Package.preload_java("Http","luan.lib.HttpLib.LOADER")
 
--- a/src/luan/modules/Reactionary.luan	Sun Jun 15 06:42:58 2014 +0000
+++ b/src/luan/modules/Reactionary.luan	Sun Jun 15 10:02:16 2014 +0000
@@ -4,7 +4,7 @@
 port = 9101
 
 function put(domain,password,dir)
-	local pc = Io.socket(host,port).pickle_client()
+	local pc = Io.Socket(host,port).Pickle_client()
 	local pickle = pc.pickle
 	pc.call %>
 		Reactionary.do_put(<%=pickle(domain)%>,<%=pickle(password)%>,<%=pickle(dir)%>)
@@ -13,11 +13,10 @@
 end
 
 function delete(domain,password)
-	local pc = Io.socket(host,port).pickle_client()
+	local pc = Io.Socket(host,port).Pickle_client()
 	local pickle = pc.pickle
 	pc.call %>
 		Reactionary.do_delete(<%=pickle(domain)%>,<%=pickle(password)%>)
 	<%
 	pc.close()
 end
-