summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Renner Berthing <esmil@mailme.dk>2012-12-16 22:35:33 +0100
committerEmil Renner Berthing <esmil@mailme.dk>2012-12-17 13:10:46 +0100
commitb08ea85185321afe903e39786b3d092d94ba356f (patch)
tree217d825e4025a455fd8405ede81f42171a4c27c8
parent524cc5c022ee02f0e04a85ed72f04a1f71fcd316 (diff)
downloadlem-b08ea85185321afe903e39786b3d092d94ba356f.tar.gz
lem-b08ea85185321afe903e39786b3d092d94ba356f.tar.xz
lem-b08ea85185321afe903e39786b3d092d94ba356f.zip
io: add input(), read(), output() and close()
-rw-r--r--lem/io.lua42
-rwxr-xr-xtest/fread.lua4
-rwxr-xr-xtest/fwrite.lua6
3 files changed, 43 insertions, 9 deletions
diff --git a/lem/io.lua b/lem/io.lua
index 517e61e..7efccf0 100644
--- a/lem/io.lua
+++ b/lem/io.lua
@@ -18,8 +18,11 @@
local io = require 'lem.io.core'
+local type = type
+local assert = assert
+local error = error
+
do
- local type = type
local parsers = io.parsers
local parser_available = parsers.available
parsers.available = nil
@@ -47,10 +50,41 @@ do
end
do
- local _write, stdout = io.Stream.write, io.stdout
+ local stdin = io.stdin
+
+ function io.input(file)
+ if not file then return stdin end
+ if type(file) == 'string' then
+ stdin = assert(io.open(file))
+ else
+ stdin = file
+ end
+ end
+
+ function io.read(...)
+ return stdin:read(...)
+ end
+end
+
+do
+ local stdout = io.stdout
+
+ function io.output(file)
+ if not file then return stdout end
+ if type(file) == 'string' then
+ stdout = assert(io.open(file))
+ else
+ stdout = file
+ end
+ end
+
+ function io.write(...)
+ return stdout:write(...)
+ end
- function io.write(str)
- return _write(stdout, str)
+ function io.close(file)
+ if not file then file = stdout end
+ return file:close()
end
end
diff --git a/test/fread.lua b/test/fread.lua
index 8267bdd..7193532 100755
--- a/test/fread.lua
+++ b/test/fread.lua
@@ -23,7 +23,7 @@ local io = require 'lem.io'
local write = io.write
print("Press enter to read '" .. (arg[1] or arg[0]) .. "'")
-io.stdin:read()
+io.read()
local threads = 2
for i = 1, threads do
@@ -61,6 +61,6 @@ while threads > 0 do
end
print "\nDone. Press enter to continue."
-io.stdin:read()
+io.read()
-- vim: set ts=2 sw=2 noet:
diff --git a/test/fwrite.lua b/test/fwrite.lua
index d76485f..5a2c9f1 100755
--- a/test/fwrite.lua
+++ b/test/fwrite.lua
@@ -23,7 +23,7 @@ local io = require 'lem.io'
local write = io.write
print("Press enter to read '" .. (arg[1] or arg[0]) .. "'")
-io.stdin:read()
+io.read()
local threads = 2
for i = 1, threads do
@@ -32,7 +32,7 @@ for i = 1, threads do
assert(getmetatable(file) == io.File, "Hmm...")
print(tostring(n) .. '-1')
- assert(file:write('Hej!\n'))
+ assert(file:write('Hej', ' ', 'med', ' ', 'dig', '!\n'))
print(tostring(n) .. '-2')
assert(file:write(tostring(n)))
print(tostring(n) .. '-3')
@@ -54,6 +54,6 @@ while threads > 0 do
end
print "\nDone. Press enter to continue."
-io.stdin:read()
+io.read()
-- vim: set ts=2 sw=2 noet: