From be57ba464fae756d5eb44cf4faf192536dec43ec Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 2 Dec 2021 10:16:00 +0200 Subject: Add $root_directory() function --- libbuild2/functions-path.cxx | 43 +++++++++++++++++++++++++++++++++++++++++- tests/function/path/testscript | 22 +++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/libbuild2/functions-path.cxx b/libbuild2/functions-path.cxx index 2953067..8362d2e 100644 --- a/libbuild2/functions-path.cxx +++ b/libbuild2/functions-path.cxx @@ -312,7 +312,11 @@ namespace build2 return ns; }; - // directory + // $directory() + // $directory() + // + // Return the directory part of the path or empty path if there is no + // directory. Directory of a root directory is an empty path. // f["directory"] += &path::directory; @@ -346,6 +350,43 @@ namespace build2 return ns; }; + // $root_directory() + // $root_directory() + // + // Return the root directory of the path or empty path if the directory is + // not absolute. + // + f["root_directory"] += &path::root_directory; + + f["root_directory"] += [](paths v) + { + dir_paths r; + for (const path& p: v) + r.push_back (p.root_directory ()); + return r; + }; + + f["root_directory"] += [](dir_paths v) + { + for (dir_path& p: v) + p = p.root_directory (); + return v; + }; + + f[".root_directory"] += [](names ns) + { + // For each path decide based on the presence of a trailing slash + // whether it is a directory. Return as list of directory names. + // + for (name& n: ns) + { + if (n.directory ()) + n.dir = n.dir.root_directory (); + else + n = convert (move (n)).root_directory (); + } + return ns; + }; // $leaf() // diff --git a/tests/function/path/testscript b/tests/function/path/testscript index b0c97c9..142c866 100644 --- a/tests/function/path/testscript +++ b/tests/function/path/testscript @@ -49,6 +49,28 @@ if! $posix $* <'print $path.directory(a/b c/d/)' >"a/ c/" : dir-names } +: root_directory +: +{ + : posix + : + if $posix + { + $* <'print $root_directory([path] /a/b)' >'/' : basics + $* <'print $root_directory([path] /)' >'/' : root + } + + : windows + : + if $windows + { + $* <'print $root_directory([path] "c:\\a\\b")' >'c:\' : basics + $* <'print $root_directory([path] "c:")' >'c:\' : root + } + + $* <'print $root_directory([path] a/b)' >'' : relative +} + : base : { -- cgit v1.1