From 0875a4cbef5e6d664488d8af2c37b3b252856750 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 10 Apr 2011 13:23:08 +0000 Subject: [PATCH] Add descendant_depth function --- source/utils.cpp | 16 ++++++++++++++++ source/utils.h | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/source/utils.cpp b/source/utils.cpp index 8f06757..4036225 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -165,5 +165,21 @@ Path relative(const Path &path, const Path &base) return result; } +int descendant_depth(const Path &path, const Path &parent) +{ + Path::Iterator i=path.begin(); + Path::Iterator j=parent.begin(); + for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ; + + if(j!=parent.end()) + return -1; + + int result = 0; + for(; i!=path.end(); ++i) + ++result; + + return result; +} + } // namespace FS } // namespace Msp diff --git a/source/utils.h b/source/utils.h index c582c22..6a4c55a 100644 --- a/source/utils.h +++ b/source/utils.h @@ -52,6 +52,10 @@ 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); +/** 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); + } // namespace FS } // namespace Msp -- 2.43.0