aboutsummaryrefslogtreecommitdiff
path: root/src/luarocks/core/dir-origianl.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/luarocks/core/dir-origianl.lua')
-rw-r--r--src/luarocks/core/dir-origianl.lua98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/luarocks/core/dir-origianl.lua b/src/luarocks/core/dir-origianl.lua
new file mode 100644
index 00000000..5d6f2c9f
--- /dev/null
+++ b/src/luarocks/core/dir-origianl.lua
@@ -0,0 +1,98 @@
1
2local dir = {}
3
4local require = nil
5--------------------------------------------------------------------------------
6
7local dir_sep = package.config:sub(1, 1)
8
9local function unquote(c)
10 local first, last = c:sub(1,1), c:sub(-1)
11 if (first == '"' and last == '"') or
12 (first == "'" and last == "'") then
13 return c:sub(2,-2)
14 end
15 return c
16end
17
18--- Describe a path in a cross-platform way.
19-- Use this function to avoid platform-specific directory
20-- separators in other modules. Removes trailing slashes from
21-- each component given, to avoid repeated separators.
22-- Separators inside strings are kept, to handle URLs containing
23-- protocols.
24-- @param ... strings representing directories
25-- @return string: a string with a platform-specific representation
26-- of the path.
27function dir.path(...)
28 local t = {...}
29 while t[1] == "" do
30 table.remove(t, 1)
31 end
32 for i, c in ipairs(t) do
33 t[i] = unquote(c)
34 end
35 return dir.normalize(table.concat(t, "/"))
36end
37
38--- Split protocol and path from an URL or local pathname.
39-- URLs should be in the "protocol://path" format.
40-- For local pathnames, "file" is returned as the protocol.
41-- @param url string: an URL or a local pathname.
42-- @return string, string: the protocol, and the pathname without the protocol.
43function dir.split_url(url)
44 assert(type(url) == "string")
45
46 url = unquote(url)
47 local protocol, pathname = url:match("^([^:]*)://(.*)")
48 if not protocol then
49 protocol = "file"
50 pathname = url
51 end
52 return protocol, pathname
53end
54
55--- Normalize a url or local path.
56-- URLs should be in the "protocol://path" format.
57-- Removes trailing and double slashes, and '.' and '..' components.
58-- for 'file' URLs, the native system's slashes are used.
59-- @param url string: an URL or a local pathname.
60-- @return string: Normalized result.
61function dir.normalize(name)
62 local protocol, pathname = dir.split_url(name)
63 pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/")
64 local pieces = {}
65 local drive = ""
66 if pathname:match("^.:") then
67 drive, pathname = pathname:match("^(.:)(.*)$")
68 end
69 pathname = pathname .. "/"
70 for piece in pathname:gmatch("(.-)/") do
71 if piece == ".." then
72 local prev = pieces[#pieces]
73 if not prev or prev == ".." then
74 table.insert(pieces, "..")
75 elseif prev ~= "" then
76 table.remove(pieces)
77 end
78 elseif piece ~= "." then
79 table.insert(pieces, piece)
80 end
81 end
82 if #pieces == 0 then
83 pathname = drive .. "."
84 elseif #pieces == 1 and pieces[1] == "" then
85 pathname = drive .. "/"
86 else
87 pathname = drive .. table.concat(pieces, "/")
88 end
89 if protocol ~= "file" then
90 pathname = protocol .. "://" .. pathname
91 else
92 pathname = pathname:gsub("/", dir_sep)
93 end
94 return pathname
95end
96
97return dir
98