7 require("config") |
7 require("config") |
8 |
8 |
9 local sep=config.dirsep |
9 local sep=config.dirsep |
10 |
10 |
11 function join(p, f) |
11 function join(p, f) |
12 return p..sep..f |
12 if p=="" then |
|
13 return f |
|
14 else |
|
15 return p..sep..f |
|
16 end |
13 end |
17 end |
14 |
18 |
15 function dirbasename(p) |
19 function dirbasename(p) |
16 local d, b = string.match(p, |
20 local d, b = string.match(p, |
17 "(.*)"..sep.."+([^"..sep.."]+)"..sep.."*$") |
21 "^(.*)"..sep.."+([^"..sep.."]+)"..sep.."*$") |
18 if not b then |
22 if not b then |
19 return string.match(p, "([^"..sep.."]*") |
23 return "", p |
20 else |
24 else |
21 return d, b |
25 return d, b |
22 end |
26 end |
23 end |
27 end |
24 |
28 |
25 function dirname(p) |
29 function dirname(p) |
26 return tuple.fst(dirbasename(p)) |
30 return tuple.fst(dirbasename(p)) |
27 end |
31 end |
28 |
32 |
|
33 function dirname_slash(p) |
|
34 local dn=dirname(p) |
|
35 if dn=="" then |
|
36 return dn |
|
37 else |
|
38 return dn..sep |
|
39 end |
|
40 end |
|
41 |
29 function basename(p) |
42 function basename(p) |
30 return tuple.snd(dirbasename(p)) |
43 return tuple.snd(dirbasename(p)) |
|
44 end |
|
45 |
|
46 function rmext(p) |
|
47 return string.gsub(p, "%.[^.]*$", "") |
31 end |
48 end |
32 |
49 |
33 -- would rather do this as an iterator, but can't |
50 -- would rather do this as an iterator, but can't |
34 -- coroutine.yield from gsub handler |
51 -- coroutine.yield from gsub handler |
35 function split(p) |
52 function split(p) |
38 local s, p2 = string.match(p, "^("..sep.."+)(.*)$") |
55 local s, p2 = string.match(p, "^("..sep.."+)(.*)$") |
39 if s then |
56 if s then |
40 table.insert(t, "/.") -- root |
57 table.insert(t, "/.") -- root |
41 p=p2 |
58 p=p2 |
42 end |
59 end |
43 string.gsub(p, "([^"..sep.."]+)"..sep.."+", |
60 string.gsub(p, "([^"..sep.."]+)", --..sep.."+", |
44 function(d) table.insert(t, d); end) |
61 function(d) table.insert(t, d); end) |
45 return t |
62 return t |
46 end |
63 end |
47 |
64 |
|
65 --[[ |
48 function parts(p) |
66 function parts(p) |
49 local s={i=1, t=split(p)} |
67 local s={i=1, t=split(p)} |
50 local function g(s) |
68 local function g(s) |
51 local v=s.t[s.i] |
69 local v=s.t[s.i] |
52 s.i=s.i+1 |
70 s.i=s.i+1 |
53 return v |
71 return v |
54 end |
72 end |
55 return g, s |
73 return g, s |
56 end |
74 end |
57 |
75 |
58 function makepath(p) |
76 function makepath(p, head) |
59 local head="" |
77 head=head or "" |
60 for d in parts(p) do |
78 for d in parts(p) do |
61 head=head..d |
79 head=head..d |
62 local a=lfs.attributes(head) |
80 local a=lfs.attributes(head) |
63 if not a then |
81 if not a then |
64 local success, e = lfs.mkdir(head) |
82 local success, e = lfs.mkdir(head) |
69 err.file(head, "not a directory") |
87 err.file(head, "not a directory") |
70 end |
88 end |
71 head=head..sep |
89 head=head..sep |
72 end |
90 end |
73 end |
91 end |
|
92 ]] |
74 |
93 |
75 --[[ |
94 function simplify(p) |
76 print(dirbasename("/foo/bar/baz")) |
95 local capts={} |
77 |
96 local start=string.match(p, "^(/)") |
78 for k in parts("//foo/bar/baz/quk") do |
97 string.gsub(p, "([^/]+)", |
79 print(k) |
98 function(e) |
|
99 if e==".." and #capts > 1 then |
|
100 capts[#capts]=nil |
|
101 elseif e~="." then |
|
102 table.insert(capts, e) |
|
103 end |
|
104 end) |
|
105 return (start or "")..table.concat(capts, "/") |
80 end |
106 end |
81 |
107 |
82 makepath("foo/bar/baz/quk") |
108 function to_root(p) |
83 --]] |
109 return string.rep("../",#(split(p))-1) |
|
110 end |