From 5758f6c082af702e8e2c6c6e6d88f987b3deedee Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 6 Mar 2022 09:52:37 -0800 Subject: reading from file a character at a time --- src/file.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src') diff --git a/src/file.lua b/src/file.lua index 18ee718..bbf9445 100644 --- a/src/file.lua +++ b/src/file.lua @@ -43,3 +43,23 @@ function writing_task(outfile, chanin) outfile:write(line) end end + +-- start_reading reads line by line by default +-- this helper permits character-by-character reading +function character_by_character(chanin, buffer_size) + local chanout = task.Channel:new(buffer_size or 50) + task.spawn(character_splitting_task, chanin, chanout) + return chanout +end + +function character_splitting_task(chanin, chanout) + while true do + local line = chanin:recv() + if line == nil then break end + local linesz = string.len(line) + for i=1,linesz do + chanout:send(string.sub(line, i, i)) + end + end + chanout:send(nil) -- end of file +end -- cgit