Mercurial Hosting > luan
diff core/src/luan/modules/IoLuan.java @ 265:454a486d9c19
allow IO on files that don't exist
git-svn-id: https://luan-java.googlecode.com/svn/trunk@266 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 29 Oct 2014 16:26:42 +0000 |
parents | 9e0d4452e649 |
children | 1507149fc447 |
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java Wed Oct 29 03:50:59 2014 +0000 +++ b/core/src/luan/modules/IoLuan.java Wed Oct 29 16:26:42 2014 +0000 @@ -18,6 +18,7 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.io.FileNotFoundException; import java.net.URL; import java.net.Socket; import java.net.ServerSocket; @@ -210,11 +211,22 @@ try { String src = read_text(); return BasicLuan.load(luan,src,name,null,false); + } catch(FileNotFoundException e) { + return null; } catch(IOException e) { throw luan.exception(e); } } + public boolean exists() throws IOException { + try { + inputStream().close(); + return true; + } catch(FileNotFoundException e) { + return false; + } + } + LuanTable table() { LuanTable tbl = Luan.newTable(); try { @@ -236,6 +248,9 @@ tbl.put( "loader", new LuanJavaFunction( LuanIn.class.getMethod( "loader", LuanState.class, String.class ), this ) ); + tbl.put( "exists", new LuanJavaFunction( + LuanIn.class.getMethod( "exists" ), this + ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -260,6 +275,10 @@ @Override public byte[] read_binary() throws IOException { return Utils.readAll(System.in); } + + @Override public boolean exists() { + return true; + } }; public static abstract class LuanIO extends LuanIn { @@ -365,7 +384,7 @@ return list; } - public boolean exists() { + @Override public boolean exists() { return file.exists(); } @@ -375,9 +394,6 @@ 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 ) ); @@ -413,8 +429,6 @@ if( Boolean.TRUE.equals(loading) ) name += ".luan"; File file = new File(name); - if( !file.exists() ) - return null; return new LuanFile(file).table(); } @@ -504,6 +518,11 @@ return classpath( luan, "luan/modules/" + path, loading ); } + public static LuanTable stdin(LuanState luan) throws LuanException { + LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); + return (LuanTable)io.get("stdin"); + } + private static LuanTable newProtocols() { LuanTable protocols = Luan.newTable(); try { @@ -516,6 +535,7 @@ IoLuan.class.getMethod( "_class", LuanState.class, String.class, Boolean.class ), null ) ); add( protocols, "luan", LuanState.class, String.class, Boolean.class ); + add( protocols, "stdin", LuanState.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); }