From 77564c5b7a3c2448a0fb7d46d1104a756eaeaf27 Mon Sep 17 00:00:00 2001 From: vng Date: Tue, 30 Oct 2012 16:24:15 +0300 Subject: [PATCH] Make GetFilesInDir with GetFilesByExt and GetFilesByRegExp function in Platform. --- .../maps/DownloadResourcesActivity.cpp | 8 +++--- .../com/mapswithme/maps/SettingsActivity.cpp | 6 ++--- generator/update_generator.cpp | 2 +- map/framework.cpp | 8 +++--- platform/platform.cpp | 25 ++++++++++++++++++- platform/platform.hpp | 9 ++++--- platform/platform_android.cpp | 12 +++++---- platform/platform_ios.mm | 4 +-- platform/platform_qt.cpp | 16 +++++++++--- platform/platform_tests/downloader_test.cpp | 9 ++++--- platform/platform_tests/platform_test.cpp | 18 +++++++------ platform/platform_unix_impl.cpp | 24 ++++++------------ platform/platform_unix_impl.hpp | 4 +-- storage/storage_tests/generate_langs.cpp | 2 +- 14 files changed, 91 insertions(+), 56 deletions(-) diff --git a/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp b/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp index 5084f78d3b..6316ed03fe 100644 --- a/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp +++ b/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp @@ -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()); } diff --git a/android/jni/com/mapswithme/maps/SettingsActivity.cpp b/android/jni/com/mapswithme/maps/SettingsActivity.cpp index 5a551f02fa..f7215ad2a1 100644 --- a/android/jni/com/mapswithme/maps/SettingsActivity.cpp +++ b/android/jni/com/mapswithme/maps/SettingsActivity.cpp @@ -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) diff --git a/generator/update_generator.cpp b/generator/update_generator.cpp index 87e3da5fb9..032957e27a 100644 --- a/generator/update_generator.cpp +++ b/generator/update_generator.cpp @@ -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[] = { diff --git a/map/framework.cpp b/map/framework.cpp index d83c0009d5..7da9a7a73f 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -152,7 +152,7 @@ CountryStatusDisplay * Framework::GetCountryStatusDisplay() const static void GetResourcesMaps(vector & 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 & 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 maps; - p.GetFilesInDir(p.WritableDir(), "*" DATA_FILE_EXTENSION, maps); + p.GetFilesByExt(p.WritableDir(), "*" DATA_FILE_EXTENSION, maps); for (vector::iterator it = maps.begin(); it != maps.end(); ++it) { feature::DataHeader header; diff --git a/platform/platform.cpp b/platform/platform.cpp index b611a0851a..e048ea1f69 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -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); +} diff --git a/platform/platform.hpp b/platform/platform.hpp index 2cd0eeac99..f5a9b9649d 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -75,9 +75,12 @@ public: typedef vector 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; diff --git a/platform/platform_android.cpp b/platform/platform_android.cpp index 16e5ad882d..2b819c8050 100644 --- a/platform/platform_android.cpp +++ b/platform/platform_android.cpp @@ -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 diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm index 1fe47165f8..c9ed79f358 100644 --- a/platform/platform_ios.mm +++ b/platform/platform_ios.mm @@ -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 diff --git a/platform/platform_qt.cpp b/platform/platform_qt.cpp index c0265141b7..22ad70de62 100644 --- a/platform/platform_qt.cpp +++ b/platform/platform_qt.cpp @@ -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 diff --git a/platform/platform_tests/downloader_test.cpp b/platform/platform_tests/downloader_test.cpp index a9a97a8431..be7b3293f8 100644 --- a/platform/platform_tests/downloader_test.cpp +++ b/platform/platform_tests/downloader_test.cpp @@ -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); } diff --git a/platform/platform_tests/platform_test.cpp b/platform/platform_tests/platform_test.cpp index 8d20467585..c94950881c 100644 --- a/platform/platform_tests/platform_test.cpp +++ b/platform/platform_tests/platform_test.cpp @@ -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) diff --git a/platform/platform_unix_impl.cpp b/platform/platform_unix_impl.cpp index c1819a0eee..90132f2b9d 100644 --- a/platform/platform_unix_impl.cpp +++ b/platform/platform_unix_impl.cpp @@ -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 & res) +void EnumerateFilesByRegExp(string const & directory, string const & regexp, + vector & 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); diff --git a/platform/platform_unix_impl.hpp b/platform/platform_unix_impl.hpp index 0823c925c5..efca59f8cb 100644 --- a/platform/platform_unix_impl.hpp +++ b/platform/platform_unix_impl.hpp @@ -5,6 +5,6 @@ namespace pl { - string GetFixedMask(string const & mask); - void EnumerateFilesInDir(string const & directory, string const & mask, vector & res); + void EnumerateFilesByRegExp(string const & directory, string const & regexp, + vector & res); } diff --git a/storage/storage_tests/generate_langs.cpp b/storage/storage_tests/generate_langs.cpp index 81b5638762..b5be1e839b 100644 --- a/storage/storage_tests/generate_langs.cpp +++ b/storage/storage_tests/generate_langs.cpp @@ -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) {