commit ba71070ba9c8626acecc2827bbcad05d3db1aa3b
parent 9e29b10eab745fe842728cf1630c460db667655e
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 10 Sep 2017 20:24:52 -0400
path: refactor absolute to relative path conversion
Diffstat:
4 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/src/api_package.cpp b/src/api_package.cpp
@@ -145,16 +145,9 @@ DEFINE_API(PackageEntry*, GetOwner, ((const char*, fn))((char*, errorOut))((int,
R"(Returns the package entry owning the given file.
Delete the returned object from memory after use with <a href="#ReaPack_FreeEntry">ReaPack_FreeEntry</a>.)",
{
- Path path(fn);
-
- const Path &rp = ReaPack::resourcePath();
-
- if(path.startsWith(rp))
- path.remove(0, rp.size());
-
try {
const Registry reg(Path::REGISTRY.prependRoot());
- const auto &owner = reg.getOwner(path);
+ const auto &owner = reg.getOwner(Path(fn).removeRoot());
if(owner) {
auto entry = new PackageEntry{owner, reg.getFiles(owner)};
diff --git a/src/path.cpp b/src/path.cpp
@@ -199,6 +199,16 @@ Path Path::prependRoot() const
return m_absolute ? *this : s_root + *this;
}
+Path Path::removeRoot() const
+{
+ Path copy(*this);
+
+ if(startsWith(s_root))
+ copy.remove(0, s_root.size());
+
+ return copy;
+}
+
bool Path::operator==(const Path &o) const
{
return m_absolute == o.absolute() && m_parts == o.m_parts;
diff --git a/src/path.hpp b/src/path.hpp
@@ -50,7 +50,9 @@ public:
std::string first() const;
std::string last() const;
bool startsWith(const Path &) const;
+
Path prependRoot() const;
+ Path removeRoot() const;
std::list<std::string>::const_iterator begin() const { return m_parts.begin(); }
std::list<std::string>::const_iterator end() const { return m_parts.end(); }
diff --git a/test/path.cpp b/test/path.cpp
@@ -188,7 +188,7 @@ TEST_CASE("remove last component of path", M) {
a.removeLast(); // no crash
}
-TEST_CASE("path generation utilities", M) {
+TEST_CASE("prepend root path", M) {
const Path path("world");
REQUIRE(path.prependRoot() == Path("world"));
@@ -198,12 +198,26 @@ TEST_CASE("path generation utilities", M) {
(void)root;
REQUIRE(path.prependRoot() == Path("hello/world"));
- REQUIRE(Path("world").prependRoot() == Path("hello/world"));
}
REQUIRE(path.prependRoot() == Path("world"));
}
+TEST_CASE("remove root path", M) {
+ const Path path("hello/world");
+
+ REQUIRE(path.removeRoot() == Path("hello/world"));
+
+ {
+ UseRootPath root(Path("hello"));
+ (void)root;
+
+ REQUIRE(path.removeRoot() == Path("world"));
+ }
+
+ REQUIRE(path.removeRoot() == Path("hello/world"));
+}
+
TEST_CASE("first and last path component", M) {
REQUIRE(Path().first().empty());
REQUIRE(Path().last().empty());