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