1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-05 23:10:30 +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 javax.annotation.Nonnull;
import java.io.*; import java.io.*;
import static dan200.computercraft.core.apis.ArgumentHelper.*;
public class EncodedInputHandle extends HandleGeneric public class EncodedInputHandle extends HandleGeneric
{ {
private final BufferedReader m_reader; private final BufferedReader m_reader;
@ -49,6 +51,7 @@ public class EncodedInputHandle extends HandleGeneric
"readLine", "readLine",
"readAll", "readAll",
"close", "close",
"read",
}; };
} }
@ -102,6 +105,26 @@ public class EncodedInputHandle extends HandleGeneric
// close // close
close(); close();
return null; 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: default:
return null; return null;
} }

View File

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

View File

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