EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Util.h
Go to the documentation of this file.
1 /*
2  * Ascent MMORPG Server
3  * Copyright (C) 2005-2008 Ascent Team <http://www.ascentemu.com/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef _UTIL_H
21 #define _UTIL_H
22 
23 #include "eve-core.h"
24 
25 #ifdef HAVE_WINDOWS_H
26 # define KILL_THIS_PROCESS TerminateProcess(GetCurrentProcess(), 0);
27 #else /* !HAVE_WINDOWS_H */
28 # define KILL_THIS_PROCESS kill(getpid(), -9);
29 #endif /* !HAVE_WINDOWS_H */
30 
31 
32  std::vector<std::string> StrSplit(const std::string &src, const std::string &sep);
33 
34  // This HAS to be called outside the threads __try / __except block!
35  void SetThreadName(const char* format, ...);
36  time_t convTimePeriod ( uint32 dLength, char dType);
37  int32 ascent_roundf(float v);
38  int32 ascent_roundd(double v);
39  bool CheckIPs(const char* szIPList);
40  int get_tokens(const char* szInput, char** pOutput, int iMaxCount, char cSeperator);
41 
42 #ifdef HAVE_WINDOWS_H
43  typedef struct tagTHREADNAME_INFO
44  {
45  DWORD dwType; // must be 0x1000
46  LPCSTR szName; // pointer to name (in user addr space)
47  DWORD dwThreadID; // thread ID (-1=caller thread)
48  DWORD dwFlags; // reserved for future use, must be zero
49  } THREADNAME_INFO;
50 #endif /* HAVE_WINDOWS_H */
51 
52  int32 GetTimePeriodFromString(const char * str);
53  std::string ConvertTimeStampToString(uint32 timestamp);
54  std::string ConvertTimeStampToStringNC(uint32 timestamp);
55  std::string ConvertTimeStampToDataTime(uint32 timestamp);
56 
57  // returns true if the ip hits the mask, otherwise false
58  bool ParseCIDRBan(unsigned int IP, unsigned int Mask, unsigned int MaskBits);
59  unsigned int MakeIP(const char * str);
60 
61 /************************************************************************/
62 /* Next piece of code borrowed from the OpenFrag project, original */
63 /* header below this comment chunk. */
64 /* @note I also added tweaks of my own, adding wide string support */
65 /************************************************************************/
66 
67 /*
68  ------------------------------------------------------------------------------------
69  LICENSE:
70  ------------------------------------------------------------------------------------
71  This file is part of OpenFrag
72  Copyright 2003 - 2008 The OpenFrag Team
73  For the latest information visit http://www.openfrag.org
74  ------------------------------------------------------------------------------------
75  This program is free software; you can redistribute it and/or modify it under
76  the terms of the GNU Lesser General Public License as published by the Free Software
77  Foundation; either version 2 of the License, or (at your option) any later
78  version.
79 
80  This program is distributed in the hope that it will be useful, but WITHOUT
81  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
82  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
83 
84  You should have received a copy of the GNU Lesser General Public License along with
85  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
86  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
87  http://www.gnu.org/copyleft/lesser.txt.
88  ------------------------------------------------------------------------------------
89  Usefull utility's for strings.
90  Thanks goes also to yake - this class is a little bit bigger.
91  ------------------------------------------------------------------------------------
92  Author: Lars Wesselius
93 */
94 
95 namespace Utils
96 {
97 class Strings
98 {
99 public:
100  /* trim
101  *
102  * Remove leading and trailing spaces from a string.
103  *
104  * @param str[in] - The string to trim.
105  * @param left[in] - Remove it on the left side.
106  * @param right[in] - Remove it on the right side.
107  * \return void (changed string)
108  */
109  static std::string trim(const std::string& str, bool left, bool right);
110 
111  /* trim
112  *
113  * Remove leading and trailing spaces from a string.
114  *
115  * @param str[in][out] - The string to trim.
116  * @param left[in] - Remove it on the left side.
117  * @param right[in] - Remove it on the right side.
118  */
119  static void trim(std::string& str, bool left, bool right);
120 
121  /* trim
122  *
123  * Remove leading and trailing spaces from a string.
124  *
125  * @param str[in] - The string to trim.
126  * @param left[in] - Remove it on the left side.
127  * @param right[in] - Remove it on the right side.
128  * \return void (changed string)
129  */
130  static std::wstring trim(const std::wstring& str, bool left, bool right);
131 
132  /* trim
133  *
134  * Remove leading and trailing spaces from a string.
135  *
136  * @param str[in][out] - The string to trim.
137  * @param left[in] - Remove it on the left side.
138  * @param right[in] - Remove it on the right side.
139  */
140  static void trim(std::wstring& str, bool left, bool right);
141 
142  /* toUpperCase
143  *
144  * Transforms a string into upper case characters.
145  *
146  * @param TString[in] - The string to convert.
147  * \return the string containing upper case characters.
148  */
149  static std::string toUpperCase(const std::string& TString);
150 
151  /* toUpperCase
152  *
153  * Transforms a string into upper case characters.
154  *
155  * @param[in][out] TString - The string to convert.
156  */
157  static void toUpperCase(std::string& TString);
158 
159  /* toUpperCase
160  *
161  * Transforms a string into upper case characters.
162  *
163  * @param TString[in] - The string to convert.
164  * \return the string containing upper case characters.
165  */
166  static std::wstring toUpperCase(const std::wstring& TString);
167 
168  /* toUpperCase
169  *
170  * Transforms a string into upper case characters.
171  *
172  * @param[in][out] TString - The string to convert.
173  */
174  static void toUpperCase(std::wstring& TString);
175 
176  /* toLowerCase
177  *
178  * Transforms a string into lower case characters.
179  *
180  * @param[in] TString - The string to convert.
181  * \return the string containing lower case characters.
182  */
183  static std::string toLowerCase(const std::string& TString);
184 
185  /* toLowerCase
186  *
187  * Transforms a string into lower case characters.
188  *
189  * @param TString[in][out] - The string to convert.
190  */
191  static void toLowerCase(std::string& TString);
192 
193  /* toLowerCase
194  *
195  * Transforms a string into lower case characters.
196  *
197  * @param[in] TString - The string to convert.
198  * \return the string containing lower case characters.
199  */
200  static std::wstring toLowerCase(const std::wstring& TString);
201 
202  /* toLowerCase
203  *
204  * Transforms a string into lower case characters.
205  *
206  * @param[in][out] TString - The string to convert.
207  */
208  static void toLowerCase(std::wstring& TString);
209 
210  /************************************************************************/
211  /* end of the OpenFrag project header */
212  /************************************************************************/
213 
220  static std::string CaseFold(std::string & str);
221 
228  static std::wstring CaseFold(std::wstring & str);
229 
236  static std::wstring StringToWString(std::string & wstr);
237 };
238 
249 class Hash
250 {
251 public:
260  static uint32 djb2_hash(const char* str)
261  {
262  uint32 hash = 5381;
263  int c;
264 
265  while ((c = *str++))
266  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
267 
268  return hash;
269  }
270 
279  static uint32 djb2_hash(const wchar_t *str)
280  {
281  uint32 hash = 5381;
282  int c;
283 
284  while ((c = *str++))
285  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
286 
287  return hash;
288  }
289 
298  static uint32 djb2_hash(const std::string& oStr)
299  {
300  const char * str = oStr.c_str();
301 
302  uint32 hash = 5381;
303  int c;
304 
305  while ((c = *str++))
306  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
307 
308  return hash;
309  }
310 
319  static uint32 djb2_hash(const std::wstring& oStr)
320  {
321  const wchar_t * str = oStr.c_str();
322 
323  uint32 hash = 5381;
324  int c;
325 
326  while ((c = *str++))
327  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
328 
329  return hash;
330  }
331 
341  static uint32 djb2_hash(const char* str, int len)
342  {
343  uint32 hash = 5381;
344  int c, i;
345 
346  for (i = 0; i < len; i++)
347  {
348  c = str[i];
349  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
350  }
351 
352  return hash;
353  }
354 
363  static uint32 sdbm_hash(std::string &oStr)
364  {
365  uint8 *str = (uint8*)oStr.c_str();
366  uint32 hash = 0;
367  int c;
368 
369  while ((c = *str++))
370  hash = c + (hash << 6) + (hash << 16) - hash;
371 
372  return hash;
373  }
374 
383  static uint32 sdbm_hash(const char *str)
384  {
385  uint32 hash = 0;
386  int c;
387 
388  while ((c = *str++))
389  hash = c + (hash << 6) + (hash << 16) - hash;
390 
391  return hash;
392  }
393 
402  static uint32 sdbm_hash(const wchar_t *str)
403  {
404  uint32 hash = 0;
405  int c;
406 
407  while ((c = *str++))
408  hash = c + (hash << 6) + (hash << 16) - hash;
409 
410  return hash;
411  }
412 
422  static uint32 sdbm_hash(const char *str, int len)
423  {
424  uint32 hash = 0;
425  int c;
426 
427  for (int i = 0; i < len; i++)
428  {
429  c = str[i];
430  hash = c + (hash << 6) + (hash << 16) - hash;
431  }
432 
433  return hash;
434  }
435 };
436 }// namespace Utils
437 
438 #endif//_UTIL_H
unsigned __int8 uint8
Definition: eve-compat.h:46
static uint32 sdbm_hash(const wchar_t *str)
sdbm algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:402
std::string ConvertTimeStampToDataTime(uint32 timestamp)
Definition: util.cpp:391
bool CheckIPs(const char *szIPList)
Definition: util.cpp:545
static uint32 djb2_hash(const char *str)
djb2 algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:260
std::vector< std::string > StrSplit(const std::string &src, const std::string &sep)
Definition: util.cpp:44
static std::wstring StringToWString(std::string &wstr)
Definition: util.cpp:777
bool ParseCIDRBan(unsigned int IP, unsigned int Mask, unsigned int MaskBits)
Definition: util.cpp:424
static std::string toUpperCase(const std::string &TString)
Definition: util.cpp:705
int32 GetTimePeriodFromString(const char *str)
static uint32 sdbm_hash(std::string &oStr)
sdbm algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:363
int get_tokens(const char *szInput, char **pOutput, int iMaxCount, char cSeperator)
Definition: util.cpp:491
a class to keep track of all the utility hash functions
Definition: Util.h:249
signed __int32 int32
Definition: eve-compat.h:49
time_t convTimePeriod(uint32 dLength, char dType)
Definition: util.cpp:96
static uint32 sdbm_hash(const char *str)
sdbm algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:383
static std::string toLowerCase(const std::string &TString)
Definition: util.cpp:729
void SetThreadName(const char *format,...)
unsigned __int32 uint32
Definition: eve-compat.h:50
static std::string CaseFold(std::string &str)
Definition: util.cpp:757
std::string ConvertTimeStampToStringNC(uint32 timestamp)
Definition: util.cpp:300
static uint32 djb2_hash(const std::string &oStr)
djb2 algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:298
static uint32 djb2_hash(const char *str, int len)
djb2 algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:341
static uint32 djb2_hash(const std::wstring &oStr)
djb2 algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:319
int32 ascent_roundf(float v)
Definition: util.cpp:24
Definition: util.cpp:609
unsigned int MakeIP(const char *str)
Definition: util.cpp:478
static std::string trim(const std::string &str, bool left, bool right)
Definition: util.cpp:611
std::string ConvertTimeStampToString(uint32 timestamp)
Definition: util.cpp:209
static uint32 sdbm_hash(const char *str, int len)
sdbm algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:422
int32 ascent_roundd(double v)
Definition: util.cpp:34
static uint32 djb2_hash(const wchar_t *str)
djb2 algorithm taken from http://www.cse.yorku.ca/~oz/hash.html slightly modified ...
Definition: Util.h:279