Debian Codegraph @

texmf-dist/doc/support/cluttex/src/texrunner/pathutil_unix.lua

raw symbols SCIP
1 --[[
2 Copyright 2016 ARATA Mizuki
3
4 This file is part of ClutTeX.
5
6 ClutTeX is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 ClutTeX is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ClutTeX. If not, see <http://www.gnu.org/licenses/>.
18 ]]
19
20 -- pathutil module for *nix
21
22 local assert = assert
23 local select = select
24 local string = string
25 local string_find = string.find
26 local string_sub = string.sub
27 local string_match = string.match
28 local string_gsub = string.gsub
29 local filesys = require "lfs"
30
31 local function basename(path)
32 local i = 0
33 while true do
34 local j = string_find(path, "/", i + 1, true)
35 if j == nil then
36 return string_sub(path, i + 1)
37 elseif j == #path then
38 return string_sub(path, i + 1, -2)
39 end
40 i = j
41 end
42 end
43
44 -- TEST CODE
45 assert(basename("/path/to/file") == "file")
46 assert(basename("/path/to/directory/") == "directory")
47 assert(basename("/file") == "file")
48 assert(basename("file") == "file")
49 -- END TEST CODE
50
51 local function dirname(path)
52 local i = 0
53 while true do
54 local j = string_find(path, "/", i + 1, true)
55 if j == nil then
56 if i == 0 then
57 -- No directory portion
58 return "."
59 elseif i == 1 then
60 -- Root
61 return "/"
62 else
63 -- Directory portion without trailing slash
64 return string_sub(path, 1, i - 1)
65 end
66 end
67 i = j
68 end
69 end
70
71 -- TEST CODE
72 assert(dirname("/path/to/file") == "/path/to")
73 assert(dirname("/path/to/directory/") == "/path/to/directory")
74 assert(dirname("/file") == "/")
75 assert(dirname("file") == ".")
76 -- END TEST CODE
77
78 local function parentdir(path)
79 local i = 0
80 while true do
81 local j = string_find(path, "/", i + 1, true)
82 if j == nil then
83 if i == 0 then
84 -- No directory portion
85 return "."
86 elseif i == 1 then
87 -- Root
88 return "/"
89 else
90 -- Directory portion without trailing slash
91 return string_sub(path, 1, i - 1)
92 end
93 elseif j == #path then
94 -- Directory portion without trailing slash
95 return string_sub(path, 1, i - 1)
96 end
97 i = j
98 end
99 end
100
101 -- TEST CODE
102 assert(parentdir("/path/to/file") == "/path/to")
103 assert(parentdir("/path/to/directory/") == "/path/to")
104 assert(parentdir("/file") == "/")
105 assert(parentdir("file") == ".")
106 -- END TEST CODE
107
108 local function trimext(path)
109 return (string_gsub(path, "%.[^/%.]*$", ""))
110 end
111
112 -- TEST CODE
113 assert(trimext("/path/to/file.ext") == "/path/to/file")
114 assert(trimext("/path/t.o/file") == "/path/t.o/file")
115 assert(trimext("file.ext") == "file")
116 assert(trimext("file.e.xt") == "file.e")
117 assert(trimext("file.ext.") == "file.ext")
118 assert(trimext("file") == "file")
119 -- END TEST CODE
120
121 local function ext(path)
122 return string_match(path, "%.([^/%.]*)$") or ""
123 end
124
125 -- TEST CODE
126 assert(ext("/path/to/file.ext") == "ext")
127 assert(ext("/path/t.o/file") == "")
128 assert(ext("file.ext") == "ext")
129 assert(ext("file.e.xt") == "xt")
130 assert(ext("file.ext.") == "")
131 assert(ext("file") == "")
132 -- END TEST CODE
133
134 local function replaceext(path, newext)
135 local newpath, n = string_gsub(path, "%.([^/%.]*)$", function() return "." .. newext end)
136 if n == 0 then
137 return newpath .. "." .. newext
138 else
139 return newpath
140 end
141 end
142
143 -- TEST CODE
144 assert(replaceext("/path/to/file.ext", "tor") == "/path/to/file.tor")
145 assert(replaceext("/path/t.o/file", "tor") == "/path/t.o/file.tor")
146 assert(replaceext("file.ext", "tor") == "file.tor")
147 assert(replaceext("file.e.xt", "tor") == "file.e.tor")
148 assert(replaceext("file.ext.", "tor") == "file.ext.tor")
149 assert(replaceext("file", "tor") == "file.tor")
150 -- END TEST CODE
151
152 local function joinpath2(x, y)
153 local xd = x
154 if string_sub(x, -1) ~= "/" then
155 xd = x .. "/"
156 end
157 if y == "." then
158 return xd
159 elseif y == ".." then
160 return dirname(x)
161 else
162 if string_sub(y, 1, 2) == "./" then
163 return xd .. string_sub(y, 3)
164 else
165 return xd .. y
166 end
167 end
168 end
169
170 local function joinpath(...)
171 local n = select("#", ...)
172 if n == 2 then
173 return joinpath2(...)
174 elseif n == 0 then
175 return "."
176 elseif n == 1 then
177 return ...
178 else
179 return joinpath(joinpath2(...), select(3, ...))
180 end
181 end
182
183 -- TEST CODE
184 assert(joinpath("/path/", "to", "somewhere") == "/path/to/somewhere")
185 assert(joinpath("/path/", "to", "somewhere", "..") == "/path/to")
186 assert(joinpath("/path/", "to", "somewhere", "..", "elsewhere") == "/path/to/elsewhere")
187 assert(joinpath("/path/", "to", "./somewhere.txt") == "/path/to/somewhere.txt")
188 -- END TEST CODE
189
190 local function abspath(path, cwd)
191 if string_sub(path, 1, 1) == "/" then
192 -- absolute path
193 return path
194 else
195 cwd = cwd or filesys.currentdir()
196 return joinpath2(cwd, path)
197 end
198 end
199
200 -- TEST CODE
201 assert(abspath("world.txt", "/hello") == "/hello/world.txt")
202 assert(abspath("/world.txt", "/hello") == "/world.txt")
203 -- END TEST CODE
204
205 return {
206 basename = basename,
207 dirname = dirname,
208 parentdir = parentdir,
209 trimext = trimext,
210 ext = ext,
211 replaceext = replaceext,
212 join = joinpath,
213 abspath = abspath,
214 }