diff options
author | mitchell <70453897+667e-11@users.noreply.github.com> | 2014-04-06 19:42:42 -0400 |
---|---|---|
committer | mitchell <70453897+667e-11@users.noreply.github.com> | 2014-04-06 19:42:42 -0400 |
commit | a46502a160ca4a2a9094c9d9ae2b2d49b71a471b (patch) | |
tree | e9bb8efb5d86632cf297e77456bc738f5136e2a1 /core/lfs_ext.lua | |
parent | eff5df2952e26b5a1a25e95befe485068df10c81 (diff) |
Added `lfs.abspath()` in order to always use absolute paths.
Thanks to Pedro Andres Aranda Gutierrez.
Diffstat (limited to 'core/lfs_ext.lua')
-rw-r--r-- | core/lfs_ext.lua | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/core/lfs_ext.lua b/core/lfs_ext.lua index b4ebe588..7fe04b89 100644 --- a/core/lfs_ext.lua +++ b/core/lfs_ext.lua @@ -2,7 +2,8 @@ --[[ This comment is for LuaDoc. --- --- Extends the `lfs` library to find files in directories. +-- Extends the `lfs` library to find files in directories and determine absolute +-- file paths. module('lfs')]] --- @@ -92,3 +93,20 @@ function lfs.dir_foreach(dir, f, filter, exclude_FILTER, recursing) end end end + +--- +-- Returns the absolute path to string *filename*. +-- `lfs.currentdir()` is prepended to a relative filename. The returned path is +-- not guaranteed to exist. +-- @param filename The relative or absolute path to a file. +-- @return string absolute path +function lfs.abspath(filename) + if filename:find(not WIN32 and '^/' or '^%a:[/\\]') then return filename end + if WIN32 then filename = filename:gsub('/', '\\') end + filename = lfs.currentdir()..(not WIN32 and '/' or '\\')..filename + filename = filename:gsub('%f[^/\\]%.[/\\]', '') -- clean up './' + while filename:find('[^/\\]+[/\\]%.%.[/\\]') do + filename = filename:gsub('[^/\\]+[/\\]%.%.[/\\]', '') -- clean up '../' + end + return filename +end |