Mercurial > pub > dyncall > bindings
diff lua/luadyncall/src/path.lua @ 0:0cfcc391201f
initial from svn dyncall-1745
author | Daniel Adler |
---|---|
date | Thu, 19 Mar 2015 22:26:28 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lua/luadyncall/src/path.lua Thu Mar 19 22:26:28 2015 +0100 @@ -0,0 +1,38 @@ +-- functions to initialize and search through paths. +-- path strings specify a set of directory patterns separated by ';'. +-- the searchpath function + +--- Initialize lua-style paths. +-- Looks up environment variable envname and substitute all ';;' by syspath +-- @param envname name of environment variable +-- @param syspath default value if envname is not set and substitution for all ';;'. +-- @return value from env variable or syspath. ';;' in env will be substituted by syspath. + +function pathinit(envname, syspath) + local envvar = os.getenv(envname) + local path + if envvar then + path = envvar:gsub(";;",syspath) + else + path = syspath + end + return path +end + + +--- find object by searching through the path +-- @param openfun function(expanded) +-- @return found, expanded + +function pathfind(path,name,openfun) + local replaced = path:gsub("?", name) + local found = nil + local fails = {} + for expanded in replaced:gmatch("([^;]+)") do + found = openfun(expanded) + if found then return found, expanded end + table.insert(fails, expanded) + end + return nil, fails +end +