aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/lzio_mk2.lua
blob: 63783669081e5b5cae40769ecfd7a81542795769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--[[--------------------------------------------------------------------

  lzio.lua
  Lua 5 buffered streams in Lua
  This file is part of Yueliang.

  Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
  The COPYRIGHT file describes the conditions
  under which this software may be distributed.

  See the ChangeLog for more information.

----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- Notes:
-- *
----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- local zio_init = require("lzio.lua")
-- local z = zio_init("@<filename>")
-- local z = zio_init("<string>")
-- z:getc()
-- * get next character from input stream
-- z:fill()
-- * fills an empty stream buffer
-- z.name
-- * name of the chunk, "@<filename>" or "=string"
----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- Format of z structure (ZIO)
-- z.n       -- bytes still unread
-- z.p       -- last read position in buffer
-- z.reader  -- chunk reader function
-- z.data    -- data buffer
-- z.name    -- name of stream
----------------------------------------------------------------------]]

return
function(buff)
--[[--------------------------------------------------------------------
-- initialize reader
-- * reader should return a string, or nil if nothing else to parse
----------------------------------------------------------------------]]
  local reader
  local z = {}
  if string.sub(buff, 1, 1) == "@" then
    ----------------------------------------------------------------
    -- create a chunk reader function from a source file
    ----------------------------------------------------------------
    z.name = buff
    local BUFFERSIZE = 512
    local h = io.open(string.sub(buff, 2), "r")
    if not h then return nil end
    reader = function()
      if not h or io.type(h) == "closed file" then return nil end
      local buff = h:read(BUFFERSIZE)
      if not buff then h:close(); h = nil end
      return buff
    end
  else
    ----------------------------------------------------------------
    -- create a chunk reader function from a source string
    ----------------------------------------------------------------
    z.name = "=string"
    reader = function()
      if not buff then return nil end
      local data = buff
      buff = nil
      return data
    end
  end
--[[--------------------------------------------------------------------
-- fills an empty stream buffer, returns first character
----------------------------------------------------------------------]]
  function z:fill()
    local data = z.reader()
    z.data = data
    if not data or data == "" then return "EOZ" end
    z.n, z.p = string.len(data) - 1, 1
    return string.sub(data, 1, 1)
  end
--[[--------------------------------------------------------------------
-- get next character, fills buffer if characters needed
----------------------------------------------------------------------]]
  function z:getc()
    local n, p = z.n, z.p + 1
    if n > 0 then
      z.n, z.p = n - 1, p
      return string.sub(z.data, p, p)
    else
      return self:fill()
    end
  end
--[[--------------------------------------------------------------------
-- initialize input stream object
----------------------------------------------------------------------]]
  if not reader then return end
  z.reader = reader
  z.data = ""
  z.n, z.p = 0, 0
  return z
--[[------------------------------------------------------------------]]
end