Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 29:9bc66d09ea48
CmdLine improvements
git-svn-id: https://luan-java.googlecode.com/svn/trunk@30 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 07 Dec 2012 09:33:31 +0000 |
parents | 9de9be530625 |
children | 8d8f4f5caef4 |
comparison
equal
deleted
inserted
replaced
28:df923e5835b2 | 29:9bc66d09ea48 |
---|---|
1 package luan.lib; | 1 package luan.lib; |
2 | 2 |
3 import java.io.File; | |
4 import java.io.Reader; | |
3 import java.io.FileReader; | 5 import java.io.FileReader; |
4 import java.io.IOException; | 6 import java.io.IOException; |
5 import java.lang.reflect.Method; | 7 import java.lang.reflect.Method; |
6 import java.util.Iterator; | 8 import java.util.Iterator; |
7 import java.util.Map; | 9 import java.util.Map; |
27 public static void register(LuaState lua) { | 29 public static void register(LuaState lua) { |
28 LuaTable t = lua.env(); | 30 LuaTable t = lua.env(); |
29 add( t, "print", new Object[0].getClass() ); | 31 add( t, "print", new Object[0].getClass() ); |
30 add( t, "type", Object.class ); | 32 add( t, "type", Object.class ); |
31 add( t, "load", LuaState.class, String.class ); | 33 add( t, "load", LuaState.class, String.class ); |
32 add( t, "loadFile", LuaState.class, String.class ); | 34 add( t, "loadfile", LuaState.class, String.class ); |
33 add( t, "pairs", LuaTable.class ); | 35 add( t, "pairs", LuaTable.class ); |
34 add( t, "ipairs", LuaTable.class ); | 36 add( t, "ipairs", LuaTable.class ); |
35 } | 37 } |
36 | 38 |
37 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { | 39 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { |
72 } | 74 } |
73 Chunk chunk = (Chunk)resultValue; | 75 Chunk chunk = (Chunk)resultValue; |
74 return chunk.newClosure(lua); | 76 return chunk.newClosure(lua); |
75 } | 77 } |
76 | 78 |
77 public static String readFile(String fileName) throws IOException { | 79 public static String readAll(Reader in) |
78 StringBuilder sb = new StringBuilder(); | 80 throws IOException |
79 FileReader in = new FileReader(fileName); | 81 { |
80 char[] buf = new char[8192]; | 82 char[] a = new char[8192]; |
83 StringBuilder buf = new StringBuilder(); | |
81 int n; | 84 int n; |
82 while( (n=in.read(buf)) != -1 ) { | 85 while( (n=in.read(a)) != -1 ) { |
83 sb.append(buf,0,n); | 86 buf.append(a,0,n); |
84 } | 87 } |
85 return sb.toString(); | 88 return buf.toString(); |
86 } | 89 } |
87 | 90 |
88 public static LuaFunction loadFile(LuaState lua,String fileName) throws LuaException,IOException { | 91 public static String read(File file) |
89 return load(lua,readFile(fileName)); | 92 throws IOException |
93 { | |
94 Reader in = new FileReader(file); | |
95 String s = readAll(in); | |
96 in.close(); | |
97 return s; | |
98 } | |
99 | |
100 | |
101 public static LuaFunction loadfile(LuaState lua,String fileName) throws LuaException,IOException { | |
102 return load(lua,read(new File(fileName))); | |
90 } | 103 } |
91 | 104 |
92 private static class TableIter { | 105 private static class TableIter { |
93 private final Iterator<Map.Entry<Object,Object>> iter; | 106 private final Iterator<Map.Entry<Object,Object>> iter; |
94 | 107 |