aboutsummaryrefslogtreecommitdiff
path: root/libbutl/default-options.txx
diff options
context:
space:
mode:
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;
+ }
}