-
Notifications
You must be signed in to change notification settings - Fork 0
/
refsapi_misc.h
343 lines (306 loc) · 7.69 KB
/
refsapi_misc.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#pragma once
#include "refsapi.h"
bool is_number(std::string &s);
size_t set_amx_string(cell *dest, const char *str, size_t max_len);
class fmt
{
const size_t size = 1024;
char *buff;
public:
fmt(char *fmt, ...)
{
buff = new char[size];
va_list arg_ptr;
va_start(arg_ptr, fmt);
Q_vsnprintf(buff, size - 1, fmt, arg_ptr);
va_end(arg_ptr);
}
~fmt()
{
if (buff != nullptr)
delete buff;
buff = nullptr;
}
char *c_str()
{
return buff;
}
};
class wfmt
{
const size_t size = 1024;
wchar_t *buff;
public:
wfmt(wchar_t *fmt, ...)
{
buff = new wchar_t[size];
va_list arg_ptr;
va_start(arg_ptr, fmt);
std::vswprintf(buff, size - 1, fmt, arg_ptr);
va_end(arg_ptr);
}
~wfmt()
{
if (buff != nullptr)
delete buff;
buff = nullptr;
}
wchar_t *c_str()
{
return buff;
}
};
inline bool is_valid_index(const size_t index)
{
return index > 0 && index <= gpGlobals->maxClients;
}
inline bool is_valid_entity(const edict_t *pEdict)
{
return !(pEdict == NULL || pEdict == nullptr || pEdict->pvPrivateData == nullptr || pEdict->free || (pEdict->v.flags & FL_KILLME));
}
inline bool is_valid_team(const int team)
{
return team >= TEAM_TERRORIST && team <= TEAM_CT;
}
inline bool is_entity_intersects(const edict_t *pEdict_1, const edict_t *pEdict_2)
{
return !(pEdict_1->v.absmin.x > pEdict_2->v.absmax.x ||
pEdict_1->v.absmin.y > pEdict_2->v.absmax.y ||
pEdict_1->v.absmin.z > pEdict_2->v.absmax.z ||
pEdict_1->v.absmax.x < pEdict_2->v.absmin.x ||
pEdict_1->v.absmax.y < pEdict_2->v.absmin.y ||
pEdict_1->v.absmax.z < pEdict_2->v.absmin.z);
}
inline std::string wstos(const wchar_t *s)
{
return g_converter.to_bytes(s);
}
inline std::string wstos(const std::wstring &s)
{
return g_converter.to_bytes(s);
}
inline std::string wstos(wfmt s)
{
return g_converter.to_bytes(s.c_str());
}
inline std::wstring stows(const std::string &s)
{
try
{
return g_converter.from_bytes(s);
}
catch (...)
{
std::wstring result;
size_t len = s.size();
//UTIL_ServerPrint("[DEBUG] stows(): catch !!! str = %s, len = %d\n", s.c_str(), len);
if (len)
{
result.reserve(len);
for (size_t i = 0; i < len; i++)
result.push_back(s[i] & 0xFF);
}
return result;
}
}
inline double stod(std::string s, bool has_min = false, float min_val = 0.0f, bool has_max = false, float max_val = 0.0f)
{
auto result = std::stod(s); // std::strtof(s.c_str(), 0); //(float)std::stod(s);
// UTIL_ServerPrint("[DEBUG] stod(): in = %s, out = %f\n", s.c_str(), result);
if (has_min && result < min_val)
result = min_val;
if (has_min && result > max_val)
result = max_val;
return result;
}
inline double roundd(double value, int precision = -6)
{
// UTIL_ServerPrint("[DEBUG] acs_roundfloat(): value = %f, precision = %d", value, precision);
auto power = pow(10.0f, -precision);
return floor(value * power + 0.5) / power;
}
inline bool file_exists(const std::wstring &name)
{
struct stat buff;
return (stat(wstos(name).c_str(), &buff) == 0);
}
inline void remove_chars(std::string &s, std::string chars = _TRIM_CHARS)
{
s.erase(std::remove_if(s.begin(), s.end(), [&](unsigned char ch)
{
for (auto& sub : chars)
if (ch == sub)
return true;
return false; }),
s.end());
}
inline void ltrim(std::string &s, std::string chars = _TRIM_CHARS)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [&](unsigned char ch)
{
for (auto& sub : chars)
if (ch == sub)
return false;
return true; }));
}
inline void ltrim(std::wstring &s, std::string chars = _TRIM_CHARS)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [&](unsigned char ch)
{
for (auto& sub : chars)
if (ch == sub)
return false;
return true; }));
}
inline void rtrim(std::string &s, std::string chars = _TRIM_CHARS)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [&](unsigned char ch)
{
for (auto& sub : chars)
if (ch == sub)
return false;
return true; })
.base(),
s.end());
}
inline void rtrim(std::wstring &s, std::string chars = _TRIM_CHARS)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [&](unsigned char ch)
{
for (auto& sub : chars)
if (ch == sub)
return false;
return true; })
.base(),
s.end());
}
inline void rtrim_zero(std::string &s)
{
auto it = std::find(s.rbegin(), s.rend(), '.');
if (it == s.rend())
return;
s.erase(std::find_if(s.rbegin(), it, [&](unsigned char ch)
{ return ch != '0'; })
.base(),
s.end());
}
inline std::string rtrim_zero_c(std::string s)
{
rtrim_zero(s);
return s;
}
inline void trim(std::string &s, std::string chars = _TRIM_CHARS)
{
rtrim(s, chars);
ltrim(s, chars);
}
inline void trim(std::wstring &s, std::string chars = _TRIM_CHARS)
{
rtrim(s, chars);
ltrim(s, chars);
}
inline std::string ltrim_c(std::string s, std::string chars = _TRIM_CHARS)
{
ltrim(s, chars);
return s;
}
inline std::wstring ltrim_c(std::wstring s, std::string chars = _TRIM_CHARS)
{
ltrim(s, chars);
return s;
}
inline std::string rtrim_c(std::string s, std::string chars = _TRIM_CHARS)
{
rtrim(s, chars);
return s;
}
inline std::wstring rtrim_c(std::wstring s, std::string chars = _TRIM_CHARS)
{
rtrim(s, chars);
return s;
}
inline std::string trim_c(std::string s, std::string chars = _TRIM_CHARS)
{
trim(s, chars);
return s;
}
inline std::wstring trim_c(std::wstring s, std::string chars = _TRIM_CHARS)
{
trim(s, chars);
return s;
}
inline void ws_convert_tolower(std::wstring &s)
{
std::transform(s.begin(), s.end(), s.begin(), std::bind(std::tolower<wchar_t>, std::placeholders::_1, _LOCALE));
}
inline int rm_quote(std::string &s)
{
int result = 0;
bool f[2];
for (auto &ch : _QQ)
{
f[0] = f[1] = 0;
if ((f[0] = s.front() == ch) && (f[1] = s.back() == ch))
{
s.erase(s.begin());
s.erase(s.end() - 1);
trim(s);
result = 1;
break;
}
else if (f[0] != f[1])
{
result = -1;
break;
}
}
return result;
}
inline int rm_quote(std::wstring &s)
{
int result = 0;
bool f[2];
for (auto &ch : _QQ)
{
f[0] = f[1] = 0;
if ((f[0] = s.front() == ch) && (f[1] = s.back() == ch))
{
s.erase(s.begin());
s.erase(s.end() - 1);
result = 1;
break;
}
else if (f[0] != f[1])
{
result = -1;
break;
}
}
return result;
}
inline std::string rm_quote_c(std::string &s)
{
for (auto &ch : _QQ)
{
if (s.front() == ch && s.back() == ch)
{
s.erase(s.begin());
s.erase(s.end() - 1);
break;
}
}
return s;
}
inline std::wstring rm_quote_c(std::wstring &s)
{
for (auto &ch : _QQ)
{
if (s.front() == ch && s.back() == ch)
{
s.erase(s.begin());
s.erase(s.end() - 1);
break;
}
}
return s;
}