diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-25 05:27:44 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-25 05:27:44 +0000 |
| commit | 888496aa821cd09d925046250ea98b1512293fd5 (patch) | |
| tree | 217e2b532762a64b58c4fffcb2cba08ba2243462 /src/ltn12.lua | |
| parent | 4fc164b8eaa0453050a0a859321c327bb2c4f776 (diff) | |
| download | luasocket-888496aa821cd09d925046250ea98b1512293fd5.tar.gz luasocket-888496aa821cd09d925046250ea98b1512293fd5.tar.bz2 luasocket-888496aa821cd09d925046250ea98b1512293fd5.zip | |
FTP low-level working.
SMTP connection oriented working.
ltn12 improved.
Diffstat (limited to 'src/ltn12.lua')
| -rw-r--r-- | src/ltn12.lua | 46 |
1 files changed, 20 insertions, 26 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index dac932b..56e6043 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
| @@ -8,6 +8,7 @@ setfenv(1, ltn12) | |||
| 8 | filter = {} | 8 | filter = {} |
| 9 | source = {} | 9 | source = {} |
| 10 | sink = {} | 10 | sink = {} |
| 11 | pump = {} | ||
| 11 | 12 | ||
| 12 | -- 2048 seems to be better in windows... | 13 | -- 2048 seems to be better in windows... |
| 13 | BLOCKSIZE = 2048 | 14 | BLOCKSIZE = 2048 |
| @@ -22,7 +23,6 @@ end | |||
| 22 | 23 | ||
| 23 | -- returns a high level filter that cycles a cycles a low-level filter | 24 | -- returns a high level filter that cycles a cycles a low-level filter |
| 24 | function filter.cycle(low, ctx, extra) | 25 | function filter.cycle(low, ctx, extra) |
| 25 | if type(low) ~= 'function' then error('invalid low-level filter', 2) end | ||
| 26 | return function(chunk) | 26 | return function(chunk) |
| 27 | local ret | 27 | local ret |
| 28 | ret, ctx = low(ctx, chunk, extra) | 28 | ret, ctx = low(ctx, chunk, extra) |
| @@ -32,8 +32,6 @@ end | |||
| 32 | 32 | ||
| 33 | -- chains two filters together | 33 | -- chains two filters together |
| 34 | local function chain2(f1, f2) | 34 | local function chain2(f1, f2) |
| 35 | if type(f1) ~= 'function' then error('invalid filter', 2) end | ||
| 36 | if type(f2) ~= 'function' then error('invalid filter', 2) end | ||
| 37 | local co = coroutine.create(function(chunk) | 35 | local co = coroutine.create(function(chunk) |
| 38 | while true do | 36 | while true do |
| 39 | local filtered1 = f1(chunk) | 37 | local filtered1 = f1(chunk) |
| @@ -58,7 +56,6 @@ end | |||
| 58 | function filter.chain(...) | 56 | function filter.chain(...) |
| 59 | local f = arg[1] | 57 | local f = arg[1] |
| 60 | for i = 2, table.getn(arg) do | 58 | for i = 2, table.getn(arg) do |
| 61 | if type(arg[i]) ~= 'function' then error('invalid filter', 2) end | ||
| 62 | f = chain2(f, arg[i]) | 59 | f = chain2(f, arg[i]) |
| 63 | end | 60 | end |
| 64 | return f | 61 | return f |
| @@ -93,7 +90,6 @@ end | |||
| 93 | 90 | ||
| 94 | -- turns a fancy source into a simple source | 91 | -- turns a fancy source into a simple source |
| 95 | function source.simplify(src) | 92 | function source.simplify(src) |
| 96 | if type(src) ~= 'function' then error('invalid source', 2) end | ||
| 97 | return function() | 93 | return function() |
| 98 | local chunk, err_or_new = src() | 94 | local chunk, err_or_new = src() |
| 99 | src = err_or_new or src | 95 | src = err_or_new or src |
| @@ -117,7 +113,6 @@ end | |||
| 117 | 113 | ||
| 118 | -- creates rewindable source | 114 | -- creates rewindable source |
| 119 | function source.rewind(src) | 115 | function source.rewind(src) |
| 120 | if type(src) ~= 'function' then error('invalid source', 2) end | ||
| 121 | local t = {} | 116 | local t = {} |
| 122 | return function(chunk) | 117 | return function(chunk) |
| 123 | if not chunk then | 118 | if not chunk then |
| @@ -132,8 +127,6 @@ end | |||
| 132 | 127 | ||
| 133 | -- chains a source with a filter | 128 | -- chains a source with a filter |
| 134 | function source.chain(src, f) | 129 | function source.chain(src, f) |
| 135 | if type(src) ~= 'function' then error('invalid source', 2) end | ||
| 136 | if type(f) ~= 'function' then error('invalid filter', 2) end | ||
| 137 | local co = coroutine.create(function() | 130 | local co = coroutine.create(function() |
| 138 | while true do | 131 | while true do |
| 139 | local chunk, err = src() | 132 | local chunk, err = src() |
| @@ -152,20 +145,21 @@ function source.chain(src, f) | |||
| 152 | end | 145 | end |
| 153 | end | 146 | end |
| 154 | 147 | ||
| 155 | -- creates a source that produces contents of several files one after the | 148 | -- creates a source that produces contents of several sources, one after the |
| 156 | -- other, as if they were concatenated | 149 | -- other, as if they were concatenated |
| 157 | function source.cat(...) | 150 | function source.cat(...) |
| 158 | local co = coroutine.create(function() | 151 | local co = coroutine.create(function() |
| 159 | local i = 1 | 152 | local i = 1 |
| 160 | while i <= table.getn(arg) do | 153 | while i <= table.getn(arg) do |
| 161 | local chunk = arg[i]:read(2048) | 154 | local chunk, err = arg[i]() |
| 162 | if chunk then coroutine.yield(chunk) | 155 | if chunk then coroutine.yield(chunk) |
| 163 | else i = i + 1 end | 156 | elseif err then return nil, err |
| 157 | else i = i + 1 end | ||
| 164 | end | 158 | end |
| 165 | end) | 159 | end) |
| 166 | return source.simplify(function() | 160 | return function() |
| 167 | return shift(coroutine.resume(co)) | 161 | return shift(coroutine.resume(co)) |
| 168 | end) | 162 | end |
| 169 | end | 163 | end |
| 170 | 164 | ||
| 171 | -- creates a sink that stores into a table | 165 | -- creates a sink that stores into a table |
| @@ -180,7 +174,6 @@ end | |||
| 180 | 174 | ||
| 181 | -- turns a fancy sink into a simple sink | 175 | -- turns a fancy sink into a simple sink |
| 182 | function sink.simplify(snk) | 176 | function sink.simplify(snk) |
| 183 | if type(snk) ~= 'function' then error('invalid sink', 2) end | ||
| 184 | return function(chunk, err) | 177 | return function(chunk, err) |
| 185 | local ret, err_or_new = snk(chunk, err) | 178 | local ret, err_or_new = snk(chunk, err) |
| 186 | if not ret then return nil, err_or_new end | 179 | if not ret then return nil, err_or_new end |
| @@ -219,8 +212,6 @@ end | |||
| 219 | 212 | ||
| 220 | -- chains a sink with a filter | 213 | -- chains a sink with a filter |
| 221 | function sink.chain(f, snk) | 214 | function sink.chain(f, snk) |
| 222 | if type(snk) ~= 'function' then error('invalid sink', 2) end | ||
| 223 | if type(f) ~= 'function' then error('invalid filter', 2) end | ||
| 224 | return function(chunk, err) | 215 | return function(chunk, err) |
| 225 | local filtered = f(chunk) | 216 | local filtered = f(chunk) |
| 226 | local done = chunk and "" | 217 | local done = chunk and "" |
| @@ -233,15 +224,18 @@ function sink.chain(f, snk) | |||
| 233 | end | 224 | end |
| 234 | end | 225 | end |
| 235 | 226 | ||
| 236 | -- pumps all data from a source to a sink | 227 | -- pumps one chunk from the source to the sink |
| 237 | function pump(src, snk) | 228 | function pump.step(src, snk) |
| 238 | if type(src) ~= 'function' then error('invalid source', 2) end | 229 | local chunk, src_err = src() |
| 239 | if type(snk) ~= 'function' then error('invalid sink', 2) end | 230 | local ret, snk_err = snk(chunk, src_err) |
| 231 | return chunk and ret and not src_err and not snk_err, src_err or snk_err | ||
| 232 | end | ||
| 233 | |||
| 234 | -- pumps all data from a source to a sink, using a step function | ||
| 235 | function pump.all(src, snk, step) | ||
| 236 | step = step or pump.step | ||
| 240 | while true do | 237 | while true do |
| 241 | local chunk, src_err = src() | 238 | local ret, err = step(src, snk) |
| 242 | local ret, snk_err = snk(chunk, src_err) | 239 | if not ret then return not err, err end |
| 243 | if not chunk or not ret then | ||
| 244 | return not src_err and not snk_err, src_err or snk_err | ||
| 245 | end | ||
| 246 | end | 240 | end |
| 247 | end | 241 | end |
