diff --git a/src/dependency_tracker.cpp b/src/dependency_tracker.cpp index 233ffe6..94c90e5 100644 --- a/src/dependency_tracker.cpp +++ b/src/dependency_tracker.cpp @@ -14,49 +14,6 @@ namespace quickbook { - // Convert the path to its canonical representation if it exists. - // Or something close if it doesn't. - static fs::path normalize_path(fs::path const& path) - { - fs::path p = fs::absolute(path); // The base of the path. - fs::path extra; // The non-existant part of the path. - int parent_count = 0; // Number of active '..' sections - - // Invariant: path is equivalent to: p / ('..' * parent_count) / extra - // i.e. if parent_count == 0: p/extra - // if parent_count == 2: p/../../extra - - // Pop path sections from path until we find an existing - // path, adjusting for any dot path sections. - while (!fs::exists(fs::status(p))) { - fs::path name = p.filename(); - p = p.parent_path(); - if (name == "..") { - ++parent_count; - } - else if (name == ".") { - } - else if (parent_count) { - --parent_count; - } - else { - extra = name / extra; - } - } - - // If there are any left over ".." sections, then add them - // on to the end of the real path, and trust Boost.Filesystem - // to sort them out. - while (parent_count) { - p = p / ".."; - --parent_count; - } - - // Cannoicalize the existing part of the path, and add 'extra' back to - // the end. - return fs::canonical(p) / extra; - } - static char const* control_escapes[16] = { "\\000", "\\001", "\\002", "\\003", "\\004", "\\005", "\\006", "\\a", diff --git a/src/path.cpp b/src/path.cpp index 987d29b..34d8d97 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -21,6 +21,49 @@ namespace quickbook { + // Convert the path to its canonical representation if it exists. + // Or something close if it doesn't. + fs::path normalize_path(fs::path const& path) + { + fs::path p = fs::absolute(path); // The base of the path. + fs::path extra; // The non-existant part of the path. + int parent_count = 0; // Number of active '..' sections + + // Invariant: path is equivalent to: p / ('..' * parent_count) / extra + // i.e. if parent_count == 0: p/extra + // if parent_count == 2: p/../../extra + + // Pop path sections from path until we find an existing + // path, adjusting for any dot path sections. + while (!fs::exists(fs::status(p))) { + fs::path name = p.filename(); + p = p.parent_path(); + if (name == "..") { + ++parent_count; + } + else if (name == ".") { + } + else if (parent_count) { + --parent_count; + } + else { + extra = name / extra; + } + } + + // If there are any left over ".." sections, then add them + // on to the end of the real path, and trust Boost.Filesystem + // to sort them out. + while (parent_count) { + p = p / ".."; + --parent_count; + } + + // Cannoicalize the existing part of the path, and add 'extra' back to + // the end. + return fs::canonical(p) / extra; + } + // Not a general purpose normalization function, just // from paths from the root directory. It strips the excess // ".." parts from a path like: "x/../../y", leaving "y". diff --git a/src/path.hpp b/src/path.hpp index 2ba71ec..9634909 100644 --- a/src/path.hpp +++ b/src/path.hpp @@ -19,6 +19,8 @@ namespace quickbook { namespace fs = boost::filesystem; + fs::path normalize_path(fs::path const& path); + // The relative path from base to path fs::path path_difference(fs::path const& base, fs::path const& path);