diff options
Diffstat (limited to 'zen')
-rwxr-xr-x | zen/dir_watcher.h | 4 | ||||
-rwxr-xr-x | zen/error_log.h | 38 | ||||
-rwxr-xr-x | zen/process_priority.cpp | 2 | ||||
-rwxr-xr-x | zen/shell_execute.h | 7 | ||||
-rwxr-xr-x | zen/string_tools.h | 61 | ||||
-rwxr-xr-x | zen/zstring.h | 2 |
6 files changed, 59 insertions, 55 deletions
diff --git a/zen/dir_watcher.h b/zen/dir_watcher.h index b4796618..f552e2b2 100755 --- a/zen/dir_watcher.h +++ b/zen/dir_watcher.h @@ -44,7 +44,7 @@ public: enum ActionType { - ACTION_CREATE, //informal only! + ACTION_CREATE, //informal! ACTION_UPDATE, //use for debugging/logging only! ACTION_DELETE, // }; @@ -52,7 +52,7 @@ public: struct Entry { ActionType action = ACTION_CREATE; - Zstring filePath; + Zstring itemPath; }; //extract accumulated changes since last call diff --git a/zen/error_log.h b/zen/error_log.h index 0de88856..4a3f5f2c 100755 --- a/zen/error_log.h +++ b/zen/error_log.h @@ -33,15 +33,13 @@ struct LogEntry Zstringw message; //std::wstring may employ small string optimization: we cannot accept bloating the "ErrorLog::entries" memory block below (think 1 million items) }; -template <class String> -String formatMessage(const LogEntry& entry); +std::wstring formatMessage(const LogEntry& entry); class ErrorLog { public: - template <class String> //a wchar_t-based string! - void logMsg(const String& text, MessageType type); + void logMsg(const std::wstring& msg, MessageType type); int getItemCount(int typeFilter = MSG_TYPE_INFO | MSG_TYPE_WARNING | MSG_TYPE_ERROR | MSG_TYPE_FATAL_ERROR) const; @@ -49,10 +47,10 @@ public: using const_iterator = std::vector<LogEntry>::const_iterator; const_iterator begin() const { return entries_.begin(); } const_iterator end () const { return entries_.end (); } - bool empty() const { return entries_.empty(); } + bool empty() const { return entries_.empty(); } private: - std::vector<LogEntry> entries_; //list of non-resolved errors and warnings + std::vector<LogEntry> entries_; }; @@ -64,10 +62,10 @@ private: //######################## implementation ########################## -template <class String> inline -void ErrorLog::logMsg(const String& text, MessageType type) +inline +void ErrorLog::logMsg(const std::wstring& msg, MessageType type) { - entries_.push_back({ std::time(nullptr), type, copyStringTo<Zstringw>(text) }); + entries_.push_back({ std::time(nullptr), type, copyStringTo<Zstringw>(msg) }); } @@ -80,8 +78,7 @@ int ErrorLog::getItemCount(int typeFilter) const namespace { -template <class String> -String formatMessageImpl(const LogEntry& entry) //internal linkage +std::wstring formatMessageImpl(const LogEntry& entry) { auto getTypeName = [&] { @@ -100,17 +97,14 @@ String formatMessageImpl(const LogEntry& entry) //internal linkage return std::wstring(); }; - String formattedText = L"[" + formatTime<String>(FORMAT_TIME, getLocalTime(entry.time)) + L"] " + copyStringTo<String>(getTypeName()) + L": "; - const size_t prefixLen = formattedText.size(); //considers UTF-16 only! + std::wstring msgFmt = L"[" + formatTime<std::wstring>(FORMAT_TIME, getLocalTime(entry.time)) + L"] " + getTypeName() + L": "; + const size_t prefixLen = msgFmt.size(); //considers UTF-16 only! for (auto it = entry.message.begin(); it != entry.message.end(); ) if (*it == L'\n') { - formattedText += L'\n'; - - String blanks; - blanks.resize(prefixLen, L' '); - formattedText += blanks; + msgFmt += L'\n'; + msgFmt.append(prefixLen, L' '); do //skip duplicate newlines { @@ -119,14 +113,14 @@ String formatMessageImpl(const LogEntry& entry) //internal linkage while (it != entry.message.end() && *it == L'\n'); } else - formattedText += *it++; + msgFmt += *it++; - return formattedText; + return msgFmt; } } -template <class String> inline -String formatMessage(const LogEntry& entry) { return formatMessageImpl<String>(entry); } +inline +std::wstring formatMessage(const LogEntry& entry) { return formatMessageImpl(entry); } } #endif //ERROR_LOG_H_8917590832147915 diff --git a/zen/process_priority.cpp b/zen/process_priority.cpp index c2a7ed20..e925f142 100755 --- a/zen/process_priority.cpp +++ b/zen/process_priority.cpp @@ -11,8 +11,6 @@ using namespace zen; -//wxPowerResourceBlocker? http://docs.wxwidgets.org/trunk/classwx_power_resource_blocker.html -//nah, "currently the power events are only available under Windows" struct PreventStandby::Impl {}; PreventStandby::PreventStandby() {} PreventStandby::~PreventStandby() {} diff --git a/zen/shell_execute.h b/zen/shell_execute.h index 43bede61..a0e5634b 100755 --- a/zen/shell_execute.h +++ b/zen/shell_execute.h @@ -68,6 +68,13 @@ void shellExecute(const Zstring& command, ExecutionType type) //throw FileError } } } + + +inline +void openWithDefaultApplication(const Zstring& itemPath) //throw FileError +{ + shellExecute("xdg-open \"" + itemPath + "\"", ExecutionType::ASYNC); // +} } #endif //SHELL_EXECUTE_H_23482134578134134 diff --git a/zen/string_tools.h b/zen/string_tools.h index e09cb61f..8746722a 100755 --- a/zen/string_tools.h +++ b/zen/string_tools.h @@ -75,8 +75,8 @@ template <class S> S trimCpy(S str, bool fromLeft = true, bo template <class S> void trim (S& str, bool fromLeft = true, bool fromRight = true); template <class S, class Function> void trim(S& str, bool fromLeft, bool fromRight, Function trimThisChar); -template <class S, class T, class U> void replace ( S& str, const T& oldTerm, const U& newTerm, bool replaceAll = true); -template <class S, class T, class U> S replaceCpy(const S& str, const T& oldTerm, const U& newTerm, bool replaceAll = true); +template <class S, class T, class U> void replace (S& str, const T& oldTerm, const U& newTerm, bool replaceAll = true); +template <class S, class T, class U> S replaceCpy(S str, const T& oldTerm, const U& newTerm, bool replaceAll = true); //high-performance conversion between numbers and strings template <class S, class Num> S numberTo(const Num& number); @@ -348,19 +348,28 @@ ZEN_INIT_DETECT_MEMBER(append); template <class S, class InputIterator> inline std::enable_if_t<HasMember_append<S>::value> stringAppend(S& str, InputIterator first, InputIterator last) { str.append(first, last); } -template <class S, class InputIterator> inline -std::enable_if_t<!HasMember_append<S>::value> stringAppend(S& str, InputIterator first, InputIterator last) { str += S(first, last); } +//inefficient append: keep disabled until really needed +//template <class S, class InputIterator> inline +//std::enable_if_t<!HasMember_append<S>::value> stringAppend(S& str, InputIterator first, InputIterator last) { str += S(first, last); } +} + + +template <class S, class T, class U> inline +S replaceCpy(S str, const T& oldTerm, const U& newTerm, bool replaceAll) +{ + replace(str, oldTerm, newTerm, replaceAll); + return str; } template <class S, class T, class U> inline -S replaceCpy(const S& str, const T& oldTerm, const U& newTerm, bool replaceAll) +void replace(S& str, const T& oldTerm, const U& newTerm, bool replaceAll) { static_assert(std::is_same_v<GetCharTypeT<S>, GetCharTypeT<T>>); static_assert(std::is_same_v<GetCharTypeT<T>, GetCharTypeT<U>>); const size_t oldLen = strLength(oldTerm); if (oldLen == 0) - return str; + return; const auto* const oldBegin = strBegin(oldTerm); const auto* const oldEnd = oldBegin + oldLen; @@ -368,35 +377,31 @@ S replaceCpy(const S& str, const T& oldTerm, const U& newTerm, bool replaceAll) const auto* const newBegin = strBegin(newTerm); const auto* const newEnd = newBegin + strLength(newTerm); - S output; - - for (auto it = str.begin();;) - { - const auto itFound = std::search(it, str.end(), - oldBegin, oldEnd); - if (itFound == str.end() && it == str.begin()) - return str; //optimize "oldTerm not found": return ref-counted copy + auto it = strBegin(str); //don't use str.begin() or wxString will return this wxUni* nonsense! + const auto* const strEnd = it + strLength(str); - impl::stringAppend(output, it, itFound); - if (itFound == str.end()) - return output; + auto itFound = std::search(it, strEnd, + oldBegin, oldEnd); + if (itFound == strEnd) + return; //optimize "oldTerm not found" + S output(it, itFound); + do + { impl::stringAppend(output, newBegin, newEnd); it = itFound + oldLen; if (!replaceAll) - { - impl::stringAppend(output, it, str.end()); - return output; - } - } -} + itFound = strEnd; + else + itFound = std::search(it, strEnd, + oldBegin, oldEnd); + impl::stringAppend(output, it, itFound); + } + while (itFound != strEnd); -template <class S, class T, class U> inline -void replace(S& str, const T& oldTerm, const U& newTerm, bool replaceAll) -{ - str = replaceCpy(str, oldTerm, newTerm, replaceAll); + str = std::move(output); } @@ -437,7 +442,7 @@ S trimCpy(S str, bool fromLeft, bool fromRight) { //implementing trimCpy() in terms of trim(), instead of the other way round, avoids memory allocations when trimming from right! trim(str, fromLeft, fromRight); - return std::move(str); //"str" is an l-value parameter => no copy elision! + return str; } diff --git a/zen/zstring.h b/zen/zstring.h index 026737da..3938cef1 100755 --- a/zen/zstring.h +++ b/zen/zstring.h @@ -125,7 +125,7 @@ S makeUpperCopy(S str) if (len > 0) makeUpperInPlace(&*str.begin(), len); - return std::move(str); //"str" is an l-value parameter => no copy elision! + return str; } |