DPL Path - Extension method
authorTomasz Iwanek <t.iwanek@samsung.com>
Mon, 19 Aug 2013 08:59:00 +0000 (10:59 +0200)
committerSoo-Hyun Choi <sh9.choi@samsung.com>
Tue, 27 Aug 2013 14:12:06 +0000 (23:12 +0900)
[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
modules/utils/src/path.cpp
tests/utils/path_tests.cpp

index 3883d3d..67ffd43 100644 (file)
@@ -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;
index 213f59b..ea1653f 100644 (file)
@@ -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)
index 3f97fe3..0d29057 100644 (file)
@@ -862,6 +862,36 @@ 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");
+}