Advertisement
Kep13

LuaZ

Dec 21st, 2020
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. --[[--------------------------------------------------------------------
  2.  
  3. lzio.lua
  4. Lua buffered streams in Lua
  5. This file is part of Yueliang.
  6.  
  7. Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
  8. The COPYRIGHT file describes the conditions
  9. under which this software may be distributed.
  10.  
  11. See the ChangeLog for more information.
  12.  
  13. ----------------------------------------------------------------------]]
  14.  
  15. --[[--------------------------------------------------------------------
  16. -- Notes:
  17. -- * EOZ is implemented as a string, "EOZ"
  18. -- * Format of z structure (ZIO)
  19. -- z.n -- bytes still unread
  20. -- z.p -- last read position position in buffer
  21. -- z.reader -- chunk reader function
  22. -- z.data -- additional data
  23. -- * Current position, p, is now last read index instead of a pointer
  24. --
  25. -- Not implemented:
  26. -- * luaZ_lookahead: used only in lapi.c:lua_load to detect binary chunk
  27. -- * luaZ_read: used only in lundump.c:ezread to read +1 bytes
  28. -- * luaZ_openspace: dropped; let Lua handle buffers as strings (used in
  29. -- lundump.c:LoadString & lvm.c:luaV_concat)
  30. -- * luaZ buffer macros: dropped; buffers are handled as strings
  31. -- * lauxlib.c:getF reader implementation has an extraline flag to
  32. -- skip over a shbang (#!) line, this is not implemented here
  33. --
  34. -- Added:
  35. -- (both of the following are vaguely adapted from lauxlib.c)
  36. -- * luaZ:make_getS: create Reader from a string
  37. -- * luaZ:make_getF: create Reader that reads from a file
  38. --
  39. -- Changed in 5.1.x:
  40. -- * Chunkreader renamed to Reader (ditto with Chunkwriter)
  41. -- * Zio struct: no more name string, added Lua state for reader
  42. -- (however, Yueliang readers do not require a Lua state)
  43. ----------------------------------------------------------------------]]
  44.  
  45. local luaZ = {}
  46.  
  47. ------------------------------------------------------------------------
  48. -- * reader() should return a string, or nil if nothing else to parse.
  49. -- Additional data can be set only during stream initialization
  50. -- * Readers are handled in lauxlib.c, see luaL_load(file|buffer|string)
  51. -- * LUAL_BUFFERSIZE=BUFSIZ=512 in make_getF() (located in luaconf.h)
  52. -- * Original Reader typedef:
  53. -- const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
  54. -- * This Lua chunk reader implementation:
  55. -- returns string or nil, no arguments to function
  56. ------------------------------------------------------------------------
  57.  
  58. ------------------------------------------------------------------------
  59. -- create a chunk reader from a source string
  60. ------------------------------------------------------------------------
  61. function luaZ:make_getS(buff)
  62. local b = buff
  63. return function() -- chunk reader anonymous function here
  64. if not b then return nil end
  65. local data = b
  66. b = nil
  67. return data
  68. end
  69. end
  70.  
  71. ------------------------------------------------------------------------
  72. -- create a chunk reader from a source file
  73. ------------------------------------------------------------------------
  74. --[[
  75. function luaZ:make_getF(filename)
  76. local LUAL_BUFFERSIZE = 512
  77. local h = io.open(filename, "r")
  78. if not h then return nil end
  79. return function() -- chunk reader anonymous function here
  80. if not h or io.type(h) == "closed file" then return nil end
  81. local buff = h:read(LUAL_BUFFERSIZE)
  82. if not buff then h:close(); h = nil end
  83. return buff
  84. end
  85. end
  86. --]]
  87. ------------------------------------------------------------------------
  88. -- creates a zio input stream
  89. -- returns the ZIO structure, z
  90. ------------------------------------------------------------------------
  91. function luaZ:init(reader, data, name)
  92. if not reader then return end
  93. local z = {}
  94. z.reader = reader
  95. z.data = data or ""
  96. z.name = name
  97. -- set up additional data for reading
  98. if not data or data == "" then z.n = 0 else z.n = #data end
  99. z.p = 0
  100. return z
  101. end
  102.  
  103. ------------------------------------------------------------------------
  104. -- fill up input buffer
  105. ------------------------------------------------------------------------
  106. function luaZ:fill(z)
  107. local buff = z.reader()
  108. z.data = buff
  109. if not buff or buff == "" then return "EOZ" end
  110. z.n, z.p = #buff - 1, 1
  111. return string.sub(buff, 1, 1)
  112. end
  113.  
  114. ------------------------------------------------------------------------
  115. -- get next character from the input stream
  116. -- * local n, p are used to optimize code generation
  117. ------------------------------------------------------------------------
  118. function luaZ:zgetc(z)
  119. local n, p = z.n, z.p + 1
  120. if n > 0 then
  121. z.n, z.p = n - 1, p
  122. return string.sub(z.data, p, p)
  123. else
  124. return self:fill(z)
  125. end
  126. end
  127.  
  128. return luaZ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement