forked from ike3/mangosbot-bots
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Helpers.cpp
103 lines (91 loc) · 2.2 KB
/
Helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "../botpch.h"
#include "playerbot.h"
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
void split(std::vector<std::string>& dest, const std::string& str, const char* delim)
{
char* pTempStr = strdup( str.c_str() );
char* pWord = strtok(pTempStr, delim);
while(pWord != NULL)
{
dest.push_back(pWord);
pWord = strtok(NULL, delim);
}
free(pTempStr);
}
vector<string>& split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s);
string item;
while(getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim)
{
vector<string> elems;
return split(s, delim, elems);
}
char *strstri(const char *haystack, const char *needle)
{
if ( !*needle )
{
return (char*)haystack;
}
for ( ; *haystack; ++haystack )
{
if ( tolower(*haystack) == tolower(*needle) )
{
const char *h = haystack, *n = needle;
for ( ; *h && *n; ++h, ++n )
{
if ( tolower(*h) != tolower(*n) )
{
break;
}
}
if ( !*n )
{
return (char*)haystack;
}
}
}
return 0;
}
uint64 extractGuid(WorldPacket& packet)
{
uint8 mask;
packet >> mask;
uint64 guid = 0;
uint8 bit = 0;
uint8 testMask = 1;
while (true)
{
if (mask & testMask)
{
uint8 word;
packet >> word;
guid += (word << bit);
}
if (bit == 7)
break;
++bit;
testMask <<= 1;
}
return guid;
}
std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) {return !std::isspace(c);}));
return s;
}
std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c) {return !std::isspace(c); }).base(), s.end());
return s;
}
std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}