diff --git a/life.tlv b/life.tlv index 7567697..22293ea 100644 --- a/life.tlv +++ b/life.tlv @@ -332,54 +332,6 @@ > '123 ', > 'test_check_screen') >end -- __teliva_timestamp: original - start_reading: - >-- primitive for reading files from a file system (or, later, network) - >-- returns a channel or nil on error - >-- read lines from the channel using :recv() - >-- recv() on the channel will indicate end of file. - >function start_reading(fs, filename) - > local result = task.Channel:new() - > local infile = io.open(filename) - > if infile == nil then return nil end - > task.spawn(reading_task, infile, result) - > return result - >end - > - >function reading_task(infile, chanout) - > for line in infile:lines() do - > chanout:send(line) - > end - > chanout:send(nil) -- eof - >end -- __teliva_timestamp: original - start_writing: - >-- primitive for writing files to a file system (or, later, network) - >-- returns a channel or nil on error - >-- write to the channel using :send() - >-- indicate you're done writing by calling :close() - >-- file will not be externally visible until :close() - >function start_writing(fs, filename) - > local result = task.Channel:new() - > local initial_filename = os.tmpname() - > local outfile = io.open(initial_filename, 'w') - > if outfile == nil then return nil end - > result.close = function() - > result:send(nil) -- end of file - > outfile:close() - > os.rename(initial_filename, filename) - > end - > task.spawn(writing_task, outfile, result) - > return result - >end - > - >function writing_task(outfile, chanin) - > while true do - > local line = chanin:recv() - > if line == nil then break end -- end of file - > outfile:write(line) - > end - >end - __teliva_timestamp: original grid: >-- main data structure diff --git a/src/file.lua b/src/file.lua new file mode 100644 index 0000000..faac0c3 --- /dev/null +++ b/src/file.lua @@ -0,0 +1,45 @@ +-- primitive for reading files from a file system (or, later, network) +-- returns a channel or nil on error +-- read lines from the channel using :recv() +-- recv() on the channel will indicate end of file. +function start_reading(fs, filename) + local result = task.Channel:new() + local infile = io.open(filename) + if infile == nil then return nil end + task.spawn(reading_task, infile, result) + return result +end + +local function reading_task(infile, chanout) + for line in infile:lines() do + chanout:send(line) + end + chanout:send(nil) -- eof +end + +-- primitive for writing files to a file system (or, later, network) +-- returns a channel or nil on error +-- write to the channel using :send() +-- indicate you're done writing by calling :close() +-- file will not be externally visible until :close() +function start_writing(fs, filename) + local result = task.Channel:new() + local initial_filename = os.tmpname() + local outfile = io.open(initial_filename, 'w') + if outfile == nil then return nil end + result.close = function() + result:send(nil) -- end of file + outfile:close() + os.rename(initial_filename, filename) + end + task.spawn(writing_task, outfile, result) + return result +end + +local function writing_task(outfile, chanin) + while true do + local line = chanin:recv() + if line == nil then break end -- end of file + outfile:write(line) + end +end diff --git a/src/lua.c b/src/lua.c index 11a1221..a553315 100644 --- a/src/lua.c +++ b/src/lua.c @@ -236,6 +236,8 @@ static int pmain (lua_State *L) { if (status != 0) return 0; status = dorequire(L, "src/task.lua", "task"); if (status != 0) return 0; + status = dorequire(L, "src/file.lua", "file"); + if (status != 0) return 0; lua_gc(L, LUA_GCRESTART, 0); s->status = handle_luainit(L); if (s->status != 0) return 0; diff --git a/template.tlv b/template.tlv index 4ec78b2..aa78955 100644 --- a/template.tlv +++ b/template.tlv @@ -333,54 +333,6 @@ > '123 ', > 'test_check_screen') >end -- __teliva_timestamp: original - start_reading: - >-- primitive for reading files from a file system (or, later, network) - >-- returns a channel or nil on error - >-- read lines from the channel using :recv() - >-- recv() on the channel will indicate end of file. - >function start_reading(fs, filename) - > local result = task.Channel:new() - > local infile = io.open(filename) - > if infile == nil then return nil end - > task.spawn(reading_task, infile, result) - > return result - >end - > - >function reading_task(infile, chanout) - > for line in infile:lines() do - > chanout:send(line) - > end - > chanout:send(nil) -- eof - >end -- __teliva_timestamp: original - start_writing: - >-- primitive for writing files to a file system (or, later, network) - >-- returns a channel or nil on error - >-- write to the channel using :send() - >-- indicate you're done writing by calling :close() - >-- file will not be externally visible until :close() - >function start_writing(fs, filename) - > local result = task.Channel:new() - > local initial_filename = os.tmpname() - > local outfile = io.open(initial_filename, 'w') - > if outfile == nil then return nil end - > result.close = function() - > result:send(nil) -- end of file - > outfile:close() - > os.rename(initial_filename, filename) - > end - > task.spawn(writing_task, outfile, result) - > return result - >end - > - >function writing_task(outfile, chanin) - > while true do - > local line = chanin:recv() - > if line == nil then break end -- end of file - > outfile:write(line) - > end - >end - __teliva_timestamp: original render: >function render(window) diff --git a/zet.tlv b/zet.tlv index 6105fda..0977572 100644 --- a/zet.tlv +++ b/zet.tlv @@ -335,54 +335,6 @@ > '123 ', > 'test_check_screen') >end -- __teliva_timestamp: original - start_reading: - >-- primitive for reading files from a file system (or, later, network) - >-- returns a channel or nil on error - >-- read lines from the channel using :recv() - >-- recv() on the channel will indicate end of file. - >function start_reading(fs, filename) - > local result = task.Channel:new() - > local infile = io.open(filename) - > if infile == nil then return nil end - > task.spawn(reading_task, infile, result) - > return result - >end - > - >function reading_task(infile, chanout) - > for line in infile:lines() do - > chanout:send(line) - > end - > chanout:send(nil) -- eof - >end -- __teliva_timestamp: original - start_writing: - >-- primitive for writing files to a file system (or, later, network) - >-- returns a channel or nil on error - >-- write to the channel using :send() - >-- indicate you're done writing by calling :close() - >-- file will not be externally visible until :close() - >function start_writing(fs, filename) - > local result = task.Channel:new() - > local initial_filename = os.tmpname() - > local outfile = io.open(initial_filename, 'w') - > if outfile == nil then return nil end - > result.close = function() - > result:send(nil) -- end of file - > outfile:close() - > os.rename(initial_filename, filename) - > end - > task.spawn(writing_task, outfile, result) - > return result - >end - > - >function writing_task(outfile, chanin) - > while true do - > local line = chanin:recv() - > if line == nil then break end -- end of file - > outfile:write(line) - > end - >end - __teliva_timestamp: original spaces: >function spaces(n)