-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
191 lines (165 loc) · 2.66 KB
/
util.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
#ifndef __SLSERVER_UTIL__
#define __SLSERVER_UTIL__
inline int parse_int_dec(const char* p) {
bool isNeg = false;
int ret = 0;
if(*p == '-') {
isNeg = true;
p++;
}
while(*p) {
if(*p < '0' || *p > '9') {
ret = 0;
break;
}
ret *= 10;
ret += *p - '0';
p++;
}
if (isNeg)
ret *= -1;
return ret;
}
inline int parse_int_hex(const char* p) {
bool isNeg = false;
int ret = 0;
if(*p == '-') {
isNeg = true;
p++;
}
if(p[0] != '0' || p[1] != 'x')
return 0;
while(*p) {
if((*p >= '0'&&*p <= '9')) {
ret *= 16;
ret += *p - '0';
}else if(*p >= 'A'&&*p <= 'F') {
ret *= 16;
ret += *p - 'A' + 10;
}else if(*p >= 'a'&&*p <= 'f') {
ret *= 16;
ret += *p - 'a' + 10;
}else{
ret = 0;
break;
}
p++;
}
if (isNeg)
ret *= -1;
return ret;
}
inline float parse_float(const char* p) {
bool isNeg = false;
float ret = 0,l = 1.f;
if(*p == '-') {
isNeg = true;
p++;
}
while(*p&&*p != '.') {
if(*p < '0' || *p > '9') {
return 0.f;
}
ret *= 10;
ret += *p - '0';
p++;
}
if(*p == '.') {
p++;
}
while(*p) {
if(*p < '0' || *p > '9') {
return 0.f;
}
l *= 0.1f;
ret += (*p - '0')*l;
p++;
}
if (isNeg)
ret *= -1;
return ret;
}
inline const char* sl_find_first_char(const char* p,char c) {
while(*p) {
if(*p == c)
break;
p++;
}
return p;
}
inline char* sl_find_first_char(char* p,char c) {
while(*p) {
if(*p == c)
break;
p++;
}
return p;
}
inline int sl_hex2num(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
inline int sl_hex2byte(const char *hex)
{
int a, b;
a = sl_hex2num(*hex++);
if (a < 0)
return -1;
b = sl_hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
#define ETH_ALEN 6
inline const char * sl_hwaddr_parse(const char *txt, unsigned char *addr)
{
size_t i;
for (i = 0; i < ETH_ALEN; i++) {
int a;
a = sl_hex2byte(txt);
if (a < 0)
return NULL;
txt += 2;
addr[i] = a&0xff;
if (i < ETH_ALEN - 1 && *txt++ != ':')
return NULL;
}
return txt;
}
inline int sl_hwaddr_aton(const char *txt,unsigned char* addr) {
return sl_hwaddr_parse(txt, addr) ? 0 : -1;
}
inline char* get_file_from_path(const char* path,char* buf) {
const char* p1;
char* p2;
p2 = buf;
p1 = path;
while(*p1)p1++;
while(p1 >= path) {
if(*p1 == '/')
break;
p1--;
}
if(*p1 == '/')
p1++;
while(*p1) {
*p2++ = *p1++;
}
*p2 = 0;
return buf;
}
inline int sl_constrain(int a,int _max,int _min) {
a = a > _max ? _max : a;
a = a < _min ? _min : a;
return a;
}
inline int sl_abs(int a) {
return a < 0 ? -a : a;
}
#endif