From e4584bf621f6ef457a944c7604888b4132ad1706 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 10 Jul 2012 18:25:39 +0300 Subject: [PATCH] Add common_ancestor function --- source/fs/utils.cpp | 10 ++++++++++ source/fs/utils.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/source/fs/utils.cpp b/source/fs/utils.cpp index a34dd4e..1da1859 100644 --- a/source/fs/utils.cpp +++ b/source/fs/utils.cpp @@ -154,6 +154,16 @@ Path relative(const Path &path, const Path &base) return result; } +Path common_ancestor(const Path &path1, const Path &path2) +{ + Path::Iterator i = path1.begin(); + Path::Iterator j = path2.begin(); + Path result; + for(; (i!=path1.end() && j!=path2.end() && *i==*j); ++i, ++j) + result /= *i; + return result; +} + int descendant_depth(const Path &path, const Path &parent) { Path::Iterator i = path.begin(); diff --git a/source/fs/utils.h b/source/fs/utils.h index df7bc76..a52e37d 100644 --- a/source/fs/utils.h +++ b/source/fs/utils.h @@ -38,6 +38,9 @@ void rename(const Path &from, const Path &to); /// Makes a path relative to some base path. That is, base/result==path. Path relative(const Path &path, const Path &base); +/// Returns the longest prefix shared by both paths. +Path common_ancestor(const Path &, const Path &); + /** Determines how many levels a path is below another. Returns -1 if path is not a descendant of parent. */ int descendant_depth(const Path &path, const Path &parent); -- 2.43.0