Make GetFilesInDir with GetFilesByExt and GetFilesByRegExp function in Platform.

This commit is contained in:
vng 2012-10-30 16:24:15 +03:00 committed by Alex Zolotarev
parent f05dcc2e48
commit 77564c5b7a
14 changed files with 91 additions and 56 deletions

View file

@ -293,18 +293,18 @@ extern "C"
Platform & pl = GetPlatform();
Platform::FilesList files;
// Move *.mwm files
pl.GetFilesInDir(from, "*" DATA_FILE_EXTENSION, files);
pl.GetFilesByExt(from, "*" DATA_FILE_EXTENSION, files);
for (size_t i = 0; i < files.size(); ++i)
{
LOG(LDEBUG, ("moving map from:", from + files[i], ", to:", to + files[i]));
my::RenameFileX((from + files[i]).c_str(), (to + files[i]).c_str());
}
// Delete not finished *.downloading files
// Delete not finished temporary files (old one from first release version).
files.clear();
pl.GetFilesInDir(from, "*" DOWNLOADING_FILE_EXTENSION, files);
pl.GetFilesInDir(from, "*" RESUME_FILE_EXTENSION, files);
pl.GetFilesByRegExp(from, "\\.(downloading$|resume$)", files);
for (size_t i = 0; i < files.size(); ++i)
my::DeleteFileX((from + files[i]).c_str());
}

View file

@ -27,11 +27,11 @@ extern "C"
// Get files to copy.
Platform & pl = GetPlatform();
char const * arrMask[] = { "*" DATA_FILE_EXTENSION, "*.ttf" };
// Get regexp like this: (\.mwm$|\.ttf$)
string const regexp = "(\\" DATA_FILE_EXTENSION "$|\\.ttf$)";
Platform::FilesList files;
for (size_t i = 0; i < ARRAY_SIZE(arrMask); ++i)
pl.GetFilesInDir(from, arrMask[i], files);
pl.GetFilesByRegExp(from, regexp, files);
// Copy all needed files.
for (size_t i = 0; i < files.size(); ++i)

View file

