parent
0dd455a15f
commit
535c2d68e7
12 changed files with 239 additions and 53 deletions
@ -0,0 +1,85 @@ |
|||||||
|
#include "Path.h" |
||||||
|
#include "StringUtils.h" |
||||||
|
|
||||||
|
Path::Path() |
||||||
|
{ |
||||||
|
XOJ_INIT_TYPE(Path); |
||||||
|
} |
||||||
|
|
||||||
|
Path::Path(const Path& other) |
||||||
|
: path(other.path) |
||||||
|
{ |
||||||
|
XOJ_INIT_TYPE(Path); |
||||||
|
} |
||||||
|
|
||||||
|
Path::Path(string path) |
||||||
|
: path(path) |
||||||
|
{ |
||||||
|
XOJ_INIT_TYPE(Path); |
||||||
|
} |
||||||
|
|
||||||
|
Path::~Path() |
||||||
|
{ |
||||||
|
XOJ_CHECK_TYPE(Path); |
||||||
|
XOJ_RELEASE_TYPE(Path); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if empty |
||||||
|
*/ |
||||||
|
bool Path::isEmpty() |
||||||
|
{ |
||||||
|
XOJ_CHECK_TYPE(Path); |
||||||
|
|
||||||
|
return path.empty(); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this is a file which exists |
||||||
|
*/ |
||||||
|
bool Path::exists() |
||||||
|
{ |
||||||
|
XOJ_CHECK_TYPE(Path); |
||||||
|
|
||||||
|
return g_file_test(path.c_str(), G_FILE_TEST_EXISTS); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the path ends with this extension |
||||||
|
*/ |
||||||
|
bool Path::hasExtension(string ext) |
||||||
|
{ |
||||||
|
XOJ_CHECK_TYPE(Path); |
||||||
|
|
||||||
|
return StringUtils::endsWith(path, ext); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the Path as String |
||||||
|
*/ |
||||||
|
string Path::str() |
||||||
|
{ |
||||||
|
XOJ_CHECK_TYPE(Path); |
||||||
|
|
||||||
|
return path; |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an uri to a path, if the uri does not start with file:// an empty Path is returned
|
||||||
|
*/ |
||||||
|
Path Path::fromUri(string uri) |
||||||
|
{ |
||||||
|
if (!StringUtils::startsWith(uri, "file://")) |
||||||
|
{ |
||||||
|
return Path(); |
||||||
|
} |
||||||
|
|
||||||
|
gchar* filename = g_filename_from_uri (uri.c_str(), NULL, NULL); |
||||||
|
Path p(filename); |
||||||
|
g_free(filename); |
||||||
|
|
||||||
|
return p; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,55 @@ |
|||||||
|
/*
|
||||||
|
* Xournal++ |
||||||
|
* |
||||||
|
* Path for file / folder handling |
||||||
|
* |
||||||
|
* @author Xournal++ Team |
||||||
|
* https://github.com/xournalpp/xournalpp
|
||||||
|
* |
||||||
|
* @license GNU GPLv2 or later |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "XournalType.h" |
||||||
|
|
||||||
|
class Path |
||||||
|
{ |
||||||
|
public: |
||||||
|
Path(); |
||||||
|
Path(const Path& other); |
||||||
|
Path(string path); |
||||||
|
virtual ~Path(); |
||||||
|
|
||||||
|
public: |
||||||
|
/**
|
||||||
|
* @return true if empty |
||||||
|
*/ |
||||||
|
bool isEmpty(); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this file / folder exists |
||||||
|
*/ |
||||||
|
bool exists(); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the path ends with this extension |
||||||
|
*/ |
||||||
|
bool hasExtension(string ext); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the Path as String |
||||||
|
*/ |
||||||
|
string str(); |
||||||
|
|
||||||
|
public: |
||||||
|
/**
|
||||||
|
* Convert an uri to a path, if the uri does not start with file:// an empty Path is returned
|
||||||
|
*/ |
||||||
|
static Path fromUri(string uri); |
||||||
|
|
||||||
|
private: |
||||||
|
XOJ_TYPE_ATTRIB; |
||||||
|
|
||||||
|
string path; |
||||||
|
}; |
||||||
@ -0,0 +1,54 @@ |
|||||||
|
/*
|
||||||
|
* Xournal++ |
||||||
|
* |
||||||
|
* This file is part of the Xournal UnitTests |
||||||
|
* |
||||||
|
* @author Xournal++ Team |
||||||
|
* https://github.com/xournalpp/xournalpp
|
||||||
|
* |
||||||
|
* @license GNU GPLv2 or later |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <config-test.h> |
||||||
|
#include <Path.h> |
||||||
|
|
||||||
|
#include <cppunit/extensions/HelperMacros.h> |
||||||
|
#include <ctime> |
||||||
|
#include <stdlib.h> |
||||||
|
|
||||||
|
using namespace std; |
||||||
|
|
||||||
|
class PathTest : public CppUnit::TestFixture |
||||||
|
{ |
||||||
|
CPPUNIT_TEST_SUITE(PathTest); |
||||||
|
|
||||||
|
CPPUNIT_TEST(testUnsupportedUri); |
||||||
|
CPPUNIT_TEST(testPathFromUri); |
||||||
|
|
||||||
|
CPPUNIT_TEST_SUITE_END(); |
||||||
|
|
||||||
|
public: |
||||||
|
void setUp() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
void tearDown() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
void testUnsupportedUri() |
||||||
|
{ |
||||||
|
Path b = Path::fromUri("http://localhost/test.txt"); |
||||||
|
CPPUNIT_ASSERT_EQUAL(true, b.isEmpty()); |
||||||
|
} |
||||||
|
|
||||||
|
void testPathFromUri() |
||||||
|
{ |
||||||
|
Path b = Path::fromUri("file:///tmp/test.txt"); |
||||||
|
CPPUNIT_ASSERT_EQUAL(false, b.isEmpty()); |
||||||
|
CPPUNIT_ASSERT_EQUAL(string("/tmp/test.txt"), b.str()); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
// Registers the fixture into the 'registry'
|
||||||
|
CPPUNIT_TEST_SUITE_REGISTRATION(PathTest); |
||||||
Loading…
Reference in new issue