Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
259:f9b201530b85 | 260:f1f7d8c7e94e |
---|---|
36 | 36 |
37 public static final LuanFunction LOADER = new LuanFunction() { | 37 public static final LuanFunction LOADER = new LuanFunction() { |
38 @Override public Object call(LuanState luan,Object[] args) { | 38 @Override public Object call(LuanState luan,Object[] args) { |
39 LuanTable module = Luan.newTable(); | 39 LuanTable module = Luan.newTable(); |
40 try { | 40 try { |
41 add( module, "File", LuanState.class, String.class ); | |
42 add( module, "read_console_line", String.class ); | 41 add( module, "read_console_line", String.class ); |
42 | |
43 module.put( "protocols", newProtocols() ); | |
44 | |
45 add( module, "get", LuanState.class, String.class ); | |
43 | 46 |
44 LuanTable stdin = Luan.newTable(); | 47 LuanTable stdin = Luan.newTable(); |
45 stdin.put( "read_text", new LuanJavaFunction( | 48 stdin.put( "read_text", new LuanJavaFunction( |
46 IoLuan.class.getMethod( "stdin_read_text" ), null | 49 IoLuan.class.getMethod( "stdin_read_text" ), null |
47 ) ); | 50 ) ); |
408 } | 411 } |
409 return tbl; | 412 return tbl; |
410 } | 413 } |
411 } | 414 } |
412 | 415 |
413 public static LuanIn luanIo(LuanState luan,String name) throws LuanException { | 416 public static LuanTable file(LuanState luan,String name) throws LuanException { |
414 check(luan,name); | 417 File file = new File(name); |
415 File file = Utils.toFile(name); | 418 if( !file.exists() ) |
416 if( file != null ) | 419 return null; |
417 return new LuanFile(file); | 420 return new LuanFile(file).table(); |
418 URL url = Utils.toUrl(name); | 421 } |
419 if( url != null ) { | 422 |
420 return new LuanUrl(url); | 423 public static LuanTable classpath(LuanState luan,String path) throws LuanException { |
421 } | 424 if( path.contains("//") ) |
422 throw luan.exception( "file '"+name+"' not found" ); | 425 return null; |
423 } | 426 URL url; |
424 | 427 if( !path.contains("#") ) { |
425 public static LuanTable File(LuanState luan,String name) throws LuanException { | 428 url = ClassLoader.getSystemResource(path); |
426 return luanIo(luan,name).table(); | 429 } else { |
430 String[] a = path.split("#"); | |
431 url = ClassLoader.getSystemResource(a[0]); | |
432 if( url==null ) { | |
433 for( int i=1; i<a.length; i++ ) { | |
434 url = ClassLoader.getSystemResource(a[0]+"/"+a[i]); | |
435 if( url != null ) { | |
436 try { | |
437 url = new URL(url,"."); | |
438 } catch(MalformedURLException e) { | |
439 throw new RuntimeException(e); | |
440 } | |
441 break; | |
442 } | |
443 } | |
444 } | |
445 } | |
446 if( url == null ) | |
447 return null; | |
448 return new LuanUrl(url).table(); | |
449 } | |
450 | |
451 private static LuanTable newProtocols() { | |
452 LuanTable protocols = Luan.newTable(); | |
453 try { | |
454 add( protocols, "file", LuanState.class, String.class ); | |
455 add( protocols, "classpath", LuanState.class, String.class ); | |
456 } catch(NoSuchMethodException e) { | |
457 throw new RuntimeException(e); | |
458 } | |
459 return protocols; | |
460 } | |
461 | |
462 private static LuanTable protocols(LuanState luan) { | |
463 LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("Io"); | |
464 if( t == null ) | |
465 return newProtocols(); | |
466 t = (LuanTable)t.get("protocols"); | |
467 if( t == null ) | |
468 return newProtocols(); | |
469 return t; | |
470 } | |
471 | |
472 public static LuanTable get(LuanState luan,String name) throws LuanException { | |
473 int i = name.indexOf(':'); | |
474 if( i == -1 ) | |
475 throw luan.exception( "invalid IO name '"+name+"', missing protocol" ); | |
476 String protocol = name.substring(0,i); | |
477 String location = name.substring(i+1); | |
478 LuanTable protocols = protocols(luan); | |
479 LuanFunction opener = (LuanFunction)protocols.get(protocol); | |
480 if( opener == null ) | |
481 throw luan.exception( "invalid protocol '"+protocol+"' in '"+name+"'" ); | |
482 return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location})); | |
427 } | 483 } |
428 | 484 |
429 public static final class LuanSocket extends LuanIO { | 485 public static final class LuanSocket extends LuanIO { |
430 private final Socket socket; | 486 private final Socket socket; |
431 | 487 |