about summary refs log tree commit diff stats
path: root/src/file.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-03-06 09:52:37 -0800
committerKartik K. Agaram <vc@akkartik.com>2022-03-06 09:52:37 -0800
commit5758f6c082af702e8e2c6c6e6d88f987b3deedee (patch)
treec826286dc9efe194cc19596501d4c47c1fe8f1d2 /src/file.lua
parentf07eb03492814aeb91546cf856b7facc3c347954 (diff)
downloadteliva-5758f6c082af702e8e2c6c6e6d88f987b3deedee.tar.gz
reading from file a character at a time
Diffstat (limited to 'src/file.lua')
-rw-r--r--src/file.lua20
1 files changed, 20 insertions, 0 deletions
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