ltn12.lua 8.7 KB

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