From 34021994cdd8db6e9732e8c4625d72ec87983f6a Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 24 Oct 2018 10:44:23 +0200 Subject: [PATCH] Fix for Android - No std::to_string. We have to implement it with a ostringstream - No pthread_cancel. So we use pthread_kill to send a signal to the thread. --- include/common/stringTools.h | 8 +++++++- src/aria2.cpp | 4 ++-- src/downloader.cpp | 8 ++++---- src/opds_dumper.cpp | 2 +- src/subprocess_unix.cpp | 21 +++++++++++++++++++++ 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/include/common/stringTools.h b/include/common/stringTools.h index fb8c42b39..1c8a29bf5 100644 --- a/include/common/stringTools.h +++ b/include/common/stringTools.h @@ -64,6 +64,12 @@ std::string lcFirst(const std::string& word); std::string toTitle(const std::string& word); std::string normalize(const std::string& word); +template +std::string to_string(T value) +{ + std::ostringstream oss; + oss << value; + return oss.str(); } - +} //namespace kiwix #endif diff --git a/src/aria2.cpp b/src/aria2.cpp index f0b867170..d4dbc7913 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -21,7 +21,7 @@ Aria2::Aria2(): m_downloadDir = getDataDirectory(); std::vector callCmd; - std::string rpc_port = "--rpc-listen-port=" + std::to_string(m_port); + std::string rpc_port = "--rpc-listen-port=" + to_string(m_port); std::string download_dir = "--dir=" + getDataDirectory(); std::string session_file = appendToDirectory(getDataDirectory(), "kiwix.session"); std::string session = "--save-session=" + session_file; @@ -32,7 +32,7 @@ Aria2::Aria2(): #else pid_t pid = getpid(); #endif - std::string stop_with_pid = "--stop-with-process=" + std::to_string(pid); + std::string stop_with_pid = "--stop-with-process=" + to_string(pid); std::string rpc_secret = "--rpc-secret=" + m_secret; m_secret = "token:"+m_secret; diff --git a/src/downloader.cpp b/src/downloader.cpp index 071afce8e..2f9a70cd1 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -72,12 +72,12 @@ void Download::updateStatus(bool follow) } catch (InvalidRPCNode& e) { } } m_status = status; - m_totalLength = std::stoull(structNode.getMember("totalLength").getValue().getAsS()); - m_completedLength = std::stoull(structNode.getMember("completedLength").getValue().getAsS()); - m_downloadSpeed = std::stoull(structNode.getMember("downloadSpeed").getValue().getAsS()); + m_totalLength = structNode.getMember("totalLength").getValue().getAsI(); + m_completedLength = structNode.getMember("completedLength").getValue().getAsI(); + m_downloadSpeed = structNode.getMember("downloadSpeed").getValue().getAsI(); try { auto verifiedLengthValue = structNode.getMember("verifiedLength").getValue(); - m_verifiedLength = std::stoull(verifiedLengthValue.getAsS()); + m_verifiedLength = verifiedLengthValue.getAsI(); } catch (InvalidRPCNode& e) { m_verifiedLength = 0; } auto filesMember = structNode.getMember("files"); auto fileStruct = filesMember.getValue().getArray().getValue(0).getStruct(); diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index c7e91fa86..1ce76b05b 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -72,7 +72,7 @@ pugi::xml_node OPDSDumper::handleBook(Book book, pugi::xml_node root_node) { acquisition_link.append_attribute("rel") = "http://opds-spec.org/acquisition/open-access"; acquisition_link.append_attribute("type") = "application/x-zim"; acquisition_link.append_attribute("href") = book.getUrl().c_str(); - acquisition_link.append_attribute("length") = std::to_string(book.getSize()).c_str(); + acquisition_link.append_attribute("length") = to_string(book.getSize()).c_str(); } if (! book.getFaviconMimeType().empty() ) { diff --git a/src/subprocess_unix.cpp b/src/subprocess_unix.cpp index 8c5272816..fad700353 100644 --- a/src/subprocess_unix.cpp +++ b/src/subprocess_unix.cpp @@ -7,6 +7,7 @@ #include #include #include +#include UnixImpl::UnixImpl(): m_pid(0), @@ -19,11 +20,31 @@ UnixImpl::UnixImpl(): UnixImpl::~UnixImpl() { kill(); +// Android has no pthread_cancel :( +#ifdef __ANDROID__ + pthread_kill(m_waitingThread, SIGUSR1); +#else pthread_cancel(m_waitingThread); +#endif } +#ifdef __ANDROID__ +void thread_exit_handler(int sig) { + pthread_exit(0); +} +#endif + void* UnixImpl::waitForPID(void* _self) { +#ifdef __ANDROID__ + struct sigaction actions; + memset(&actions, 0, sizeof(actions)); + sigemptyset(&actions.sa_mask); + actions.sa_flags = 0; + actions.sa_handler = thread_exit_handler; + sigaction(SIGUSR1, &actions, NULL); +#endif + UnixImpl* self = static_cast(_self); waitpid(self->m_pid, NULL, WEXITED);