| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
local string = require("string") |
| 11 |
local table = require("table") |
| 12 |
local unpack = unpack or table.unpack |
| 13 |
local base = _G |
| 14 |
local _M = {} |
| 15 |
if module then |
| 16 |
ltn12 = _M |
| 17 |
end |
| 18 |
module("ltn12") |
| 19 |
local filter,source,sink,pump = {},{},{},{} |
| 20 |
|
| 21 |
_M.filter = filter |
| 22 |
_M.source = source |
| 23 |
_M.sink = sink |
| 24 |
_M.pump = pump |
| 25 |
|
| 26 |
local unpack = unpack or table.unpack |
| 27 |
local select = base.select |
| 28 |
|
| 29 |
|
| 30 |
_M.BLOCKSIZE = 2048 |
| 31 |
_M._VERSION = "LTN12 1.0.3" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
function filter.cycle(low, ctx, extra) |
| 38 |
base.assert(low) |
| 39 |
return function(chunk) |
| 40 |
local ret |
| 41 |
ret, ctx = low(ctx, chunk, extra) |
| 42 |
return ret |
| 43 |
end |
| 44 |
end |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
function filter.chain(...) |
| 49 |
local arg = {...} |
| 50 |
local n = base.select('#',...) |
| 51 |
local top, index = 1, 1 |
| 52 |
local retry = "" |
| 53 |
return function(chunk) |
| 54 |
retry = chunk and retry |
| 55 |
while true do |
| 56 |
if index == top then |
| 57 |
chunk = arg[index](chunk) |
| 58 |
if chunk == "" or top == n then return chunk |
| 59 |
elseif chunk then index = index + 1 |
| 60 |
else |
| 61 |
top = top+1 |
| 62 |
index = top |
| 63 |
end |
| 64 |
else |
| 65 |
chunk = arg[index](chunk or "") |
| 66 |
if chunk == "" then |
| 67 |
index = index - 1 |
| 68 |
chunk = retry |
| 69 |
elseif chunk then |
| 70 |
if index == n then return chunk |
| 71 |
else index = index + 1 end |
| 72 |
else base.error("filter returned inappropriate nil") end |
| 73 |
end |
| 74 |
end |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
local function empty() |
| 83 |
return nil |
| 84 |
end |
| 85 |
|
| 86 |
function source.empty() |
| 87 |
return empty |
| 88 |
end |
| 89 |
|
| 90 |
|
| 91 |
function source.error(err) |
| 92 |
return function() |
| 93 |
return nil, err |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
|
| 98 |
function source.file(handle, io_err) |
| 99 |
if handle then |
| 100 |
return function() |
| 101 |
local chunk = handle:read(_M.BLOCKSIZE) |
| 102 |
if not chunk then handle:close() end |
| 103 |
return chunk |
| 104 |
end |
| 105 |
else return source.error(io_err or "unable to open file") end |
| 106 |
end |
| 107 |
|
| 108 |
|
| 109 |
function source.simplify(src) |
| 110 |
base.assert(src) |
| 111 |
return function() |
| 112 |
local chunk, err_or_new = src() |
| 113 |
src = err_or_new or src |
| 114 |
if not chunk then return nil, err_or_new |
| 115 |
else return chunk end |
| 116 |
end |
| 117 |
end |
| 118 |
|
| 119 |
|
| 120 |
function source.string(s) |
| 121 |
if s then |
| 122 |
local i = 1 |
| 123 |
return function() |
| 124 |
local chunk = string.sub(s, i, i+_M.BLOCKSIZE-1) |
| 125 |
i = i + _M.BLOCKSIZE |
| 126 |
if chunk ~= "" then return chunk |
| 127 |
else return nil end |
| 128 |
end |
| 129 |
else return source.empty() end |
| 130 |
end |
| 131 |
|
| 132 |
|
| 133 |
function source.rewind(src) |
| 134 |
base.assert(src) |
| 135 |
local t = {} |
| 136 |
return function(chunk) |
| 137 |
if not chunk then |
| 138 |
chunk = table.remove(t) |
| 139 |
if not chunk then return src() |
| 140 |
else return chunk end |
| 141 |
else |
| 142 |
table.insert(t, chunk) |
| 143 |
end |
| 144 |
end |
| 145 |
end |
| 146 |
|
| 147 |
|
| 148 |
function source.chain(src, f, ...) |
| 149 |
if ... then f=filter.chain(f, ...) end |
| 150 |
base.assert(src and f) |
| 151 |
local last_in, last_out = "", "" |
| 152 |
local state = "feeding" |
| 153 |
local err |
| 154 |
return function() |
| 155 |
if not last_out then |
| 156 |
base.error('source is empty!', 2) |
| 157 |
end |
| 158 |
while true do |
| 159 |
if state == "feeding" then |
| 160 |
last_in, err = src() |
| 161 |
if err then return nil, err end |
| 162 |
last_out = f(last_in) |
| 163 |
if not last_out then |
| 164 |
if last_in then |
| 165 |
base.error('filter returned inappropriate nil') |
| 166 |
else |
| 167 |
return nil |
| 168 |
end |
| 169 |
elseif last_out ~= "" then |
| 170 |
state = "eating" |
| 171 |
if last_in then last_in = "" end |
| 172 |
return last_out |
| 173 |
end |
| 174 |
else |
| 175 |
last_out = f(last_in) |
| 176 |
if last_out == "" then |
| 177 |
if last_in == "" then |
| 178 |
state = "feeding" |
| 179 |
else |
| 180 |
base.error('filter returned ""') |
| 181 |
end |
| 182 |
elseif not last_out then |
| 183 |
if last_in then |
| 184 |
base.error('filter returned inappropriate nil') |
| 185 |
else |
| 186 |
return nil |
| 187 |
end |
| 188 |
else |
| 189 |
return last_out |
| 190 |
end |
| 191 |
end |
| 192 |
end |
| 193 |
end |
| 194 |
end |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
function source.cat(...) |
| 200 |
local arg = {...} |
| 201 |
local src = table.remove(arg, 1) |
| 202 |
return function() |
| 203 |
while src do |
| 204 |
local chunk, err = src() |
| 205 |
if chunk then return chunk end |
| 206 |
if err then return nil, err end |
| 207 |
src = table.remove(arg, 1) |
| 208 |
end |
| 209 |
end |
| 210 |
end |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
function sink.table(t) |
| 217 |
t = t or {} |
| 218 |
local f = function(chunk, err) |
| 219 |
if chunk then table.insert(t, chunk) end |
| 220 |
return 1 |
| 221 |
end |
| 222 |
return f, t |
| 223 |
end |
| 224 |
|
| 225 |
|
| 226 |
function sink.simplify(snk) |
| 227 |
base.assert(snk) |
| 228 |
return function(chunk, err) |
| 229 |
local ret, err_or_new = snk(chunk, err) |
| 230 |
if not ret then return nil, err_or_new end |
| 231 |
snk = err_or_new or snk |
| 232 |
return 1 |
| 233 |
end |
| 234 |
end |
| 235 |
|
| 236 |
|
| 237 |
function sink.file(handle, io_err) |
| 238 |
if handle then |
| 239 |
return function(chunk, err) |
| 240 |
if not chunk then |
| 241 |
handle:close() |
| 242 |
return 1 |
| 243 |
else return handle:write(chunk) end |
| 244 |
end |
| 245 |
else return sink.error(io_err or "unable to open file") end |
| 246 |
end |
| 247 |
|
| 248 |
|
| 249 |
local function null() |
| 250 |
return 1 |
| 251 |
end |
| 252 |
|
| 253 |
function sink.null() |
| 254 |
return null |
| 255 |
end |
| 256 |
|
| 257 |
|
| 258 |
function sink.error(err) |
| 259 |
return function() |
| 260 |
return nil, err |
| 261 |
end |
| 262 |
end |
| 263 |
|
| 264 |
|
| 265 |
function sink.chain(f, snk, ...) |
| 266 |
if ... then |
| 267 |
local args = { f, snk, ... } |
| 268 |
snk = table.remove(args, #args) |
| 269 |
f = filter.chain(unpack(args)) |
| 270 |
end |
| 271 |
base.assert(f and snk) |
| 272 |
return function(chunk, err) |
| 273 |
if chunk ~= "" then |
| 274 |
local filtered = f(chunk) |
| 275 |
local done = chunk and "" |
| 276 |
while true do |
| 277 |
local ret, snkerr = snk(filtered, err) |
| 278 |
if not ret then return nil, snkerr end |
| 279 |
if filtered == done then return 1 end |
| 280 |
filtered = f(done) |
| 281 |
end |
| 282 |
else return 1 end |
| 283 |
end |
| 284 |
end |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
function pump.step(src, snk) |
| 291 |
local chunk, src_err = src() |
| 292 |
local ret, snk_err = snk(chunk, src_err) |
| 293 |
if chunk and ret then return 1 |
| 294 |
else return nil, src_err or snk_err end |
| 295 |
end |
| 296 |
|
| 297 |
|
| 298 |
function pump.all(src, snk, step) |
| 299 |
base.assert(src and snk) |
| 300 |
step = step or pump.step |
| 301 |
while true do |
| 302 |
local ret, err = step(src, snk) |
| 303 |
if not ret then |
| 304 |
if err then return nil, err |
| 305 |
else return 1 end |
| 306 |
end |
| 307 |
end |
| 308 |
end |
| 309 |
|
| 310 |
return _M |