From 6b7075adc71104c5f6ad652b99fb753565eb67d8 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 18 Nov 2016 17:28:46 +0200 Subject: Add function machinery, implement path.normalize() Note that multi-argument functions are not yet "callable" since there is no support for value packs. --- build2/functions-path.cxx | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 build2/functions-path.cxx (limited to 'build2/functions-path.cxx') diff --git a/build2/functions-path.cxx b/build2/functions-path.cxx new file mode 100644 index 0000000..b76b277 --- /dev/null +++ b/build2/functions-path.cxx @@ -0,0 +1,53 @@ +// file : build2/functions-path.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include +#include + +using namespace std; + +namespace build2 +{ + static value + path_thunk (vector_view args, const function_overload& f) + try + { + return function_family::default_thunk (move (args), f); + } + catch (const invalid_path& e) + { + error << "invalid path: '" << e.path << "'"; + throw failed (); + } + + void + path_functions () + { + function_family f ("path", &path_thunk); + + // normalize + // + f["normalize"] = [](path p) {p.normalize (); return p;}; + f["normalize"] = [](paths v) {for (auto& p: v) p.normalize (); return v;}; + + f["normalize"] = [](dir_path p) {p.normalize (); return p;}; + f["normalize"] = [](dir_paths v) {for (auto& p: v) p.normalize (); return v;}; + + f[".normalize"] = [](names ns) + { + // For each path decide based on the presence of a trailing slash + // whether it is a directory. Return as untyped list of (potentially + // mixed) paths. + // + for (name& n: ns) + { + if (n.directory ()) + n.dir.normalize (); + else + n.value = convert (move (n)).normalize ().string (); + } + return ns; + }; + } +} -- cgit v1.1