comparison share/lua/5.2/luarocks/dir.lua @ 1132:d137f631bad5

<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
author HackBot
date Fri, 14 Dec 2012 22:24:27 +0000
parents
children
comparison
equal deleted inserted replaced
1131:ff65dfb63263 1132:d137f631bad5
1
2 --- Generic utilities for handling pathnames.
3 module("luarocks.dir", package.seeall)
4
5 separator = "/"
6
7 --- Strip the path off a path+filename.
8 -- @param pathname string: A path+name, such as "/a/b/c"
9 -- or "\a\b\c".
10 -- @return string: The filename without its path, such as "c".
11 function base_name(pathname)
12 assert(type(pathname) == "string")
13
14 local base = pathname:gsub("[/\\]*$", ""):match(".*[/\\]([^/\\]*)")
15 return base or pathname
16 end
17
18 --- Strip the name off a path+filename.
19 -- @param pathname string: A path+name, such as "/a/b/c".
20 -- @return string: The filename without its path, such as "/a/b/".
21 -- For entries such as "/a/b/", "/a/" is returned. If there are
22 -- no directory separators in input, "" is returned.
23 function dir_name(pathname)
24 assert(type(pathname) == "string")
25
26 return (pathname:gsub("/*$", ""):match("(.*/)[^/]*")) or ""
27 end
28
29 --- Describe a path in a cross-platform way.
30 -- Use this function to avoid platform-specific directory
31 -- separators in other modules. Removes trailing slashes from
32 -- each component given, to avoid repeated separators.
33 -- Separators inside strings are kept, to handle URLs containing
34 -- protocols.
35 -- @param ... strings representing directories
36 -- @return string: a string with a platform-specific representation
37 -- of the path.
38 function path(...)
39 local items = {...}
40 local i = 1
41 while items[i] do
42 items[i] = items[i]:gsub("(.+)/+$", "%1")
43 if items[i] == "" then
44 table.remove(items, i)
45 else
46 i = i + 1
47 end
48 end
49 return (table.concat(items, "/"):gsub("(.+)/+$", "%1"))
50 end
51
52 --- Split protocol and path from an URL or local pathname.
53 -- URLs should be in the "protocol://path" format.
54 -- For local pathnames, "file" is returned as the protocol.
55 -- @param url string: an URL or a local pathname.
56 -- @return string, string: the protocol, and the pathname without the protocol.
57 function split_url(url)
58 assert(type(url) == "string")
59
60 local protocol, pathname = url:match("^([^:]*)://(.*)")
61 if not protocol then
62 protocol = "file"
63 pathname = url
64 end
65 return protocol, pathname
66 end
67
68 function normalize(name)
69 return name:gsub("\\", "/"):gsub("(.)/*$", "%1")
70 end