|
16 | 16 | other {mustBeTextScalar} |
17 | 17 | end |
18 | 18 |
|
| 19 | +if stdlib.has_python() |
| 20 | + rel = relative_to_python(base, other); |
| 21 | +elseif stdlib.dotnet_api() >= 5 |
| 22 | + rel = relative_to_dotnet(base, other); |
| 23 | +else |
| 24 | + error('no supported relative path method found, please install .NET or "buildtool mex"') |
| 25 | +end |
19 | 26 |
|
20 | | -if stdlib.dotnet_api() >= 5 |
| 27 | +end |
21 | 28 |
|
22 | | - if strempty(base) && strempty(other), rel = "."; return, end |
23 | 29 |
|
24 | | - if strempty(other), rel = base; return, end |
25 | | - if strempty(base), rel = other; return, end |
| 30 | +function rel = relative_to_dotnet(base, other) |
26 | 31 |
|
27 | | - bis = stdlib.is_absolute(base); |
28 | | - ois = stdlib.is_absolute(other); |
| 32 | +if strempty(base) && strempty(other) |
| 33 | + rel = "."; |
| 34 | + return |
| 35 | +end |
29 | 36 |
|
30 | | - if bis ~= ois, rel = ""; return, end |
| 37 | +if strempty(other) |
| 38 | + rel = base; |
| 39 | + return |
| 40 | +end |
| 41 | +if strempty(base) |
| 42 | + rel = other; |
| 43 | + return |
| 44 | +end |
31 | 45 |
|
32 | | - base = fullfile(base); |
33 | | - other = fullfile(other); |
| 46 | +bis = stdlib.is_absolute(base); |
| 47 | +ois = stdlib.is_absolute(other); |
34 | 48 |
|
35 | | - if bis && ~(startsWith(base, other) || startsWith(other, base)) |
36 | | - rel = ""; |
| 49 | +if bis ~= ois |
| 50 | + rel = ""; |
| 51 | + return |
| 52 | +end |
| 53 | + |
| 54 | +base = fullfile(base); |
| 55 | +other = fullfile(other); |
| 56 | + |
| 57 | +if bis && ~(startsWith(base, other) || startsWith(other, base)) |
| 58 | + rel = ""; |
| 59 | +else |
| 60 | + % https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath |
| 61 | + rel = string(System.IO.Path.GetRelativePath(base, other)); |
| 62 | +end |
| 63 | + |
| 64 | +end |
| 65 | + |
| 66 | + |
| 67 | +function rel = relative_to_python(base, other) |
| 68 | + |
| 69 | +try |
| 70 | + bp = py.pathlib.Path(other); |
| 71 | + if stdlib.python_version() >= "3.12" |
| 72 | + r = bp.relative_to(base, pyargs(walk_up=true)); |
37 | 73 | else |
38 | | - % https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath |
39 | | - rel = string(System.IO.Path.GetRelativePath(base, other)); |
| 74 | + r = bp.relative_to(base); |
40 | 75 | end |
41 | | -elseif stdlib.has_python() |
42 | | - try |
43 | | - bp = py.pathlib.Path(other); |
44 | | - if stdlib.python_version() >= "3.12" |
45 | | - r = bp.relative_to(base, pyargs(walk_up=true)); |
46 | | - else |
47 | | - r = bp.relative_to(base); |
48 | | - end |
49 | | - rel = string(py.str(r)); |
50 | | - catch e |
51 | | - if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError') |
52 | | - rel = ""; |
53 | | - end |
| 76 | + rel = string(py.str(r)); |
| 77 | +catch e |
| 78 | + if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError') |
| 79 | + rel = ""; |
54 | 80 | end |
55 | | -else |
56 | | - error('no supported relative path method found, please install .NET or "buildtool mex"') |
57 | 81 | end |
58 | 82 |
|
59 | 83 | end |
|
0 commit comments