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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
-- filename : l-file.lua
-- comment : split off from luat-lib
-- author : Hans Hagen, PRAGMA-ADE, Hasselt NL
-- copyright: PRAGMA ADE / ConTeXt Development Team
-- license : see context related readme files
if not versions then versions = { } end versions['l-file'] = 1.001
if not file then file = { } end
function file.removesuffix(filename)
return filename:gsub("%.[%a%d]+$", "")
end
function file.addsuffix(filename, suffix)
if not filename:find("%.[%a%d]+$") then
return filename .. "." .. suffix
else
return filename
end
end
function file.replacesuffix(filename, suffix)
if not filename:find("%.[%a%d]+$") then
return filename .. "." .. suffix
else
return (filename:gsub("%.[%a%d]+$","."..suffix))
end
end
function file.dirname(name)
return name:match("^(.+)[/\\].-$") or ""
end
function file.basename(name)
return name:match("^.+[/\\](.-)$") or name
end
function file.nameonly(name)
return ((name:match("^.+[/\\](.-)$") or name):gsub("%..*$",""))
end
function file.extname(name)
return name:match("^.+%.([^/\\]-)$") or ""
end
file.suffix = file.extname
function file.stripsuffix(name)
return (name:gsub("%.[%a%d]+$",""))
end
--~ function file.join(...)
--~ local t = { ... }
--~ for i=1,#t do
--~ t[i] = (t[i]:gsub("\\","/")):gsub("/+$","")
--~ end
--~ return table.concat(t,"/")
--~ end
--~ print(file.join("x/","/y"))
--~ print(file.join("http://","/y"))
--~ print(file.join("http://a","/y"))
--~ print(file.join("http:///a","/y"))
--~ print(file.join("//nas-1","/y"))
function file.join(...)
local pth = table.concat({...},"/")
pth = pth:gsub("\\","/")
local a, b = pth:match("^(.*://)(.*)$")
if a and b then
return a .. b:gsub("//+","/")
end
a, b = pth:match("^(//)(.*)$")
if a and b then
return a .. b:gsub("//+","/")
end
return (pth:gsub("//+","/"))
end
function file.is_writable(name)
local f = io.open(name, 'w')
if f then
f:close()
return true
else
return false
end
end
function file.is_readable(name)
local f = io.open(name,'r')
if f then
f:close()
return true
else
return false
end
end
--~ function file.split_path(str)
--~ if str:find(';') then
--~ return str:splitchr(";")
--~ else
--~ return str:splitchr(io.pathseparator)
--~ end
--~ end
-- todo: lpeg
function file.split_path(str)
local t = { }
str = str:gsub("\\", "/")
str = str:gsub("(%a):([;/])", "%1\001%2")
for name in str:gmatch("([^;:]+)") do
if name ~= "" then
name = name:gsub("\001",":")
t[#t+1] = name
end
end
return t
end
function file.join_path(tab)
return table.concat(tab,io.pathseparator) -- can have trailing //
end
--~ print('test' .. " == " .. file.collapse_path("test"))
--~ print("test/test" .. " == " .. file.collapse_path("test/test"))
--~ print("test/test/test" .. " == " .. file.collapse_path("test/test/test"))
--~ print("test/test" .. " == " .. file.collapse_path("test/../test/test"))
--~ print("test" .. " == " .. file.collapse_path("test/../test"))
--~ print("../test" .. " == " .. file.collapse_path("../test"))
--~ print("../test/" .. " == " .. file.collapse_path("../test/"))
--~ print("a/a" .. " == " .. file.collapse_path("a/b/c/../../a"))
--~ function file.collapse_path(str)
--~ local ok, n = false, 0
--~ while not ok do
--~ ok = true
--~ str, n = str:gsub("[^%./]+/%.%./", function(s)
--~ ok = false
--~ return ""
--~ end)
--~ end
--~ return (str:gsub("/%./","/"))
--~ end
function file.collapse_path(str)
local n = 1
while n > 0 do
str, n = str:gsub("([^/%.]+/%.%./)","")
end
return (str:gsub("/%./","/"))
end
function file.robustname(str)
return (str:gsub("[^%a%d%/%-%.\\]+","-"))
end
file.readdata = io.loaddata
file.savedata = io.savedata
function file.copy(oldname,newname)
file.savedata(newname,io.loaddata(oldname))
end
|