1
//------------------------------------------------------------------------
2
// UTILITY : general purpose functions
3
//------------------------------------------------------------------------
5
// GL-Friendly Node Builder (C) 2000-2005 Andrew Apted
7
// Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
9
// This program is free software; you can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
//------------------------------------------------------------------------
44
// Allocate memory with error checking. Zeros the memory.
46
void *UtilCalloc(int size)
48
void *ret = calloc(1, size);
51
FatalError("Out of memory (cannot allocate %d bytes)", size);
59
// Reallocate memory with error checking.
61
void *UtilRealloc(void *old, int size)
63
void *ret = realloc(old, size);
66
FatalError("Out of memory (cannot reallocate %d bytes)", size);
74
// Free the memory with error checking.
76
void UtilFree(void *data)
79
InternalError("Trying to free a NULL pointer");
87
// Duplicate a string with error checking.
89
char *UtilStrDup(const char *str)
92
int len = (int)strlen(str);
94
result = UtilCalloc(len+1);
97
memcpy(result, str, len);
107
// Duplicate a limited length string.
109
char *UtilStrNDup(const char *str, int size)
114
for (len=0; len < size && str[len]; len++)
117
result = UtilCalloc(len+1);
120
memcpy(result, str, len);
127
int UtilStrCaseCmp(const char *A, const char *B)
129
for (; *A || *B; A++, B++)
131
// this test also catches end-of-string conditions
132
if (toupper(*A) != toupper(*B))
133
return (toupper(*A) - toupper(*B));
144
// Rounds the value _up_ to the nearest power of two.
146
int UtilRoundPOW2(int x)
155
for (tmp=x / 2; tmp; tmp /= 2)
165
// Translate (dx, dy) into an angle value (degrees)
167
angle_g UtilComputeAngle(float_g dx, float_g dy)
172
return (dy > 0) ? 90.0 : 270.0;
174
angle = atan2((double) dy, (double) dx) * 180.0 / M_PI;
186
int UtilFileExists(const char *filename)
188
FILE *fp = fopen(filename, "rb");
202
const char *UtilTimeString(void)
208
static char str_buf[200];
210
GetSystemTime(&sys_time);
212
sprintf(str_buf, "%04d-%02d-%02d %02d:%02d:%02d.%04d",
213
sys_time.wYear, sys_time.wMonth, sys_time.wDay,
214
sys_time.wHour, sys_time.wMinute, sys_time.wSecond,
215
sys_time.wMilliseconds * 10);
219
#else // LINUX or MACOSX
222
struct tm *calend_time;
224
static char str_buf[200];
226
if (time(&epoch_time) == (time_t)-1)
229
calend_time = localtime(&epoch_time);
233
sprintf(str_buf, "%04d-%02d-%02d %02d:%02d:%02d.%04d",
234
calend_time->tm_year + 1900, calend_time->tm_mon + 1,
235
calend_time->tm_mday,
236
calend_time->tm_hour, calend_time->tm_min,
237
calend_time->tm_sec, 0);
244
//------------------------------------------------------------------------
245
// Adler-32 CHECKSUM Code
246
//------------------------------------------------------------------------
248
void Adler32_Begin(uint32_g *crc)
253
void Adler32_AddBlock(uint32_g *crc, const uint8_g *data, int length)
255
uint32_g s1 = (*crc) & 0xFFFF;
256
uint32_g s2 = ((*crc) >> 16) & 0xFFFF;
258
for (; length > 0; data++, length--)
260
s1 = (s1 + *data) % 65521;
261
s2 = (s2 + s1) % 65521;
264
*crc = (s2 << 16) | s1;
267
void Adler32_Finish(uint32_g *crc)