diff options
author | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-10 14:24:39 +0100 |
---|---|---|
committer | Emil Renner Berthing <esmil@mailme.dk> | 2012-12-17 10:11:06 +0100 |
commit | dfcd5ee52877c26f3fe9eb18ee8e5bbf99921636 (patch) | |
tree | c6a537cba6e988ff4a9d45aec6a582aeaf97a86d /test/fwrite.lua | |
parent | 9e69d37f9e7a00b9902ebe5618904871397c7999 (diff) | |
download | lem-dfcd5ee52877c26f3fe9eb18ee8e5bbf99921636.tar.gz lem-dfcd5ee52877c26f3fe9eb18ee8e5bbf99921636.tar.xz lem-dfcd5ee52877c26f3fe9eb18ee8e5bbf99921636.zip |
streams: add file:write() method
Diffstat (limited to 'test/fwrite.lua')
-rwxr-xr-x | test/fwrite.lua | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/test/fwrite.lua b/test/fwrite.lua new file mode 100755 index 0000000..ff4c484 --- /dev/null +++ b/test/fwrite.lua @@ -0,0 +1,61 @@ +#!bin/lem +-- +-- This file is part of LEM, a Lua Event Machine. +-- Copyright 2011-2012 Emil Renner Berthing +-- +-- LEM is free software: you can redistribute it and/or +-- modify it under the terms of the GNU General Public License as +-- published by the Free Software Foundation, either version 3 of +-- the License, or (at your option) any later version. +-- +-- LEM is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with LEM. If not, see <http://www.gnu.org/licenses/>. +-- + +local utils = require 'lem.utils' +local stream = require 'lem.streams' + +local write +do + local stdout = stream.stdout + function write(str) + return stdout:write(str) + end +end + +print("Press enter to read '" .. (arg[1] or arg[0]) .. "'") +stream.stdin:read() + +local threads = 2 +for i = 1, threads do + utils.spawn(function(n) + local file = assert(stream.open('file' .. tostring(n) .. '.txt', 'w')) + assert(getmetatable(file) == stream.File, "Hmm...") + + print(tostring(n) .. '-1') + assert(file:write('Hej!\n')) + print(tostring(n) .. '-2') + assert(file:write(tostring(n))) + print(tostring(n) .. '-3') + assert(file:write('\n')) + print(tostring(n) .. '-4') + + threads = threads - 1 + end, i) +end + +local yield = utils.yield +while threads > 0 do + write('.') + yield() +end + +print "\nDone. Press enter to continue." +stream.stdin:read() + +-- vim: set ts=2 sw=2 noet: |