diff options
author | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-16 22:35:33 +0100 |
---|---|---|
committer | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-17 13:10:46 +0100 |
commit | b08ea85185321afe903e39786b3d092d94ba356f (patch) | |
tree | 217d825e4025a455fd8405ede81f42171a4c27c8 | |
parent | 524cc5c022ee02f0e04a85ed72f04a1f71fcd316 (diff) | |
download | lem-b08ea85185321afe903e39786b3d092d94ba356f.tar.gz lem-b08ea85185321afe903e39786b3d092d94ba356f.tar.xz lem-b08ea85185321afe903e39786b3d092d94ba356f.zip |
io: add input(), read(), output() and close()
-rw-r--r-- | lem/io.lua | 42 | ||||
-rwxr-xr-x | test/fread.lua | 4 | ||||
-rwxr-xr-x | test/fwrite.lua | 6 |
3 files changed, 43 insertions, 9 deletions
@@ -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: |