源战役客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

309 rindas
9.3 KiB

pirms 4 nedēļām
  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 rewindable source
  119. -- function source.rewind(src)
  120. -- base.assert(src)
  121. -- local t = {}
  122. -- return function(chunk)
  123. -- if not chunk then
  124. -- chunk = table.remove(t)
  125. -- if not chunk then return src()
  126. -- else return chunk end
  127. -- else
  128. -- table.insert(t, chunk)
  129. -- end
  130. -- end
  131. -- end
  132. -- -- chains a source with one or several filter(s)
  133. -- function source.chain(src, f, ...)
  134. -- if ... then f=filter.chain(f, ...) end
  135. -- base.assert(src and f)
  136. -- local last_in, last_out = "", ""
  137. -- local state = "feeding"
  138. -- local err
  139. -- return function()
  140. -- if not last_out then
  141. -- base.error('source is empty!', 2)
  142. -- end
  143. -- while true do
  144. -- if state == "feeding" then
  145. -- last_in, err = src()
  146. -- if err then return nil, err end
  147. -- last_out = f(last_in)
  148. -- if not last_out then
  149. -- if last_in then
  150. -- base.error('filter returned inappropriate nil')
  151. -- else
  152. -- return nil
  153. -- end
  154. -- elseif last_out ~= "" then
  155. -- state = "eating"
  156. -- if last_in then last_in = "" end
  157. -- return last_out
  158. -- end
  159. -- else
  160. -- last_out = f(last_in)
  161. -- if last_out == "" then
  162. -- if last_in == "" then
  163. -- state = "feeding"
  164. -- else
  165. -- base.error('filter returned ""')
  166. -- end
  167. -- elseif not last_out then
  168. -- if last_in then
  169. -- base.error('filter returned inappropriate nil')
  170. -- else
  171. -- return nil
  172. -- end
  173. -- else
  174. -- return last_out
  175. -- end
  176. -- end
  177. -- end
  178. -- end
  179. -- end
  180. -- -- creates a source that produces contents of several sources, one after the
  181. -- -- other, as if they were concatenated
  182. -- -- (thanks to Wim Couwenberg)
  183. -- function source.cat(...)
  184. -- local arg = {...}
  185. -- local src = table.remove(arg, 1)
  186. -- return function()
  187. -- while src do
  188. -- local chunk, err = src()
  189. -- if chunk then return chunk end
  190. -- if err then return nil, err end
  191. -- src = table.remove(arg, 1)
  192. -- end
  193. -- end
  194. -- end
  195. -- -----------------------------------------------------------------------------
  196. -- -- Sink stuff
  197. -- -----------------------------------------------------------------------------
  198. -- -- creates a sink that stores into a table
  199. -- function sink.table(t)
  200. -- t = t or {}
  201. -- local f = function(chunk, err)
  202. -- if chunk then table.insert(t, chunk) end
  203. -- return 1
  204. -- end
  205. -- return f, t
  206. -- end
  207. -- -- turns a fancy sink into a simple sink
  208. -- function sink.simplify(snk)
  209. -- base.assert(snk)
  210. -- return function(chunk, err)
  211. -- local ret, err_or_new = snk(chunk, err)
  212. -- if not ret then return nil, err_or_new end
  213. -- snk = err_or_new or snk
  214. -- return 1
  215. -- end
  216. -- end
  217. -- -- creates a file sink
  218. -- function sink.file(handle, io_err)
  219. -- if handle then
  220. -- return function(chunk, err)
  221. -- if not chunk then
  222. -- handle:close()
  223. -- return 1
  224. -- else return handle:write(chunk) end
  225. -- end
  226. -- else return sink.error(io_err or "unable to open file") end
  227. -- end
  228. -- -- creates a sink that discards data
  229. -- local function null()
  230. -- return 1
  231. -- end
  232. -- function sink.null()
  233. -- return null
  234. -- end
  235. -- -- creates a sink that just returns an error
  236. -- function sink.error(err)
  237. -- return function()
  238. -- return nil, err
  239. -- end
  240. -- end
  241. -- -- chains a sink with one or several filter(s)
  242. -- function sink.chain(f, snk, ...)
  243. -- if ... then
  244. -- local args = { f, snk, ... }
  245. -- snk = table.remove(args, #args)
  246. -- f = filter.chain(unpack(args))
  247. -- end
  248. -- base.assert(f and snk)
  249. -- return function(chunk, err)
  250. -- if chunk ~= "" then
  251. -- local filtered = f(chunk)
  252. -- local done = chunk and ""
  253. -- while true do
  254. -- local ret, snkerr = snk(filtered, err)
  255. -- if not ret then return nil, snkerr end
  256. -- if filtered == done then return 1 end
  257. -- filtered = f(done)
  258. -- end
  259. -- else return 1 end
  260. -- end
  261. -- end
  262. -- -----------------------------------------------------------------------------
  263. -- -- Pump stuff
  264. -- -----------------------------------------------------------------------------
  265. -- -- pumps one chunk from the source to the sink
  266. -- function pump.step(src, snk)
  267. -- local chunk, src_err = src()
  268. -- local ret, snk_err = snk(chunk, src_err)
  269. -- if chunk and ret then return 1
  270. -- else return nil, src_err or snk_err end
  271. -- end
  272. -- -- pumps all data from a source to a sink, using a step function
  273. -- function pump.all(src, snk, step)
  274. -- base.assert(src and snk)
  275. -- step = step or pump.step
  276. -- while true do
  277. -- local ret, err = step(src, snk)
  278. -- if not ret then
  279. -- if err then return nil, err
  280. -- else return 1 end
  281. -- end
  282. -- end
  283. -- end
  284. -- return _M