From 89d69320571811fc36adf84ea9a11a5df704ee9c Mon Sep 17 00:00:00 2001 From: vng Date: Fri, 29 Jun 2012 20:44:09 -0700 Subject: [PATCH] Factor out common Platform implementations for Unix-based OS. --- platform/platform.cpp | 18 +++++++++ platform/platform.pro | 9 ++++- platform/platform_android.cpp | 72 ++------------------------------- platform/platform_ios.mm | 61 ++-------------------------- platform/platform_linux.cpp | 23 +---------- platform/platform_qt.cpp | 11 +---- platform/platform_unix_impl.cpp | 58 ++++++++++++++++++++++++++ platform/platform_unix_impl.hpp | 10 +++++ platform/platform_win.cpp | 4 +- 9 files changed, 104 insertions(+), 162 deletions(-) create mode 100644 platform/platform_unix_impl.cpp create mode 100644 platform/platform_unix_impl.hpp diff --git a/platform/platform.cpp b/platform/platform.cpp index 4d52fa7a1a..ab5bf9fd8c 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -3,6 +3,8 @@ #include "../coding/sha2.hpp" #include "../coding/base64.hpp" +#include "../base/logging.hpp" + string Platform::ReadPathForFile(string const & file) const { @@ -38,3 +40,19 @@ string Platform::DefaultUrlsJSON() const { return "[\"http://1st.default.server/\",\"http://2nd.default.server/\",\"http://3rd.default.server/\"]"; } + +void Platform::GetFontNames(FilesList & res) const +{ + string arr[] = { WritableDir(), ResourcesDir() }; + + for (size_t i = 0; i < ARRAY_SIZE(arr); ++i) + { + LOG(LDEBUG, ("Searching for fonts in", arr[i])); + GetFilesInDir(arr[i], "*.ttf", res); + } + + sort(res.begin(), res.end()); + res.erase(unique(res.begin(), res.end()), res.end()); + + LOG(LDEBUG, ("Font files:", (res))); +} diff --git a/platform/platform.pro b/platform/platform.pro index f8625ae34d..4572240ac3 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -49,6 +49,11 @@ macx*|iphone* { OBJECTIVE_SOURCES += http_thread_apple.mm } +!win32* { + HEADERS += platform_unix_impl.hpp + SOURCES += platform_unix_impl.cpp +} + # common sources for all platforms HEADERS += \ @@ -62,7 +67,7 @@ HEADERS += \ chunks_download_strategy.hpp \ servers_list.hpp \ constants.hpp \ - file_name_utils.hpp + file_name_utils.hpp \ SOURCES += \ preferred_languages.cpp \ @@ -72,4 +77,4 @@ SOURCES += \ chunks_download_strategy.cpp \ platform.cpp \ servers_list.cpp \ - file_name_utils.cpp + file_name_utils.cpp \ diff --git a/platform/platform_android.cpp b/platform/platform_android.cpp index 8046a07e5e..d905d53e37 100644 --- a/platform/platform_android.cpp +++ b/platform/platform_android.cpp @@ -1,4 +1,5 @@ #include "platform.hpp" +#include "platform_unix_impl.hpp" #include "constants.hpp" #include "../coding/zip_reader.hpp" @@ -6,9 +7,7 @@ #include "../base/logging.hpp" #include "../base/thread.hpp" -#include #include -#include Platform::Platform() : m_impl(0) @@ -17,13 +16,6 @@ Platform::Platform() : m_impl(0) Platform::~Platform() {} -/// @warning doesn't work for files inside .apk (zip)!!! -bool Platform::IsFileExistsByFullPath(string const & filePath) -{ - struct stat s; - return stat(filePath.c_str(), &s) == 0; -} - ModelReader * Platform::GetReader(string const & file) const { if (IsFileExistsByFullPath(m_writableDir + file)) @@ -41,19 +33,6 @@ ModelReader * Platform::GetReader(string const & file) const } } -namespace -{ - 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 Platform::GetFilesInDir(string const & directory, string const & mask, FilesList & res) { if (ZipFileReader::IsZip(directory)) @@ -62,7 +41,7 @@ void Platform::GetFilesInDir(string const & directory, string const & mask, File FilesList fList; ZipFileReader::FilesList(directory, fList); - string const fixedMask = GetFixedMask(mask); + string const fixedMask = pl::GetFixedMask(mask); for (FilesList::iterator it = fList.begin(); it != fList.end(); ++it) { @@ -78,24 +57,7 @@ void Platform::GetFilesInDir(string const & directory, string const & mask, File } } else - { - DIR * dir; - struct dirent * entry; - if ((dir = opendir(directory.c_str())) == NULL) - return; - - string const fixedMask = GetFixedMask(mask); - - 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); - } - - closedir(dir); - } + pl::EnumerateFilesInDir(directory, mask, res); } int Platform::CpuCores() const @@ -118,22 +80,6 @@ string Platform::DeviceName() const return "Android"; } -void Platform::GetFontNames(FilesList & res) const -{ - string arr[] = { WritableDir(), ResourcesDir() }; - - for (size_t i = 0; i < ARRAY_SIZE(arr); ++i) - { - LOG(LDEBUG, ("Searching for fonts in", arr[i])); - GetFilesInDir(arr[i], "*.ttf", res); - } - - sort(res.begin(), res.end()); - res.erase(unique(res.begin(), res.end()), res.end()); - - LOG(LDEBUG, ("Font files:", (res))); -} - int Platform::ScaleEtalonSize() const { return 512 + 256; @@ -157,18 +103,6 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const } } -/// @warning doesn't work for files inside .apk (zip)!!! -bool Platform::GetFileSizeByFullPath(string const & filePath, uint64_t & size) -{ - struct stat s; - if (stat(filePath.c_str(), &s) == 0) - { - size = s.st_size; - return true; - } - return false; -} - void Platform::RunOnGuiThread(TFunctor const & fn) { /// @todo diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm index 7032985764..3a377d9fe1 100644 --- a/platform/platform_ios.mm +++ b/platform/platform_ios.mm @@ -1,12 +1,11 @@ #include "platform.hpp" +#include "platform_unix_impl.hpp" #include "constants.hpp" #include "../coding/file_reader.hpp" #include "../coding/base64.hpp" #include "../coding/sha2.hpp" -#include -#include #include #include #include @@ -81,57 +80,9 @@ Platform::~Platform() delete m_impl; } -bool Platform::IsFileExistsByFullPath(string const & filePath) -{ - struct stat s; - return stat(filePath.c_str(), &s) == 0; -} - void Platform::GetFilesInDir(string const & directory, string const & mask, FilesList & res) { - DIR * dir; - struct dirent * entry; - - if ((dir = opendir(directory.c_str())) == NULL) - return; - - // TODO: take wildcards into account... - string mask_fixed = mask; - if (mask_fixed.size() && mask_fixed[0] == '*') - mask_fixed.erase(0, 1); - - do - { - if ((entry = readdir(dir)) != NULL) - { - string fname(entry->d_name); - size_t index = fname.rfind(mask_fixed); - if (index != string::npos && index == fname.size() - mask_fixed.size()) - { - // TODO: By some strange reason under simulator stat returns -1, - // may be because of symbolic links?.. - //struct stat fileStatus; - //if (stat(string(directory + fname).c_str(), &fileStatus) == 0 && - // (fileStatus.st_mode & S_IFDIR) == 0) - //{ - res.push_back(fname); - //} - } - } - } while (entry != NULL); - - closedir(dir); -} - -bool Platform::GetFileSizeByFullPath(string const & filePath, uint64_t & size) -{ - struct stat s; - if (stat(filePath.c_str(), &s) == 0) - { - size = s.st_size; - return true; - } - return false; + pl::EnumerateFilesInDir(directory, mask, res); } bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const @@ -140,18 +91,12 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const { return GetFileSizeByFullPath(ReadPathForFile(fileName), size); } - catch (std::exception const &) + catch (RootException const &) { return false; } } -void Platform::GetFontNames(FilesList & res) const -{ - GetFilesInDir(ResourcesDir(), "*.ttf", res); - sort(res.begin(), res.end()); -} - ModelReader * Platform::GetReader(string const & file) const { return new FileReader(ReadPathForFile(file), diff --git a/platform/platform_linux.cpp b/platform/platform_linux.cpp index cd433951a5..b3c20ab0ad 100644 --- a/platform/platform_linux.cpp +++ b/platform/platform_linux.cpp @@ -4,20 +4,7 @@ #include #include -#include -//static bool GetUserWritableDir(string & outDir) -//{ -// char * path = ::getenv("HOME"); -// if (path) -// { -// outDir = path; -// outDir += "/.MapsWithMe/"; -// ::mkdir(outDir.c_str(), 0755); -// return true; -// } -// return false; -//} /// @return directory where binary resides, including slash at the end static bool GetBinaryFolder(string & outPath) @@ -59,12 +46,6 @@ Platform::~Platform() { } -bool Platform::IsFileExistsByFullPath(string const & filePath) -{ - struct stat s; - return stat(filePath.c_str(), &s) == 0; -} - int Platform::CpuCores() const { const long numCPU = sysconf(_SC_NPROCESSORS_ONLN); @@ -80,12 +61,12 @@ string Platform::UniqueClientId() const void Platform::RunOnGuiThread(TFunctor const & fn) { - // @TODO + /// @todo fn(); } void Platform::RunAsync(TFunctor const & fn, Priority p) { - // @TODO + /// @todo fn(); } diff --git a/platform/platform_qt.cpp b/platform/platform_qt.cpp index 8dff47b7e2..743f6d350f 100644 --- a/platform/platform_qt.cpp +++ b/platform/platform_qt.cpp @@ -30,7 +30,7 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const { return GetFileSizeByFullPath(ReadPathForFile(fileName), size); } - catch (std::exception const &) + catch (RootException const &) { return false; } @@ -50,15 +50,6 @@ string Platform::DeviceName() const return OMIM_OS_NAME; } -void Platform::GetFontNames(FilesList & res) const -{ - GetFilesInDir(ResourcesDir(), "*.ttf", res); - GetFilesInDir(WritableDir(), "*.ttf", res); - sort(res.begin(), res.end()); - res.erase(unique(res.begin(), res.end()), res.end()); - CHECK(!res.empty(), ("Can't find any valid font in", ResourcesDir(), WritableDir())); -} - int Platform::PreCachingDepth() const { return 3; diff --git a/platform/platform_unix_impl.cpp b/platform/platform_unix_impl.cpp new file mode 100644 index 0000000000..80bf718de2 --- /dev/null +++ b/platform/platform_unix_impl.cpp @@ -0,0 +1,58 @@ +#include "platform.hpp" +#include "platform_unix_impl.hpp" + +#include +#include + + +bool Platform::IsFileExistsByFullPath(string const & filePath) +{ + struct stat s; + return stat(filePath.c_str(), &s) == 0; +} + +bool Platform::GetFileSizeByFullPath(string const & filePath, uint64_t & size) +{ + struct stat s; + if (stat(filePath.c_str(), &s) == 0) + { + size = s.st_size; + return true; + } + else return false; +} + +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) +{ + DIR * dir; + struct dirent * entry; + if ((dir = opendir(directory.c_str())) == NULL) + return; + + string const fixedMask = GetFixedMask(mask); + + 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); + } + + closedir(dir); +} + +} diff --git a/platform/platform_unix_impl.hpp b/platform/platform_unix_impl.hpp new file mode 100644 index 0000000000..0823c925c5 --- /dev/null +++ b/platform/platform_unix_impl.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "../std/string.hpp" +#include "../std/vector.hpp" + +namespace pl +{ + string GetFixedMask(string const & mask); + void EnumerateFilesInDir(string const & directory, string const & mask, vector & res); +} diff --git a/platform/platform_win.cpp b/platform/platform_win.cpp index 4edfe71d71..78fac476da 100644 --- a/platform/platform_win.cpp +++ b/platform/platform_win.cpp @@ -110,12 +110,12 @@ string Platform::UniqueClientId() const void Platform::RunOnGuiThread(TFunctor const & fn) { - // @TODO + /// @todo fn(); } void Platform::RunAsync(TFunctor const & fn, Priority p) { - // @TODO + /// @todo fn(); }