2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- LTN12 - Filters, sources, sinks and pumps.
|
|
|
|
-- LuaSocket toolkit.
|
|
|
|
-- Author: Diego Nehab
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
2004-09-27 06:01:18 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Declare module
|
|
|
|
-----------------------------------------------------------------------------
|
2004-11-27 08:58:04 +01:00
|
|
|
local string = require("string")
|
|
|
|
local table = require("table")
|
2005-06-14 06:29:23 +02:00
|
|
|
local base = _G
|
2013-06-05 12:36:51 +02:00
|
|
|
local _M = {}
|
|
|
|
if module then -- heuristic for exporting a global package table
|
|
|
|
ltn12 = _M
|
|
|
|
end
|
2013-05-27 10:45:09 +02:00
|
|
|
local filter,source,sink,pump = {},{},{},{}
|
|
|
|
|
|
|
|
_M.filter = filter
|
|
|
|
_M.source = source
|
|
|
|
_M.sink = sink
|
|
|
|
_M.pump = pump
|
2004-02-11 04:31:53 +01:00
|
|
|
|
|
|
|
-- 2048 seems to be better in windows...
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.BLOCKSIZE = 2048
|
2013-06-11 13:10:03 +02:00
|
|
|
_M._VERSION = "LTN12 1.0.3"
|
2004-02-11 04:31:53 +01:00
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Filter stuff
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- returns a high level filter that cycles a low-level filter
|
2004-02-11 04:31:53 +01:00
|
|
|
function filter.cycle(low, ctx, extra)
|
2004-11-27 08:58:04 +01:00
|
|
|
base.assert(low)
|
2004-02-11 04:31:53 +01:00
|
|
|
return function(chunk)
|
|
|
|
local ret
|
|
|
|
ret, ctx = low(ctx, chunk, extra)
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-28 01:59:12 +01:00
|
|
|
-- chains a bunch of filters together
|
|
|
|
-- (thanks to Wim Couwenberg)
|
2005-11-22 09:33:29 +01:00
|
|
|
function filter.chain(...)
|
2012-09-17 18:30:26 +02:00
|
|
|
local arg = {...}
|
2013-05-27 10:45:09 +02:00
|
|
|
local n = select('#',...)
|
2004-11-28 01:59:12 +01:00
|
|
|
local top, index = 1, 1
|
2004-11-28 03:36:07 +01:00
|
|
|
local retry = ""
|
2004-07-29 07:11:21 +02:00
|
|
|
return function(chunk)
|
2004-11-28 03:36:07 +01:00
|
|
|
retry = chunk and retry
|
2005-11-22 09:33:29 +01:00
|
|
|
while true do
|
2004-11-28 01:59:12 +01:00
|
|
|
if index == top then
|
|
|
|
chunk = arg[index](chunk)
|
2004-11-28 03:36:07 +01:00
|
|
|
if chunk == "" or top == n then return chunk
|
|
|
|
elseif chunk then index = index + 1
|
2005-11-22 09:33:29 +01:00
|
|
|
else
|
|
|
|
top = top+1
|
2004-11-28 01:59:12 +01:00
|
|
|
index = top
|
|
|
|
end
|
|
|
|
else
|
2004-11-28 03:36:07 +01:00
|
|
|
chunk = arg[index](chunk or "")
|
2004-11-28 01:59:12 +01:00
|
|
|
if chunk == "" then
|
|
|
|
index = index - 1
|
2004-11-28 03:36:07 +01:00
|
|
|
chunk = retry
|
2004-11-28 01:59:12 +01:00
|
|
|
elseif chunk then
|
|
|
|
if index == n then return chunk
|
|
|
|
else index = index + 1 end
|
2004-11-28 03:36:07 +01:00
|
|
|
else base.error("filter returned inappropriate nil") end
|
2004-11-17 05:55:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-02-11 04:31:53 +01:00
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Source stuff
|
|
|
|
-----------------------------------------------------------------------------
|
2004-02-11 04:31:53 +01:00
|
|
|
-- create an empty source
|
2004-03-18 08:01:14 +01:00
|
|
|
local function empty()
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function source.empty()
|
|
|
|
return empty
|
|
|
|
end
|
|
|
|
|
|
|
|
-- returns a source that just outputs an error
|
|
|
|
function source.error(err)
|
2004-02-11 04:31:53 +01:00
|
|
|
return function()
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- creates a file source
|
|
|
|
function source.file(handle, io_err)
|
|
|
|
if handle then
|
|
|
|
return function()
|
2013-05-27 10:45:09 +02:00
|
|
|
local chunk = handle:read(_M.BLOCKSIZE)
|
2004-02-11 04:31:53 +01:00
|
|
|
if not chunk then handle:close() end
|
|
|
|
return chunk
|
|
|
|
end
|
2004-03-19 06:04:03 +01:00
|
|
|
else return source.error(io_err or "unable to open file") end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- turns a fancy source into a simple source
|
|
|
|
function source.simplify(src)
|
2004-11-27 08:58:04 +01:00
|
|
|
base.assert(src)
|
2004-02-11 04:31:53 +01:00
|
|
|
return function()
|
|
|
|
local chunk, err_or_new = src()
|
|
|
|
src = err_or_new or src
|
|
|
|
if not chunk then return nil, err_or_new
|
|
|
|
else return chunk end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- creates string source
|
|
|
|
function source.string(s)
|
|
|
|
if s then
|
|
|
|
local i = 1
|
|
|
|
return function()
|
2013-05-27 10:45:09 +02:00
|
|
|
local chunk = string.sub(s, i, i+_M.BLOCKSIZE-1)
|
|
|
|
i = i + _M.BLOCKSIZE
|
2004-02-11 04:31:53 +01:00
|
|
|
if chunk ~= "" then return chunk
|
|
|
|
else return nil end
|
|
|
|
end
|
2004-03-18 08:01:14 +01:00
|
|
|
else return source.empty() end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- creates rewindable source
|
|
|
|
function source.rewind(src)
|
2004-11-27 08:58:04 +01:00
|
|
|
base.assert(src)
|
2004-02-11 04:31:53 +01:00
|
|
|
local t = {}
|
|
|
|
return function(chunk)
|
|
|
|
if not chunk then
|
|
|
|
chunk = table.remove(t)
|
|
|
|
if not chunk then return src()
|
|
|
|
else return chunk end
|
|
|
|
else
|
|
|
|
table.insert(t, chunk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-18 11:01:46 +02:00
|
|
|
-- chains a source with one or several filter(s)
|
|
|
|
function source.chain(src, f, ...)
|
|
|
|
if ... then f=filter.chain(f, ...) end
|
2004-11-27 08:58:04 +01:00
|
|
|
base.assert(src and f)
|
2004-11-28 01:59:12 +01:00
|
|
|
local last_in, last_out = "", ""
|
2005-10-07 06:40:59 +02:00
|
|
|
local state = "feeding"
|
|
|
|
local err
|
2004-11-28 01:59:12 +01:00
|
|
|
return function()
|
2005-10-07 06:40:59 +02:00
|
|
|
if not last_out then
|
|
|
|
base.error('source is empty!', 2)
|
|
|
|
end
|
|
|
|
while true do
|
|
|
|
if state == "feeding" then
|
2004-11-28 01:59:12 +01:00
|
|
|
last_in, err = src()
|
|
|
|
if err then return nil, err end
|
|
|
|
last_out = f(last_in)
|
2005-11-22 09:33:29 +01:00
|
|
|
if not last_out then
|
2005-10-07 06:40:59 +02:00
|
|
|
if last_in then
|
2005-11-22 09:33:29 +01:00
|
|
|
base.error('filter returned inappropriate nil')
|
2005-10-07 06:40:59 +02:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
elseif last_out ~= "" then
|
|
|
|
state = "eating"
|
|
|
|
if last_in then last_in = "" end
|
|
|
|
return last_out
|
|
|
|
end
|
2005-11-22 09:33:29 +01:00
|
|
|
else
|
2005-10-07 06:40:59 +02:00
|
|
|
last_out = f(last_in)
|
2005-11-22 09:33:29 +01:00
|
|
|
if last_out == "" then
|
|
|
|
if last_in == "" then
|
2005-10-07 06:40:59 +02:00
|
|
|
state = "feeding"
|
|
|
|
else
|
2005-11-22 09:33:29 +01:00
|
|
|
base.error('filter returned ""')
|
2005-10-07 06:40:59 +02:00
|
|
|
end
|
|
|
|
elseif not last_out then
|
2005-11-22 09:33:29 +01:00
|
|
|
if last_in then
|
|
|
|
base.error('filter returned inappropriate nil')
|
2005-10-07 06:40:59 +02:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return last_out
|
2004-11-28 01:59:12 +01:00
|
|
|
end
|
2004-03-16 07:42:53 +01:00
|
|
|
end
|
|
|
|
end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
2004-03-16 07:42:53 +01:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
-- creates a source that produces contents of several sources, one after the
|
2004-03-16 07:42:53 +01:00
|
|
|
-- other, as if they were concatenated
|
2005-01-02 23:44:00 +01:00
|
|
|
-- (thanks to Wim Couwenberg)
|
2004-03-16 07:42:53 +01:00
|
|
|
function source.cat(...)
|
2012-09-17 18:30:26 +02:00
|
|
|
local arg = {...}
|
2005-01-02 23:44:00 +01:00
|
|
|
local src = table.remove(arg, 1)
|
2004-05-25 07:27:44 +02:00
|
|
|
return function()
|
2005-01-02 23:44:00 +01:00
|
|
|
while src do
|
|
|
|
local chunk, err = src()
|
|
|
|
if chunk then return chunk end
|
|
|
|
if err then return nil, err end
|
|
|
|
src = table.remove(arg, 1)
|
|
|
|
end
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Sink stuff
|
|
|
|
-----------------------------------------------------------------------------
|
2004-02-11 04:31:53 +01:00
|
|
|
-- creates a sink that stores into a table
|
|
|
|
function sink.table(t)
|
|
|
|
t = t or {}
|
|
|
|
local f = function(chunk, err)
|
|
|
|
if chunk then table.insert(t, chunk) end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
return f, t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- turns a fancy sink into a simple sink
|
|
|
|
function sink.simplify(snk)
|
2004-11-27 08:58:04 +01:00
|
|
|
base.assert(snk)
|
2004-02-11 04:31:53 +01:00
|
|
|
return function(chunk, err)
|
|
|
|
local ret, err_or_new = snk(chunk, err)
|
|
|
|
if not ret then return nil, err_or_new end
|
|
|
|
snk = err_or_new or snk
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- creates a file sink
|
|
|
|
function sink.file(handle, io_err)
|
|
|
|
if handle then
|
|
|
|
return function(chunk, err)
|
2005-11-22 09:33:29 +01:00
|
|
|
if not chunk then
|
2004-02-11 04:31:53 +01:00
|
|
|
handle:close()
|
2004-03-21 08:50:15 +01:00
|
|
|
return 1
|
|
|
|
else return handle:write(chunk) end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
2004-03-18 08:01:14 +01:00
|
|
|
else return sink.error(io_err or "unable to open file") end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- creates a sink that discards data
|
|
|
|
local function null()
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function sink.null()
|
|
|
|
return null
|
|
|
|
end
|
|
|
|
|
2004-03-18 08:01:14 +01:00
|
|
|
-- creates a sink that just returns an error
|
|
|
|
function sink.error(err)
|
|
|
|
return function()
|
|
|
|
return nil, err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-18 11:01:46 +02:00
|
|
|
-- chains a sink with one or several filter(s)
|
|
|
|
function sink.chain(f, snk, ...)
|
|
|
|
if ... then
|
|
|
|
local args = { f, snk, ... }
|
|
|
|
snk = table.remove(args, #args)
|
|
|
|
f = filter.chain(unpack(args))
|
|
|
|
end
|
2004-11-27 08:58:04 +01:00
|
|
|
base.assert(f and snk)
|
2004-02-11 04:31:53 +01:00
|
|
|
return function(chunk, err)
|
2004-11-28 01:59:12 +01:00
|
|
|
if chunk ~= "" then
|
|
|
|
local filtered = f(chunk)
|
|
|
|
local done = chunk and ""
|
|
|
|
while true do
|
|
|
|
local ret, snkerr = snk(filtered, err)
|
|
|
|
if not ret then return nil, snkerr end
|
|
|
|
if filtered == done then return 1 end
|
|
|
|
filtered = f(done)
|
|
|
|
end
|
|
|
|
else return 1 end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Pump stuff
|
|
|
|
-----------------------------------------------------------------------------
|
2004-05-25 07:27:44 +02:00
|
|
|
-- pumps one chunk from the source to the sink
|
|
|
|
function pump.step(src, snk)
|
|
|
|
local chunk, src_err = src()
|
|
|
|
local ret, snk_err = snk(chunk, src_err)
|
2005-08-12 07:56:32 +02:00
|
|
|
if chunk and ret then return 1
|
|
|
|
else return nil, src_err or snk_err end
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- pumps all data from a source to a sink, using a step function
|
|
|
|
function pump.all(src, snk, step)
|
2004-11-27 08:58:04 +01:00
|
|
|
base.assert(src and snk)
|
2004-05-25 07:27:44 +02:00
|
|
|
step = step or pump.step
|
2004-03-16 07:42:53 +01:00
|
|
|
while true do
|
2004-05-25 07:27:44 +02:00
|
|
|
local ret, err = step(src, snk)
|
2005-11-22 09:33:29 +01:00
|
|
|
if not ret then
|
2005-08-12 07:56:32 +02:00
|
|
|
if err then return nil, err
|
|
|
|
else return 1 end
|
|
|
|
end
|
2004-02-11 04:31:53 +01:00
|
|
|
end
|
|
|
|
end
|
2004-11-27 08:58:04 +01:00
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
return _M
|