-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.lua
111 lines (84 loc) · 1.97 KB
/
utils.lua
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
107
108
109
110
111
local LFS = require ('lfs')
local Path = require ('map.path')
local Utils = {}
local function deep_copy (old, ignore_metatable)
local new
if type (old) == 'table' then
new = {}
for key, value in pairs (old) do
new [deep_copy (key)] = deep_copy (value)
end
if not ignore_metatable then
setmetatable (new, deep_copy (getmetatable (old)))
end
else
new = old
end
return new
end
Utils.deep_copy = deep_copy
local function process_entry (path, pattern, plain, list, exists)
if exists [path] then
return
elseif Path.is_directory (path) then
local entries = {}
for entry in LFS.dir (path) do
if entry ~= '.' and entry ~= '..' then
entries [#entries + 1] = entry
end
end
table.sort (entries)
for _, entry in ipairs (entries) do
process_entry (Path.join (path, entry), pattern, plain, list, exists)
end
elseif Path.is_file (path)
and (not pattern or path:find (pattern, 1, plain))
then
list [#list + 1] = path
exists [path] = true
end
end
function Utils.load_files (paths, pattern, plain)
local list = {}
local exists = {}
if type (paths) == 'string' then
paths = { paths }
end
for _, path in ipairs (paths) do
process_entry (path, pattern, plain, list, exists)
end
return list
end
do
local proxies = setmetatable ({}, { __mode = 'k' })
function Utils.read_only (input)
if type (input) ~= 'table' then
return input
end
local proxy = proxies [input]
if not proxy then
proxy = setmetatable ({}, {
__index = function (_, key)
return Utils.read_only (input [key])
end,
__newindex = function ()
error ('table is read-only', 2)
end,
__pairs = function ()
local key, value, real_key
return function ()
key, value = next (input, real_key)
real_key = key
key = Utils.read_only (key)
value = Utils.read_only (value)
return key, value
end
end,
__metatable = false
})
proxies [input] = proxy
end
return proxy
end
end
return Utils