diff options
Diffstat (limited to 'src/base/path.lua')
-rw-r--r-- | src/base/path.lua | 182 |
1 files changed, 20 insertions, 162 deletions
diff --git a/src/base/path.lua b/src/base/path.lua index 14d00f6..508e8be 100644 --- a/src/base/path.lua +++ b/src/base/path.lua @@ -6,43 +6,6 @@ -- --- Get the absolute file path from a relative path. The requested --- file path doesn't actually need to exist. --- - - function path.getabsolute(p) - -- normalize the target path - p = path.translate(p, "/") - if (p == "") then p = "." end - - -- if the directory is already absolute I don't need to do anything - local result = iif (path.isabsolute(p), nil, os.getcwd()) - - -- split up the supplied relative path and tackle it bit by bit - for n, part in ipairs(p:explode("/", true)) do - if (part == "" and n == 1) then - result = "/" - elseif (part == "..") then - result = path.getdirectory(result) - elseif (part ~= ".") then - -- Environment variables embedded in the path need to be treated - -- as relative paths; path.join() makes them absolute - if (part:startswith("$") and n > 1) then - result = result .. "/" .. part - else - result = path.join(result, part) - end - end - end - - -- if I end up with a trailing slash remove it - result = iif(result:endswith("/"), result:sub(1, -2), result) - - return result - end - - --- -- Retrieve the filename portion of a path, without any extension. -- @@ -55,10 +18,10 @@ return name end end - - + + -- --- Retrieve the directory portion of a path, or an empty string if +-- Retrieve the directory portion of a path, or an empty string if -- the path does not include a directory. -- @@ -99,9 +62,9 @@ return "" end end - - - + + + -- -- Retrieve the filename portion of a path. -- @@ -114,71 +77,7 @@ return p end end - - --- --- Returns the relative path from src to dest. --- - - function path.getrelative(src, dst) - -- normalize the two paths - src = path.getabsolute(src) - dst = path.getabsolute(dst) - - -- same directory? - if (src == dst) then - return "." - end - - -- dollar macro? Can't tell what the real path is; use absolute - -- This enables paths like $(SDK_ROOT)/include to work correctly. - if dst:startswith("$") then - return dst - end - - src = src .. "/" - dst = dst .. "/" - - -- find the common leading directories - local idx = 0 - while (true) do - local tst = src:find("/", idx + 1, true) - if tst then - if src:sub(1,tst) == dst:sub(1,tst) then - idx = tst - else - break - end - else - break - end - end - - -- if they have nothing in common return absolute path - local first = src:find("/", 0, true) - if idx <= first then - return dst:sub(1, -2) - end - - -- trim off the common directories from the front - src = src:sub(idx + 1) - dst = dst:sub(idx + 1) - - -- back up from dst to get to this common parent - local result = "" - idx = src:find("/") - while (idx) do - result = result .. "../" - idx = src:find("/", idx + 1) - end - - -- tack on the path down to the dst from here - result = result .. dst - -- remove the trailing slash - return result:sub(1, -2) - end - -- -- Returns true if the filename represents a C/C++ source code file. This check @@ -191,13 +90,13 @@ local ext = path.getextension(fname):lower() return table.contains(extensions, ext) end - + function path.iscppfile(fname) local extensions = { ".cc", ".cpp", ".cxx", ".c", ".s", ".m", ".mm" } local ext = path.getextension(fname):lower() return table.contains(extensions, ext) end - + function path.iscppheader(fname) local extensions = { ".h", ".hh", ".hpp", ".hxx" } local ext = path.getextension(fname):lower() @@ -217,40 +116,6 @@ return table.contains(extensions, ext) end - --- --- Join one or more pieces of a path together into a single path. --- --- @param ... --- One or more path strings. --- @return --- The joined path. --- - - function path.join(...) - local numargs = select("#", ...) - if numargs == 0 then - return ""; - end - - local allparts = {} - for i = numargs, 1, -1 do - local part = select(i, ...) - if part and #part > 0 and part ~= "." then - -- trim off trailing slashes - while part:endswith("/") do - part = part:sub(1, -2) - end - - table.insert(allparts, 1, part) - if path.isabsolute(part) then - break - end - end - end - - return table.concat(allparts, "/") - end -- @@ -263,31 +128,24 @@ p = path.getrelative(newbase, p) return p end - - + + -- -- Convert the separators in a path from one form to another. If `sep` -- is nil, then a platform-specific separator is used. -- + local builtin_translate = path.translate + function path.translate(p, sep) - if (type(p) == "table") then - local result = { } - for _, value in ipairs(p) do - table.insert(result, path.translate(value)) - end - return result - else - if (not sep) then - if (os.is("windows")) then - sep = "\\" - else - sep = "/" - end + if not sep then + if os.is("windows") then + sep = "\\" + else + sep = "/" end - local result = p:gsub("[/\\]", sep) - return result end + return builtin_translate(p, sep) end @@ -309,10 +167,10 @@ -- have competing star replacements to worry about pattern = pattern:gsub("%*%*", "\001") pattern = pattern:gsub("%*", "\002") - + -- Replace the placeholders with their Lua patterns pattern = pattern:gsub("\001", ".*") pattern = pattern:gsub("\002", "[^/]*") - + return pattern end |