forked from bunder/qpakman
-
Notifications
You must be signed in to change notification settings - Fork 2
/
u_util.cc
259 lines (194 loc) · 5.08 KB
/
u_util.cc
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
//------------------------------------------------------------------------
// Utility functions
//------------------------------------------------------------------------
//
// Copyright (c) 2008 Andrew J Apted
// Copyright (c) 2015 Anton Leontiev
//
// 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.
//
//------------------------------------------------------------------------
#include "headers.h"
#ifdef UNIX
#include <sys/time.h>
#include <unistd.h>
#endif
int StringCaseCmp(const char *A, const char *B)
{
for (; *A || *B; A++, B++)
{
// this test also catches end-of-string conditions
if (toupper(*A) != toupper(*B))
return (toupper(*A) - toupper(*B));
}
return 0;
}
int StringCaseCmpPartial(const char *A, const char *B)
{
// Checks that the string B occurs at the front of string A.
// NOTE: This function is not symmetric, A can be longer than B and
// still match, but the match always fails if A is shorter than B.
for (; *B; A++, B++)
{
// this test also catches end-of-string conditions
if (toupper(*A) != toupper(*B))
return (toupper(*A) - toupper(*B));
}
return 0;
}
void StringMaxCopy(char *dest, const char *src, int max)
{
for (; *src && max > 0; max--)
{
*dest++ = *src++;
}
*dest = 0;
}
char *StringUpper(const char *name)
{
char *copy = StringDup(name);
for (char *p = copy; *p; p++)
*p = toupper(*p);
return copy;
}
char *StringNew(int length)
{
// length does not include the trailing NUL.
char *s = (char *) calloc(length + 1, 1);
if (! s)
AssertFail("Out of memory (%d bytes for string)\n", length);
return s;
}
char *StringDup(const char *orig)
{
char *s = strdup(orig);
if (! s)
AssertFail("Out of memory (copy string)\n");
return s;
}
char *StringPrintf(const char *str, ...)
{
/* Algorithm: keep doubling the allocated buffer size
* until the output fits. Based on code by Darren Salt.
*/
char *buf = NULL;
int buf_size = 128;
for (;;)
{
va_list args;
int out_len;
buf_size *= 2;
buf = (char*)realloc(buf, buf_size);
if (!buf)
AssertFail("Out of memory (formatting string)");
va_start(args, str);
out_len = vsnprintf(buf, buf_size, str, args);
va_end(args);
// old versions of vsnprintf() simply return -1 when
// the output doesn't fit.
if (out_len < 0 || out_len >= buf_size)
continue;
return buf;
}
}
void StringFree(const char *str)
{
if (str)
{
free((void*) str);
}
}
//------------------------------------------------------------------------
/* Thomas Wang's 32-bit Mix function */
u32_t IntHash(u32_t key)
{
key += ~(key << 15);
key ^= (key >> 10);
key += (key << 3);
key ^= (key >> 6);
key += ~(key << 11);
key ^= (key >> 16);
return key;
}
u32_t StringHash(const char *str)
{
u32_t hash = 0;
if (str)
while (*str)
hash = (hash << 5) - hash + *str++;
return hash;
}
double PerpDist(double x, double y,
double x1, double y1, double x2, double y2)
{
x -= x1; y -= y1;
x2 -= x1; y2 -= y1;
double len = sqrt(x2*x2 + y2*y2);
SYS_ASSERT(len > 0);
return (x * y2 - y * x2) / len;
}
double AlongDist(double x, double y,
double x1, double y1, double x2, double y2)
{
x -= x1; y -= y1;
x2 -= x1; y2 -= y1;
double len = sqrt(x2*x2 + y2*y2);
SYS_ASSERT(len > 0);
return (x * x2 + y * y2) / len;
}
double CalcAngle(double sx, double sy, double ex, double ey)
{
// result is Degrees (0 <= angle < 360).
// East (increasing X) --> 0 degrees
// North (increasing Y) --> 90 degrees
ex -= sx;
ey -= sy;
if (fabs(ex) < 0.0001)
return (ey > 0) ? 90.0 : 270.0;
if (fabs(ey) < 0.0001)
return (ex > 0) ? 0.0 : 180.0;
double angle = atan2(ey, ex) * 180.0 / M_PI;
if (angle < 0)
angle += 360.0;
return angle;
}
double ComputeDist(double sx, double sy, double ex, double ey)
{
return sqrt((ex-sx)*(ex-sx) + (ey-sy)*(ey-sy));
}
double ComputeDist(double sx, double sy, double sz,
double ex, double ey, double ez)
{
return sqrt((ex-sx)*(ex-sx) + (ey-sy)*(ey-sy) + (ez-sz)*(ez-sz));
}
//------------------------------------------------------------------------
u32_t TimeGetMillies()
{
// Note: you *MUST* handle overflow (it *WILL* happen)
#ifdef WIN32
unsigned long ticks = GetTickCount();
return (u32_t) ticks;
#else // UNIX or MacOSX
struct timeval tm;
gettimeofday(&tm, NULL);
return (u32_t) ((tm.tv_sec * 1000) + (tm.tv_usec / 1000));
#endif
}
void TimeDelay(u32_t millies)
{
#ifdef WIN32
::Sleep(millies);
#else // LINUX or MacOSX
usleep(millies * 1000);
#endif
}
//--- editor settings ---
// vi:ts=2:sw=2:expandtab