diff options
Diffstat (limited to 'src/common/Utilities')
-rw-r--r-- | src/common/Utilities/AsioHacksFwd.h | 68 | ||||
-rw-r--r-- | src/common/Utilities/AsioHacksImpl.h | 32 | ||||
-rw-r--r-- | src/common/Utilities/EventMap.cpp | 16 | ||||
-rw-r--r-- | src/common/Utilities/EventMap.h | 19 | ||||
-rw-r--r-- | src/common/Utilities/EventProcessor.cpp | 2 | ||||
-rw-r--r-- | src/common/Utilities/EventProcessor.h | 4 | ||||
-rw-r--r-- | src/common/Utilities/Hash.h | 51 | ||||
-rw-r--r-- | src/common/Utilities/Optional.h | 25 | ||||
-rw-r--r-- | src/common/Utilities/OptionalFwd.h | 31 | ||||
-rw-r--r-- | src/common/Utilities/Random.cpp | 5 | ||||
-rw-r--r-- | src/common/Utilities/Random.h | 1 | ||||
-rw-r--r-- | src/common/Utilities/StartProcess.cpp | 29 | ||||
-rw-r--r-- | src/common/Utilities/StartProcess.h | 11 | ||||
-rw-r--r-- | src/common/Utilities/StringFormat.h | 2 | ||||
-rw-r--r-- | src/common/Utilities/TaskScheduler.cpp | 1 | ||||
-rw-r--r-- | src/common/Utilities/TaskScheduler.h | 12 | ||||
-rw-r--r-- | src/common/Utilities/Timer.h | 1 | ||||
-rw-r--r-- | src/common/Utilities/Util.cpp | 64 | ||||
-rw-r--r-- | src/common/Utilities/Util.h | 72 |
19 files changed, 318 insertions, 128 deletions
diff --git a/src/common/Utilities/AsioHacksFwd.h b/src/common/Utilities/AsioHacksFwd.h new file mode 100644 index 00000000000..402c390f4f0 --- /dev/null +++ b/src/common/Utilities/AsioHacksFwd.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef AsioHacksFwd_h__ +#define AsioHacksFwd_h__ + +namespace boost +{ + namespace posix_time + { + class ptime; + } + + namespace asio + { + namespace ip + { + class address; + + class tcp; + + template <typename InternetProtocol> + class basic_endpoint; + + typedef basic_endpoint<tcp> tcp_endpoint; + + template <typename InternetProtocol> + class resolver_service; + + template <typename InternetProtocol, typename ResolverService> + class basic_resolver; + + typedef basic_resolver<tcp, resolver_service<tcp>> tcp_resolver; + } + + template <typename Time> + struct time_traits; + + template <typename TimeType, typename TimeTraits> + class deadline_timer_service; + + template <typename Time, typename TimeTraits, typename TimerService> + class basic_deadline_timer; + + typedef basic_deadline_timer<posix_time::ptime, time_traits<posix_time::ptime>, deadline_timer_service<posix_time::ptime, time_traits<posix_time::ptime>>> deadline_timer; + } +} + +namespace Trinity +{ + class AsioStrand; +} + +#endif // AsioHacksFwd_h__ diff --git a/src/common/Utilities/AsioHacksImpl.h b/src/common/Utilities/AsioHacksImpl.h new file mode 100644 index 00000000000..86888927a72 --- /dev/null +++ b/src/common/Utilities/AsioHacksImpl.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef AsioHacksImpl_h__ +#define AsioHacksImpl_h__ + +#include <boost/asio/strand.hpp> + +namespace Trinity +{ + class AsioStrand : public boost::asio::io_service::strand + { + public: + AsioStrand(boost::asio::io_service& io_service) : boost::asio::io_service::strand(io_service) { } + }; +} + +#endif // AsioHacksImpl_h__ diff --git a/src/common/Utilities/EventMap.cpp b/src/common/Utilities/EventMap.cpp index d4ea70174c4..e1c46d0ae65 100644 --- a/src/common/Utilities/EventMap.cpp +++ b/src/common/Utilities/EventMap.cpp @@ -16,6 +16,7 @@ */ #include "EventMap.h" +#include "Random.h" void EventMap::Reset() { @@ -32,6 +33,11 @@ void EventMap::SetPhase(uint8 phase) _phase = uint8(1 << (phase - 1)); } +void EventMap::ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/) +{ + ScheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); +} + void EventMap::ScheduleEvent(uint32 eventId, uint32 time, uint32 group /*= 0*/, uint8 phase /*= 0*/) { if (group && group <= 8) @@ -43,6 +49,16 @@ void EventMap::ScheduleEvent(uint32 eventId, uint32 time, uint32 group /*= 0*/, _eventMap.insert(EventStore::value_type(_time + time, eventId)); } +void EventMap::RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/) +{ + RescheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); +} + +void EventMap::Repeat(uint32 minTime, uint32 maxTime) +{ + Repeat(urand(minTime, maxTime)); +} + uint32 EventMap::ExecuteEvent() { while (!Empty()) diff --git a/src/common/Utilities/EventMap.h b/src/common/Utilities/EventMap.h index edcc7a64455..22899ea6b60 100644 --- a/src/common/Utilities/EventMap.h +++ b/src/common/Utilities/EventMap.h @@ -18,9 +18,9 @@ #ifndef _EVENT_MAP_H_ #define _EVENT_MAP_H_ -#include "Common.h" +#include "Define.h" #include "Duration.h" -#include "Util.h" +#include <map> class TC_COMMON_API EventMap { @@ -134,10 +134,7 @@ public: * @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group. * @param phase The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases. */ - void ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0) - { - ScheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); - } + void ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0); /** * @name ScheduleEvent @@ -171,10 +168,7 @@ public: * @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group. * @param phase The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases. */ - void RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0) - { - RescheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); - } + void RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0); /** * @name RescheduleEvent @@ -227,10 +221,7 @@ public: * @param minTime Minimum time until the event occurs. * @param maxTime Maximum time until the event occurs. */ - void Repeat(uint32 minTime, uint32 maxTime) - { - Repeat(urand(minTime, maxTime)); - } + void Repeat(uint32 minTime, uint32 maxTime); /** * @name ExecuteEvent diff --git a/src/common/Utilities/EventProcessor.cpp b/src/common/Utilities/EventProcessor.cpp index 039ca37b9d7..0149f222da8 100644 --- a/src/common/Utilities/EventProcessor.cpp +++ b/src/common/Utilities/EventProcessor.cpp @@ -44,7 +44,7 @@ void EventProcessor::Update(uint32 p_time) m_time += p_time; // main event loop - EventList::iterator i; + std::multimap<uint64, BasicEvent*>::iterator i; while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time) { // get and remove event from queue diff --git a/src/common/Utilities/EventProcessor.h b/src/common/Utilities/EventProcessor.h index 2f65957581c..9a356b0e3f5 100644 --- a/src/common/Utilities/EventProcessor.h +++ b/src/common/Utilities/EventProcessor.h @@ -68,8 +68,6 @@ class TC_COMMON_API BasicEvent uint64 m_execTime; // planned time of next execution, filled by event handler }; -typedef std::multimap<uint64, BasicEvent*> EventList; - class TC_COMMON_API EventProcessor { public: @@ -84,7 +82,7 @@ class TC_COMMON_API EventProcessor protected: uint64 m_time; - EventList m_events; + std::multimap<uint64, BasicEvent*> m_events; }; #endif diff --git a/src/common/Utilities/Hash.h b/src/common/Utilities/Hash.h new file mode 100644 index 00000000000..fe2047f7024 --- /dev/null +++ b/src/common/Utilities/Hash.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TrinityCore_Hash_h__ +#define TrinityCore_Hash_h__ + +#include <functional> +#include <utility> + +namespace Trinity +{ + template<typename T> + inline void hash_combine(std::size_t& seed, T const& val) + { + seed ^= std::hash<T>()(val) + 0x9E3779B9 + (seed << 6) + (seed >> 2); + } +} + + //! Hash implementation for std::pair to allow using pairs in unordered_set or as key for unordered_map + //! Individual types used in pair must be hashable by std::hash +namespace std +{ + template<class K, class V> + struct hash<std::pair<K, V>> + { + public: + size_t operator()(std::pair<K, V> const& p) const + { + size_t hashVal = 0; + Trinity::hash_combine(hashVal, p.first); + Trinity::hash_combine(hashVal, p.second); + return hashVal; + } + }; +} + +#endif // TrinityCore_Hash_h__ diff --git a/src/common/Utilities/Optional.h b/src/common/Utilities/Optional.h new file mode 100644 index 00000000000..b46f5cba5b5 --- /dev/null +++ b/src/common/Utilities/Optional.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TrinityCore_Optional_h__ +#define TrinityCore_Optional_h__ + +#include "OptionalFwd.h" +#include <boost/optional.hpp> +#include <boost/utility/in_place_factory.hpp> + +#endif // TrinityCore_Optional_h__ diff --git a/src/common/Utilities/OptionalFwd.h b/src/common/Utilities/OptionalFwd.h new file mode 100644 index 00000000000..d6b7a48f66e --- /dev/null +++ b/src/common/Utilities/OptionalFwd.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef OptionalFwd_h__ +#define OptionalFwd_h__ + +namespace boost +{ + template <class T> + class optional; +} + +//! Optional helper class to wrap optional values within. +template <class T> +using Optional = boost::optional<T>; + +#endif // OptionalFwd_h__ diff --git a/src/common/Utilities/Random.cpp b/src/common/Utilities/Random.cpp index 3a0cdedcf4b..890ba1b8fce 100644 --- a/src/common/Utilities/Random.cpp +++ b/src/common/Utilities/Random.cpp @@ -16,7 +16,6 @@ */ #include "Random.h" -#include "Common.h" #include "Errors.h" #include "SFMT.h" #include <boost/thread/tss.hpp> @@ -53,8 +52,8 @@ uint32 urand(uint32 min, uint32 max) uint32 urandms(uint32 min, uint32 max) { ASSERT(max >= min); - ASSERT(INT_MAX / IN_MILLISECONDS >= max); - return GetRng()->URandom(min * IN_MILLISECONDS, max * IN_MILLISECONDS); + ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max); + return GetRng()->URandom(min * Milliseconds::period::den, max * Milliseconds::period::den); } float frand(float min, float max) diff --git a/src/common/Utilities/Random.h b/src/common/Utilities/Random.h index ded0a6c6e5c..d200282093a 100644 --- a/src/common/Utilities/Random.h +++ b/src/common/Utilities/Random.h @@ -21,7 +21,6 @@ #include "Define.h" #include "Duration.h" #include <limits> -#include <random> /* Return a random number in the range min..max. */ TC_COMMON_API int32 irand(int32 min, int32 max); diff --git a/src/common/Utilities/StartProcess.cpp b/src/common/Utilities/StartProcess.cpp index 6f4676232ca..dc25cde3767 100644 --- a/src/common/Utilities/StartProcess.cpp +++ b/src/common/Utilities/StartProcess.cpp @@ -16,27 +16,20 @@ */ #include "StartProcess.h" - -#include <atomic> -#include <thread> -#include <functional> +#include "Errors.h" +#include "Log.h" +#include "Optional.h" #include <boost/algorithm/string/join.hpp> -#include <boost/iostreams/stream.hpp> #include <boost/iostreams/copy.hpp> -#include <boost/iostreams/concepts.hpp> -#include <boost/iostreams/device/file_descriptor.hpp> #include <boost/process.hpp> -#include <boost/system/system_error.hpp> - -#include "Common.h" -#include "Log.h" using namespace boost::process; using namespace boost::process::initializers; using namespace boost::iostreams; -namespace Trinity { +namespace Trinity +{ template<typename T> class TCLogSink @@ -51,7 +44,7 @@ public: TCLogSink(T callback) : callback_(std::move(callback)) { } - std::streamsize write(const char* str, std::streamsize size) + std::streamsize write(char const* str, std::streamsize size) { callback_(std::string(str, size)); return size; @@ -250,19 +243,15 @@ std::shared_ptr<AsyncProcessResult> return handle; } -Optional<std::string> SearchExecutableInPath(std::string const& filename) +std::string SearchExecutableInPath(std::string const& filename) { try { - auto result = search_path(filename); - if (result.empty()) - return boost::none; - else - return result; + return search_path(filename); } catch (...) { - return boost::none; + return ""; } } diff --git a/src/common/Utilities/StartProcess.h b/src/common/Utilities/StartProcess.h index 120c4f26ea6..2f8f6ffdec5 100644 --- a/src/common/Utilities/StartProcess.h +++ b/src/common/Utilities/StartProcess.h @@ -18,11 +18,14 @@ #ifndef Process_h__ #define Process_h__ +#include "Define.h" #include <future> #include <memory> -#include "Common.h" +#include <vector> +#include <string> -namespace Trinity { +namespace Trinity +{ /// Starts a process with the given arguments and parameters and will block /// until the process is finished. @@ -59,8 +62,8 @@ TC_COMMON_API std::shared_ptr<AsyncProcessResult> bool secure = false); /// Searches for the given executable in the PATH variable -/// and returns a present optional when it was found. -TC_COMMON_API Optional<std::string> SearchExecutableInPath(std::string const& filename); +/// and returns a non-empty string when it was found. +TC_COMMON_API std::string SearchExecutableInPath(std::string const& filename); } // namespace Trinity diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h index acda4b41c9f..12eeb2a6d14 100644 --- a/src/common/Utilities/StringFormat.h +++ b/src/common/Utilities/StringFormat.h @@ -31,7 +31,7 @@ namespace Trinity } /// Returns true if the given char pointer is null. - inline bool IsFormatEmptyOrNull(const char* fmt) + inline bool IsFormatEmptyOrNull(char const* fmt) { return fmt == nullptr; } diff --git a/src/common/Utilities/TaskScheduler.cpp b/src/common/Utilities/TaskScheduler.cpp index 730ef8d4b89..a9dcf5dc09b 100644 --- a/src/common/Utilities/TaskScheduler.cpp +++ b/src/common/Utilities/TaskScheduler.cpp @@ -16,6 +16,7 @@ */ #include "TaskScheduler.h" +#include "Errors.h" TaskScheduler& TaskScheduler::ClearValidator() { diff --git a/src/common/Utilities/TaskScheduler.h b/src/common/Utilities/TaskScheduler.h index d6c399639bc..0213fb90f9b 100644 --- a/src/common/Utilities/TaskScheduler.h +++ b/src/common/Utilities/TaskScheduler.h @@ -18,6 +18,9 @@ #ifndef _TASK_SCHEDULER_H_ #define _TASK_SCHEDULER_H_ +#include "Duration.h" +#include "Optional.h" +#include "Random.h" #include <algorithm> #include <chrono> #include <vector> @@ -26,11 +29,6 @@ #include <utility> #include <set> -#include <boost/optional.hpp> - -#include "Util.h" -#include "Duration.h" - class TaskContext; /// The TaskScheduler class provides the ability to schedule std::function's in the near future. @@ -73,13 +71,13 @@ class TC_COMMON_API TaskScheduler timepoint_t _end; duration_t _duration; - boost::optional<group_t> _group; + Optional<group_t> _group; repeated_t _repeated; task_handler_t _task; public: // All Argument construct - Task(timepoint_t const& end, duration_t const& duration, boost::optional<group_t> const& group, + Task(timepoint_t const& end, duration_t const& duration, Optional<group_t> const& group, repeated_t const repeated, task_handler_t const& task) : _end(end), _duration(duration), _group(group), _repeated(repeated), _task(task) { } diff --git a/src/common/Utilities/Timer.h b/src/common/Utilities/Timer.h index e774808bcb0..30a8873102c 100644 --- a/src/common/Utilities/Timer.h +++ b/src/common/Utilities/Timer.h @@ -19,6 +19,7 @@ #ifndef TRINITY_TIMER_H #define TRINITY_TIMER_H +#include "Define.h" #include <chrono> inline uint32 getMSTime() diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 58b02addc37..01df50a03de 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -18,11 +18,12 @@ #include "Util.h" #include "Common.h" -#include "CompilerDefs.h" -#include "utf8.h" -#include "Errors.h" // for ASSERT -#include <stdarg.h> -#include <boost/algorithm/string/case_conv.hpp> +#include <boost/asio/ip/address.hpp> +#include <utf8.h> +#include <algorithm> +#include <sstream> +#include <cstdarg> +#include <ctime> #if TRINITY_COMPILER == TRINITY_COMPILER_GNU #include <sys/socket.h> @@ -100,7 +101,7 @@ void stripLineInvisibleChars(std::string &str) } #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) -struct tm* localtime_r(const time_t* time, struct tm *result) +struct tm* localtime_r(time_t const* time, struct tm *result) { localtime_s(result, time); return result; @@ -130,7 +131,7 @@ std::string secsToTimeString(uint64 timeInSecs, bool shortText, bool hoursOnly) return ss.str(); } -int32 MoneyStringToMoney(const std::string& moneyString) +int32 MoneyStringToMoney(std::string const& moneyString) { int32 money = 0; @@ -149,7 +150,7 @@ int32 MoneyStringToMoney(const std::string& moneyString) if (gCount + sCount + cCount != 1) return 0; - uint32 amount = atoi(*itr); + uint32 amount = strtoul(*itr, nullptr, 10); if (gCount == 1) money += amount * 100 * 100; else if (sCount == 1) @@ -161,7 +162,7 @@ int32 MoneyStringToMoney(const std::string& moneyString) return money; } -uint32 TimeStringToSecs(const std::string& timestring) +uint32 TimeStringToSecs(std::string const& timestring) { uint32 secs = 0; uint32 buffer = 0; @@ -171,8 +172,8 @@ uint32 TimeStringToSecs(const std::string& timestring) { if (isdigit(*itr)) { - buffer*=10; - buffer+= (*itr)-'0'; + buffer *= 10; + buffer += (*itr) - '0'; } else { @@ -184,9 +185,9 @@ uint32 TimeStringToSecs(const std::string& timestring) case 's': multiplier = 1; break; default : return 0; //bad format } - buffer*=multiplier; - secs+=buffer; - buffer=0; + buffer *= multiplier; + secs += buffer; + buffer = 0; } } @@ -214,16 +215,16 @@ bool IsIPAddress(char const* ipaddress) if (!ipaddress) return false; - // Let the big boys do it. - // Drawback: all valid ip address formats are recognized e.g.: 12.23, 121234, 0xABCD) - return inet_addr(ipaddress) != INADDR_NONE; + boost::system::error_code error; + boost::asio::ip::address::from_string(ipaddress, error); + return !error; } /// create PID file uint32 CreatePIDFile(std::string const& filename) { FILE* pid_file = fopen(filename.c_str(), "w"); - if (pid_file == NULL) + if (pid_file == nullptr) return 0; uint32 pid = GetPID(); @@ -374,6 +375,16 @@ bool WStrToUtf8(std::wstring const& wstr, std::string& utf8str) typedef wchar_t const* const* wstrlist; +void wstrToUpper(std::wstring& str) +{ + std::transform(str.begin(), str.end(), str.begin(), wcharToUpper); +} + +void wstrToLower(std::wstring& str) +{ + std::transform(str.begin(), str.end(), str.begin(), wcharToLower); +} + std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension) { // supported only Cyrillic cases @@ -400,12 +411,12 @@ std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension) static wchar_t const j_End[] = { wchar_t(1), wchar_t(0x0439), wchar_t(0x0000)}; static wchar_t const* const dropEnds[6][8] = { - { &a_End[1], &o_End[1], &ya_End[1], &ie_End[1], &soft_End[1], &j_End[1], NULL, NULL }, - { &a_End[1], &ya_End[1], &yeru_End[1], &i_End[1], NULL, NULL, NULL, NULL }, - { &ie_End[1], &u_End[1], &yu_End[1], &i_End[1], NULL, NULL, NULL, NULL }, - { &u_End[1], &yu_End[1], &o_End[1], &ie_End[1], &soft_End[1], &ya_End[1], &a_End[1], NULL }, - { &oj_End[1], &io_j_End[1], &ie_j_End[1], &o_m_End[1], &io_m_End[1], &ie_m_End[1], &yu_End[1], NULL }, - { &ie_End[1], &i_End[1], NULL, NULL, NULL, NULL, NULL, NULL } + { &a_End[1], &o_End[1], &ya_End[1], &ie_End[1], &soft_End[1], &j_End[1], nullptr, nullptr }, + { &a_End[1], &ya_End[1], &yeru_End[1], &i_End[1], nullptr, nullptr, nullptr, nullptr }, + { &ie_End[1], &u_End[1], &yu_End[1], &i_End[1], nullptr, nullptr, nullptr, nullptr }, + { &u_End[1], &yu_End[1], &o_End[1], &ie_End[1], &soft_End[1], &ya_End[1], &a_End[1], nullptr }, + { &oj_End[1], &io_j_End[1], &ie_j_End[1], &o_m_End[1], &io_m_End[1], &ie_m_End[1], &yu_End[1], nullptr }, + { &ie_End[1], &i_End[1], nullptr, nullptr, nullptr, nullptr, nullptr, nullptr } }; for (wchar_t const* const* itr = &dropEnds[declension][0]; *itr; ++itr) @@ -552,12 +563,13 @@ void HexStrToByteArray(std::string const& str, uint8* out, bool reverse /*= fals for (int32 i = init; i != end; i += 2 * op) { char buffer[3] = { str[i], str[i + 1], '\0' }; - out[j++] = uint8(strtoul(buffer, NULL, 16)); + out[j++] = uint8(strtoul(buffer, nullptr, 16)); } } bool StringToBool(std::string const& str) { - std::string lowerStr = boost::algorithm::to_lower_copy(str); + std::string lowerStr = str; + std::transform(str.begin(), str.end(), lowerStr.begin(), ::tolower); return lowerStr == "1" || lowerStr == "true" || lowerStr == "yes"; } diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index 92a2f601c3f..458c783152c 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -21,24 +21,10 @@ #include "Define.h" #include "Errors.h" -#include "Random.h" -#include <algorithm> #include <string> +#include <sstream> #include <vector> -#include <list> -#include <map> -#include <ctime> - -// Searcher for map of structs -template<typename T, class S> struct Finder -{ - T val_; - T S::* idMember_; - - Finder(T val, T S::* idMember) : val_(val), idMember_(idMember) {} - bool operator()(const std::pair<int, S> &obj) { return obj.second.*idMember_ == val_; } -}; class TC_COMMON_API Tokenizer { @@ -70,12 +56,12 @@ private: TC_COMMON_API void stripLineInvisibleChars(std::string &src); -TC_COMMON_API int32 MoneyStringToMoney(const std::string& moneyString); +TC_COMMON_API int32 MoneyStringToMoney(std::string const& moneyString); -TC_COMMON_API struct tm* localtime_r(const time_t* time, struct tm *result); +TC_COMMON_API struct tm* localtime_r(time_t const* time, struct tm *result); TC_COMMON_API std::string secsToTimeString(uint64 timeInSecs, bool shortText = false, bool hoursOnly = false); -TC_COMMON_API uint32 TimeStringToSecs(const std::string& timestring); +TC_COMMON_API uint32 TimeStringToSecs(std::string const& timestring); TC_COMMON_API std::string TimeToTimestampStr(time_t t); // Percentage calculation @@ -289,15 +275,8 @@ inline wchar_t wcharToLower(wchar_t wchar) return wchar; } -inline void wstrToUpper(std::wstring& str) -{ - std::transform( str.begin(), str.end(), str.begin(), wcharToUpper ); -} - -inline void wstrToLower(std::wstring& str) -{ - std::transform( str.begin(), str.end(), str.begin(), wcharToLower ); -} +TC_COMMON_API void wstrToUpper(std::wstring& str); +TC_COMMON_API void wstrToLower(std::wstring& str); TC_COMMON_API std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension); @@ -382,7 +361,7 @@ public: part[2] = p3; } - inline bool operator <(const flag96 &right) const + inline bool operator<(flag96 const& right) const { for (uint8 i = 3; i > 0; --i) { @@ -394,7 +373,7 @@ public: return false; } - inline bool operator ==(const flag96 &right) const + inline bool operator==(flag96 const& right) const { return ( @@ -404,12 +383,12 @@ public: ); } - inline bool operator !=(const flag96 &right) const + inline bool operator!=(flag96 const& right) const { - return !this->operator ==(right); + return !(*this == right); } - inline flag96 & operator =(const flag96 &right) + inline flag96& operator=(flag96 const& right) { part[0] = right.part[0]; part[1] = right.part[1]; @@ -417,13 +396,12 @@ public: return *this; } - inline flag96 operator &(const flag96 &right) const + inline flag96 operator&(flag96 const& right) const { - return flag96(part[0] & right.part[0], part[1] & right.part[1], - part[2] & right.part[2]); + return flag96(part[0] & right.part[0], part[1] & right.part[1], part[2] & right.part[2]); } - inline flag96 & operator &=(const flag96 &right) + inline flag96& operator&=(flag96 const& right) { part[0] &= right.part[0]; part[1] &= right.part[1]; @@ -431,13 +409,12 @@ public: return *this; } - inline flag96 operator |(const flag96 &right) const + inline flag96 operator|(flag96 const& right) const { - return flag96(part[0] | right.part[0], part[1] | right.part[1], - part[2] | right.part[2]); + return flag96(part[0] | right.part[0], part[1] | right.part[1], part[2] | right.part[2]); } - inline flag96 & operator |=(const flag96 &right) + inline flag96& operator |=(flag96 const& right) { part[0] |= right.part[0]; part[1] |= right.part[1]; @@ -445,18 +422,17 @@ public: return *this; } - inline flag96 operator ~() const + inline flag96 operator~() const { return flag96(~part[0], ~part[1], ~part[2]); } - inline flag96 operator ^(const flag96 &right) const + inline flag96 operator^(flag96 const& right) const { - return flag96(part[0] ^ right.part[0], part[1] ^ right.part[1], - part[2] ^ right.part[2]); + return flag96(part[0] ^ right.part[0], part[1] ^ right.part[1], part[2] ^ right.part[2]); } - inline flag96 & operator ^=(const flag96 &right) + inline flag96& operator^=(flag96 const& right) { part[0] ^= right.part[0]; part[1] ^= right.part[1]; @@ -471,15 +447,15 @@ public: inline bool operator !() const { - return !this->operator bool(); + return !(bool(*this)); } - inline uint32 & operator [](uint8 el) + inline uint32& operator[](uint8 el) { return part[el]; } - inline const uint32 & operator [](uint8 el) const + inline uint32 const& operator [](uint8 el) const { return part[el]; } |