-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
92 lines (92 loc) · 2.35 KB
/
utils.lua
File metadata and controls
92 lines (92 loc) · 2.35 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
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
local _intstr
if _VERSION == "Lua 5.3" then
getchar = function (f) return string.unpack("<i1", f:read(1)) end
getuchar = function (f) return string.unpack("<I1", f:read(1)) end
getint = function (f) return string.unpack("<i4", f:read(4)) end
getuint = function (f) return string.unpack("<I4", f:read(4)) end
_intstr = function (x) return string.pack("<i4", x) end
else
getchar = function (f)
local z = string.byte(f:read(1))
return z >= 0x80 and z - 0x100 or z
end
getuchar = function (f)
return string.byte(f:read(1))
end
getint = function (f)
local z = string.byte(f:read(1))
z = z + string.byte(f:read(1)) * 0x100
z = z + string.byte(f:read(1)) * 0x10000
z = z + string.byte(f:read(1)) * 0x1000000
return z >= 0x80000000 and z - 0x100000000 or z
end
getuint = function (f)
local z = string.byte(f:read(1))
z = z + string.byte(f:read(1)) * 0x100
z = z + string.byte(f:read(1)) * 0x10000
z = z + string.byte(f:read(1)) * 0x1000000
return z
end
_intstr = function (x)
x = x % 0x100000000
return string.char(
x % 0x100,
math.floor(x / 0x100) % 0x100,
math.floor(x / 0x10000) % 0x100,
math.floor(x / 0x1000000) % 0x100
)
end
end
intstr = function (...)
local t = {}
for _, v in ipairs(table.pack(...)) do t[#t + 1] = _intstr(v) end
return table.concat(t)
end
getstr = function (f)
local str = ""
while true do
local c = f:read(1)
if c == "\0" then break end
str = str .. c
end
return str
end
getstr2 = function (f)
local str = ""
repeat
local line = f:read(256)
local c = string.gsub(line, "\0.*", "")
str = str .. c
f:seek("cur", #c - #line)
until #c < 256
return str
end
pad0 = function (s, l)
return string.sub(s, 1, l) .. string.rep("\x00", l - #s)
end
enum = function (names)
local t = {}
for i, k in ipairs(names) do
t[k] = i
t[i] = k
end
return t
end
size = function (t)
local z = 0
for _ in pairs(t) do z = z + 1 end
return z
end
split = function (str, delim)
str = str .. delim
local b, e = 0, 0
local pos = 1
local out = {}
repeat
b, e = string.find(str, delim, pos, true)
local s = string.sub(str, pos, b - 1)
table.insert(out, s)
pos = e + 1
until e == string.len(str)
return out
end