From 5be2202b2eb367525fe9e48e0ea4a23fa76e64dc Mon Sep 17 00:00:00 2001 From: Wilma456 Date: Sat, 16 Sep 2017 16:06:27 +0200 Subject: [PATCH 1/3] Add read() to Filehandle --- .../core/apis/handles/EncodedInputHandle.java | 29 +++++++++++++++++++ .../assets/computercraft/lua/rom/apis/io.lua | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 1cc5141a1..75bc549b4 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -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,32 @@ public class EncodedInputHandle extends HandleGeneric // close close(); return null; + case 3: + // read + checkOpen(); + try + { + double count = optNumber( args, 0, 1 ); + StringBuilder result = new StringBuilder( "" ); + for ( int i = 1; i < count+1; i++ ) { + int character = m_reader.read(); + if ( character == -1 ) { + if ( result.length() == 0 ) { + return null; + } + else { + return new Object[] { result.toString() }; + } + } + char c = (char) character; + result.append( c ); + } + return new Object[] { result.toString() }; + } + catch( IOException e ) + { + return null; + } default: return null; } diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua index 4667bf50d..51bd5ac13 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua @@ -91,6 +91,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" ) end From f20a7afa7fbc094d6c4136f2081542aae0947667 Mon Sep 17 00:00:00 2001 From: "Wilma456 (Jakob0815)" Date: Mon, 18 Sep 2017 15:22:44 +0200 Subject: [PATCH 2/3] Better Code --- .../core/apis/handles/EncodedInputHandle.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 75bc549b4..467330f63 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -5,6 +5,7 @@ import dan200.computercraft.api.lua.LuaException; import javax.annotation.Nonnull; import java.io.*; +import java.util.Arrays; import static dan200.computercraft.core.apis.ArgumentHelper.*; @@ -110,22 +111,16 @@ public class EncodedInputHandle extends HandleGeneric checkOpen(); try { - double count = optNumber( args, 0, 1 ); - StringBuilder result = new StringBuilder( "" ); - for ( int i = 1; i < count+1; i++ ) { - int character = m_reader.read(); - if ( character == -1 ) { - if ( result.length() == 0 ) { - return null; - } - else { - return new Object[] { result.toString() }; - } - } - char c = (char) character; - result.append( c ); + int count = optInt( args, 0, 1 ); + if( count <= 0 || count >= 1024 * 16 ) + { + throw new LuaException( "Count out of range" ); } - return new Object[] { result.toString() }; + char[] bytes = new char[ count ]; + count = m_reader.read( bytes ); + if( count < 0 ) return null; + String str = new String( bytes ); + return new Object[] { str }; } catch( IOException e ) { From 4fb0240a36c36d8a2b218a4e366f95a43a077016 Mon Sep 17 00:00:00 2001 From: Wilma456 Date: Sun, 24 Sep 2017 17:36:20 +0200 Subject: [PATCH 3/3] Changes suggested by SquidDev and update help file --- .../computercraft/core/apis/handles/EncodedInputHandle.java | 3 +-- src/main/resources/assets/computercraft/lua/rom/help/fs.txt | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 467330f63..9f722f797 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -5,7 +5,6 @@ import dan200.computercraft.api.lua.LuaException; import javax.annotation.Nonnull; import java.io.*; -import java.util.Arrays; import static dan200.computercraft.core.apis.ArgumentHelper.*; @@ -119,7 +118,7 @@ public class EncodedInputHandle extends HandleGeneric char[] bytes = new char[ count ]; count = m_reader.read( bytes ); if( count < 0 ) return null; - String str = new String( bytes ); + String str = new String( bytes, 0, count ); return new Object[] { str }; } catch( IOException e ) diff --git a/src/main/resources/assets/computercraft/lua/rom/help/fs.txt b/src/main/resources/assets/computercraft/lua/rom/help/fs.txt index 400e5ba6f..363273b12 100644 --- a/src/main/resources/assets/computercraft/lua/rom/help/fs.txt +++ b/src/main/resources/assets/computercraft/lua/rom/help/fs.txt @@ -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() \ No newline at end of file +close()