fw4spl
pathDifference.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include <algorithm>
8 
9 #include "fwTools/pathDifference.hpp"
10 
11 
12 namespace fwTools
13 {
14 boost::filesystem::path getPathDifference( boost::filesystem::path path1, boost::filesystem::path path2 )
15 {
16  using boost::filesystem::path;
17  path pathDiff;
18 
19  // search iterator on path for the first element difference
20  std::pair<path::iterator, path::iterator> fstChange;
21  fstChange = std::mismatch(path1.begin(), path1.end(), path2.begin());
22 
23  path::iterator i1 = fstChange.first;
24  path::iterator i2 = fstChange.second;
25 
26  // move updir from path1 position : climb to first difference in folder hierarchy
27  while (i1 != path1.end() )
28  {
29  pathDiff /= "..";
30  ++i1;
31  }
32 
33  // mode down_dir to path2
34  while (i2 != path2.end() )
35  {
36  pathDiff /= *i2;
37  ++i2;
38  }
39 
40  return pathDiff;
41 }
42 
43 }
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
FWTOOLS_API boost::filesystem::path getPathDifference(boost::filesystem::path path1, boost::filesystem::path path2)
Return the relative difference within two paths.