From e4a1e53064f3a677f66d36e3cdc4494ab5dcb979 Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Sat, 2 Feb 2019 09:50:30 +0100 Subject: [PATCH 1/2] fixes #816 Remove only known extensions --- src/util/Path.cpp | 35 ++++++++++++++--------------------- src/util/Path.h | 2 +- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/util/Path.cpp b/src/util/Path.cpp index 090b8403..229ec4e3 100644 --- a/src/util/Path.cpp +++ b/src/util/Path.cpp @@ -2,7 +2,7 @@ #include "StringUtils.h" #include - +#include Path::Path() { @@ -124,32 +124,25 @@ bool Path::hasExtension(string ext) return pathExt == ext; } +#define REMOVE_EXTENSION(ext) \ + if (StringUtils::endsWith(plower, ext)) \ + { \ + path = path.substr(0, path.length() - strlen(ext)); \ + return;\ + } + /** - * Clear the extension (last .xyz or .pdf.xoj, .pdf.xopp) + * Clear the the last known extension (last .pdf, .pdf.xoj, .pdf.xopp etc.) */ void Path::clearExtensions() { string plower = StringUtils::toLowerCase(path); - if (StringUtils::endsWith(plower, ".pdf.xoj")) - { - path = path.substr(0, path.length() - 8); - return; - } - - if (StringUtils::endsWith(plower, ".pdf.xopp")) - { - path = path.substr(0, path.length() - 9); - return; - } - - size_t separator = path.find_last_of("/\\"); - size_t dotPos = path.find_last_of("."); - if (dotPos == string::npos || (dotPos < separator && separator != string::npos)) - { - return; - } - path = path.substr(0, dotPos); + REMOVE_EXTENSION(".pdf.xoj"); + REMOVE_EXTENSION(".pdf.xopp"); + REMOVE_EXTENSION(".xoj"); + REMOVE_EXTENSION(".xopp"); + REMOVE_EXTENSION(".pdf"); } /** diff --git a/src/util/Path.h b/src/util/Path.h index 94db6e6b..2b7aacc0 100644 --- a/src/util/Path.h +++ b/src/util/Path.h @@ -73,7 +73,7 @@ public: bool hasExtension(string ext); /** - * Clear the extension (last .xyz or .pdf.xoj, .pdf.xopp) + * Clear the the last known extension (last .pdf, .pdf.xoj, .pdf.xopp etc.) */ void clearExtensions(); From e1f674ff894793af556a741c844cf494857ff62f Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Sat, 2 Feb 2019 10:00:38 +0100 Subject: [PATCH 2/2] Fixed UnitTest --- test/util/PathTest.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/test/util/PathTest.cpp b/test/util/PathTest.cpp index 9db15a26..f4651ac7 100644 --- a/test/util/PathTest.cpp +++ b/test/util/PathTest.cpp @@ -91,15 +91,31 @@ public: { Path a = Path("C:\\test\\abc\\xyz.txt"); a.clearExtensions(); - CPPUNIT_ASSERT_EQUAL(string("C:\\test\\abc\\xyz"), a.str()); + CPPUNIT_ASSERT_EQUAL(string("C:\\test\\abc\\xyz.txt"), a.str()); Path b = Path("/test/asdf.TXT"); b.clearExtensions(); - CPPUNIT_ASSERT_EQUAL(string("/test/asdf"), b.str()); + CPPUNIT_ASSERT_EQUAL(string("/test/asdf.TXT"), b.str()); b = Path("/test/asdf.asdf/asdf"); b.clearExtensions(); CPPUNIT_ASSERT_EQUAL(string("/test/asdf.asdf/asdf"), b.str()); + + b = Path("/test/asdf.PDF"); + b.clearExtensions(); + CPPUNIT_ASSERT_EQUAL(string("/test/asdf"), b.str()); + + b = Path("/test/asdf.PDF.xoj"); + b.clearExtensions(); + CPPUNIT_ASSERT_EQUAL(string("/test/asdf"), b.str()); + + b = Path("/test/asdf.xoj"); + b.clearExtensions(); + CPPUNIT_ASSERT_EQUAL(string("/test/asdf"), b.str()); + + b = Path("/test/asdf.pdf"); + b.clearExtensions(); + CPPUNIT_ASSERT_EQUAL(string("/test/asdf"), b.str()); } };