Improve io.read() behavior.

This commit is contained in:
James Roseborough
2010-05-10 20:39:07 +00:00
parent 82e9d89115
commit 719ec63d5d

View File

@@ -317,7 +317,7 @@ public class IoLib extends OneArgFunction {
// io.read(...) -> (...)
public Varargs _io_read(Varargs args) throws IOException {
checkopen(infile);
checkopen(input());
return ioread(infile,args);
}
@@ -494,14 +494,20 @@ public class IoLib extends OneArgFunction {
return NIL;
return LuaString.valueOf(b, 0, r);
}
public static LuaValue freaduntil(File f,int delim) throws IOException {
public static LuaValue freaduntil(File f,boolean lineonly) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
try {
while ( true ) {
c = f.read();
if ( c < 0 || c == delim )
break;
if ( lineonly ) {
loop: while ( (c = f.read()) > 0 ) {
switch ( c ) {
case '\r': break;
case '\n': break loop;
default: baos.write(c); break;
}
}
} else {
while ( (c = f.read()) > 0 )
baos.write(c);
}
} catch ( EOFException e ) {
@@ -512,14 +518,14 @@ public class IoLib extends OneArgFunction {
(LuaValue) LuaString.valueOf(baos.toByteArray());
}
public static LuaValue freadline(File f) throws IOException {
return freaduntil(f,'\n');
return freaduntil(f,true);
}
public static LuaValue freadall(File f) throws IOException {
int n = f.remaining();
if ( n >= 0 ) {
return freadbytes(f, n);
} else {
return freaduntil(f,-1);
return freaduntil(f,false);
}
}
public static double freadnumber(File f) throws IOException {