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
|
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
module(..., package.seeall)
local map = require('acf.util').map
function is_absolute(path)
return string.sub(path, 1, 1) == '/'
end
function to_absolute(path, base)
if not is_absolute(path) then
path = base..(base ~= '/' and '/' or '')..path
end
local comps = split(path)
local i = 1
while i <= #comps do
if comps[i] == '..' then
if i == 1 then error('Invalid path: '..path) end
table.remove(comps, i - 1)
table.remove(comps, i - 1)
i = i - 1
else i = i + 1 end
end
return '/'..table.concat(comps, '/')
end
local function escape(comp)
local res = string.gsub(comp, '([\\/])', '\\%1')
return res
end
function join(parent, name) return mjoin(parent, escape(name)) end
function mjoin(p1, p2, ...)
if not p2 then return p1 end
if not is_absolute(p2) then p2 = '/'..p2 end
return mjoin((p1 == '/' and '' or p1)..p2, unpack(arg))
end
function split(path)
local res = {}
local comp = ''
local function merge(s) if s > '' then table.insert(res, s) end end
while true do
local prefix, sep, suffix = string.match(path, '([^\\/]*)([\\/])(.*)')
if not prefix then
merge(comp..path)
return res
end
comp = comp..prefix
if sep == '\\' then
comp = comp..string.sub(suffix, 1, 1)
path = string.sub(suffix, 2, -1)
else
merge(comp)
comp = ''
path = suffix
end
end
end
function parent(path)
local comps = split(path)
table.remove(comps)
return mjoin('/', unpack(map(escape, comps)))
end
function name(path)
local comps = split(path)
return comps[#comps]
end
|