From d2d7828685ff9e9b8c2988c9efc69ed7b6af9923 Mon Sep 17 00:00:00 2001 From: vng Date: Mon, 8 Jul 2013 17:31:26 +0300 Subject: Add my::GetFileExtension. --- coding/coding_tests/file_utils_test.cpp | 5 +++++ coding/file_name_utils.cpp | 6 ++++++ coding/file_name_utils.hpp | 2 ++ map/framework.cpp | 31 +++++++++++++++---------------- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/coding/coding_tests/file_utils_test.cpp b/coding/coding_tests/file_utils_test.cpp index 73aa71c766..be81fa8f3f 100644 --- a/coding/coding_tests/file_utils_test.cpp +++ b/coding/coding_tests/file_utils_test.cpp @@ -6,6 +6,7 @@ UNIT_TEST(FileName_Smoke) { string name = "/Users/xxx/Documents/test.test"; + TEST_EQUAL(my::GetFileExtension(name), ".test", ()); my::GetNameFromFullPath(name); TEST_EQUAL(name, "test.test", ()); my::GetNameFromFullPath(name); @@ -14,20 +15,24 @@ UNIT_TEST(FileName_Smoke) TEST_EQUAL(name, "test", ()); name = "C:\\My Documents\\test.test"; + TEST_EQUAL(my::GetFileExtension(name), ".test", ()); my::GetNameFromFullPath(name); TEST_EQUAL(name, "test.test", ()); my::GetNameWithoutExt(name); TEST_EQUAL(name, "test", ()); name = "/"; + TEST_EQUAL(my::GetFileExtension(name), string(), ()); my::GetNameFromFullPath(name); TEST(name.empty(), ()); name = "C:\\"; + TEST_EQUAL(my::GetFileExtension(name), string(), ()); my::GetNameFromFullPath(name); TEST(name.empty(), ()); name = "../test"; + TEST_EQUAL(my::GetFileExtension(name), string(), ()); my::GetNameFromFullPath(name); TEST_EQUAL(name, "test", ()); my::GetNameWithoutExt(name); diff --git a/coding/file_name_utils.cpp b/coding/file_name_utils.cpp index 4df9a29328..f1ddf046b4 100644 --- a/coding/file_name_utils.cpp +++ b/coding/file_name_utils.cpp @@ -11,6 +11,12 @@ void GetNameWithoutExt(string & name) name.erase(i); } +string GetFileExtension(string const & name) +{ + size_t const pos = name.find_last_of("./\\"); + return ((pos != string::npos && name[pos] == '.') ? name.substr(pos) : string()); +} + void GetNameFromFullPath(string & name) { string::size_type const i = name.find_last_of("/\\"); diff --git a/coding/file_name_utils.hpp b/coding/file_name_utils.hpp index 6ce102f7d2..f45f9fcb91 100644 --- a/coding/file_name_utils.hpp +++ b/coding/file_name_utils.hpp @@ -7,6 +7,8 @@ namespace my { /// Remove extension from file name. void GetNameWithoutExt(string & name); + /// @return File extension with the dot or empty string if no extension found. + string GetFileExtension(string const & name); /// Get file name from full path. void GetNameFromFullPath(string & name); diff --git a/map/framework.cpp b/map/framework.cpp index a6af08795d..7b28d7039a 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -29,6 +29,7 @@ #include "../coding/internal/file_data.hpp" #include "../coding/zip_reader.hpp" #include "../coding/url_encode.hpp" +#include "../coding/file_name_utils.hpp" #include "../geometry/angles.hpp" #include "../geometry/distance_on_sphere.hpp" @@ -501,32 +502,29 @@ void Framework::ClearBookmarks() namespace { -// @return extension with a dot (or empty string if no extension is present) + +/// @return extension with a dot in lower case string const GetFileExt(string const & filePath) { - size_t const pos = filePath.rfind('.'); - if (pos == string::npos) - return string(); - string lowerCaseExtension = string(filePath, pos, filePath.size() - pos); - transform(lowerCaseExtension.begin(), lowerCaseExtension.end(), lowerCaseExtension.begin(), ::tolower); - return lowerCaseExtension; + string ext = my::GetFileExtension(filePath); + transform(ext.begin(), ext.end(), ext.begin(), ::tolower); + return ext; } string const GetFileName(string const & filePath) { - size_t pos = filePath.rfind('/'); - if (pos == string::npos) - return filePath; - ++pos; - return string(filePath, pos, filePath.size() - pos); + string ret = filePath; + my::GetNameFromFullPath(ret); + return ret; } -string const GenerateValidandUniqFilePathForKLM(string const & filename) +string const GenerateValidAndUniqueFilePathForKLM(string const & fileName) { - string filePath = BookmarkCategory::RemoveInvalidSymbols(filename); + string filePath = BookmarkCategory::RemoveInvalidSymbols(fileName); filePath = BookmarkCategory::GenerateUniqueFileName(GetPlatform().WritableDir(), filePath); return filePath; } + } bool Framework::AddBookmarksFile(string const & filePath) @@ -535,7 +533,7 @@ bool Framework::AddBookmarksFile(string const & filePath) string fileSavePath; if (fileExt == BOOKMARKS_FILE_EXTENSION) { - fileSavePath = GenerateValidandUniqFilePathForKLM( GetFileName(filePath) ); + fileSavePath = GenerateValidAndUniqueFilePathForKLM(GetFileName(filePath)); if (!my::CopyFileX(filePath, fileSavePath)) return false; } @@ -556,7 +554,8 @@ bool Framework::AddBookmarksFile(string const & filePath) } if (kmlFileName.empty()) return false; - fileSavePath = GenerateValidandUniqFilePathForKLM(kmlFileName); + + fileSavePath = GenerateValidAndUniqueFilePathForKLM(kmlFileName); ZipFileReader::UnzipFile(filePath, kmlFileName, fileSavePath); } catch (RootException const & e) -- cgit v1.2.3