comparison src/luan/lib/OsLib.java @ 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
children 0517a4a7fcc5
comparison
equal deleted inserted replaced
144:2e92f0a6fcac 145:90f38a5d0e0a
1 package luan.lib;
2
3 import java.io.File;
4 import luan.LuanState;
5 import luan.LuanTable;
6 import luan.LuanFunction;
7 import luan.LuanJavaFunction;
8 import luan.LuanException;
9
10
11 public final class OsLib {
12
13 public static final LuanFunction LOADER = new LuanFunction() {
14 @Override public Object call(LuanState luan,Object[] args) {
15 LuanTable module = new LuanTable();
16 try {
17 add( module, "File", String.class );
18 } catch(NoSuchMethodException e) {
19 throw new RuntimeException(e);
20 }
21 return module;
22 }
23 };
24
25 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
26 t.put( method, new LuanJavaFunction(OsLib.class.getMethod(method,parameterTypes),null) );
27 }
28
29 public static class LuanFile {
30 private final File file;
31
32 public LuanFile(String name) {
33 this(new File(name));
34 }
35
36 public LuanFile(File file) {
37 this.file = file;
38 }
39
40 public LuanTable child(String name) {
41 return new LuanFile(new File(file,name)).table();
42 }
43
44 public LuanTable io_file() {
45 return new IoLib.LuanFile(file).table();
46 }
47
48 LuanTable table() {
49 LuanTable tbl = new LuanTable();
50 try {
51 tbl.put( "name", new LuanJavaFunction(
52 File.class.getMethod( "toString" ), file
53 ) );
54 tbl.put( "exists", new LuanJavaFunction(
55 File.class.getMethod( "exists" ), file
56 ) );
57 tbl.put( "is_directory", new LuanJavaFunction(
58 File.class.getMethod( "isDirectory" ), file
59 ) );
60 tbl.put( "is_file", new LuanJavaFunction(
61 File.class.getMethod( "isFile" ), file
62 ) );
63 tbl.put( "delete", new LuanJavaFunction(
64 File.class.getMethod( "delete" ), file
65 ) );
66 tbl.put( "mkdir", new LuanJavaFunction(
67 File.class.getMethod( "mkdir" ), file
68 ) );
69 tbl.put( "mkdirs", new LuanJavaFunction(
70 File.class.getMethod( "mkdirs" ), file
71 ) );
72 tbl.put( "child", new LuanJavaFunction(
73 LuanFile.class.getMethod( "child", String.class ), this
74 ) );
75 tbl.put( "io_file", new LuanJavaFunction(
76 LuanFile.class.getMethod( "io_file" ), this
77 ) );
78 } catch(NoSuchMethodException e) {
79 throw new RuntimeException(e);
80 }
81 return tbl;
82 }
83 }
84
85 public static LuanTable File(String name) throws LuanException {
86 return new LuanFile(name).table();
87 }
88
89 }