diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-10-12 22:35:20 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-10-12 22:35:20 +0000 |
commit | 4964552718b81dba2c36fc08494aefae89a799f4 (patch) | |
tree | 5473a82045a7f66f31c56f4cbd151b16a61e7e90 /src/ltn12.lua | |
parent | 396946b63a95fc9949b75aed82c7ac1c132ccd40 (diff) | |
download | luasocket-4964552718b81dba2c36fc08494aefae89a799f4.tar.gz luasocket-4964552718b81dba2c36fc08494aefae89a799f4.tar.bz2 luasocket-4964552718b81dba2c36fc08494aefae89a799f4.zip |
My own ltn12.filter.chain is done.
Implemented part of DB's suggestion for ftp.
Mimetest.lua generates the test file for base64 instead of loading from disk.
Diffstat (limited to 'src/ltn12.lua')
-rw-r--r-- | src/ltn12.lua | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index 6c5ea28..9917ce8 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
@@ -31,40 +31,40 @@ function filter.cycle(low, ctx, extra) | |||
31 | end | 31 | end |
32 | end | 32 | end |
33 | 33 | ||
34 | -- chains a bunch of filters together | 34 | local function chain2(f1, f2) |
35 | -- by Wim Couwenberg | 35 | local ff1, ff2 = "", "" |
36 | function filter.chain(...) | ||
37 | local current = 1 | ||
38 | local bottom = 1 | ||
39 | local top = table.getn(arg) | ||
40 | local retry = "" | ||
41 | return function(chunk) | 36 | return function(chunk) |
42 | if chunk ~= retry then | 37 | local rf1 = chunk and "" |
43 | current = bottom | 38 | local rf2 = ff1 and "" |
44 | retry = chunk and retry | 39 | -- if f2 still has pending data, get it and return it |
40 | if ff2 ~= rf2 then | ||
41 | ff2 = f2(rf2) | ||
42 | if ff2 ~= "" then return ff2 end | ||
43 | end | ||
44 | -- here we know f2 needs more data | ||
45 | -- we try to get it from f1 | ||
46 | ff1 = f1(chunk) | ||
47 | while 1 do | ||
48 | -- if f1 can't produce data, we need more data from the user | ||
49 | if ff1 == "" then return "" end | ||
50 | -- otherwise we pass new data to f2 until it produces something | ||
51 | -- or f1 runs out of data too | ||
52 | ff2 = f2(ff1) | ||
53 | if ff2 ~= "" then return ff2 end | ||
54 | ff1 = f1(rf1) | ||
45 | end | 55 | end |
46 | repeat | ||
47 | if current == bottom then | ||
48 | chunk = arg[current](chunk) | ||
49 | if chunk == "" or bottom == top then return chunk | ||
50 | elseif chunk then current = current + 1 | ||
51 | else | ||
52 | bottom = bottom + 1 | ||
53 | current = bottom | ||
54 | end | ||
55 | else | ||
56 | chunk = arg[current](chunk or "") | ||
57 | if chunk == "" then | ||
58 | chunk = retry | ||
59 | current = current - 1 | ||
60 | elseif current == top then return chunk | ||
61 | else current = current + 1 | ||
62 | end | ||
63 | end | ||
64 | until false | ||
65 | end | 56 | end |
66 | end | 57 | end |
67 | 58 | ||
59 | -- chains a bunch of filters together | ||
60 | function filter.chain(...) | ||
61 | local f = arg[1] | ||
62 | for i = 2, table.getn(arg) do | ||
63 | f = chain2(f, arg[i]) | ||
64 | end | ||
65 | return f | ||
66 | end | ||
67 | |||
68 | ----------------------------------------------------------------------------- | 68 | ----------------------------------------------------------------------------- |
69 | -- Source stuff | 69 | -- Source stuff |
70 | ----------------------------------------------------------------------------- | 70 | ----------------------------------------------------------------------------- |