From 282415983fdd1284be0faae691581e52e5da1eb9 Mon Sep 17 00:00:00 2001 From: Tomasz Iwanek Date: Mon, 19 Aug 2013 10:59:00 +0200 Subject: [PATCH] DPL Path - Extension method [Issue#] WGL-428 [Bug] No way to get file extension [Cause] Extension() function is needed for wrt installer [Solution] Add function and test case [Verification] Run tests: wrt-commons-tests-utils --output=text --regexp="path_" Change-Id: I8a2a889303f0278471b283044e5acfd634963622 --- modules/utils/include/dpl/utils/path.h | 5 +++ modules/utils/src/path.cpp | 42 ++++++++++++++++---------- tests/utils/path_tests.cpp | 37 ++++++++++++++++++++--- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/modules/utils/include/dpl/utils/path.h b/modules/utils/include/dpl/utils/path.h index 3883d3d..67ffd43 100644 --- a/modules/utils/include/dpl/utils/path.h +++ b/modules/utils/include/dpl/utils/path.h @@ -116,6 +116,11 @@ public: * @return full path */ std::string Fullpath() const; + /** + * @brief Extension + * @return extension + */ + std::string Extension() const; bool Exists() const; bool IsDir() const; diff --git a/modules/utils/src/path.cpp b/modules/utils/src/path.cpp index 213f59b..ea1653f 100644 --- a/modules/utils/src/path.cpp +++ b/modules/utils/src/path.cpp @@ -163,6 +163,23 @@ std::string Path::Fullpath() const return std::string ("/") + ret; } +std::string Path::Extension() const +{ + if(m_parts.empty()) return ""; + + const std::string& last = *--m_parts.end(); + + std::string::size_type pos = last.find_last_of("."); + if(pos != std::string::npos) + { + return last.substr(pos + 1); + } + else + { + return ""; + } +} + //foreach Path::Iterator Path::begin() const { @@ -340,23 +357,16 @@ bool Path::isSubPath(const Path & other) const bool Path::hasExtension(const std::string& extension) const { - LogDebug("Looking for extension " << extension << " in: " << this->Filename()); + LogPedantic("Looking for extension " << extension); - size_t extLen = extension.length(); - - if(m_parts.empty()) return false; - if(extLen == 0) return false; - - const std::string& last = *--m_parts.end(); - size_t lastLen = last.length(); - - if(lastLen < (1 + extLen)) return false; - - const char last_tmp = last[ lastLen - (1 + extLen) ]; - if(last_tmp != '.') return false; - - if(last.substr(lastLen - extLen) != extension) return false; - return true; + if(Extension() == extension) + { + return true; + } + else + { + return false; + } } void MakeDir(const Path & path, mode_t mode) diff --git a/tests/utils/path_tests.cpp b/tests/utils/path_tests.cpp index 3f97fe3..0d29057 100644 --- a/tests/utils/path_tests.cpp +++ b/tests/utils/path_tests.cpp @@ -861,6 +861,36 @@ Description: Tests if file extension is correct Expected: Proper recognition of extensions */ RUNNER_TEST(path_extension_test) +{ + Path path1("/path/to/file.dot"); + Path path2("/path/to/file..dot"); + Path path3("/path/to/file..dot."); + Path path4("/path/to/file..dot.dot"); + Path path5("/path/to.dot/file"); + Path path6("./path/to/file"); + Path path7("./path/to/file"); + Path path8("/path/../file.xml"); + Path path9("/path/../file.XML"); + Path path10("/path/../file.myfileextension"); + + RUNNER_ASSERT(path1.Extension() == "dot"); + RUNNER_ASSERT(path2.Extension() == "dot"); + RUNNER_ASSERT(path3.Extension() == ""); + RUNNER_ASSERT(path4.Extension() == "dot"); + RUNNER_ASSERT(path5.Extension() == ""); + RUNNER_ASSERT(path6.Extension() == ""); + RUNNER_ASSERT(path7.Extension() == ""); + RUNNER_ASSERT(path8.Extension() == "xml"); + RUNNER_ASSERT(path9.Extension() != "xml"); + RUNNER_ASSERT(path10.Extension() == "myfileextension"); +} + +/* +Name: path_has_extension_test +Description: Tests if file extension is correct +Expected: Proper recognition of extensions +*/ +RUNNER_TEST(path_has_extension_test) { Path dirTest = Path("extension"); @@ -893,7 +923,6 @@ RUNNER_TEST(path_extension_test) RUNNER_ASSERT_MSG(!path6.hasExtension(".JS"), "Wrong argument in hasExtension() function"); - RUNNER_ASSERT_MSG(!path3.hasExtension(""), "Extension length is 0"); - - RUNNER_ASSERT_MSG(!path4.hasExtension(""), "Not a directory"); -} \ No newline at end of file + RUNNER_ASSERT_MSG(path3.hasExtension(""), "Extension length should be 0"); + RUNNER_ASSERT_MSG(path4.hasExtension(""), "There should be no extension"); +} -- 2.34.1