1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
-- original file : ltn12.lua
-- for more into : see util-soc.lua
local select, unpack = select, unpack
local insert, remove = table.insert, table.remove
local sub = string.sub
local function report(fmt,first,...)
if logs then
report = logs and logs.reporter("ltn12")
report(fmt,first,...)
elseif fmt then
fmt = "ltn12: " .. fmt
if first then
print(format(fmt,first,...))
else
print(fmt)
end
end
end
local filter = { }
local source = { }
local sink = { }
local pump = { }
local ltn12 = {
_VERSION = "LTN12 1.0.3",
BLOCKSIZE = 2048,
filter = filter,
source = source,
sink = sink,
pump = pump,
report = report,
}
-- returns a high level filter that cycles a low-level filter
function filter.cycle(low, ctx, extra)
if low then
return function(chunk)
return (low(ctx, chunk, extra))
end
end
end
-- chains a bunch of filters together
function filter.chain(...)
local arg = { ... }
local n = select('#',...)
local top = 1
local index = 1
local retry = ""
return function(chunk)
retry = chunk and retry
while true do
local action = arg[index]
if index == top then
chunk = action(chunk)
if chunk == "" or top == n then
return chunk
elseif chunk then
index = index + 1
else
top = top + 1
index = top
end
else
chunk = action(chunk or "")
if chunk == "" then
index = index - 1
chunk = retry
elseif chunk then
if index == n then
return chunk
else
index = index + 1
end
else
report("error: filter returned inappropriate 'nil'")
return
end
end
end
end
end
-- create an empty source
local function empty()
return nil
end
function source.empty()
return empty
end
-- returns a source that just outputs an error
local function sourceerror(err)
return function()
return nil, err
end
end
source.error = sourceerror
-- creates a file source
function source.file(handle, io_err)
if handle then
local blocksize = ltn12.BLOCKSIZE
return function()
local chunk = handle:read(blocksize)
if not chunk then
handle:close()
end
return chunk
end
else
return sourceerror(io_err or "unable to open file")
end
end
-- turns a fancy source into a simple source
function source.simplify(src)
return function()
local chunk, err_or_new = src()
if err_or_new then
src = err_or_new
end
if chunk then
return chunk
else
return nil, err_or_new
end
end
end
-- creates string source
function source.string(s)
if s then
local blocksize = ltn12.BLOCKSIZE
local i = 1
return function()
local nexti = i + blocksize
local chunk = sub(s, i, nexti - 1)
i = nexti
if chunk ~= "" then
return chunk
else
return nil
end
end
else return source.empty() end
end
-- creates rewindable source
function source.rewind(src)
local t = { }
return function(chunk)
if chunk then
insert(t, chunk)
else
chunk = remove(t)
if chunk then
return chunk
else
return src()
end
end
end
end
-- chains a source with one or several filter(s)
function source.chain(src, f, ...)
if ... then
f = filter.chain(f, ...)
end
local last_in = ""
local last_out = ""
local state = "feeding"
local err
return function()
if not last_out then
report("error: source is empty")
return
end
while true do
if state == "feeding" then
last_in, err = src()
if err then
return nil, err
end
last_out = f(last_in)
if not last_out then
if last_in then
report("error: filter returned inappropriate 'nil'")
end
return nil
elseif last_out ~= "" then
state = "eating"
if last_in then
last_in = ""
end
return last_out
end
else
last_out = f(last_in)
if last_out == "" then
if last_in == "" then
state = "feeding"
else
report("error: filter returned nothing")
return
end
elseif not last_out then
if last_in then
report("filter returned inappropriate 'nil'")
end
return nil
else
return last_out
end
end
end
end
end
-- creates a source that produces contents of several sources, one after the
-- other, as if they were concatenated
function source.cat(...)
local arg = { ... }
local src = remove(arg,1)
return function()
while src do
local chunk, err = src()
if chunk then
return chunk
end
if err then
return nil, err
end
src = remove(arg,1)
end
end
end
-- creates a sink that stores into a table
function sink.table(t)
if not t then
t = { }
end
local f = function(chunk, err)
if chunk then
insert(t, chunk)
end
return 1
end
return f, t
end
-- turns a fancy sink into a simple sink
function sink.simplify(snk)
return function(chunk, err)
local ret, err_or_new = snk(chunk, err)
if not ret then
return nil, err_or_new
end
if err_or_new then
snk = err_or_new
end
return 1
end
end
-- creates a sink that discards data
local function null()
return 1
end
function sink.null()
return null
end
-- creates a sink that just returns an error
local function sinkerror(err)
return function()
return nil, err
end
end
sink.error = sinkerror
-- creates a file sink
function sink.file(handle, io_err)
if handle then
return function(chunk, err)
if not chunk then
handle:close()
return 1
else
return handle:write(chunk)
end
end
else
return sinkerror(io_err or "unable to open file")
end
end
-- chains a sink with one or several filter(s)
function sink.chain(f, snk, ...)
if ... then
local args = { f, snk, ... }
snk = remove(args, #args)
f = filter.chain(unpack(args))
end
return function(chunk, err)
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
end
end
-- 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)
if chunk and ret then
return 1
else
return nil, src_err or snk_err
end
end
-- pumps all data from a source to a sink, using a step function
function pump.all(src, snk, step)
if not step then
step = pump.step
end
while true do
local ret, err = step(src, snk)
if not ret then
if err then
return nil, err
else
return 1
end
end
end
end
package.loaded["ltn12"] = ltn12
return ltn12
|