From 1cdb233cedd12069a4aa5d761e20b42e842ec58f Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 5 Aug 2021 11:20:30 +0300 Subject: Convert default_options_start() to template function which accepts iterators range --- libbutl/default-options.txx | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'libbutl/default-options.txx') diff --git a/libbutl/default-options.txx b/libbutl/default-options.txx index eaf4235..dc809ad 100644 --- a/libbutl/default-options.txx +++ b/libbutl/default-options.txx @@ -318,4 +318,43 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. r.insert (r.end (), cmd_args.begin (), cmd_args.end ()); return r; } + + template + optional + default_options_start (const optional& home, I b, I e, F&& f) + { + if (home) + assert (home->absolute () && home->normalized ()); + + if (b == e) + return nullopt; + + // Use the first directory as a start. + // + I i (b); + dir_path d (f (i)); + + // Try to find a common prefix for each subsequent directory. + // + for (++i; i != e; ++i) + { + bool p (false); + + for (; + !(d.root () || (home && d == *home)); + d = d.directory ()) + { + if (f (i).sub (d)) + { + p = true; + break; + } + } + + if (!p) + return nullopt; + } + + return d; + } } -- cgit v1.1