From: Mikko Rasa Date: Tue, 10 Jul 2012 15:25:39 +0000 (+0300) Subject: Add common_ancestor function X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=e4584bf621f6ef457a944c7604888b4132ad1706 Add common_ancestor function --- 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);