From d48b85d50cdb30af941c0dc4eccbb8971e902011 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 26 May 2024 10:16:33 +0100 Subject: [PATCH] Add r+/w+ support to io library --- .../dan200/computercraft/core/apis/FSAPI.java | 8 ++++---- .../data/computercraft/lua/rom/apis/io.lua | 16 +++++++++------- .../resources/test-rom/spec/apis/io_spec.lua | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java b/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java index a359320dd..794d8b7c4 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java +++ b/projects/core/src/main/java/dan200/computercraft/core/apis/FSAPI.java @@ -309,10 +309,10 @@ public class FSAPI implements ILuaAPI { *

* The {@code mode} string can be any of the following: *

*

diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/io.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/io.lua index 34d6d075a..6291318da 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/io.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/io.lua @@ -365,24 +365,26 @@ end -- or [`nil`], plus an error message. -- -- The `mode` string can be any of the following: --- - **"r"**: Read mode --- - **"w"**: Write mode --- - **"a"**: Append mode +-- - **"r"**: Read mode. +-- - **"w"**: Write mode. +-- - **"a"**: Append mode. +-- - **"r+"**: Update mode (allows reading and writing), all data is preserved. +-- - **"w+"**: Update mode, all data is erased. -- -- The mode may also have a `b` at the end, which opens the file in "binary --- mode". This allows you to read binary files, as well as seek within a file. +-- mode". This has no impact on functionality. -- -- @tparam string filename The name of the file to open. --- @tparam[opt] string mode The mode to open the file with. This defaults to `rb`. +-- @tparam[opt] string mode The mode to open the file with. This defaults to `r`. -- @treturn[1] Handle The opened file. -- @treturn[2] nil In case of an error. -- @treturn[2] string The reason the file could not be opened. +-- @changed 1.111.0 Add support for `r+` and `w+`. function open(filename, mode) expect(1, filename, "string") expect(2, mode, "string", "nil") - local sMode = mode and mode:gsub("%+", "") or "r" - local file, err = fs.open(filename, sMode) + local file, err = fs.open(filename, mode or "r") if not file then return nil, err end return make_file(file) diff --git a/projects/core/src/test/resources/test-rom/spec/apis/io_spec.lua b/projects/core/src/test/resources/test-rom/spec/apis/io_spec.lua index dd850d530..daa1a54cc 100644 --- a/projects/core/src/test/resources/test-rom/spec/apis/io_spec.lua +++ b/projects/core/src/test/resources/test-rom/spec/apis/io_spec.lua @@ -329,4 +329,19 @@ describe("The io library", function() expect(read_all(file)):eq("alo\n " .. t .. " ;end of file\n") end) end) + + describe("read/write handles", function() + it("can read and write to a file", function() + write_file(file, "an example file") + + local handle = io.open(file, "r+") + expect(handle:read(3)):eq("an ") + + handle:write("exciting file") + expect(handle:seek("cur")):eq(16) + + handle:seek("set", 0) + expect(handle:read("*a")):eq("an exciting file") + end) + end) end)