1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 11:10:29 +00:00

Merge pull request #455 from Wilma456/fileread

Add read() to Filehandle
This commit is contained in:
Daniel Ratcliffe 2018-01-13 00:58:19 +00:00 committed by GitHub
commit 3e265c27ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View File

@ -6,6 +6,8 @@ import dan200.computercraft.api.lua.LuaException;
import javax.annotation.Nonnull;
import java.io.*;
import static dan200.computercraft.core.apis.ArgumentHelper.*;
public class EncodedInputHandle extends HandleGeneric
{
private final BufferedReader m_reader;
@ -49,6 +51,7 @@ public class EncodedInputHandle extends HandleGeneric
"readLine",
"readAll",
"close",
"read",
};
}
@ -102,6 +105,26 @@ public class EncodedInputHandle extends HandleGeneric
// close
close();
return null;
case 3:
// read
checkOpen();
try
{
int count = optInt( args, 0, 1 );
if( count <= 0 || count >= 1024 * 16 )
{
throw new LuaException( "Count out of range" );
}
char[] bytes = new char[ count ];
count = m_reader.read( bytes );
if( count < 0 ) return null;
String str = new String( bytes, 0, count );
return new Object[] { str };
}
catch( IOException e )
{
return null;
}
default:
return null;
}

View File

@ -94,6 +94,8 @@ function open( _sPath, _sMode )
return file.readLine()
elseif sFormat == "*a" then
return file.readAll()
elseif _G.type( sFormat ) == "number" then
return file.read( sFormat )
else
error( "Unsupported format", 2 )
end

View File

@ -22,6 +22,7 @@ Functions on files opened with mode "r":
readLine()
readAll()
close()
read( number )
Functions on files opened with mode "w" or "a":
write( string )
@ -36,4 +37,4 @@ close()
Functions on files opened with mode "wb" or "ab":
write( byte )
flush()
close()
close()