aboutsummaryrefslogtreecommitdiff
path: root/libbutl/default-options.txx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2021-08-05 11:20:30 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-08-05 12:14:19 +0300
commit1cdb233cedd12069a4aa5d761e20b42e842ec58f (patch)
tree9f8a50e9e6876f5cd03eb662db949d7b7a67eeb2 /libbutl/default-options.txx
parent060bb0b8ca44affa817600f21caa33cf54d5ecad (diff)
Convert default_options_start() to template function which accepts iterators range
Diffstat (limited to 'libbutl/default-options.txx')
-rw-r--r--libbutl/default-options.txx39
1 files changed, 39 insertions, 0 deletions
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 <typename I, typename F>
+ optional<dir_path>
+ default_options_start (const optional<dir_path>& 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;
+ }
}