Mercurial Hosting > luan
comparison src/luan/lib/IoLib.java @ 164:78ba371ea1e9
merge Os.File into Io.File and remove Os
git-svn-id: https://luan-java.googlecode.com/svn/trunk@165 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 20 Jun 2014 23:23:16 +0000 |
parents | cc3a0578edac |
children |
comparison
equal
deleted
inserted
replaced
163:3c95a2291d64 | 164:78ba371ea1e9 |
---|---|
347 | 347 |
348 @Override public String to_string() { | 348 @Override public String to_string() { |
349 return file.toString(); | 349 return file.toString(); |
350 } | 350 } |
351 | 351 |
352 public LuanTable os_file() { | 352 public LuanTable child(String name) { |
353 return new OsLib.LuanFile(file).table(); | 353 return new LuanFile(new File(file,name)).table(); |
354 } | |
355 | |
356 public LuanTable children() { | |
357 File[] files = file.listFiles(); | |
358 if( files==null ) | |
359 return null; | |
360 LuanTable list = new LuanTable(); | |
361 for( File f : files ) { | |
362 list.add(new LuanFile(f).table()); | |
363 } | |
364 return list; | |
365 } | |
366 | |
367 public boolean exists() { | |
368 return Utils.exists(file); | |
354 } | 369 } |
355 | 370 |
356 @Override LuanTable table() { | 371 @Override LuanTable table() { |
357 LuanTable tbl = super.table(); | 372 LuanTable tbl = super.table(); |
358 try { | 373 try { |
359 tbl.put( "os_file", new LuanJavaFunction( | 374 tbl.put( "name", new LuanJavaFunction( |
360 LuanFile.class.getMethod( "os_file" ), this | 375 File.class.getMethod( "getName" ), file |
376 ) ); | |
377 tbl.put( "exists", new LuanJavaFunction( | |
378 LuanFile.class.getMethod( "exists" ), this | |
379 ) ); | |
380 tbl.put( "is_directory", new LuanJavaFunction( | |
381 File.class.getMethod( "isDirectory" ), file | |
382 ) ); | |
383 tbl.put( "is_file", new LuanJavaFunction( | |
384 File.class.getMethod( "isFile" ), file | |
385 ) ); | |
386 tbl.put( "delete", new LuanJavaFunction( | |
387 File.class.getMethod( "delete" ), file | |
388 ) ); | |
389 tbl.put( "mkdir", new LuanJavaFunction( | |
390 File.class.getMethod( "mkdir" ), file | |
391 ) ); | |
392 tbl.put( "mkdirs", new LuanJavaFunction( | |
393 File.class.getMethod( "mkdirs" ), file | |
394 ) ); | |
395 tbl.put( "last_modified", new LuanJavaFunction( | |
396 File.class.getMethod( "lastModified" ), file | |
397 ) ); | |
398 tbl.put( "child", new LuanJavaFunction( | |
399 LuanFile.class.getMethod( "child", String.class ), this | |
400 ) ); | |
401 tbl.put( "children", new LuanJavaFunction( | |
402 LuanFile.class.getMethod( "children" ), this | |
361 ) ); | 403 ) ); |
362 } catch(NoSuchMethodException e) { | 404 } catch(NoSuchMethodException e) { |
363 throw new RuntimeException(e); | 405 throw new RuntimeException(e); |
364 } | 406 } |
365 return tbl; | 407 return tbl; |
368 | 410 |
369 public static LuanIn luanIo(LuanState luan,String name) throws LuanException { | 411 public static LuanIn luanIo(LuanState luan,String name) throws LuanException { |
370 if( Utils.isFile(name) ) | 412 if( Utils.isFile(name) ) |
371 return new LuanFile(name); | 413 return new LuanFile(name); |
372 String url = Utils.toUrl(name); | 414 String url = Utils.toUrl(name); |
373 if( url == null ) | 415 if( url != null ) { |
374 throw luan.exception( "file '"+name+"' not found" ); | 416 try { |
375 try { | 417 return new LuanUrl(url); |
376 return new LuanUrl(url); | 418 } catch(MalformedURLException e) { |
377 } catch(MalformedURLException e) { | 419 throw new RuntimeException(e); |
378 throw new RuntimeException(e); | 420 } |
379 } | 421 } |
422 throw luan.exception( "file '"+name+"' not found" ); | |
380 } | 423 } |
381 | 424 |
382 public static LuanTable File(LuanState luan,String name) throws LuanException { | 425 public static LuanTable File(LuanState luan,String name) throws LuanException { |
383 return luanIo(luan,name).table(); | 426 return luanIo(luan,name).table(); |
384 } | 427 } |