Mercurial Hosting > luan
diff core/src/luan/modules/IoLuan.java @ 758:c29d11d675fd
added Json.toString() and rpc now sends tables as json
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 19 Jul 2016 00:57:37 -0600 |
parents | ff865e954934 |
children | ae612dfc57cb |
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java Mon Jul 18 20:18:02 2016 -0600 +++ b/core/src/luan/modules/IoLuan.java Tue Jul 19 00:57:37 2016 -0600 @@ -171,6 +171,21 @@ } + private static File objToFile(Object obj) { + if( obj instanceof String ) { + return new File((String)obj); + } + if( obj instanceof LuanTable ) { + LuanTable t = (LuanTable)obj; + Object java = t.rawGet("java"); + if( java instanceof LuanFile ) { + LuanFile luanFile = (LuanFile)java; + return luanFile.file; + } + } + return null; + } + public static abstract class LuanIn { public abstract InputStream inputStream() throws IOException, LuanException; @@ -213,13 +228,16 @@ } } - public void unzip(String path) throws IOException, LuanException { + public void unzip(Object path) throws IOException, LuanException { + File pathFile = objToFile(path); + if( pathFile==null ) + throw new LuanException( "bad argument #1 to 'unzip' (string or file table expected)" ); ZipInputStream in = new ZipInputStream(new BufferedInputStream(inputStream())); ZipEntry entry; while( (entry = in.getNextEntry()) != null ) { if( entry.isDirectory() ) continue; - File file = new File(path,entry.getName()); + File file = new File(pathFile,entry.getName()); file.getParentFile().mkdirs(); OutputStream out = new FileOutputStream(file); Utils.copyAll(in,out); @@ -255,7 +273,7 @@ LuanIn.class.getMethod( "exists" ), this ) ); tbl.rawPut( "unzip", new LuanJavaFunction( - LuanIn.class.getMethod( "unzip", String.class ), this + LuanIn.class.getMethod( "unzip", Object.class ), this ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); @@ -333,12 +351,15 @@ return binaryWriter(new BufferedOutputStream(outputStream())); } - public void zip(LuanState luan,String basePath,LuanTable filePathList) throws LuanException, IOException { + public void zip(LuanState luan,Object basePathObj,LuanTable filePathList) throws LuanException, IOException { + File basePathFile = objToFile(basePathObj); + if( basePathFile==null ) + throw new LuanException( "bad argument #1 to 'zip' (string or file table expected)" ); String[] filePaths; if( filePathList==null ) { - File file = new File(basePath).getCanonicalFile(); + File file = basePathFile.getCanonicalFile(); filePaths = new String[]{file.toString()}; - basePath = file.getParent(); + basePathFile = file.getParentFile(); } else { List list = filePathList.asList(); filePaths = new String[list.size()]; @@ -349,8 +370,7 @@ filePaths[i] = (String)obj; } } - if( !basePath.endsWith("/") ) - basePath += '/'; + String basePath = basePathFile.toString() + '/'; ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream())); zip(out,basePath,filePaths); out.close(); @@ -393,7 +413,7 @@ LuanIO.class.getMethod( "binary_writer" ), this ) ); tbl.rawPut( "zip", new LuanJavaFunction( - LuanIO.class.getMethod( "zip", LuanState.class, String.class, LuanTable.class ), this + LuanIO.class.getMethod( "zip", LuanState.class, Object.class, LuanTable.class ), this ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); @@ -537,8 +557,12 @@ return file.exists(); } - public boolean rename_to(String dest) { - return file.renameTo(new File(dest)); + public void rename_to(Object destObj) throws LuanException { + File dest = objToFile(destObj); + if( dest==null ) + throw new LuanException( "bad argument #1 to 'objToFile' (string or file table expected)" ); + if( !file.renameTo(dest) ) + throw new LuanException("couldn't rename file "+file+" to "+dest); } public LuanTable canonical(LuanState luan) throws LuanException, IOException { @@ -605,7 +629,7 @@ LuanFile.class.getMethod( "parent", LuanState.class ), this ) ); tbl.rawPut( "rename_to", new LuanJavaFunction( - LuanFile.class.getMethod( "rename_to", String.class ), this + LuanFile.class.getMethod( "rename_to", Object.class ), this ) ); tbl.rawPut( "canonical", new LuanJavaFunction( LuanFile.class.getMethod( "canonical", LuanState.class ), this