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
{
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)
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");
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");
+}