From 71f9ba2a26d3eedb7f087926809bc11138954bb1 Mon Sep 17 00:00:00 2001 From: Jihoon Chung Date: Tue, 9 Jul 2013 22:35:08 +0900 Subject: [PATCH] [Release] wrt-commons_0.2.135 Change-Id: I0766ce87b085f356e37dd491c096f6b1cd1c8b51 --- modules/utils/include/dpl/utils/path.h | 1 + modules/utils/src/path.cpp | 21 +++++++++++++++++ packaging/wrt-commons.spec | 2 +- tests/utils/path_tests.cpp | 43 ++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/modules/utils/include/dpl/utils/path.h b/modules/utils/include/dpl/utils/path.h index de8840b..ff7b544 100644 --- a/modules/utils/include/dpl/utils/path.h +++ b/modules/utils/include/dpl/utils/path.h @@ -130,6 +130,7 @@ public: * @throws If prefix does not match to this path object */ bool isSubPath(const Path & other) const; + bool hasExtension(const std::string& extension) const; bool operator==(const Path & other) const; bool operator!=(const Path & other) const; diff --git a/modules/utils/src/path.cpp b/modules/utils/src/path.cpp index 5f36011..51b54aa 100644 --- a/modules/utils/src/path.cpp +++ b/modules/utils/src/path.cpp @@ -314,6 +314,27 @@ bool Path::isSubPath(const Path & other) const return true; } +bool Path::hasExtension(const std::string& extension) const +{ + LogDebug("Looking for extension " << extension << " in: " << this->Filename()); + + 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; +} + void MakeDir(const Path & path, mode_t mode) { path.RootGuard(); diff --git a/packaging/wrt-commons.spec b/packaging/wrt-commons.spec index 42f9bbb..206ea76 100644 --- a/packaging/wrt-commons.spec +++ b/packaging/wrt-commons.spec @@ -1,7 +1,7 @@ #git:framework/web/wrt-commons Name: wrt-commons Summary: Wrt common library -Version: 0.2.134 +Version: 0.2.135 Release: 1 Group: Development/Libraries License: Apache License, Version 2.0 diff --git a/tests/utils/path_tests.cpp b/tests/utils/path_tests.cpp index 4e957e2..7ad96d2 100644 --- a/tests/utils/path_tests.cpp +++ b/tests/utils/path_tests.cpp @@ -834,3 +834,46 @@ RUNNER_TEST(path_iterator_copy_constructor) RUNNER_ASSERT_MSG(*iter2 == dirTest.end(), "Iterator is in broken state"); iter2.reset(); } + +/* +Name: path_extension_test +Description: Tests if file extension is correct +Expected: Proper recognition of extensions +*/ +RUNNER_TEST(path_extension_test) +{ + + Path dirTest = Path("extension"); + + Path path1 = dirTest / "file1.XML"; + Path path2 = dirTest / "file2.JPG"; + Path path3 = dirTest / "file3."; + Path path4 = dirTest / "file4"; + Path path5 = dirTest / "file5.HTML"; + Path path6 = dirTest / "file6.JS"; + Path path7 = dirTest / "file7.VERY_VERY_LONG_EXTENSION"; + Path path8 = dirTest / "file8.VERY.VERY.LONG.EXTENSION.WITH.DOTS"; + + RUNNER_ASSERT_MSG(path1.hasExtension("XML"), "Problem with comparison"); + RUNNER_ASSERT_MSG(path2.hasExtension("JPG"), "Problem with comparison"); + RUNNER_ASSERT_MSG(path5.hasExtension("HTML"), "Problem with comparison"); + RUNNER_ASSERT_MSG(path6.hasExtension("JS"), "Problem with comparison"); + RUNNER_ASSERT_MSG(path7.hasExtension("VERY_VERY_LONG_EXTENSION"), + "Problem with comparison"); + RUNNER_ASSERT_MSG(path8.hasExtension("DOTS"), "Problem with comparison"); + + RUNNER_ASSERT_MSG(!path1.hasExtension(".XML"), + "Wrong argument in hasExtension() function"); + RUNNER_ASSERT_MSG(!path1.hasExtension("MXL"), + "Wrong argument in hasExtension() function"); + RUNNER_ASSERT_MSG(!path2.hasExtension(".JPG"), + "Wrong argument in hasExtension() function"); + RUNNER_ASSERT_MSG(!path5.hasExtension(".HTML"), + "Wrong argument in hasExtension() function"); + 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 -- 2.7.4