Mercurial Hosting > luan
changeset 164:78ba371ea1e9
merge Os.File into Io.File and remove Os
git-svn-id: https://luan-java.googlecode.com/svn/trunk@165 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 20 Jun 2014 23:23:16 +0000 |
parents | 3c95a2291d64 |
children | 94bbc4cbc106 |
files | src/luan/lib/IoLib.java src/luan/lib/OsLib.java src/luan/lib/init.luan |
diffstat | 3 files changed, 53 insertions(+), 114 deletions(-) [+] |
line wrap: on
line diff
diff -r 3c95a2291d64 -r 78ba371ea1e9 src/luan/lib/IoLib.java --- a/src/luan/lib/IoLib.java Fri Jun 20 09:32:36 2014 +0000 +++ b/src/luan/lib/IoLib.java Fri Jun 20 23:23:16 2014 +0000 @@ -349,15 +349,57 @@ return file.toString(); } - public LuanTable os_file() { - return new OsLib.LuanFile(file).table(); + public LuanTable child(String name) { + return new LuanFile(new File(file,name)).table(); + } + + public LuanTable children() { + File[] files = file.listFiles(); + if( files==null ) + return null; + LuanTable list = new LuanTable(); + for( File f : files ) { + list.add(new LuanFile(f).table()); + } + return list; + } + + public boolean exists() { + return Utils.exists(file); } @Override LuanTable table() { LuanTable tbl = super.table(); try { - tbl.put( "os_file", new LuanJavaFunction( - LuanFile.class.getMethod( "os_file" ), this + tbl.put( "name", new LuanJavaFunction( + File.class.getMethod( "getName" ), file + ) ); + tbl.put( "exists", new LuanJavaFunction( + LuanFile.class.getMethod( "exists" ), this + ) ); + 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( "last_modified", new LuanJavaFunction( + File.class.getMethod( "lastModified" ), file + ) ); + tbl.put( "child", new LuanJavaFunction( + LuanFile.class.getMethod( "child", String.class ), this + ) ); + tbl.put( "children", new LuanJavaFunction( + LuanFile.class.getMethod( "children" ), this ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); @@ -370,13 +412,14 @@ if( Utils.isFile(name) ) return new LuanFile(name); String url = Utils.toUrl(name); - if( url == null ) - throw luan.exception( "file '"+name+"' not found" ); - try { - return new LuanUrl(url); - } catch(MalformedURLException e) { - throw new RuntimeException(e); + if( url != null ) { + try { + return new LuanUrl(url); + } catch(MalformedURLException e) { + throw new RuntimeException(e); + } } + throw luan.exception( "file '"+name+"' not found" ); } public static LuanTable File(LuanState luan,String name) throws LuanException {
diff -r 3c95a2291d64 -r 78ba371ea1e9 src/luan/lib/OsLib.java --- a/src/luan/lib/OsLib.java Fri Jun 20 09:32:36 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,103 +0,0 @@ -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 children() { - File[] files = file.listFiles(); - if( files==null ) - return null; - LuanTable list = new LuanTable(); - for( File f : files ) { - list.add(new LuanFile(f).table()); - } - return list; - } - - public boolean exists() { - return Utils.exists(file); - } - - LuanTable table() { - LuanTable tbl = new IoLib.LuanFile(file).table(); - try { - tbl.put( "name", new LuanJavaFunction( - File.class.getMethod( "getName" ), file - ) ); - tbl.put( "exists", new LuanJavaFunction( - LuanFile.class.getMethod( "exists" ), this - ) ); - 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( "last_modified", new LuanJavaFunction( - File.class.getMethod( "lastModified" ), file - ) ); - tbl.put( "child", new LuanJavaFunction( - LuanFile.class.getMethod( "child", String.class ), this - ) ); - tbl.put( "children", new LuanJavaFunction( - LuanFile.class.getMethod( "children" ), this - ) ); - } catch(NoSuchMethodException e) { - throw new RuntimeException(e); - } - return tbl; - } - } - - public static LuanTable File(String name) throws LuanException { - return new LuanFile(name).table(); - } - -}
diff -r 3c95a2291d64 -r 78ba371ea1e9 src/luan/lib/init.luan --- a/src/luan/lib/init.luan Fri Jun 20 09:32:36 2014 +0000 +++ b/src/luan/lib/init.luan Fri Jun 20 23:23:16 2014 +0000 @@ -55,7 +55,6 @@ Package.global_import("Html","luan.lib.HtmlLib.LOADER") Package.global_import("Thread","luan.lib.ThreadLib.LOADER") Package.global_import("Binary","luan.lib.BinaryLib.LOADER") -Package.global_import("Os","luan.lib.OsLib.LOADER") function Io.print_to(out,...)