diff options
Diffstat (limited to 'src/boost-utils.cc')
-rw-r--r-- | src/boost-utils.cc | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/boost-utils.cc b/src/boost-utils.cc new file mode 100644 index 0000000..f81917a --- /dev/null +++ b/src/boost-utils.cc @@ -0,0 +1,42 @@ +#include "boost-utils.h" +#include <stdio.h> +#include <iostream> + +fs::path relativePath(const fs::path &path, const fs::path &relative_to) +{ + // create absolute paths + fs::path p = fs::absolute(path); + fs::path r = fs::absolute(relative_to); + + // if root paths are different, return absolute path + if (p.root_path() != r.root_path()) + return p; + + // initialize relative path + fs::path result; + + // find out where the two paths diverge + fs::path::const_iterator itr_path = p.begin(); + fs::path::const_iterator itr_relative_to = r.begin(); + while (*itr_path == *itr_relative_to && itr_path != p.end() && itr_relative_to != r.end()) { + ++itr_path; + ++itr_relative_to; + } + + // add "../" for each remaining token in relative_to + if (itr_relative_to != r.end()) { + ++itr_relative_to; + while (itr_relative_to != r.end()) { + result /= ".."; + ++itr_relative_to; + } + } + + // add remaining path + while (itr_path != p.end()) { + result /= *itr_path; + ++itr_path; + } + + return result; +} |