[go: up one dir, main page]

Menu

[d6a55f]: / utilslib.c  Maximize  Restore  History

Download this file

513 lines (422 with data), 9.6 kB

  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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* VisPatch : Quake level patcher for water visibility.
*
* Copyright (C) 1996-1997 Id Software, Inc.
* Copyright (C) 1997-2006 Andy Bay <IMarvinTPA@bigfoot.com>
* Copyright (C) 2006-2011 O. Sezer <sezero@users.sourceforge.net>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to:
*
* Free Software Foundation, Inc.
* 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "arch_def.h"
#include "compiler.h"
#include "q_stdinc.h"
#include "q_endian.h"
#include <sys/stat.h>
#if defined(PLATFORM_WINDOWS)
#include <windows.h>
#include <io.h>
#include <direct.h>
#endif /* PLATFORM_WINDOWS */
#if defined(PLATFORM_UNIX)
#include <unistd.h>
#include <dirent.h>
#include <fnmatch.h>
#endif /* PLATFORM_UNIX */
#include <ctype.h>
#include "utilslib.h"
/*============================================================================*/
/* platform dependant (v)snprintf function names: */
#if defined(PLATFORM_WINDOWS)
#define snprintf_func _snprintf
#define vsnprintf_func _vsnprintf
#else
#define snprintf_func snprintf
#define vsnprintf_func vsnprintf
#endif
int q_vsnprintf(char *str, size_t size, const char *format, va_list args)
{
int ret;
ret = vsnprintf_func (str, size, format, args);
if (ret < 0)
ret = (int)size;
if (size == 0) /* no buffer */
return ret;
if ((size_t)ret >= size)
str[size - 1] = '\0';
return ret;
}
int q_snprintf (char *str, size_t size, const char *format, ...)
{
int ret;
va_list argptr;
va_start (argptr, format);
ret = q_vsnprintf (str, size, format, argptr);
va_end (argptr);
return ret;
}
static inline int q_isupper(int c)
{
return (c >= 'A' && c <= 'Z');
}
static inline int q_tolower(int c)
{
return ((q_isupper(c)) ? (c | ('a' - 'A')) : c);
}
int q_strcasecmp(const char * s1, const char * s2)
{
const char * p1 = s1;
const char * p2 = s2;
char c1, c2;
if (p1 == p2)
return 0;
do
{
c1 = q_tolower (*p1++);
c2 = q_tolower (*p2++);
if (c1 == '\0')
break;
} while (c1 == c2);
return (int)(c1 - c2);
}
int q_strncasecmp(const char *s1, const char *s2, size_t n)
{
const char * p1 = s1;
const char * p2 = s2;
char c1, c2;
if (p1 == p2 || n == 0)
return 0;
do
{
c1 = q_tolower (*p1++);
c2 = q_tolower (*p2++);
if (c1 == '\0' || c1 != c2)
break;
} while (--n > 0);
return (int)(c1 - c2);
}
char *q_strlwr (char *str)
{
char *c;
c = str;
while (*c)
{
*c = q_tolower(*c);
c++;
}
return str;
}
char *q_strrev (char *str)
{
char a, *b, *e;
b = e = str;
while (*e++)
;
e -= 2;
while ( b < e )
{
a = *b;
*b = *e;
*e = a;
b++;
e--;
}
return str;
}
/*============================================================================*/
void Error (const char *error, ...)
{
va_list argptr;
fprintf (stderr, "*** ERROR: ***\n");
va_start (argptr, error);
vfprintf (stderr, error, argptr);
va_end (argptr);
fprintf (stderr, "\n");
exit (1);
}
/*============================================================================*/
#if defined(PLATFORM_WINDOWS)
static HANDLE findhandle = INVALID_HANDLE_VALUE;
static WIN32_FIND_DATA finddata;
static char findstr[MAX_OSPATH];
const char *Sys_FindFirstFile (const char *path, const char *pattern)
{
if (findhandle != INVALID_HANDLE_VALUE)
Error ("FindFirst without FindClose");
q_snprintf (findstr, sizeof(findstr), "%s/%s", path, pattern);
findhandle = FindFirstFile(findstr, &finddata);
if (findhandle == INVALID_HANDLE_VALUE)
return NULL;
if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return Sys_FindNextFile();
return finddata.cFileName;
}
const char *Sys_FindNextFile (void)
{
if (findhandle == INVALID_HANDLE_VALUE)
return NULL;
while (FindNextFile(findhandle, &finddata) != 0)
{
if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
return finddata.cFileName;
}
return NULL;
}
void Sys_FindClose (void)
{
if (findhandle != INVALID_HANDLE_VALUE)
{
FindClose(findhandle);
findhandle = INVALID_HANDLE_VALUE;
}
}
int Sys_filesize (const char *filename)
{
HANDLE fh;
WIN32_FIND_DATA data;
int size;
fh = FindFirstFile(filename, &data);
if (fh == INVALID_HANDLE_VALUE)
return -1;
FindClose(fh);
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return -1;
// we're not dealing with gigabytes of files.
// size should normally smaller than INT_MAX.
// size = (data.nFileSizeHigh * (MAXDWORD + 1)) + data.nFileSizeLow;
size = (int) data.nFileSizeLow;
return size;
}
int Sys_getcwd (char *buf, size_t size)
{
const int sz = (int) size;
if (sz <= 0) return 0;
if (_getcwd(buf, sz) == NULL)
return 1;
return 0;
}
#endif /* PLATFORM_WINDOWS */
//============================================================================
#if defined(PLATFORM_UNIX)
static DIR *finddir;
static struct dirent *finddata;
static char *findpath, *findpattern;
static char matchpath[MAX_OSPATH];
const char *Sys_FindFirstFile (const char *path, const char *pattern)
{
if (finddir)
Error ("FindFirst without FindClose");
finddir = opendir (path);
if (!finddir)
return NULL;
findpattern = strdup (pattern);
if (!findpattern)
{
Sys_FindClose();
return NULL;
}
findpath = strdup (path);
if (!findpath)
{
Sys_FindClose();
return NULL;
}
if (*findpath != '\0')
{
/* searching under "/" won't be a good idea, for example.. */
size_t siz = strlen(findpath) - 1;
if (findpath[siz] == '/' || findpath[siz] == '\\')
findpath[siz] = '\0';
}
return Sys_FindNextFile();
}
const char *Sys_FindNextFile (void)
{
struct stat test;
if (!finddir)
return NULL;
while ((finddata = readdir(finddir)) != NULL)
{
if (!fnmatch (findpattern, finddata->d_name, FNM_PATHNAME))
{
q_snprintf(matchpath, sizeof(matchpath), "%s/%s", findpath, finddata->d_name);
if ( (stat(matchpath, &test) == 0)
&& S_ISREG(test.st_mode))
return finddata->d_name;
}
}
return NULL;
}
void Sys_FindClose (void)
{
if (finddir != NULL)
{
closedir(finddir);
finddir = NULL;
}
if (findpath != NULL)
{
free (findpath);
findpath = NULL;
}
if (findpattern != NULL)
{
free (findpattern);
findpattern = NULL;
}
}
int Sys_filesize (const char *filename)
{
struct stat status;
if (stat(filename, &status) == -1)
return -1;
if (! S_ISREG(status.st_mode))
return -1;
return (int) status.st_size;
}
int Sys_getcwd (char *buf, size_t size)
{
if (getcwd(buf, size) == NULL)
return 1;
return 0;
}
#endif /* PLATFORM_UNIX */
/*============================================================================*/
/*========== BYTE ORDER STUFF ================================================*/
#if ENDIAN_RUNTIME_DETECT
#define __byteswap_func static
#else
#define __byteswap_func
#endif
int host_byteorder;
int host_bigendian; /* qboolean */
FUNC_NOINLINE FUNC_NOCLONE
unsigned int get_0x12345678 (void) {
return 0x12345678;
/* U N I X */
}
int DetectByteorder (void)
{
volatile union {
unsigned int i;
unsigned char c[4];
} bint;
bint.i = get_0x12345678 ();
/*
BE_ORDER: 12 34 56 78
U N I X
LE_ORDER: 78 56 34 12
X I N U
PDP_ORDER: 34 12 78 56
N U X I
*/
if (bint.c[0] == 0x12)
return BIG_ENDIAN;
if (bint.c[0] == 0x78)
return LITTLE_ENDIAN;
if (bint.c[0] == 0x34)
return PDP_ENDIAN;
return -1;
}
__byteswap_func
int LongSwap (int l)
{
unsigned char b1, b2, b3, b4;
b1 = l & 255;
b2 = (l>>8 ) & 255;
b3 = (l>>16) & 255;
b4 = (l>>24) & 255;
return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
}
#if ENDIAN_RUNTIME_DETECT
__byteswap_func
int LongNoSwap (int l)
{
return l;
}
int (*BigLong) (int);
int (*LittleLong) (int);
#endif /* ENDIAN_RUNTIME_DETECT */
void ByteOrder_Init (void)
{
host_byteorder = DetectByteorder ();
host_bigendian = (host_byteorder == BIG_ENDIAN);
#if ENDIAN_RUNTIME_DETECT
switch (host_byteorder)
{
case BIG_ENDIAN:
BigLong = LongNoSwap;
LittleLong = LongSwap;
break;
case LITTLE_ENDIAN:
BigLong = LongSwap;
LittleLong = LongNoSwap;
break;
default:
break;
}
#endif /* ENDIAN_RUNTIME_DETECT */
}
/* call this from your main() */
void ValidateByteorder (void)
{
const char *endianism[] = { "BE", "LE", "PDP", "Unknown" };
const char *tmp;
ByteOrder_Init ();
switch (host_byteorder)
{
case BIG_ENDIAN:
tmp = endianism[0];
break;
case LITTLE_ENDIAN:
tmp = endianism[1];
break;
case PDP_ENDIAN:
tmp = endianism[2];
host_byteorder = -1; /* not supported */
break;
default:
tmp = endianism[3];
break;
}
if (host_byteorder < 0)
Error ("Unsupported byte order.");
// printf("Detected byte order: %s\n", tmp);
#if !ENDIAN_RUNTIME_DETECT
if (host_byteorder != BYTE_ORDER)
{
const char *tmp2;
switch (BYTE_ORDER)
{
case BIG_ENDIAN:
tmp2 = endianism[0];
break;
case LITTLE_ENDIAN:
tmp2 = endianism[1];
break;
case PDP_ENDIAN:
tmp2 = endianism[2];
break;
default:
tmp2 = endianism[3];
break;
}
Error ("Detected byte order %s doesn't match compiled %s order!", tmp, tmp2);
}
#endif /* ENDIAN_RUNTIME_DETECT */
}