comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:0cfcc391201f
1 -- functions to initialize and search through paths.
2 -- path strings specify a set of directory patterns separated by ';'.
3 -- the searchpath function
4
5 --- Initialize lua-style paths.
6 -- Looks up environment variable envname and substitute all ';;' by syspath
7 -- @param envname name of environment variable
8 -- @param syspath default value if envname is not set and substitution for all ';;'.
9 -- @return value from env variable or syspath. ';;' in env will be substituted by syspath.
10
11 function pathinit(envname, syspath)
12 local envvar = os.getenv(envname)
13 local path
14 if envvar then
15 path = envvar:gsub(";;",syspath)
16 else
17 path = syspath
18 end
19 return path
20 end
21
22
23 --- find object by searching through the path
24 -- @param openfun function(expanded)
25 -- @return found, expanded
26
27 function pathfind(path,name,openfun)
28 local replaced = path:gsub("?", name)
29 local found = nil
30 local fails = {}
31 for expanded in replaced:gmatch("([^;]+)") do
32 found = openfun(expanded)
33 if found then return found, expanded end
34 table.insert(fails, expanded)
35 end
36 return nil, fails
37 end
38