From 017e56b81ba735c39c43701f737ac7dde55da7b4 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Wed, 9 May 2018 00:04:33 +0200 Subject: 9.5 --- zen/error_log.h | 14 ++++++------ zen/file_error.h | 5 ++-- zen/format_unit.cpp | 8 +++---- zen/format_unit.h | 12 +++++----- zen/i18n.h | 2 +- zen/process_priority.h | 6 ++--- zen/shell_execute.h | 15 ++++++++++-- zen/shutdown.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ zen/shutdown.h | 18 +++++++++++++++ zen/sys_error.cpp | 6 +++++ zen/sys_error.h | 4 +++- zen/time.h | 6 ++--- zen/zstring.cpp | 9 +++++--- zen/zstring.h | 11 +++++++++ 14 files changed, 145 insertions(+), 33 deletions(-) create mode 100755 zen/shutdown.cpp create mode 100755 zen/shutdown.h create mode 100755 zen/sys_error.cpp (limited to 'zen') diff --git a/zen/error_log.h b/zen/error_log.h index 90016666..0d62dd1f 100755 --- a/zen/error_log.h +++ b/zen/error_log.h @@ -49,12 +49,12 @@ public: //subset of std::vector<> interface: using const_iterator = std::vector::const_iterator; - const_iterator begin() const { return entries.begin(); } - const_iterator end () const { return entries.end (); } - bool empty() const { return entries.empty(); } + const_iterator begin() const { return entries_.begin(); } + const_iterator end () const { return entries_.end (); } + bool empty() const { return entries_.empty(); } private: - std::vector entries; //list of non-resolved errors and warnings + std::vector entries_; //list of non-resolved errors and warnings }; @@ -70,14 +70,14 @@ template inline void ErrorLog::logMsg(const String& text, zen::MessageType type) { const LogEntry newEntry = { std::time(nullptr), type, copyStringTo(text) }; - entries.push_back(newEntry); + entries_.push_back(newEntry); } inline int ErrorLog::getItemCount(int typeFilter) const { - return static_cast(std::count_if(entries.begin(), entries.end(), [&](const LogEntry& e) { return e.type & typeFilter; })); + return static_cast(std::count_if(entries_.begin(), entries_.end(), [&](const LogEntry& e) { return e.type & typeFilter; })); } @@ -103,7 +103,7 @@ String formatMessageImpl(const LogEntry& entry) //internal linkage return std::wstring(); }; - String formattedText = L"[" + formatTime(FORMAT_TIME, getLocalTime(entry.time)) + L"] " + copyStringTo(getTypeName()) + L": "; + String formattedText = L"[" + formatTime(FORMAT_TIME, getLocalTime(entry.time)) + L"] " + copyStringTo(getTypeName()) + L": "; const size_t prefixLen = formattedText.size(); for (auto it = entry.message.begin(); it != entry.message.end(); ) diff --git a/zen/file_error.h b/zen/file_error.h index b318e708..18e790de 100755 --- a/zen/file_error.h +++ b/zen/file_error.h @@ -14,8 +14,7 @@ namespace zen { -//A high-level exception class giving detailed context information for end users -class FileError +class FileError //A high-level exception class giving detailed context information for end users { public: explicit FileError(const std::wstring& msg) : msg_(msg) {} @@ -28,7 +27,7 @@ private: std::wstring msg_; }; -#define DEFINE_NEW_FILE_ERROR(X) struct X : public FileError { X(const std::wstring& msg) : FileError(msg) {} X(const std::wstring& msg, const std::wstring& descr) : FileError(msg, descr) {} }; +#define DEFINE_NEW_FILE_ERROR(X) struct X : public zen::FileError { X(const std::wstring& msg) : FileError(msg) {} X(const std::wstring& msg, const std::wstring& descr) : FileError(msg, descr) {} }; DEFINE_NEW_FILE_ERROR(ErrorTargetExisting); //DEFINE_NEW_FILE_ERROR(ErrorTargetPathMissing); diff --git a/zen/format_unit.cpp b/zen/format_unit.cpp index c0667fb1..e62c9286 100755 --- a/zen/format_unit.cpp +++ b/zen/format_unit.cpp @@ -39,7 +39,7 @@ std::wstring zen::formatThreeDigitPrecision(double value) } -std::wstring zen::filesizeToShortString(int64_t size) +std::wstring zen::formatFilesizeShort(int64_t size) { //if (size < 0) return _("Error"); -> really? @@ -122,7 +122,7 @@ std::wstring roundToBlock(double timeInHigh, } -std::wstring zen::remainingTimeToString(double timeInSec) +std::wstring zen::formatRemainingTime(double timeInSec) { const int steps10[] = { 1, 2, 5, 10 }; const int steps24[] = { 1, 2, 3, 4, 6, 8, 12, 24 }; @@ -154,7 +154,7 @@ std::wstring zen::remainingTimeToString(double timeInSec) //} -std::wstring zen::fractionToString(double fraction) +std::wstring zen::formatFraction(double fraction) { return printNumber(L"%.2f", fraction * 100.0) + L'%'; //no need to internationalize fraction!? } @@ -188,7 +188,7 @@ std::wstring zen::ffs_Impl::includeNumberSeparator(const std::wstring& number) } -std::wstring zen::utcToLocalTimeString(int64_t utcTime) +std::wstring zen::formatUtcToLocalTime(int64_t utcTime) { auto errorMsg = [&] { return _("Error") + L" (time_t: " + numberTo(utcTime) + L")"; }; diff --git a/zen/format_unit.h b/zen/format_unit.h index 336df74c..3dcc6858 100755 --- a/zen/format_unit.h +++ b/zen/format_unit.h @@ -15,16 +15,16 @@ namespace zen { -std::wstring filesizeToShortString(int64_t filesize); -std::wstring remainingTimeToString(double timeInSec); -std::wstring fractionToString(double fraction); //within [0, 1] -std::wstring utcToLocalTimeString(int64_t utcTime); //like Windows Explorer would... +std::wstring formatFilesizeShort(int64_t filesize); +std::wstring formatRemainingTime(double timeInSec); +std::wstring formatFraction(double fraction); //within [0, 1] +std::wstring formatUtcToLocalTime(int64_t utcTime); //like Windows Explorer would... std::wstring formatTwoDigitPrecision (double value); //format with fixed number of digits std::wstring formatThreeDigitPrecision(double value); //(unless value is too large) template -std::wstring toGuiString(NumberType number); //format integer number including thousands separator +std::wstring formatNumber(NumberType number); //format integer number including thousands separator @@ -43,7 +43,7 @@ std::wstring includeNumberSeparator(const std::wstring& number); } template inline -std::wstring toGuiString(NumberType number) +std::wstring formatNumber(NumberType number) { static_assert(IsInteger::value, ""); return ffs_Impl::includeNumberSeparator(zen::numberTo(number)); diff --git a/zen/i18n.h b/zen/i18n.h index ebe22459..e6d97b7b 100755 --- a/zen/i18n.h +++ b/zen/i18n.h @@ -106,7 +106,7 @@ std::wstring translate(const std::wstring& singular, const std::wstring& plural, return translation; } //fallback: - return replaceCpy(std::abs(n64) == 1 ? singular : plural, L"%x", toGuiString(n)); + return replaceCpy(std::abs(n64) == 1 ? singular : plural, L"%x", formatNumber(n)); } } diff --git a/zen/process_priority.h b/zen/process_priority.h index 07679b0c..ac96a8ae 100755 --- a/zen/process_priority.h +++ b/zen/process_priority.h @@ -3,13 +3,13 @@ // * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * // * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved * // ***************************************************************************** + #ifndef PROCESS_PRIORITY_H_83421759082143245 #define PROCESS_PRIORITY_H_83421759082143245 #include #include "file_error.h" - namespace zen { //signal a "busy" state to the operating system @@ -20,7 +20,7 @@ public: ~PreventStandby(); private: struct Impl; - const std::unique_ptr pimpl; + const std::unique_ptr pimpl_; }; //lower CPU and file I/O priorities @@ -31,7 +31,7 @@ public: ~ScheduleForBackgroundProcessing(); private: struct Impl; - const std::unique_ptr pimpl; + const std::unique_ptr pimpl_; }; } diff --git a/zen/shell_execute.h b/zen/shell_execute.h index 7dcd6653..077f18e7 100755 --- a/zen/shell_execute.h +++ b/zen/shell_execute.h @@ -39,12 +39,23 @@ void shellExecute(const Zstring& command, ExecutionType type) //throw FileError if (type == EXEC_TYPE_SYNC) { //Posix::system - execute a shell command - int rv = ::system(command.c_str()); //do NOT use std::system as its documentation says nothing about "WEXITSTATUS(rv)", ect... + const int rv = ::system(command.c_str()); //do NOT use std::system as its documentation says nothing about "WEXITSTATUS(rv)", ect... if (rv == -1 || WEXITSTATUS(rv) == 127) //http://linux.die.net/man/3/system "In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127)" throw FileError(_("Incorrect command line:") + L"\n" + utfTo(command)); } else - runAsync([=] { int rv = ::system(command.c_str()); (void)rv; }); + { + runAsync([=] { int rv = ::system(command.c_str()); (void)rv; }); + + warn_static("finish:") + + + + + + + + } } } } diff --git a/zen/shutdown.cpp b/zen/shutdown.cpp new file mode 100755 index 00000000..b21ab8db --- /dev/null +++ b/zen/shutdown.cpp @@ -0,0 +1,62 @@ +// ***************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * +// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved * +// ***************************************************************************** + +#include "shutdown.h" + #include + + +using namespace zen; + + + + +void zen::shutdownSystem() //throw FileError +{ + //https://linux.die.net/man/2/reboot => needs admin rights! + + //should work without admin rights: + shellExecute("sleep 1; systemctl poweroff", EXEC_TYPE_ASYNC); //throw FileError + //sleep 1: give FFS some time to properly shut down! + //Linux: main thread will wait on detached threads! + warn_static("get rid of shellExecute's thread implementation!") + +} + + +void zen::suspendSystem() //throw FileError +{ + //should work without admin rights: + shellExecute("systemctl suspend", EXEC_TYPE_ASYNC); //throw FileError + +} + +/* +Command line alternatives: + +#ifdef ZEN_WIN +#ifdef ZEN_WIN_VISTA_AND_LATER + Shut down: shutdown /s /t 60 + Sleep: rundll32.exe powrprof.dll,SetSuspendState Sleep + Log off: shutdown /l +#else //XP + Shut down: shutdown -s -t 60 + Standby: rundll32.exe powrprof.dll,SetSuspendState //this triggers standby OR hibernate, depending on whether hibernate setting is active! no suspend on XP? + Log off: shutdown -l +#endif + +#elif defined ZEN_LINUX + Shut down: systemctl poweroff //alternative requiring admin: sudo shutdown -h 1 + Sleep: systemctl suspend //alternative requiring admin: sudo pm-suspend + Log off: gnome-session-quit --no-prompt + //alternative requiring admin: sudo killall Xorg + //alternative without admin: dbus-send --session --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1 + +#elif defined ZEN_MAC + Shut down: osascript -e 'tell application "System Events" to shut down' + Sleep: osascript -e 'tell application "System Events" to sleep' + Log off: osascript -e 'tell application "System Events" to log out' +#endif +*/ diff --git a/zen/shutdown.h b/zen/shutdown.h new file mode 100755 index 00000000..e64dee41 --- /dev/null +++ b/zen/shutdown.h @@ -0,0 +1,18 @@ +// ***************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * +// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved * +// ***************************************************************************** + +#ifndef SHUTDOWN_H_3423847870238407783265 +#define SHUTDOWN_H_3423847870238407783265 + +#include "file_error.h" + +namespace zen +{ +void shutdownSystem(); //throw FileError +void suspendSystem(); // +} + +#endif //SHUTDOWN_H_3423847870238407783265 diff --git a/zen/sys_error.cpp b/zen/sys_error.cpp new file mode 100755 index 00000000..fd4c8f1e --- /dev/null +++ b/zen/sys_error.cpp @@ -0,0 +1,6 @@ +// ***************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * +// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved * +// ***************************************************************************** + diff --git a/zen/sys_error.h b/zen/sys_error.h index 026a3be5..f4361e69 100755 --- a/zen/sys_error.h +++ b/zen/sys_error.h @@ -26,6 +26,8 @@ ErrorCode getLastError(); std::wstring formatSystemError(const std::wstring& functionName, ErrorCode ec); std::wstring formatSystemError(const std::wstring& functionName, const std::wstring& errorCode, const std::wstring& errorMsg); + + //A low-level exception class giving (non-translated) detail information only - same conceptional level like "GetLastError()"! class SysError { @@ -37,7 +39,7 @@ private: std::wstring msg_; }; -#define DEFINE_NEW_SYS_ERROR(X) struct X : public SysError { X(const std::wstring& msg) : SysError(msg) {} }; +#define DEFINE_NEW_SYS_ERROR(X) struct X : public zen::SysError { X(const std::wstring& msg) : SysError(msg) {} }; diff --git a/zen/time.h b/zen/time.h index 3323a318..2ac67399 100755 --- a/zen/time.h +++ b/zen/time.h @@ -191,11 +191,11 @@ bool isValid(const std::tm& t) auto inRange = [](int value, int minVal, int maxVal) { return minVal <= value && value <= maxVal; }; //http://www.cplusplus.com/reference/clibrary/ctime/tm/ - return inRange(t.tm_sec , 0, 61) && - inRange(t.tm_min , 0, 59) && + return inRange(t.tm_sec, 0, 61) && + inRange(t.tm_min, 0, 59) && inRange(t.tm_hour, 0, 23) && inRange(t.tm_mday, 1, 31) && - inRange(t.tm_mon , 0, 11) && + inRange(t.tm_mon, 0, 11) && //tm_year inRange(t.tm_wday, 0, 6) && inRange(t.tm_yday, 0, 365); diff --git a/zen/zstring.cpp b/zen/zstring.cpp index 6b41af13..fb62424a 100755 --- a/zen/zstring.cpp +++ b/zen/zstring.cpp @@ -74,9 +74,12 @@ int cmpStringNaturalLinux(const char* lhs, size_t lhsLen, const char* rhs, size_ - compare strings after conceptually creating blocks of whitespace/numbers/text - implement strict weak ordering! - don't follow broken "strnatcasecmp": https://github.com/php/php-src/blob/master/ext/standard/strnatcmp.c - 1. incorrect non-ASCII CI-comparison 2. incorrect bounds checks - 3. incorrect trimming of *all* whitespace 4. arbitrary handling of leading 0 only at string begin - 5. incorrect handling of whitespace following a number 6. code is a mess + 1. incorrect non-ASCII CI-comparison + 2. incorrect bounds checks + 3. incorrect trimming of *all* whitespace + 4. arbitrary handling of leading 0 only at string begin + 5. incorrect handling of whitespace following a number + 6. code is a mess */ for (;;) { diff --git a/zen/zstring.h b/zen/zstring.h index 273efd2f..33f48990 100755 --- a/zen/zstring.h +++ b/zen/zstring.h @@ -72,6 +72,17 @@ S ciReplaceCpy(const S& str, const T& oldTerm, const U& newTerm); +//common unicode sequences +const wchar_t EM_DASH = L'\u2014'; +const wchar_t* const SPACED_DASH = L" \u2013 "; //using 'EN DASH' +const wchar_t LTR_MARK = L'\u200E'; //UTF-8: E2 80 8E +const wchar_t RTL_MARK = L'\u200F'; //UTF-8: E2 80 8F +const wchar_t ELLIPSIS = L'\u2026'; //"..." + + + + + //################################# inline implementation ######################################## inline -- cgit