From 185aea5b20198f0f285664c81266580358668dfc Mon Sep 17 00:00:00 2001 From: vng Date: Tue, 27 Aug 2013 01:26:23 +0300 Subject: [PATCH] Avoid copy-paste. --- android/jni/com/mapswithme/maps/Framework.cpp | 16 ++- map/framework.cpp | 33 +---- storage/storage.cpp | 114 +++++++++--------- storage/storage.hpp | 6 +- 4 files changed, 66 insertions(+), 103 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index eb0b046c8a..2c5a7aee01 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -598,19 +598,17 @@ namespace android string Framework::GetOutdatedCountriesString() { - vector countries; - int count = Storage().GetOutdatedCountries(countries); - if (count == 0) - return ""; + vector countries; + Storage().GetOutdatedCountries(countries); - string concated = ""; - for (int i = 0; i < countries.size(); i++) + string res; + for (size_t i = 0; i < countries.size(); ++i) { - concated.append(countries[i].Name()); + res += countries[i]->Name(); if (i < countries.size() - 1) - concated.append(", "); + res += ", "; } - return concated; + return res; } } diff --git a/map/framework.cpp b/map/framework.cpp index 4b6c02f393..d5b98a1f3f 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -285,38 +285,7 @@ void Framework::DeleteCountry(TIndex const & index) TStatus Framework::GetCountryStatus(TIndex const & index) const { - using namespace storage; - - TStatus res = m_storage.CountryStatus(index); - - if (res == EUnknown) - { - Country const & c = m_storage.CountryByIndex(index); - LocalAndRemoteSizeT const size = c.Size(); - - if (size.first == 0) - return ENotDownloaded; - - if (size.second == 0) - return EUnknown; - - res = EOnDisk; - if (size.first != size.second) - { - /// @todo Do better version check, not just size comparison. - - // Additional check for .ready file. - // Use EOnDisk status if it's good, or EOnDiskOutOfDate otherwise. - Platform const & pl = GetPlatform(); - string const fName = pl.WritablePathForFile(c.GetFile().GetFileWithExt() + READY_FILE_EXTENSION); - - uint64_t sz = 0; - if (!pl.GetFileSizeByFullPath(fName, sz) || sz != size.second) - res = EOnDiskOutOfDate; - } - } - - return res; + return m_storage.CountryStatusEx(index); } string Framework::GetCountryName(storage::TIndex const & index) const diff --git a/storage/storage.cpp b/storage/storage.cpp index 66e48bd2a9..f828ce8f6c 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -10,6 +10,7 @@ #include "../coding/file_reader.hpp" #include "../coding/file_container.hpp" #include "../coding/url_encode.hpp" +#include "../coding/file_name_utils.hpp" #include "../base/logging.hpp" #include "../base/string_utils.hpp" @@ -146,6 +147,39 @@ namespace storage return EUnknown; } + TStatus Storage::CountryStatusEx(TIndex const & index) const + { + TStatus res = CountryStatus(index); + if (res == EUnknown) + { + Country const & c = CountryByIndex(index); + LocalAndRemoteSizeT const size = c.Size(); + + if (size.first == 0) + return ENotDownloaded; + + if (size.second == 0) + return EUnknown; + + res = EOnDisk; + if (size.first != size.second) + { + /// @todo Do better version check, not just size comparison. + + // Additional check for .ready file. + // Use EOnDisk status if it's good, or EOnDiskOutOfDate otherwise. + Platform const & pl = GetPlatform(); + string const fName = pl.WritablePathForFile(c.GetFile().GetFileWithExt() + READY_FILE_EXTENSION); + + uint64_t sz = 0; + if (!pl.GetFileSizeByFullPath(fName, sz) || sz != size.second) + res = EOnDiskOutOfDate; + } + } + + return res; + } + void Storage::DownloadCountry(TIndex const & index) { // check if we already downloading this country @@ -400,74 +434,36 @@ namespace storage return TIndex(); } - static bool IsNotUpdatable(string const & t) + namespace { - return (t == WORLD_COASTS_FILE_NAME) || (t == WORLD_FILE_NAME); - } - - static string RemoveExt(string const & s) - { - return s.substr(0, s.find_last_of('.')); - } - - class IsNotOutdatedFilter - { - Storage const & m_storage; - - public: - IsNotOutdatedFilter(Storage const & storage) - : m_storage(storage) - {} - - bool operator()(string const & fileName) + bool IsNotUpdatable(string const & t) { - const TIndex index = m_storage.FindIndexByFile(fileName); - TStatus res = m_storage.CountryStatus(index); - - if (res == EUnknown) - { - Country const & c = m_storage.CountryByIndex(index); - LocalAndRemoteSizeT const size = c.Size(); - - if (size.first == 0) - return ENotDownloaded; - - if (size.second == 0) - return EUnknown; - - res = EOnDisk; - if (size.first != size.second) - { - /// @todo Do better version check, not just size comparison. - - // Additional check for .ready file. - // Use EOnDisk status if it's good, or EOnDiskOutOfDate otherwise. - Platform const & pl = GetPlatform(); - string const fName = pl.WritablePathForFile(c.GetFile().GetFileWithExt() + READY_FILE_EXTENSION); - - uint64_t sz = 0; - if (!pl.GetFileSizeByFullPath(fName, sz) || sz != size.second) - res = EOnDiskOutOfDate; - } - } - return res != EOnDiskOutOfDate; + return (t == WORLD_COASTS_FILE_NAME) || (t == WORLD_FILE_NAME); } - }; - int Storage::GetOutdatedCountries(vector & list) const + class IsNotOutdatedFilter + { + Storage const & m_storage; + public: + IsNotOutdatedFilter(Storage const & storage) : m_storage(storage) {} + bool operator() (string const & file) const + { + return (m_storage.CountryStatusEx(m_storage.FindIndexByFile(file)) != EOnDiskOutOfDate); + } + }; + } + + void Storage::GetOutdatedCountries(vector & res) const { Platform & pl = GetPlatform(); Platform::FilesList fList; pl.GetFilesByExt(pl.WritableDir(), DATA_FILE_EXTENSION, fList); - Platform::FilesList fListNoExt(fList.size()); - transform(fList.begin(), fList.end(), fListNoExt.begin(), RemoveExt); - fListNoExt.erase(remove_if(fListNoExt.begin(), fListNoExt.end(), IsNotUpdatable), fListNoExt.end()); - fListNoExt.erase(remove_if(fListNoExt.begin(), fListNoExt.end(), IsNotOutdatedFilter(*this)), fListNoExt.end()); + for_each(fList.begin(), fList.end(), bind(&my::GetNameWithoutExt, _1)); + fList.erase(remove_if(fList.begin(), fList.end(), &IsNotUpdatable), fList.end()); + fList.erase(remove_if(fList.begin(), fList.end(), IsNotOutdatedFilter(*this)), fList.end()); - for (int i = 0; i < fListNoExt.size(); ++i) - list.push_back(CountryByIndex(FindIndexByFile(fListNoExt[i]))); - - return fListNoExt.size(); + for (size_t i = 0; i < fList.size(); ++i) + res.push_back(&CountryByIndex(FindIndexByFile(fList[i]))); } } diff --git a/storage/storage.hpp b/storage/storage.hpp index 123bc234fb..4627bed3d8 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -113,6 +113,7 @@ namespace storage string const & CountryFileName(TIndex const & index) const; LocalAndRemoteSizeT CountrySizeInBytes(TIndex const & index) const; TStatus CountryStatus(TIndex const & index) const; + TStatus CountryStatusEx(TIndex const & index) const; //m2::RectD CountryBounds(TIndex const & index) const; void DownloadCountry(TIndex const & index); @@ -123,9 +124,8 @@ namespace storage string GetFileDownloadUrl(string const & baseUrl, string const & fName) const; - /// @param list is populated with oudated countries - /// @return number of outdated countries in the list - int GetOutdatedCountries(vector & list) const; + /// @param[out] res Populated with oudated countries. + void GetOutdatedCountries(vector & res) const; int64_t GetCurrentDataVersion() const { return m_currentVersion; }