Avoid copy-paste.

This commit is contained in:
vng 2013-08-27 01:26:23 +03:00 committed by Alex Zolotarev
parent c4e834261c
commit 185aea5b20
4 changed files with 66 additions and 103 deletions

View file

@ -598,19 +598,17 @@ namespace android
string Framework::GetOutdatedCountriesString()
{
vector<storage::Country> countries;
int count = Storage().GetOutdatedCountries(countries);
if (count == 0)
return "";
vector<storage::Country const *> 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;
}
}

View file

@ -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

View file

@ -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<Country> & 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<Country const *> & 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])));
}
}

View file

@ -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<Country> & list) const;
/// @param[out] res Populated with oudated countries.
void GetOutdatedCountries(vector<Country const *> & res) const;
int64_t GetCurrentDataVersion() const { return m_currentVersion; }