Debian Codegraph @

texmf-dist/doc/support/cluttex/src/texrunner/pathutil_windows.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
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)
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([[c:\path\to/directory\]]) == "directory")
48 assert(basename("/file") == "file")
49 assert(basename("file") == "file")
50 -- END TEST CODE
51
52 local function dirname(path)
53 local i = 0
54 while true do
55 local j = string_find(path, "[\\/]", i + 1)
56 if j == nil then
57 if i == 0 then
58 -- No directory portion
59 return "."
60 elseif i == 1 then
61 -- Root
62 return string_sub(path, 1, 1)
63 else
64 -- Directory portion without trailing slash
65 return string_sub(path, 1, i - 1)
66 end
67 end
68 i = j
69 end
70 end
71
72 -- TEST CODE
73 assert(dirname("/path/to/file") == "/path/to")
74 assert(dirname("/path/to/directory/") == "/path/to/directory")
75 assert(dirname([[c:/path\to/file]]) == [[c:/path\to]])
76 assert(dirname("/file") == "/")
77 assert(dirname("file") == ".")
78 -- END TEST CODE
79
80 local function parentdir(path)
81 local i = 0
82 while true do
83 local j = string_find(path, "[\\/]", i + 1)
84 if j == nil then
85 if i == 0 then
86 -- No directory portion
87 return "."
88 elseif i == 1 then
89 -- Root
90 return string_sub(path, 1, 1)
91 else
92 -- Directory portion without trailing slash
93 return string_sub(path, 1, i - 1)
94 end
95 elseif j == #path then
96 -- Directory portion without trailing slash
97 return string_sub(path, 1, i - 1)
98 end
99 i = j
100 end
101 end
102
103 -- TEST CODE
104 assert(parentdir("/path/to/file") == "/path/to")
105 assert(parentdir("/path/to/directory/") == "/path/to")
106 assert(parentdir("/file") == "/")
107 assert(parentdir("file") == ".")
108 -- END TEST CODE
109
110 local function trimext(path)
111 return (string_gsub(path, "%.[^\\/%.]*$", ""))
112 end
113
114 -- TEST CODE
115 assert(trimext("/path/to/file.ext") == "/path/to/file")
116 assert(trimext("/path/t.o/file") == "/path/t.o/file")
117 assert(trimext([[c:/path/t.o\file]]) == [[c:/path/t.o\file]])
118 assert(trimext("file.ext") == "file")
119 assert(trimext("file.e.xt") == "file.e")
120 assert(trimext("file.ext.") == "file.ext")
121 assert(trimext("file") == "file")
122 -- END TEST CODE
123
124 local function ext(path)
125 return string_match(path, "%.([^\\/%.]*)$") or ""
126 end
127
128 -- TEST CODE
129 assert(ext("/path/to/file.ext") == "ext")
130 assert(ext("/path/t.o/file") == "")
131 assert(ext([[c:/path/t.o\file]]) == "")
132 assert(ext("file.ext") == "ext")
133 assert(ext("file.e.xt") == "xt")
134 assert(ext("file.ext.") == "")
135 assert(ext("file") == "")
136 -- END TEST CODE
137
138 local function replaceext(path, newext)
139 local newpath, n = string_gsub(path, "%.([^\\/%.]*)$", function() return "." .. newext end)
140 if n == 0 then
141 return newpath .. "." .. newext
142 else
143 return newpath
144 end
145 end
146
147 -- TEST CODE
148 assert(replaceext("/path/to/file.ext", "tor") == "/path/to/file.tor")
149 assert(replaceext("/path/t.o/file", "tor") == "/path/t.o/file.tor")
150 assert(replaceext([[c:/path/t.o\file]], "tor") == [[c:/path/t.o\file.tor]])
151 assert(replaceext("file.ext", "tor") == "file.tor")
152 assert(replaceext("file.e.xt", "tor") == "file.e.tor")
153 assert(replaceext("file.ext.", "tor") == "file.ext.tor")
154 assert(replaceext("file", "tor") == "file.tor")
155 -- END TEST CODE
156
157 local function joinpath2(x, y)
158 local xd = x
159 local last = string_sub(x, -1)
160 if last ~= "/" and last ~= "\\" then
161 xd = x .. "\\"
162 end
163 if y == "." then
164 return xd
165 elseif y == ".." then
166 return dirname(x)
167 else
168 if string_match(y, "^%.[\\/]") then
169 return xd .. string_sub(y, 3)
170 else
171 return xd .. y
172 end
173 end
174 end
175
176 local function joinpath(...)
177 local n = select("#", ...)
178 if n == 2 then
179 return joinpath2(...)
180 elseif n == 0 then
181 return "."
182 elseif n == 1 then
183 return ...
184 else
185 return joinpath(joinpath2(...), select(3, ...))
186 end
187 end
188
189 -- TEST CODE
190 assert(joinpath("/path/", "to", "somewhere") == [[/path/to\somewhere]])
191 assert(joinpath("/path/", "to", "somewhere", "..") == [[/path/to]])
192 assert(joinpath("/path/", "to", "somewhere", "..", "elsewhere") == [[/path/to\elsewhere]])
193 assert(joinpath("/path/", "to", "./somewhere.txt") == "/path/to/somewhere.txt")
194 -- END TEST CODE
195
196 -- https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
197 local function isabspath(path)
198 local init = string_sub(path, 1, 1)
199 return init == "\\" or init == "/" or string_match(path, "^%a:[/\\]")
200 end
201
202 local function abspath(path, cwd)
203 if isabspath(path) then
204 -- absolute path
205 return path
206 else
207 -- TODO: relative path with a drive letter is not supported
208 cwd = cwd or filesys.currentdir()
209 return joinpath2(cwd, path)
210 end
211 end
212
213 return {
214 basename = basename,
215 dirname = dirname,
216 parentdir = parentdir,
217 trimext = trimext,
218 ext = ext,
219 replaceext = replaceext,
220 join = joinpath,
221 abspath = abspath,
222 }