ABSOLUTE Determine absolute path

c = absolute(p); Path "p" need not exist. Absolute path will be relative to pwd if path does not exist.

c = absolute(p, base); the "base" path is used instead of pwd.

Contents

Inputs

Outputs

does not normalize path non-existant path is made absolute relative to pwd

function c = absolute(p, base, expand_tilde)
arguments
  p (1,1) string
  base (1,1) string = ""
  expand_tilde (1,1) logical = true
end

if expand_tilde
  c = stdlib.expanduser(p);
else
  c = stdlib.posix(p);
end

if stdlib.is_absolute(c)
 return
end

if stdlib.len(base) == 0
  b = pwd();
elseif expand_tilde
  b = stdlib.expanduser(base);
else
  b = base;
end

if ~stdlib.is_absolute(b)
  b = strcat(pwd(), "/", b);
end

b = stdlib.posix(b);

if stdlib.len(c) == 0
  c = b;
else
  c = strcat(b, "/", c);
end

end


%!assert(absolute('', '', false), posix(pwd))
%!assert(absolute('a/b', '', false), posix(strcat(pwd(), '/a/b')))