Handling relative .. and . paths on embedded platforms
On some of our less capable embedded systems we are sometimes working with filesystems that while they support directories, do not support the notion of . and .. to refer to the local and parent directories in a path. This can be troublesome because with Storyboard historically we have encouraged people to access their content in a project path relative fashion (from Lua) using the SCRIPT_ROOT variable:
local filename = gre.SCRIPT_ROOT .. "../translations/english.csv"
This would open up a file (english.csv) located in a translations directory under the root of the project. On some embedded platforms, most notealby those platforms using a simplified DOS filesystem such as FreeRTOS and embOS this type of file resolution would result in an error because the path would ultimately be resolved as:
local filename = "msd:/projectdir/scripts/../translations/english.csv"
To avoid this failure on these platforms we can write a small cover for the Lua io.open() API call that intercepts the name and then performs a replacement over the . and .. entries and then invokes the standard open functionality:
-- Not all filesystems support the notion of /../ /./ so we trim it back
function DeDotPath(path)
local npath, opath;
opath = path
-- Strip out the /./ entries
while(true) do
npath = string.gsub(opath, "/\./","/")
if(npath == opath) then
break
end
opath = npath
end
-- Convert any /something/../ with /
while(true) do
npath = string.gsub(opath, "/[^/]+/\.\./", "/")
if(npath == opath) then
break
end
opath = npath
end
return opath
end
local originalOpen = io.open
io.open = function(name, mode)
local newName = DeDotPath(name)
return originalOpen(newName, mode)
end
Using this shim, you can now continue to use the gre.SCRIPT_ROOT variable to build your paths regardless of the filesystem technology you use.
Thomas
Comments
Please sign in to leave a comment.