Mercurial Hosting > luan
diff core/src/luan/modules/IoLuan.java @ 260:f1f7d8c7e94e
add Io.protocols
git-svn-id: https://luan-java.googlecode.com/svn/trunk@261 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 28 Oct 2014 23:25:13 +0000 |
parents | 97d175772fed |
children | 715c4a6e1169 |
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java Tue Oct 28 21:20:48 2014 +0000 +++ b/core/src/luan/modules/IoLuan.java Tue Oct 28 23:25:13 2014 +0000 @@ -38,9 +38,12 @@ @Override public Object call(LuanState luan,Object[] args) { LuanTable module = Luan.newTable(); try { - add( module, "File", LuanState.class, String.class ); add( module, "read_console_line", String.class ); + module.put( "protocols", newProtocols() ); + + add( module, "get", LuanState.class, String.class ); + LuanTable stdin = Luan.newTable(); stdin.put( "read_text", new LuanJavaFunction( IoLuan.class.getMethod( "stdin_read_text" ), null @@ -410,20 +413,73 @@ } } - public static LuanIn luanIo(LuanState luan,String name) throws LuanException { - check(luan,name); - File file = Utils.toFile(name); - if( file != null ) - return new LuanFile(file); - URL url = Utils.toUrl(name); - if( url != null ) { - return new LuanUrl(url); - } - throw luan.exception( "file '"+name+"' not found" ); + public static LuanTable file(LuanState luan,String name) throws LuanException { + File file = new File(name); + if( !file.exists() ) + return null; + return new LuanFile(file).table(); } - public static LuanTable File(LuanState luan,String name) throws LuanException { - return luanIo(luan,name).table(); + public static LuanTable classpath(LuanState luan,String path) throws LuanException { + if( path.contains("//") ) + return null; + URL url; + if( !path.contains("#") ) { + url = ClassLoader.getSystemResource(path); + } else { + String[] a = path.split("#"); + url = ClassLoader.getSystemResource(a[0]); + if( url==null ) { + for( int i=1; i<a.length; i++ ) { + url = ClassLoader.getSystemResource(a[0]+"/"+a[i]); + if( url != null ) { + try { + url = new URL(url,"."); + } catch(MalformedURLException e) { + throw new RuntimeException(e); + } + break; + } + } + } + } + if( url == null ) + return null; + return new LuanUrl(url).table(); + } + + private static LuanTable newProtocols() { + LuanTable protocols = Luan.newTable(); + try { + add( protocols, "file", LuanState.class, String.class ); + add( protocols, "classpath", LuanState.class, String.class ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + return protocols; + } + + private static LuanTable protocols(LuanState luan) { + LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("Io"); + if( t == null ) + return newProtocols(); + t = (LuanTable)t.get("protocols"); + if( t == null ) + return newProtocols(); + return t; + } + + public static LuanTable get(LuanState luan,String name) throws LuanException { + int i = name.indexOf(':'); + if( i == -1 ) + throw luan.exception( "invalid IO name '"+name+"', missing protocol" ); + String protocol = name.substring(0,i); + String location = name.substring(i+1); + LuanTable protocols = protocols(luan); + LuanFunction opener = (LuanFunction)protocols.get(protocol); + if( opener == null ) + throw luan.exception( "invalid protocol '"+protocol+"' in '"+name+"'" ); + return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location})); } public static final class LuanSocket extends LuanIO {