@ -82,7 +82,7 @@ namespace update
bool UpdateCountries(string const & dataDir)
{
Platform::FilesList mwmFiles;
GetPlatform().GetFilesInDir(dataDir, "*"DATA_FILE_EXTENSION, mwmFiles);
GetPlatform().GetFilesByExt(dataDir, "*" DATA_FILE_EXTENSION, mwmFiles);
// remove some files from list
char const * filesToRemove[] = {

View file

@ -152,7 +152,7 @@ CountryStatusDisplay * Framework::GetCountryStatusDisplay() const
static void GetResourcesMaps(vector<string> & outMaps)
{
Platform & pl = GetPlatform();
pl.GetFilesInDir(pl.ResourcesDir(), "*" DATA_FILE_EXTENSION, outMaps);
pl.GetFilesByExt(pl.ResourcesDir(), "*" DATA_FILE_EXTENSION, outMaps);
}
Framework::Framework()
@ -350,7 +350,7 @@ void Framework::LoadBookmarks()
string const dir = GetPlatform().WritableDir();
Platform::FilesList files;
Platform::GetFilesInDir(dir, "*.kml", files);
Platform::GetFilesByExt(dir, "*.kml", files);
for (size_t i = 0; i < files.size(); ++i)
{
BookmarkCategory * cat = BookmarkCategory::CreateFromKMLFile(dir + files[i]);
@ -528,7 +528,7 @@ void Framework::ClearBookmarks()
void Framework::GetLocalMaps(vector<string> & outMaps) const
{
Platform & pl = GetPlatform();
pl.GetFilesInDir(pl.WritableDir(), "*" DATA_FILE_EXTENSION, outMaps);
pl.GetFilesByExt(pl.WritableDir(), "*" DATA_FILE_EXTENSION, outMaps);
}
void Framework::PrepareToShutdown()
@ -1333,7 +1333,7 @@ void Framework::DeleteOldMaps()
{
Platform & p = GetPlatform();
vector<string> maps;
p.GetFilesInDir(p.WritableDir(), "*" DATA_FILE_EXTENSION, maps);
p.GetFilesByExt(p.WritableDir(), "*" DATA_FILE_EXTENSION, maps);
for (vector<string>::iterator it = maps.begin(); it != maps.end(); ++it)
{
feature::DataHeader header;

View file

@ -59,7 +59,7 @@ void Platform::GetFontNames(FilesList & res) const
for (size_t i = 0; i < ARRAY_SIZE(arr); ++i)
{
LOG(LDEBUG, ("Searching for fonts in", arr[i]));
GetFilesInDir(arr[i], "*.ttf", res);
GetFilesByExt(arr[i], "*.ttf", res);
}
sort(res.begin(), res.end());
@ -67,3 +67,26 @@ void Platform::GetFontNames(FilesList & res) const
LOG(LINFO, ("Available font files:", (res)));
}
void Platform::GetFilesByExt(string const & directory, string const & ext, FilesList & outFiles)
{
// Transform extension mask to regexp (*.mwm -> \.mwm$)
ASSERT ( !ext.empty(), () );
string regexp;
if (ext[0] == '*')
{
regexp = ext + '$';
regexp[0] = '\\';
}
else if (ext[0] == '.')
{
regexp = '\\' + ext + '$';
}
else
{
regexp = "\\." + ext + '$';
}
GetFilesByRegExp(directory, regexp, outFiles);
}

View file

@ -75,9 +75,12 @@ public:
typedef vector<string> FilesList;
/// Retrieves files list contained in given directory
/// @param directory directory path with slash at the end
/// @param mask files extension to find, like ".map" etc
/// @return number of files found in outFiles
static void GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles);
//@{
/// @param ext files extension to find, like ".mwm", "*.ttf" etc
static void GetFilesByExt(string const & directory, string const & ext, FilesList & outFiles);
static void GetFilesByRegExp(string const & directory, string const & regexp, FilesList & outFiles);
//@}
/// @return false if file is not exist
/// @note Check files in Writable dir first, and in ReadDir if not exist in Writable dir
bool GetFileSizeByName(string const & fileName, uint64_t & size) const;

View file

@ -1,6 +1,7 @@
#include "platform.hpp"
#include "platform_unix_impl.hpp"
#include "constants.hpp"
#include "regexp.hpp"
#include "../coding/zip_reader.hpp"
@ -33,7 +34,7 @@ ModelReader * Platform::GetReader(string const & file) const
}
}
void Platform::GetFilesInDir(string const & directory, string const & mask, FilesList & res)
void Platform::GetFilesByRegExp(string const & directory, string const & regexp, FilesList & res)
{
if (ZipFileReader::IsZip(directory))
{
@ -41,14 +42,15 @@ void Platform::GetFilesInDir(string const & directory, string const & mask, File
FilesList fList;
ZipFileReader::FilesList(directory, fList);
string const fixedMask = pl::GetFixedMask(mask);
regexp::RegExpT exp;
regexp::Create(regexp, exp);
for (FilesList::iterator it = fList.begin(); it != fList.end(); ++it)
{
if (it->find(fixedMask) != string::npos)
if (regexp::IsExist(*it, exp))
{
// Remove assets/ prefix - clean files are needed for fonts white/blacklisting logic
static size_t const ASSETS_LENGTH = 7;
size_t const ASSETS_LENGTH = 7;
if (it->find("assets/") == 0)
it->erase(0, ASSETS_LENGTH);
@ -57,7 +59,7 @@ void Platform::GetFilesInDir(string const & directory, string const & mask, File
}
}
else
pl::EnumerateFilesInDir(directory, mask, res);
pl::EnumerateFilesByRegExp(directory, regexp, res);
}
int Platform::CpuCores() const

View file

@ -84,9 +84,9 @@ Platform::~Platform()
delete m_impl;
}
void Platform::GetFilesInDir(string const & directory, string const & mask, FilesList & res)
void Platform::GetFilesByRegExp(string const & directory, string const & regexp, FilesList & res)
{
pl::EnumerateFilesInDir(directory, mask, res);
pl::EnumerateFilesByRegExp(directory, regexp, res);
}
bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const

View file

@ -1,5 +1,6 @@
#include "platform.hpp"
#include "constants.hpp"
#include "regexp.hpp"
#include "../coding/file_reader.hpp"
@ -29,13 +30,20 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const
}
}
void Platform::GetFilesInDir(string const & directory, string const & mask, FilesList & outFiles)
void Platform::GetFilesByRegExp(string const & directory, string const & regexp, FilesList & outFiles)
{
QDir dir(directory.c_str(), mask.c_str(), QDir::Unsorted,
QDir::Files | QDir::Readable | QDir::Dirs | QDir::NoDotAndDotDot);
regexp::RegExpT exp;
regexp::Create(regexp, exp);
QDir dir(QString::fromUtf8(directory.c_str()));
int const count = dir.count();
for (int i = 0; i < count; ++i)
outFiles.push_back(dir[i].toUtf8().data());
{
string const name = dir[i].toUtf8().data();
if (regexp::IsExist(name, exp))
outFiles.push_back(name);
}
}
string Platform::DeviceName() const

View file

@ -372,10 +372,13 @@ namespace
{
void DeleteTempDownloadFiles()
{
// remove data from previously failed files
// Remove data from previously failed files.
// Get regexp like this: (\.downloading3$|\.resume3$)
string const regexp = "(\\" RESUME_FILE_EXTENSION "$|\\" DOWNLOADING_FILE_EXTENSION "$)";
Platform::FilesList files;
Platform::GetFilesInDir(".", "*" RESUME_FILE_EXTENSION, files);
Platform::GetFilesInDir(".", "*" DOWNLOADING_FILE_EXTENSION, files);
Platform::GetFilesByRegExp(".", regexp, files);
for (Platform::FilesList::iterator it = files.begin(); it != files.end(); ++it)
FileWriter::DeleteFileX(*it);
}

View file

@ -60,18 +60,22 @@ UNIT_TEST(GetReader)
TEST_EQUAL(wasException, true, ());
}
UNIT_TEST(GetFilesInDir)
UNIT_TEST(GetFilesInDir_Smoke)
{
Platform & pl = GetPlatform();
Platform::FilesList files;
Platform::FilesList files1, files2;
pl.GetFilesInDir(pl.WritableDir(), "*" DATA_FILE_EXTENSION, files);
TEST_GREATER(files.size(), 0, ("/data/ folder should contain some data files"));
string const dir = pl.WritableDir();
files.clear();
pl.GetFilesByExt(dir, "*" DATA_FILE_EXTENSION, files1);
TEST_GREATER(files1.size(), 0, ("/data/ folder should contain some data files"));
pl.GetFilesInDir(pl.WritableDir(), "asdnonexistentfile.dsa", files);
TEST_EQUAL(files.size(), 0, ());
pl.GetFilesByRegExp(dir, ".*\\" DATA_FILE_EXTENSION "$", files2);
TEST_EQUAL(files1, files2, ());
files1.clear();
pl.GetFilesByExt(dir, "asdnonexistentfile.dsa", files1);
TEST_EQUAL(files1.size(), 0, ());
}
UNIT_TEST(GetFileSize)

View file

@ -1,5 +1,6 @@
#include "platform.hpp"
#include "platform_unix_impl.hpp"
#include "regexp.hpp"
#include "../base/logging.hpp"
@ -52,31 +53,22 @@ Platform::TStorageStatus Platform::GetWritableStorageStatus(uint64_t neededSize)
namespace pl
{
string GetFixedMask(string const & mask)
{
// Filter out according to the mask.
// @TODO we don't support wildcards at the moment
if (!mask.empty() && mask[0] == '*')
return string(mask.c_str() + 1);
else
return mask;
}
void EnumerateFilesInDir(string const & directory, string const & mask, vector<string> & res)
void EnumerateFilesByRegExp(string const & directory, string const & regexp,
vector<string> & res)
{
DIR * dir;
struct dirent * entry;
if ((dir = opendir(directory.c_str())) == NULL)
return;
string const fixedMask = GetFixedMask(mask);
regexp::RegExpT exp;
regexp::Create(regexp, exp);
while ((entry = readdir(dir)) != 0)
{
string const fname(entry->d_name);
size_t const index = fname.rfind(fixedMask);
if ((index != string::npos) && (index == fname.size() - fixedMask.size()))
res.push_back(fname);
string const name(entry->d_name);
if (regexp::IsExist(name, exp))
res.push_back(name);
}
closedir(dir);

View file

@ -5,6 +5,6 @@
namespace pl
{
string GetFixedMask(string const & mask);
void EnumerateFilesInDir(string const & directory, string const & mask, vector<string> & res);
void EnumerateFilesByRegExp(string const & directory, string const & regexp,
vector<string> & res);
}

View file

@ -148,7 +148,7 @@ UNIT_TEST(MergeLanguages)
};
Platform::FilesList fList;
GetPlatform().GetFilesInDir(paths[0], "*.meta", fList);
GetPlatform().GetFilesByExt(paths[0], "*.meta", fList);
for (size_t i = 0; i < fList.size(); ++i)
{