annotate lua/luadyncall/src/path.lua @ 56:16151547265e

made callback example python 2 compatible, and added some randomness to numbers to sort
author Tassilo Philipp
date Tue, 02 Feb 2021 21:07:41 +0100
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 -- functions to initialize and search through paths.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 -- path strings specify a set of directory patterns separated by ';'.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 -- the searchpath function
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 --- Initialize lua-style paths.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 -- Looks up environment variable envname and substitute all ';;' by syspath
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 -- @param envname name of environment variable
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 -- @param syspath default value if envname is not set and substitution for all ';;'.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 -- @return value from env variable or syspath. ';;' in env will be substituted by syspath.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 function pathinit(envname, syspath)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 local envvar = os.getenv(envname)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 local path
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 if envvar then
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 path = envvar:gsub(";;",syspath)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 else
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 path = syspath
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 return path
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 --- find object by searching through the path
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 -- @param openfun function(expanded)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 -- @return found, expanded
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 function pathfind(path,name,openfun)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 local replaced = path:gsub("?", name)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 local found = nil
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 local fails = {}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 for expanded in replaced:gmatch("([^;]+)") do
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 found = openfun(expanded)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 if found then return found, expanded end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 table.insert(fails, expanded)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 return nil, fails
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38