ltn12
The ltn12 namespace implements the ideas described in LTN012, Filters sources and sinks.
-- loads the LTN12 module
local ltn12 = require("ltn12")
filter
filter.chain(vararg) | Returns a filter that passes all data it receives through each of a series of given filters. |
filter.cycle(low, ctx, extra) | Returns a high-level filter that cycles though a low-level filter by passing it each chunk and updating a context between calls. |
ltn12
ltn12.filter | Contains #filter related functions. |
ltn12.pump | Contains #pump related functions. |
ltn12.sink | Contains #sink related functions. |
ltn12.source | Contains #source related functions. |
pump
pump.all(source, sink) | Pumps all data from a source to a sink. |
pump.step(source, sink) | Pumps one chunk of data from a source to a sink. |
sink
sink.chain(filter, sink) | Creates and returns a new sink that passes data through a filter before sending it to a given sink. |
sink.error(message) | Creates and returns a sink that aborts transmission with the error message. |
sink.file(handle, message) | Creates a sink that sends data to a file. |
sink.null() | Returns a sink that ignores all data it receives. |
sink.simplify(sink) | Creates and returns a simple sink given a fancy #sink. |
sink.table(table) | Creates a sink that stores all chunks in a table. |
source
source.cat(vararg) | Creates a new source that produces the concatenation of the data produced by a number of sources. |
source.chain(source, filter) | Creates a new source that passes data through a filter before returning it. |
source.empty() | Creates and returns an empty source. |
source.error(message) | Creates and returns a source that aborts transmission with the error message. |
source.file(handle, message) | Creates a source that produces the contents of a file. |
source.simplify(source) | Creates and returns a simple source given a fancy source. |
source.string(string) | Creates and returns a source that produces the contents of a string, chunk by chunk. |
filter
Provides filters.
filter.chain(vararg)
vararg
:
Simple filters.
The chained filter.
-- load required modules
local ltn12 = require("ltn12")
local mime = require("mime")
-- create a silly identity filter
id = ltn12.filter.chain(
mime.encode("quoted-printable"),
mime.encode("base64"),
mime.decode("base64"),
mime.decode("quoted-printable")
)
filter.cycle(low, ctx, extra)
low
:
The low-level filter to be cycled.
ctx
:
(optional) The initial context.
extra
:
(optional) Any extra argument the low-level filter might take.
The high-level filter.
-- load the ltn12 module
local ltn12 = require("ltn12")
-- the base64 mime filter factory
encodet['base64'] = function()
return ltn12.filter.cycle(b64, "")
end
ltn12
LTN12 implementation.
ltn12.filter
ltn12.pump
ltn12.sink
ltn12.source
pump
Provide pumps.
pump.all(source, sink)
source
:
Source to pump.
sink
:
Sink to fill.
pump.step(source, sink)
source
:
Source to pump.
sink
:
Sink to fill.
sink
Provides sinks.
sink.chain(filter, sink)
filter
:
sink
:
sink.error(message)
message
:
#sink: A #sink that aborts transmission with the error message.
sink.file(handle, message)
handle
:
A file handle. If handle is #nil, message should give the
reason for failure.
message
:
The reason for failure, when handle is #nil.
A #sink that sends all data to the given handle and closes the file when done, or a sink that aborts the transmission with the error message.
-- load the ltn12 module
local ltn12 = require("ltn12")
-- copy a file
ltn12.pump.all(
ltn12.source.file(io.open("original.png")),
ltn12.sink.file(io.open("copy.png"))
)
sink.null()
A #sink that ignores all data it receives.
sink.simplify(sink)
sink
:
Fancy sink.
#sink: A simple #sink.
sink.table(table)
table
:
(optional) Used to hold the chunks. If #nil, the function
creates its own table.
#sink, #table: The sink and the table used to store the chunks.
-- load needed modules
local http = require("socket.http")
local ltn12 = require("ltn12")
-- a simplified http.get function
function http.get(u)
local t = {}
local respt = request{
url = u,
sink = ltn12.sink.table(t)
}
return table.concat(t), respt.headers, respt.code
end
source
Provides sources.
source.cat(vararg)
vararg
:
The original sources.
#source: The new #source.
source.chain(source, filter)
source
:
Passes data through a filter before returning it.
filter
:
#source: The new #source.
source.empty()
#source: An empty #source.
source.error(message)
message
:
#source: A #source that aborts transmission with the error message.
source.file(handle, message)
In the following example, notice how the prototype is designed to fit nicely with the io.open function.
-- load the ltn12 module
local ltn12 = require("ltn12")
-- copy a file
ltn12.pump.all(
ltn12.source.file(io.open("original.png")),
ltn12.sink.file(io.open("copy.png"))
)
handle
:
A file handle. If handle is #nil, message should give the
reason for failure.
message
:
Gives the reason for failure, when handle is #nil.
#source: A #source that reads chunks of data from given handle and returns it to the user, closing the file when done, or a source that aborts the transmission with the error message.
source.simplify(source)
source
:
A fancy #source.
#source: A simple #source.
source.string(string)
string
:
Content of given #source.
#source: A #source that produces the contents of given string, chunk by chunk.