-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20-05_file_as_array_extension.lua
More file actions
51 lines (45 loc) · 1.17 KB
/
20-05_file_as_array_extension.lua
File metadata and controls
51 lines (45 loc) · 1.17 KB
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
function fileAsArray(filename)
proxyFileTable = {}
mtProxyFile = {}
mtProxyFile.__index = function(_, key)
local f = assert(io.open(filename, 'r'))
f:seek("set", key-1)
local byteAtPos = f:read(1)
f:close()
return byteAtPos
end
mtProxyFile.__newindex = function(_, key, val)
local f = assert(io.open(filename, 'r+'))
f:seek("set", key-1)
f:write(val)
f:close()
end
mtProxyFile.__len = function(t)
local f = assert(io.open(filename, 'r'))
local size = f:seek("end")
f:close()
return size
end
mtProxyFile.__pairs = function()
return function(_, k)
if k == nil then
k = 0
end
local f = assert(io.open(filename, 'r'))
f:seek("set", k)
local byteAtPos = f:read(1)
f:close()
k = k + 1
if byteAtPos then
return k, byteAtPos
end
end
end
setmetatable(proxyFileTable, mtProxyFile)
return proxyFileTable
end
local t = fileAsArray("empty")
print(#t)
for _, v in pairs(t) do
print(v)
end