aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/Utilities
diff options
context:
space:
mode:
authorXTZGZoReX <none@none>2010-06-06 23:18:57 +0200
committerXTZGZoReX <none@none>2010-06-06 23:18:57 +0200
commit57f64f1f5a8eccff388211e0d08bf9d630d48ada (patch)
treed2136a7d198b667d9cbda7c845da112c4aa577db /src/server/shared/Utilities
parent51121d0769619b1690fb140079244c121a39afc8 (diff)
* Restructuring shared.
--HG-- branch : trunk
Diffstat (limited to 'src/server/shared/Utilities')
-rw-r--r--src/server/shared/Utilities/ProgressBar.cpp85
-rw-r--r--src/server/shared/Utilities/ProgressBar.h42
-rw-r--r--src/server/shared/Utilities/ServiceWin32.cpp263
-rw-r--r--src/server/shared/Utilities/ServiceWin32.h31
-rw-r--r--src/server/shared/Utilities/SignalHandler.h43
-rw-r--r--src/server/shared/Utilities/Timer.h215
-rw-r--r--src/server/shared/Utilities/Util.cpp468
-rw-r--r--src/server/shared/Utilities/Util.h536
8 files changed, 1683 insertions, 0 deletions
diff --git a/src/server/shared/Utilities/ProgressBar.cpp b/src/server/shared/Utilities/ProgressBar.cpp
new file mode 100644
index 00000000000..c24d9f86f6b
--- /dev/null
+++ b/src/server/shared/Utilities/ProgressBar.cpp
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+
+#include "ProgressBar.h"
+
+char const* const barGoLink::empty = " ";
+#ifdef _WIN32
+char const* const barGoLink::full = "\x3D";
+#else
+char const* const barGoLink::full = "*";
+#endif
+
+barGoLink::~barGoLink()
+{
+ printf( "\n" );
+ fflush(stdout);
+}
+
+barGoLink::barGoLink( int row_count )
+{
+ rec_no = 0;
+ rec_pos = 0;
+ indic_len = 50;
+ num_rec = row_count;
+ #ifdef _WIN32
+ printf( "\x3D" );
+ #else
+ printf( "[" );
+ #endif
+ for (int i = 0; i < indic_len; i++ ) printf( empty );
+ #ifdef _WIN32
+ printf( "\x3D 0%%\r\x3D" );
+ #else
+ printf( "] 0%%\r[" );
+ #endif
+ fflush(stdout);
+}
+
+void barGoLink::step( void )
+{
+ int i, n;
+
+ if ( num_rec == 0 ) return;
+ ++rec_no;
+ n = rec_no * indic_len / num_rec;
+ if ( n != rec_pos )
+ {
+ #ifdef _WIN32
+ printf( "\r\x3D" );
+ #else
+ printf( "\r[" );
+ #endif
+ for (i = 0; i < n; i++ ) printf( full );
+ for (; i < indic_len; i++ ) printf( empty );
+ float percent = (((float)n/(float)indic_len)*100);
+ #ifdef _WIN32
+ printf( "\x3D %i%% \r\x3D", (int)percent);
+ #else
+ printf( "] %i%% \r[", (int)percent);
+ #endif
+ fflush(stdout);
+
+ rec_pos = n;
+ }
+}
+
diff --git a/src/server/shared/Utilities/ProgressBar.h b/src/server/shared/Utilities/ProgressBar.h
new file mode 100644
index 00000000000..e7565590278
--- /dev/null
+++ b/src/server/shared/Utilities/ProgressBar.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#ifndef TRINITYCORE_PROGRESSBAR_H
+#define TRINITYCORE_PROGRESSBAR_H
+
+#include "Platform/Define.h"
+
+class barGoLink
+{
+ static char const * const empty;
+ static char const * const full;
+
+ int rec_no;
+ int rec_pos;
+ int num_rec;
+ int indic_len;
+
+ public:
+
+ void step( void );
+ barGoLink( int );
+ ~barGoLink();
+};
+#endif
+
diff --git a/src/server/shared/Utilities/ServiceWin32.cpp b/src/server/shared/Utilities/ServiceWin32.cpp
new file mode 100644
index 00000000000..2c1df2ff483
--- /dev/null
+++ b/src/server/shared/Utilities/ServiceWin32.cpp
@@ -0,0 +1,263 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifdef WIN32
+
+#include "Common.h"
+#include "Log.h"
+#include <cstring>
+#include <windows.h>
+#include <winsvc.h>
+
+#if !defined(WINADVAPI)
+#if !defined(_ADVAPI32_)
+#define WINADVAPI DECLSPEC_IMPORT
+#else
+#define WINADVAPI
+#endif
+#endif
+
+extern int main(int argc, char ** argv);
+extern char serviceLongName[];
+extern char serviceName[];
+extern char serviceDescription[];
+
+extern int m_ServiceStatus;
+
+SERVICE_STATUS serviceStatus;
+
+SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
+
+typedef WINADVAPI BOOL (WINAPI *CSD_T)(SC_HANDLE, DWORD, LPCVOID);
+
+bool WinServiceInstall()
+{
+ CSD_T ChangeService_Config2;
+ HMODULE advapi32;
+ SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
+
+ if (serviceControlManager)
+ {
+ char path[_MAX_PATH + 10];
+ if (GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0)
+ {
+ SC_HANDLE service;
+ std::strcat(path, " --service");
+ service = CreateService(serviceControlManager,
+ serviceName, // name of service
+ serviceLongName, // service name to display
+ SERVICE_ALL_ACCESS, // desired access
+ // service type
+ SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
+ SERVICE_AUTO_START, // start type
+ SERVICE_ERROR_IGNORE, // error control type
+ path, // service's binary
+ 0, // no load ordering group
+ 0, // no tag identifier
+ 0, // no dependencies
+ 0, // LocalSystem account
+ 0); // no password
+ if (service)
+ {
+ advapi32 = GetModuleHandle("ADVAPI32.DLL");
+ if(!advapi32)
+ {
+ CloseServiceHandle(service);
+ CloseServiceHandle(serviceControlManager);
+ return false;
+ }
+
+ ChangeService_Config2 = (CSD_T) GetProcAddress(advapi32, "ChangeServiceConfig2A");
+ if (!ChangeService_Config2)
+ {
+ CloseServiceHandle(service);
+ CloseServiceHandle(serviceControlManager);
+ return false;
+ }
+
+ SERVICE_DESCRIPTION sdBuf;
+ sdBuf.lpDescription = serviceDescription;
+ ChangeService_Config2(
+ service, // handle to service
+ SERVICE_CONFIG_DESCRIPTION, // change: description
+ &sdBuf); // new data
+
+ SC_ACTION _action[1];
+ _action[0].Type = SC_ACTION_RESTART;
+ _action[0].Delay = 10000;
+ SERVICE_FAILURE_ACTIONS sfa;
+ ZeroMemory(&sfa, sizeof(SERVICE_FAILURE_ACTIONS));
+ sfa.lpsaActions = _action;
+ sfa.cActions = 1;
+ sfa.dwResetPeriod =INFINITE;
+ ChangeService_Config2(
+ service, // handle to service
+ SERVICE_CONFIG_FAILURE_ACTIONS, // information level
+ &sfa); // new data
+
+ CloseServiceHandle(service);
+
+ }
+ }
+ CloseServiceHandle(serviceControlManager);
+ }
+ return true;
+}
+
+bool WinServiceUninstall()
+{
+ SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
+
+ if (serviceControlManager)
+ {
+ SC_HANDLE service = OpenService(serviceControlManager,
+ serviceName, SERVICE_QUERY_STATUS | DELETE);
+ if (service)
+ {
+ SERVICE_STATUS serviceStatus2;
+ if (QueryServiceStatus(service, &serviceStatus2))
+ {
+ if (serviceStatus2.dwCurrentState == SERVICE_STOPPED)
+ DeleteService(service);
+ }
+ CloseServiceHandle(service);
+ }
+
+ CloseServiceHandle(serviceControlManager);
+ }
+ return true;
+}
+
+void WINAPI ServiceControlHandler(DWORD controlCode)
+{
+ switch (controlCode)
+ {
+ case SERVICE_CONTROL_INTERROGATE:
+ break;
+
+ case SERVICE_CONTROL_SHUTDOWN:
+ case SERVICE_CONTROL_STOP:
+ serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
+ SetServiceStatus(serviceStatusHandle, &serviceStatus);
+
+ m_ServiceStatus = 0;
+ return;
+
+ case SERVICE_CONTROL_PAUSE:
+ m_ServiceStatus = 2;
+ serviceStatus.dwCurrentState = SERVICE_PAUSED;
+ SetServiceStatus(serviceStatusHandle, &serviceStatus);
+ break;
+
+ case SERVICE_CONTROL_CONTINUE:
+ serviceStatus.dwCurrentState = SERVICE_RUNNING;
+ SetServiceStatus(serviceStatusHandle, &serviceStatus);
+ m_ServiceStatus = 1;
+ break;
+
+ default:
+ if ( controlCode >= 128 && controlCode <= 255 )
+ // user defined control code
+ break;
+ else
+ // unrecognized control code
+ break;
+ }
+
+ SetServiceStatus(serviceStatusHandle, &serviceStatus);
+}
+
+void WINAPI ServiceMain(DWORD argc, char *argv[])
+{
+ // initialise service status
+ serviceStatus.dwServiceType = SERVICE_WIN32;
+ serviceStatus.dwCurrentState = SERVICE_START_PENDING;
+ serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
+ serviceStatus.dwWin32ExitCode = NO_ERROR;
+ serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
+ serviceStatus.dwCheckPoint = 0;
+ serviceStatus.dwWaitHint = 0;
+
+ serviceStatusHandle = RegisterServiceCtrlHandler(serviceName, ServiceControlHandler);
+
+ if ( serviceStatusHandle )
+ {
+ char path[_MAX_PATH + 1];
+ unsigned int i, last_slash = 0;
+
+ GetModuleFileName(0, path, sizeof(path)/sizeof(path[0]));
+
+ for (i = 0; i < std::strlen(path); i++)
+ {
+ if (path[i] == '\\') last_slash = i;
+ }
+
+ path[last_slash] = 0;
+
+ // service is starting
+ serviceStatus.dwCurrentState = SERVICE_START_PENDING;
+ SetServiceStatus(serviceStatusHandle, &serviceStatus);
+
+ // do initialisation here
+ SetCurrentDirectory(path);
+
+ // running
+ serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
+ serviceStatus.dwCurrentState = SERVICE_RUNNING;
+ SetServiceStatus( serviceStatusHandle, &serviceStatus );
+
+ ////////////////////////
+ // service main cycle //
+ ////////////////////////
+
+ m_ServiceStatus = 1;
+ argc = 1;
+ main(argc , argv);
+
+ // service was stopped
+ serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
+ SetServiceStatus(serviceStatusHandle, &serviceStatus);
+
+ // do cleanup here
+
+ // service is now stopped
+ serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
+ serviceStatus.dwCurrentState = SERVICE_STOPPED;
+ SetServiceStatus(serviceStatusHandle, &serviceStatus);
+ }
+}
+
+bool WinServiceRun()
+{
+ SERVICE_TABLE_ENTRY serviceTable[] =
+ {
+ { serviceName, ServiceMain },
+ { 0, 0 }
+ };
+
+ if (!StartServiceCtrlDispatcher(serviceTable))
+ {
+ sLog.outError("StartService Failed. Error [%u]", ::GetLastError());
+ return false;
+ }
+ return true;
+}
+#endif
+
diff --git a/src/server/shared/Utilities/ServiceWin32.h b/src/server/shared/Utilities/ServiceWin32.h
new file mode 100644
index 00000000000..ddac785a0fb
--- /dev/null
+++ b/src/server/shared/Utilities/ServiceWin32.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifdef WIN32
+#ifndef _WIN32_SERVICE_
+#define _WIN32_SERVICE_
+
+bool WinServiceInstall();
+bool WinServiceUninstall();
+bool WinServiceRun();
+
+#endif // _WIN32_SERVICE_
+#endif // WIN32
+
diff --git a/src/server/shared/Utilities/SignalHandler.h b/src/server/shared/Utilities/SignalHandler.h
new file mode 100644
index 00000000000..0c4d8143c41
--- /dev/null
+++ b/src/server/shared/Utilities/SignalHandler.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __SIGNAL_HANDLER_H__
+#define __SIGNAL_HANDLER_H__
+
+#include <ace/Event_Handler.h>
+
+namespace Trinity
+{
+
+/// Handle termination signals
+class SignalHandler : public ACE_Event_Handler
+{
+ public:
+ int handle_signal(int SigNum, siginfo_t* = NULL, ucontext_t* = NULL)
+ {
+ HandleSignal(SigNum);
+ return 0;
+ }
+ virtual void HandleSignal(int SigNum) {};
+};
+
+}
+
+#endif /* __SIGNAL_HANDLER_H__ */
diff --git a/src/server/shared/Utilities/Timer.h b/src/server/shared/Utilities/Timer.h
new file mode 100644
index 00000000000..3e0a369b655
--- /dev/null
+++ b/src/server/shared/Utilities/Timer.h
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef TRINITY_TIMER_H
+#define TRINITY_TIMER_H
+
+#include "Platform/CompilerDefs.h"
+
+#if PLATFORM == PLATFORM_WINDOWS
+# include <ace/config-all.h>
+# include <mmsystem.h>
+# include <time.h>
+#else
+# if defined(__APPLE_CC__)
+# include <time.h>
+# endif
+# include <sys/time.h>
+# include <sys/timeb.h>
+#endif
+
+#if PLATFORM == PLATFORM_WINDOWS
+inline uint32 getMSTime() { return GetTickCount(); }
+#else
+inline uint32 getMSTime()
+{
+ struct timeval tv;
+ struct timezone tz;
+ gettimeofday( &tv, &tz );
+ return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
+}
+#endif
+
+inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
+{
+ // getMSTime() have limited data range and this is case when it overflow in this tick
+ if (oldMSTime > newMSTime)
+ return (0xFFFFFFFF - oldMSTime) + newMSTime;
+ else
+ return newMSTime - oldMSTime;
+}
+
+struct IntervalTimer
+{
+ public:
+
+ IntervalTimer()
+ : _interval(0), _current(0)
+ {
+ }
+
+ void Update(time_t diff)
+ {
+ _current += diff;
+ if (_current < 0)
+ _current = 0;
+ }
+
+ bool Passed()
+ {
+ return _current >= _interval;
+ }
+
+ void Reset()
+ {
+ if (_current >= _interval)
+ _current -= _interval;
+ }
+
+ void SetCurrent(time_t current)
+ {
+ _current = current;
+ }
+
+ void SetInterval(time_t interval)
+ {
+ _interval = interval;
+ }
+
+ time_t GetInterval() const
+ {
+ return _interval;
+ }
+
+ time_t GetCurrent() const
+ {
+ return _current;
+ }
+
+ private:
+
+ time_t _interval;
+ time_t _current;
+};
+
+struct TimeTracker
+{
+ public:
+
+ TimeTracker(time_t expiry)
+ : i_expiryTime(expiry)
+ {
+ }
+
+ void Update(time_t diff)
+ {
+ i_expiryTime -= diff;
+ }
+
+ bool Passed() const
+ {
+ return i_expiryTime <= 0;
+ }
+
+ void Reset(time_t interval)
+ {
+ i_expiryTime = interval;
+ }
+
+ time_t GetExpiry() const
+ {
+ return i_expiryTime;
+ }
+
+ private:
+
+ time_t i_expiryTime;
+};
+
+struct TimeTrackerSmall
+{
+ public:
+
+ TimeTrackerSmall(uint32 expiry)
+ : i_expiryTime(expiry)
+ {
+ }
+
+ void Update(int32 diff)
+ {
+ i_expiryTime -= diff;
+ }
+
+ bool Passed() const
+ {
+ return i_expiryTime <= 0;
+ }
+
+ void Reset(uint32 interval)
+ {
+ i_expiryTime = interval;
+ }
+
+ int32 GetExpiry() const
+ {
+ return i_expiryTime;
+ }
+
+ private:
+
+ int32 i_expiryTime;
+};
+
+struct PeriodicTimer
+{
+ public:
+
+ PeriodicTimer(int32 period, int32 start_time)
+ : i_expireTime(start_time), i_period(period)
+ {
+ }
+
+ bool Update(const uint32 &diff)
+ {
+ if ((i_expireTime -= diff) > 0)
+ return false;
+
+ i_expireTime += i_period > diff ? i_period : diff;
+ return true;
+ }
+
+ void SetPeriodic(int32 period, int32 start_time)
+ {
+ i_expireTime = start_time;
+ i_period = period;
+ }
+
+ // Tracker interface
+ void TUpdate(int32 diff) { i_expireTime -= diff; }
+ bool TPassed() const { return i_expireTime <= 0; }
+ void TReset(int32 diff, int32 period) { i_expireTime += period > diff ? period : diff; }
+
+ private:
+
+ int32 i_period;
+ int32 i_expireTime;
+};
+
+#endif
diff --git a/src/server/shared/Utilities/Util.cpp b/src/server/shared/Utilities/Util.cpp
new file mode 100644
index 00000000000..a3c017fdbfd
--- /dev/null
+++ b/src/server/shared/Utilities/Util.cpp
@@ -0,0 +1,468 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "Util.h"
+
+#include "sockets/socket_include.h"
+#include "utf8cpp/utf8.h"
+#include "mersennetwister/MersenneTwister.h"
+#include <ace/TSS_T.h>
+
+typedef ACE_TSS<MTRand> MTRandTSS;
+static MTRandTSS mtRand;
+
+int32 irand (int32 min, int32 max)
+{
+ return int32 (mtRand->randInt (max - min)) + min;
+}
+
+uint32 urand (uint32 min, uint32 max)
+{
+ return mtRand->randInt (max - min) + min;
+}
+
+int32 rand32 ()
+{
+ return mtRand->randInt ();
+}
+
+double rand_norm(void)
+{
+ return mtRand->randExc ();
+}
+
+double rand_chance (void)
+{
+ return mtRand->randExc (100.0);
+}
+
+Tokens StrSplit(const std::string &src, const std::string &sep)
+{
+ Tokens r;
+ std::string s;
+ for (std::string::const_iterator i = src.begin(); i != src.end(); i++)
+ {
+ if (sep.find(*i) != std::string::npos)
+ {
+ if (s.length()) r.push_back(s);
+ s = "";
+ }
+ else
+ {
+ s += *i;
+ }
+ }
+ if (s.length()) r.push_back(s);
+ return r;
+}
+
+void stripLineInvisibleChars(std::string &str)
+{
+ static std::string invChars = " \t\7\n";
+
+ size_t wpos = 0;
+
+ bool space = false;
+ for (size_t pos = 0; pos < str.size(); ++pos)
+ {
+ if(invChars.find(str[pos])!=std::string::npos)
+ {
+ if(!space)
+ {
+ str[wpos++] = ' ';
+ space = true;
+ }
+ }
+ else
+ {
+ if(wpos!=pos)
+ str[wpos++] = str[pos];
+ else
+ ++wpos;
+ space = false;
+ }
+ }
+
+ if(wpos < str.size())
+ str.erase(wpos,str.size());
+ if(str.find("|TInterface")!=std::string::npos)
+ str.clear();
+
+}
+
+std::string secsToTimeString(uint32 timeInSecs, bool shortText, bool hoursOnly)
+{
+ uint32 secs = timeInSecs % MINUTE;
+ uint32 minutes = timeInSecs % HOUR / MINUTE;
+ uint32 hours = timeInSecs % DAY / HOUR;
+ uint32 days = timeInSecs / DAY;
+
+ std::ostringstream ss;
+ if(days)
+ ss << days << (shortText ? "d" : " Day(s) ");
+ if(hours || hoursOnly)
+ ss << hours << (shortText ? "h" : " Hour(s) ");
+ if(!hoursOnly)
+ {
+ if(minutes)
+ ss << minutes << (shortText ? "m" : " Minute(s) ");
+ if(secs || (!days && !hours && !minutes) )
+ ss << secs << (shortText ? "s" : " Second(s).");
+ }
+
+ return ss.str();
+}
+
+uint32 TimeStringToSecs(const std::string& timestring)
+{
+ uint32 secs = 0;
+ uint32 buffer = 0;
+ uint32 multiplier = 0;
+
+ for (std::string::const_iterator itr = timestring.begin(); itr != timestring.end(); itr++ )
+ {
+ if(isdigit(*itr))
+ {
+ buffer*=10;
+ buffer+= (*itr)-'0';
+ }
+ else
+ {
+ switch(*itr)
+ {
+ case 'd': multiplier = DAY; break;
+ case 'h': multiplier = HOUR; break;
+ case 'm': multiplier = MINUTE; break;
+ case 's': multiplier = 1; break;
+ default : return 0; //bad format
+ }
+ buffer*=multiplier;
+ secs+=buffer;
+ buffer=0;
+ }
+ }
+
+ return secs;
+}
+
+std::string TimeToTimestampStr(time_t t)
+{
+ tm* aTm = localtime(&t);
+ // YYYY year
+ // MM month (2 digits 01-12)
+ // DD day (2 digits 01-31)
+ // HH hour (2 digits 00-23)
+ // MM minutes (2 digits 00-59)
+ // SS seconds (2 digits 00-59)
+ char buf[20];
+ snprintf(buf,20,"%04d-%02d-%02d_%02d-%02d-%02d",aTm->tm_year+1900,aTm->tm_mon+1,aTm->tm_mday,aTm->tm_hour,aTm->tm_min,aTm->tm_sec);
+ return std::string(buf);
+}
+
+/// Check if the string is a valid ip address representation
+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;
+}
+
+/// create PID file
+uint32 CreatePIDFile(const std::string& filename)
+{
+ FILE * pid_file = fopen (filename.c_str(), "w" );
+ if (pid_file == NULL)
+ return 0;
+
+#ifdef WIN32
+ DWORD pid = GetCurrentProcessId();
+#else
+ pid_t pid = getpid();
+#endif
+
+ fprintf(pid_file, "%d", pid );
+ fclose(pid_file);
+
+ return (uint32)pid;
+}
+
+size_t utf8length(std::string& utf8str)
+{
+ try
+ {
+ return utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size());
+ }
+ catch(std::exception)
+ {
+ utf8str = "";
+ return 0;
+ }
+}
+
+void utf8truncate(std::string& utf8str,size_t len)
+{
+ try
+ {
+ size_t wlen = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size());
+ if(wlen <= len)
+ return;
+
+ std::wstring wstr;
+ wstr.resize(wlen);
+ utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]);
+ wstr.resize(len);
+ char* oend = utf8::utf16to8(wstr.c_str(),wstr.c_str()+wstr.size(),&utf8str[0]);
+ utf8str.resize(oend-(&utf8str[0])); // remove unused tail
+ }
+ catch(std::exception)
+ {
+ utf8str = "";
+ }
+}
+
+bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
+{
+ try
+ {
+ size_t len = utf8::distance(utf8str,utf8str+csize);
+ if(len > wsize)
+ {
+ if(wsize > 0)
+ wstr[0] = L'\0';
+ wsize = 0;
+ return false;
+ }
+
+ wsize = len;
+ utf8::utf8to16(utf8str,utf8str+csize,wstr);
+ wstr[len] = L'\0';
+ }
+ catch(std::exception)
+ {
+ if(wsize > 0)
+ wstr[0] = L'\0';
+ wsize = 0;
+ return false;
+ }
+
+ return true;
+}
+
+bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr)
+{
+ try
+ {
+ size_t len = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size());
+ wstr.resize(len);
+
+ utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]);
+ }
+ catch(std::exception)
+ {
+ wstr = L"";
+ return false;
+ }
+
+ return true;
+}
+
+bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str)
+{
+ try
+ {
+ std::string utf8str2;
+ utf8str2.resize(size*4); // allocate for most long case
+
+ char* oend = utf8::utf16to8(wstr,wstr+size,&utf8str2[0]);
+ utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail
+ utf8str = utf8str2;
+ }
+ catch(std::exception)
+ {
+ utf8str = "";
+ return false;
+ }
+
+ return true;
+}
+
+bool WStrToUtf8(std::wstring wstr, std::string& utf8str)
+{
+ try
+ {
+ std::string utf8str2;
+ utf8str2.resize(wstr.size()*4); // allocate for most long case
+
+ char* oend = utf8::utf16to8(wstr.c_str(),wstr.c_str()+wstr.size(),&utf8str2[0]);
+ utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail
+ utf8str = utf8str2;
+ }
+ catch(std::exception)
+ {
+ utf8str = "";
+ return false;
+ }
+
+ return true;
+}
+
+typedef wchar_t const* const* wstrlist;
+
+std::wstring GetMainPartOfName(std::wstring wname, uint32 declension)
+{
+ // supported only Cyrillic cases
+ if(wname.size() < 1 || !isCyrillicCharacter(wname[0]) || declension > 5)
+ return wname;
+
+ // Important: end length must be <= MAX_INTERNAL_PLAYER_NAME-MAX_PLAYER_NAME (3 currently)
+
+ static wchar_t const a_End[] = { wchar_t(1), wchar_t(0x0430),wchar_t(0x0000)};
+ static wchar_t const o_End[] = { wchar_t(1), wchar_t(0x043E),wchar_t(0x0000)};
+ static wchar_t const ya_End[] = { wchar_t(1), wchar_t(0x044F),wchar_t(0x0000)};
+ static wchar_t const ie_End[] = { wchar_t(1), wchar_t(0x0435),wchar_t(0x0000)};
+ static wchar_t const i_End[] = { wchar_t(1), wchar_t(0x0438),wchar_t(0x0000)};
+ static wchar_t const yeru_End[] = { wchar_t(1), wchar_t(0x044B),wchar_t(0x0000)};
+ static wchar_t const u_End[] = { wchar_t(1), wchar_t(0x0443),wchar_t(0x0000)};
+ static wchar_t const yu_End[] = { wchar_t(1), wchar_t(0x044E),wchar_t(0x0000)};
+ static wchar_t const oj_End[] = { wchar_t(2), wchar_t(0x043E),wchar_t(0x0439),wchar_t(0x0000)};
+ static wchar_t const ie_j_End[] = { wchar_t(2), wchar_t(0x0435),wchar_t(0x0439),wchar_t(0x0000)};
+ static wchar_t const io_j_End[] = { wchar_t(2), wchar_t(0x0451),wchar_t(0x0439),wchar_t(0x0000)};
+ static wchar_t const o_m_End[] = { wchar_t(2), wchar_t(0x043E),wchar_t(0x043C),wchar_t(0x0000)};
+ static wchar_t const io_m_End[] = { wchar_t(2), wchar_t(0x0451),wchar_t(0x043C),wchar_t(0x0000)};
+ static wchar_t const ie_m_End[] = { wchar_t(2), wchar_t(0x0435),wchar_t(0x043C),wchar_t(0x0000)};
+ static wchar_t const soft_End[] = { wchar_t(1), wchar_t(0x044C),wchar_t(0x0000)};
+ 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 }
+ };
+
+ for (wchar_t const * const* itr = &dropEnds[declension][0]; *itr; ++itr)
+ {
+ size_t len = size_t((*itr)[-1]); // get length from string size field
+
+ if(wname.substr(wname.size()-len,len)==*itr)
+ return wname.substr(0,wname.size()-len);
+ }
+
+ return wname;
+}
+
+bool utf8ToConsole(const std::string& utf8str, std::string& conStr)
+{
+#if PLATFORM == PLATFORM_WINDOWS
+ std::wstring wstr;
+ if(!Utf8toWStr(utf8str,wstr))
+ return false;
+
+ conStr.resize(wstr.size());
+ CharToOemBuffW(&wstr[0],&conStr[0],wstr.size());
+#else
+ // not implemented yet
+ conStr = utf8str;
+#endif
+
+ return true;
+}
+
+bool consoleToUtf8(const std::string& conStr,std::string& utf8str)
+{
+#if PLATFORM == PLATFORM_WINDOWS
+ std::wstring wstr;
+ wstr.resize(conStr.size());
+ OemToCharBuffW(&conStr[0],&wstr[0],conStr.size());
+
+ return WStrToUtf8(wstr,utf8str);
+#else
+ // not implemented yet
+ utf8str = conStr;
+ return true;
+#endif
+}
+
+bool Utf8FitTo(const std::string& str, std::wstring search)
+{
+ std::wstring temp;
+
+ if(!Utf8toWStr(str,temp))
+ return false;
+
+ // converting to lower case
+ wstrToLower( temp );
+
+ if(temp.find(search) == std::wstring::npos)
+ return false;
+
+ return true;
+}
+
+void utf8printf(FILE *out, const char *str, ...)
+{
+ va_list ap;
+ va_start(ap, str);
+ vutf8printf(out, str, &ap);
+ va_end(ap);
+}
+
+void vutf8printf(FILE *out, const char *str, va_list* ap)
+{
+#if PLATFORM == PLATFORM_WINDOWS
+ char temp_buf[32*1024];
+ wchar_t wtemp_buf[32*1024];
+
+ size_t temp_len = vsnprintf(temp_buf, 32*1024, str, *ap);
+
+ size_t wtemp_len = 32*1024-1;
+ Utf8toWStr(temp_buf, temp_len, wtemp_buf, wtemp_len);
+
+ CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len+1);
+ fprintf(out, temp_buf);
+#else
+ vfprintf(out, str, *ap);
+#endif
+}
+
+void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result)
+{
+ std::ostringstream ss;
+ for (uint32 i=0; i<arrayLen; ++i)
+ {
+ for (uint8 j=0; j<2; ++j)
+ {
+ unsigned char nibble = 0x0F & (bytes[i]>>((1-j)*4));
+ char encodedNibble;
+ if(nibble < 0x0A)
+ encodedNibble = '0'+nibble;
+ else
+ encodedNibble = 'A'+nibble-0x0A;
+ ss << encodedNibble;
+ }
+ }
+ result = ss.str();
+}
+
diff --git a/src/server/shared/Utilities/Util.h b/src/server/shared/Utilities/Util.h
new file mode 100644
index 00000000000..4f997725d25
--- /dev/null
+++ b/src/server/shared/Utilities/Util.h
@@ -0,0 +1,536 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _UTIL_H
+#define _UTIL_H
+
+#include "Common.h"
+
+#include <string>
+#include <vector>
+
+typedef std::vector<std::string> Tokens;
+
+Tokens StrSplit(const std::string &src, const std::string &sep);
+
+void stripLineInvisibleChars(std::string &src);
+
+std::string secsToTimeString(uint32 timeInSecs, bool shortText = false, bool hoursOnly = false);
+uint32 TimeStringToSecs(const std::string& timestring);
+std::string TimeToTimestampStr(time_t t);
+
+inline uint32 secsToTimeBitFields(time_t secs)
+{
+ tm* lt = localtime(&secs);
+ return (lt->tm_year - 100) << 24 | lt->tm_mon << 20 | (lt->tm_mday - 1) << 14 | lt->tm_wday << 11 | lt->tm_hour << 6 | lt->tm_min;
+}
+
+/* Return a random number in the range min..max; (max-min) must be smaller than 32768. */
+ int32 irand(int32 min, int32 max);
+
+/* Return a random number in the range min..max (inclusive). For reliable results, the difference
+* between max and min should be less than RAND32_MAX. */
+ uint32 urand(uint32 min, uint32 max);
+
+/* Return a random number in the range 0 .. RAND32_MAX. */
+ int32 rand32();
+
+/* Return a random double from 0.0 to 1.0 (exclusive). Floats support only 7 valid decimal digits.
+ * A double supports up to 15 valid decimal digits and is used internally (RAND32_MAX has 10 digits).
+ * With an FPU, there is usually no difference in performance between float and double. */
+ double rand_norm(void);
+
+/* Return a random double from 0.0 to 99.9999999999999. Floats support only 7 valid decimal digits.
+ * A double supports up to 15 valid decimal digits and is used internaly (RAND32_MAX has 10 digits).
+ * With an FPU, there is usually no difference in performance between float and double. */
+ double rand_chance(void);
+
+/* Return true if a random roll fits in the specified chance (range 0-100). */
+inline bool roll_chance_f(float chance)
+{
+ return chance > rand_chance();
+}
+
+/* Return true if a random roll fits in the specified chance (range 0-100). */
+inline bool roll_chance_i(int chance)
+{
+ return chance > irand(0, 99);
+}
+
+inline void ApplyModUInt32Var(uint32& var, int32 val, bool apply)
+{
+ int32 cur = var;
+ cur += (apply ? val : -val);
+ if(cur < 0)
+ cur = 0;
+ var = cur;
+}
+
+inline void ApplyModFloatVar(float& var, float val, bool apply)
+{
+ var += (apply ? val : -val);
+ if(var < 0)
+ var = 0;
+}
+
+inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
+{
+ if (val == -100.0f) // prevent set var to zero
+ val = -99.99f;
+ var *= (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val));
+}
+
+bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr);
+// in wsize==max size of buffer, out wsize==real string size
+bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize);
+inline bool Utf8toWStr(const std::string& utf8str, wchar_t* wstr, size_t& wsize)
+{
+ return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize);
+}
+
+bool WStrToUtf8(std::wstring wstr, std::string& utf8str);
+// size==real string size
+bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str);
+
+size_t utf8length(std::string& utf8str); // set string to "" if invalid utf8 sequence
+void utf8truncate(std::string& utf8str,size_t len);
+
+inline bool isBasicLatinCharacter(wchar_t wchar)
+{
+ if(wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
+ return true;
+ if(wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
+ return true;
+ return false;
+}
+
+inline bool isExtendedLatinCharacter(wchar_t wchar)
+{
+ if(isBasicLatinCharacter(wchar))
+ return true;
+ if(wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
+ return true;
+ if(wchar >= 0x00D8 && wchar <= 0x00DF) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
+ return true;
+ if(wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
+ return true;
+ if(wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
+ return true;
+ if(wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
+ return true;
+ if(wchar >= 0x0100 && wchar <= 0x012F) // LATIN CAPITAL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK
+ return true;
+ if(wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
+ return true;
+ return false;
+}
+
+inline bool isCyrillicCharacter(wchar_t wchar)
+{
+ if(wchar >= 0x0410 && wchar <= 0x044F) // CYRILLIC CAPITAL LETTER A - CYRILLIC SMALL LETTER YA
+ return true;
+ if(wchar == 0x0401 || wchar == 0x0451) // CYRILLIC CAPITAL LETTER IO, CYRILLIC SMALL LETTER IO
+ return true;
+ return false;
+}
+
+inline bool isEastAsianCharacter(wchar_t wchar)
+{
+ if(wchar >= 0x1100 && wchar <= 0x11F9) // Hangul Jamo
+ return true;
+ if(wchar >= 0x3041 && wchar <= 0x30FF) // Hiragana + Katakana
+ return true;
+ if(wchar >= 0x3131 && wchar <= 0x318E) // Hangul Compatibility Jamo
+ return true;
+ if(wchar >= 0x31F0 && wchar <= 0x31FF) // Katakana Phonetic Ext.
+ return true;
+ if(wchar >= 0x3400 && wchar <= 0x4DB5) // CJK Ideographs Ext. A
+ return true;
+ if(wchar >= 0x4E00 && wchar <= 0x9FC3) // Unified CJK Ideographs
+ return true;
+ if(wchar >= 0xAC00 && wchar <= 0xD7A3) // Hangul Syllables
+ return true;
+ if(wchar >= 0xFF01 && wchar <= 0xFFEE) // Halfwidth forms
+ return true;
+ return false;
+}
+
+inline bool isNumeric(wchar_t wchar)
+{
+ return (wchar >= L'0' && wchar <=L'9');
+}
+
+inline bool isNumeric(char c)
+{
+ return (c >= '0' && c <='9');
+}
+
+inline bool isNumericOrSpace(wchar_t wchar)
+{
+ return isNumeric(wchar) || wchar == L' ';
+}
+
+inline bool isBasicLatinString(std::wstring wstr, bool numericOrSpace)
+{
+ for (size_t i = 0; i < wstr.size(); ++i)
+ if(!isBasicLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
+ return false;
+ return true;
+}
+
+inline bool isExtendedLatinString(std::wstring wstr, bool numericOrSpace)
+{
+ for (size_t i = 0; i < wstr.size(); ++i)
+ if(!isExtendedLatinCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
+ return false;
+ return true;
+}
+
+inline bool isCyrillicString(std::wstring wstr, bool numericOrSpace)
+{
+ for (size_t i = 0; i < wstr.size(); ++i)
+ if(!isCyrillicCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
+ return false;
+ return true;
+}
+
+inline bool isEastAsianString(std::wstring wstr, bool numericOrSpace)
+{
+ for (size_t i = 0; i < wstr.size(); ++i)
+ if(!isEastAsianCharacter(wstr[i]) && (!numericOrSpace || !isNumericOrSpace(wstr[i])))
+ return false;
+ return true;
+}
+
+inline wchar_t wcharToUpper(wchar_t wchar)
+{
+ if(wchar >= L'a' && wchar <= L'z') // LATIN SMALL LETTER A - LATIN SMALL LETTER Z
+ return wchar_t(uint16(wchar)-0x0020);
+ if(wchar == 0x00DF) // LATIN SMALL LETTER SHARP S
+ return wchar_t(0x1E9E);
+ if(wchar >= 0x00E0 && wchar <= 0x00F6) // LATIN SMALL LETTER A WITH GRAVE - LATIN SMALL LETTER O WITH DIAERESIS
+ return wchar_t(uint16(wchar)-0x0020);
+ if(wchar >= 0x00F8 && wchar <= 0x00FE) // LATIN SMALL LETTER O WITH STROKE - LATIN SMALL LETTER THORN
+ return wchar_t(uint16(wchar)-0x0020);
+ if(wchar >= 0x0101 && wchar <= 0x012F) // LATIN SMALL LETTER A WITH MACRON - LATIN SMALL LETTER I WITH OGONEK (only %2=1)
+ {
+ if(wchar % 2 == 1)
+ return wchar_t(uint16(wchar)-0x0001);
+ }
+ if(wchar >= 0x0430 && wchar <= 0x044F) // CYRILLIC SMALL LETTER A - CYRILLIC SMALL LETTER YA
+ return wchar_t(uint16(wchar)-0x0020);
+ if(wchar == 0x0451) // CYRILLIC SMALL LETTER IO
+ return wchar_t(0x0401);
+
+ return wchar;
+}
+
+inline wchar_t wcharToUpperOnlyLatin(wchar_t wchar)
+{
+ return isBasicLatinCharacter(wchar) ? wcharToUpper(wchar) : wchar;
+}
+
+inline wchar_t wcharToLower(wchar_t wchar)
+{
+ if(wchar >= L'A' && wchar <= L'Z') // LATIN CAPITAL LETTER A - LATIN CAPITAL LETTER Z
+ return wchar_t(uint16(wchar)+0x0020);
+ if(wchar >= 0x00C0 && wchar <= 0x00D6) // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
+ return wchar_t(uint16(wchar)+0x0020);
+ if(wchar >= 0x00D8 && wchar <= 0x00DE) // LATIN CAPITAL LETTER O WITH STROKE - LATIN CAPITAL LETTER THORN
+ return wchar_t(uint16(wchar)+0x0020);
+ if(wchar >= 0x0100 && wchar <= 0x012E) // LATIN CAPITAL LETTER A WITH MACRON - LATIN CAPITAL LETTER I WITH OGONEK (only %2=0)
+ {
+ if(wchar % 2 == 0)
+ return wchar_t(uint16(wchar)+0x0001);
+ }
+ if(wchar == 0x1E9E) // LATIN CAPITAL LETTER SHARP S
+ return wchar_t(0x00DF);
+ if(wchar == 0x0401) // CYRILLIC CAPITAL LETTER IO
+ return wchar_t(0x0451);
+ if(wchar >= 0x0410 && wchar <= 0x042F) // CYRILLIC CAPITAL LETTER A - CYRILLIC CAPITAL LETTER YA
+ return wchar_t(uint16(wchar)+0x0020);
+
+ 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 );
+}
+
+std::wstring GetMainPartOfName(std::wstring wname, uint32 declension);
+
+bool utf8ToConsole(const std::string& utf8str, std::string& conStr);
+bool consoleToUtf8(const std::string& conStr,std::string& utf8str);
+bool Utf8FitTo(const std::string& str, std::wstring search);
+void utf8printf(FILE *out, const char *str, ...);
+void vutf8printf(FILE *out, const char *str, va_list* ap);
+
+bool IsIPAddress(char const* ipaddress);
+uint32 CreatePIDFile(const std::string& filename);
+
+void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result);
+#endif
+
+//handler for operations on large flags
+#ifndef _FLAG96
+#define _FLAG96
+
+#ifndef PAIR64_HIPART
+#define PAIR64_HIPART(x) (uint32)((uint64(x) >> 32) & UI64LIT(0x00000000FFFFFFFF))
+#define PAIR64_LOPART(x) (uint32)(uint64(x) & UI64LIT(0x00000000FFFFFFFF))
+#endif
+
+class flag96
+{
+private:
+ uint32 part[3];
+public:
+ flag96(uint32 p1=0,uint32 p2=0,uint32 p3=0)
+ {
+ part[0]=p1;
+ part[1]=p2;
+ part[2]=p3;
+ }
+
+ flag96(uint64 p1, uint32 p2)
+ {
+ part[0]=PAIR64_LOPART(p1);
+ part[1]=PAIR64_HIPART(p1);
+ part[2]=p2;
+ }
+
+ inline bool IsEqual(uint32 p1=0, uint32 p2=0, uint32 p3=0) const
+ {
+ return (
+ part[0]==p1 &&
+ part[1]==p2 &&
+ part[2]==p3);
+ };
+
+ inline bool HasFlag(uint32 p1=0, uint32 p2=0, uint32 p3=0) const
+ {
+ return (
+ part[0]&p1 ||
+ part[1]&p2 ||
+ part[2]&p3);
+ };
+
+ inline void Set(uint32 p1=0, uint32 p2=0, uint32 p3=0)
+ {
+ part[0]=p1;
+ part[1]=p2;
+ part[2]=p3;
+ };
+
+ template<class type>
+ inline bool operator < (type & right)
+ {
+ for (uint8 i=3; i > 0; --i)
+ {
+ if (part[i-1]<right.part[i-1])
+ return 1;
+ else if (part[i-1]>right.part[i-1])
+ return 0;
+ }
+ return 0;
+ };
+
+ template<class type>
+ inline bool operator < (type & right) const
+ {
+ for (uint8 i = 3; i > 0; --i)
+ {
+ if (part[i-1]<right.part[i-1])
+ return 1;
+ else if (part[i-1]>right.part[i-1])
+ return 0;
+ }
+ return 0;
+ };
+
+ template<class type>
+ inline bool operator != (type & right)
+ {
+ if (part[0]!=right.part[0]
+ || part[1]!=right.part[1]
+ || part[2]!=right.part[2])
+ return true;
+ return false;
+ }
+
+ template<class type>
+ inline bool operator != (type & right) const
+ {
+ if (part[0]!=right.part[0]
+ || part[1]!=right.part[1]
+ || part[2]!=right.part[2])
+ return true;
+ return false;
+ };
+
+ template<class type>
+ inline bool operator == (type & right)
+ {
+ if (part[0]!=right.part[0]
+ || part[1]!=right.part[1]
+ || part[2]!=right.part[2])
+ return false;
+ return true;
+ };
+
+ template<class type>
+ inline bool operator == (type & right) const
+ {
+ if (part[0]!=right.part[0]
+ || part[1]!=right.part[1]
+ || part[2]!=right.part[2])
+ return false;
+ return true;
+ };
+
+ template<class type>
+ inline void operator = (type & right)
+ {
+ part[0]=right.part[0];
+ part[1]=right.part[1];
+ part[2]=right.part[2];
+ };
+
+ template<class type>
+ inline flag96 operator & (type & right)
+ {
+ flag96 ret(part[0] & right.part[0],part[1] & right.part[1],part[2] & right.part[2]);
+ return
+ ret;
+ };
+ template<class type>
+ inline flag96 operator & (type & right) const
+ {
+ flag96 ret(part[0] & right.part[0],part[1] & right.part[1],part[2] & right.part[2]);
+ return
+ ret;
+ };
+
+ template<class type>
+ inline void operator &= (type & right)
+ {
+ *this=*this & right;
+ };
+
+ template<class type>
+ inline flag96 operator | (type & right)
+ {
+ flag96 ret(part[0] | right.part[0],part[1] | right.part[1],part[2] | right.part[2]);
+ return
+ ret;
+ };
+
+ template<class type>
+ inline flag96 operator | (type & right) const
+ {
+ flag96 ret(part[0] | right.part[0],part[1] | right.part[1],part[2] | right.part[2]);
+ return
+ ret;
+ };
+
+ template<class type>
+ inline void operator |= (type & right)
+ {
+ *this=*this | right;
+ };
+
+ inline void operator ~ ()
+ {
+ part[2]=~part[2];
+ part[1]=~part[1];
+ part[0]=~part[0];
+ };
+
+ template<class type>
+ inline flag96 operator ^ (type & right)
+ {
+ flag96 ret(part[0] ^ right.part[0],part[1] ^ right.part[1],part[2] ^ right.part[2]);
+ return
+ ret;
+ };
+
+ template<class type>
+ inline flag96 operator ^ (type & right) const
+ {
+ flag96 ret(part[0] ^ right.part[0],part[1] ^ right.part[1],part[2] ^ right.part[2]);
+ return
+ ret;
+ };
+
+ template<class type>
+ inline void operator ^= (type & right)
+ {
+ *this=*this^right;
+ };
+
+ inline operator bool() const
+ {
+ return(
+ part[0] != 0 ||
+ part[1] != 0 ||
+ part[2] != 0);
+ };
+
+ inline operator bool()
+ {
+ return(
+ part[0] != 0 ||
+ part[1] != 0 ||
+ part[2] != 0);
+ };
+
+ inline bool operator ! () const
+ {
+ return(
+ part[0] == 0 &&
+ part[1] == 0 &&
+ part[2] == 0);
+ };
+
+ inline bool operator ! ()
+ {
+ return(
+ part[0] == 0 &&
+ part[1] == 0 &&
+ part[2] == 0);
+ };
+
+ inline uint32 & operator[](uint8 el)
+ {
+ return (part[el]);
+ };
+
+ inline const uint32 & operator[](uint8 el) const
+ {
+ return (part[el]);
+ };
+};
+#endif