ExperimentalGear/scripts/api/platform/filesys_impl.lua

127 lines
3.2 KiB
Lua
Raw Normal View History

require "common.globals"
---@class FileSys
local FileSys = {
__name = "FileSys",
sep = "/"
}
---Return a normalized absolutized version of `path`
---@param path string
---@return string
function FileSys.abspath(path)
return FileSys.normpath(FileSys.join(FileSys.getcwd(), path))
end
---Return the base name of `path`
---@param path string
---@return string
function FileSys.basename(path)
return string.sub(path, string.rfind(path, FileSys.sep, 1, true) + 1)
end
---Return the directory name of `path`
---@param path string
---@return string
function FileSys.dirname(path)
return FileSys.split(path)[1]
end
---Return true if `path` refers to an existing path
---@param path string
---@return boolean, string?
function FileSys.exists(path)
local ok, err, code = os.rename(path, path)
if not ok then
game.Log("err: "..err..", code: "..code, game.LOGGER_DEBUG)
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
---Return a string representing the current working directory
---@return string
function FileSys.getcwd()
assert(false, FileSys.__name .. ".getcwd() : Not Implemented")
return ""
end
---Join one or more path components with the platform specific separator
---@param path string
---@param ... string
---@return string
function FileSys.join(path, ...)
local vargs = { select(1, ...) }
local joinedpath = path
for _, value in ipairs(vargs) do
joinedpath = joinedpath .. FileSys.sep .. value
end
return joinedpath
end
---Normalize `path`, collapse redundant separators and up references
---@param path string
---@return string
function FileSys.normpath(path)
local sep = FileSys.sep
--remove multiple slashes
path = path:gsub("("..sep..")"..sep.."+", "%1")
--remove './'
path = path:gsub("%."..sep, "")
--remove all up references
local count = 0
local upRefPattern = "%w+"..sep.."%.%."..sep
repeat
path, count = path:gsub(upRefPattern, "")
until count ~= 0
--remove last slash
path = path:gsub("(.-)"..sep.."$", "%1")
return path
end
---Return a relative filepath to `path`, optionally relative to `relativeTo`
---@param path string
---@param relativeTo? string
---@return string
function FileSys.relpath(path, relativeTo)
relativeTo = relativeTo or FileSys.getcwd()
path = path:sub(path:find(relativeTo, 1, true)[2] + 1)
return path
end
---Return a list of files and directory names in `path`
---@param path string
---@return string[]
function FileSys.scandir(path)
assert(false, FileSys.__name .. ".scandir() : Not Implemented")
return {}
end
---Split `path` to (head, tail), tail is the last component, head is everything else
---@param path string
---@return string, string
function FileSys.split(path)
local lastSep = path:rfind(FileSys.sep, 1, true)
return path:sub(1, lastSep), path:sub(lastSep + 1)
end
---Split `path` to (root, ext), such that `root + ext == path`, ext is either empty or starts with a '.'
---@param path string
---@return string, string
function FileSys.splitext(path)
local lastSep = path:rfind(".", 1, true)
return path:sub(1, lastSep - 1), path:sub(lastSep)
end
return FileSys