Merge pull request #832 from andreasb242/master

fixes #816
presentation
andreasb242 7 years ago committed by GitHub
commit e02f7f9844
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      src/util/Path.cpp
  2. 2
      src/util/Path.h
  3. 20
      test/util/PathTest.cpp

@ -2,7 +2,7 @@
#include "StringUtils.h"
#include <glib/gstdio.h>
#include <string.h>
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");
}
/**

@ -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();

@ -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());
}
};

Loading…
Cancel
Save