| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "cmdlib.h" |
| 25 |
#include <sys/types.h> |
| 26 |
#include <sys/stat.h> |
| 27 |
|
| 28 |
#ifdef WIN32 |
| 29 |
#include <direct.h> |
| 30 |
#include <windows.h> |
| 31 |
#elif defined(NeXT) |
| 32 |
#include <libc.h> |
| 33 |
#else |
| 34 |
#include <unistd.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#define BASEDIRNAME "quake" // assumed to have a 2 or 3 following |
| 38 |
#define PATHSEPERATOR '/' |
| 39 |
|
| 40 |
|
| 41 |
int myargc; |
| 42 |
char **myargv; |
| 43 |
|
| 44 |
char com_token[1024]; |
| 45 |
qboolean com_eof; |
| 46 |
|
| 47 |
qboolean archive; |
| 48 |
char archivedir[1024]; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#define MAX_EX_ARGC 1024 |
| 59 |
int ex_argc; |
| 60 |
char *ex_argv[MAX_EX_ARGC]; |
| 61 |
#ifdef _WIN32 |
| 62 |
#include "io.h" |
| 63 |
void ExpandWildcards( int *argc, char ***argv ) |
| 64 |
{ |
| 65 |
struct _finddata_t fileinfo; |
| 66 |
intptr_t handle; |
| 67 |
int i; |
| 68 |
char filename[2048]; |
| 69 |
char filebase[1024]; |
| 70 |
char *path; |
| 71 |
|
| 72 |
ex_argc = 0; |
| 73 |
for (i=0 ; i<*argc ; i++) |
| 74 |
{ |
| 75 |
path = (*argv)[i]; |
| 76 |
if ( path[0] == '-' |
| 77 |
|| ( !strstr(path, "*") && !strstr(path, "?") ) ) |
| 78 |
{ |
| 79 |
ex_argv[ex_argc++] = path; |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
handle = _findfirst (path, &fileinfo); |
| 84 |
if (handle == -1) |
| 85 |
return; |
| 86 |
|
| 87 |
ExtractFilePath (path, filebase); |
| 88 |
|
| 89 |
do |
| 90 |
{ |
| 91 |
snprintf (filename, sizeof(filename), "%s%s", filebase, fileinfo.name); |
| 92 |
ex_argv[ex_argc++] = copystring (filename); |
| 93 |
} while (_findnext( handle, &fileinfo ) != -1); |
| 94 |
|
| 95 |
_findclose (handle); |
| 96 |
} |
| 97 |
|
| 98 |
*argc = ex_argc; |
| 99 |
*argv = ex_argv; |
| 100 |
} |
| 101 |
#else |
| 102 |
void ExpandWildcards (int *argc, char ***argv) |
| 103 |
{ |
| 104 |
} |
| 105 |
#endif |
| 106 |
|
| 107 |
#ifdef WIN_ERROR |
| 108 |
#include <windows.h> |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
void Error( const char *error, ... ) |
| 117 |
{ |
| 118 |
va_list argptr; |
| 119 |
char text[1024]; |
| 120 |
char text2[1024]; |
| 121 |
int err; |
| 122 |
|
| 123 |
err = GetLastError (); |
| 124 |
|
| 125 |
va_start (argptr,error); |
| 126 |
vsprintf (text, error,argptr); |
| 127 |
va_end (argptr); |
| 128 |
|
| 129 |
snprintf (text2, sizeof(text2), "%s\nGetLastError() = %i", text, err); |
| 130 |
MessageBox(NULL, text2, "Error", 0 ); |
| 131 |
|
| 132 |
exit (1); |
| 133 |
} |
| 134 |
|
| 135 |
#else |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
void Error( const char *error, ...) |
| 144 |
{ |
| 145 |
va_list argptr; |
| 146 |
|
| 147 |
_printf ("\n************ ERROR ************\n"); |
| 148 |
|
| 149 |
va_start (argptr,error); |
| 150 |
vprintf (error,argptr); |
| 151 |
va_end (argptr); |
| 152 |
_printf ("\r\n"); |
| 153 |
|
| 154 |
exit (1); |
| 155 |
} |
| 156 |
#endif |
| 157 |
|
| 158 |
|
| 159 |
qboolean verbose = qfalse; |
| 160 |
void qprintf( const char *format, ... ) { |
| 161 |
va_list argptr; |
| 162 |
|
| 163 |
if (!verbose) |
| 164 |
return; |
| 165 |
|
| 166 |
va_start (argptr,format); |
| 167 |
vprintf (format,argptr); |
| 168 |
va_end (argptr); |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
#ifdef WIN32 |
| 173 |
HWND hwndOut = NULL; |
| 174 |
qboolean lookedForServer = qfalse; |
| 175 |
UINT wm_BroadcastCommand = -1; |
| 176 |
#endif |
| 177 |
|
| 178 |
void _printf( const char *format, ... ) { |
| 179 |
va_list argptr; |
| 180 |
char text[4096]; |
| 181 |
#ifdef WIN32 |
| 182 |
ATOM a; |
| 183 |
#endif |
| 184 |
va_start (argptr,format); |
| 185 |
vsprintf (text, format, argptr); |
| 186 |
va_end (argptr); |
| 187 |
|
| 188 |
printf("%s", text); |
| 189 |
|
| 190 |
#ifdef WIN32 |
| 191 |
if (!lookedForServer) { |
| 192 |
lookedForServer = qtrue; |
| 193 |
hwndOut = FindWindow(NULL, "Q3Map Process Server"); |
| 194 |
if (hwndOut) { |
| 195 |
wm_BroadcastCommand = RegisterWindowMessage( "Q3MPS_BroadcastCommand" ); |
| 196 |
} |
| 197 |
} |
| 198 |
if (hwndOut) { |
| 199 |
a = GlobalAddAtom(text); |
| 200 |
PostMessage(hwndOut, wm_BroadcastCommand, 0, (LPARAM)a); |
| 201 |
} |
| 202 |
#endif |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
char qdir[1024]; |
| 218 |
char gamedir[1024]; |
| 219 |
char writedir[1024]; |
| 220 |
|
| 221 |
void SetQdirFromPath( const char *path ) |
| 222 |
{ |
| 223 |
char temp[1024]; |
| 224 |
const char *c; |
| 225 |
const char *sep; |
| 226 |
int len, count; |
| 227 |
|
| 228 |
if (!(path[0] == '/' || path[0] == '\\' || path[1] == ':')) |
| 229 |
{ |
| 230 |
Q_getwd (temp); |
| 231 |
strcat (temp, path); |
| 232 |
path = temp; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
len = strlen(BASEDIRNAME); |
| 238 |
for (c=path+strlen(path)-1 ; c != path ; c--) |
| 239 |
{ |
| 240 |
int i; |
| 241 |
|
| 242 |
if (!Q_strncasecmp (c, BASEDIRNAME, len)) |
| 243 |
{ |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
sep = c + len; |
| 250 |
count = 1; |
| 251 |
while (*sep && *sep != '/' && *sep != '\\') |
| 252 |
{ |
| 253 |
sep++; |
| 254 |
count++; |
| 255 |
} |
| 256 |
strncpy (qdir, path, c+len+count-path); |
| 257 |
qprintf ("qdir: %s\n", qdir); |
| 258 |
for ( i = 0; i < strlen( qdir ); i++ ) |
| 259 |
{ |
| 260 |
if ( qdir[i] == '\\' ) |
| 261 |
qdir[i] = '/'; |
| 262 |
} |
| 263 |
|
| 264 |
c += len+count; |
| 265 |
while (*c) |
| 266 |
{ |
| 267 |
if (*c == '/' || *c == '\\') |
| 268 |
{ |
| 269 |
strncpy (gamedir, path, c+1-path); |
| 270 |
|
| 271 |
for ( i = 0; i < strlen( gamedir ); i++ ) |
| 272 |
{ |
| 273 |
if ( gamedir[i] == '\\' ) |
| 274 |
gamedir[i] = '/'; |
| 275 |
} |
| 276 |
|
| 277 |
qprintf ("gamedir: %s\n", gamedir); |
| 278 |
|
| 279 |
if ( !writedir[0] ) |
| 280 |
strcpy( writedir, gamedir ); |
| 281 |
else if ( writedir[strlen( writedir )-1] != '/' ) |
| 282 |
{ |
| 283 |
writedir[strlen( writedir )] = '/'; |
| 284 |
writedir[strlen( writedir )+1] = 0; |
| 285 |
} |
| 286 |
|
| 287 |
return; |
| 288 |
} |
| 289 |
c++; |
| 290 |
} |
| 291 |
Error ("No gamedir in %s", path); |
| 292 |
return; |
| 293 |
} |
| 294 |
} |
| 295 |
Error ("SetQdirFromPath: no '%s' in %s", BASEDIRNAME, path); |
| 296 |
} |
| 297 |
|
| 298 |
char *ExpandArg (const char *path) |
| 299 |
{ |
| 300 |
static char full[1024]; |
| 301 |
|
| 302 |
if (path[0] != '/' && path[0] != '\\' && path[1] != ':') |
| 303 |
{ |
| 304 |
Q_getwd (full); |
| 305 |
strcat (full, path); |
| 306 |
} |
| 307 |
else |
| 308 |
strcpy (full, path); |
| 309 |
return full; |
| 310 |
} |
| 311 |
|
| 312 |
char *ExpandPath (const char *path) |
| 313 |
{ |
| 314 |
static char full[1024]; |
| 315 |
if (!qdir[0]) |
| 316 |
Error ("ExpandPath called without qdir set"); |
| 317 |
if (path[0] == '/' || path[0] == '\\' || path[1] == ':') { |
| 318 |
strcpy( full, path ); |
| 319 |
return full; |
| 320 |
} |
| 321 |
snprintf (full, sizeof(full), "%s%s", qdir, path); |
| 322 |
return full; |
| 323 |
} |
| 324 |
|
| 325 |
char *ExpandGamePath (const char *path) |
| 326 |
{ |
| 327 |
static char full[1024]; |
| 328 |
if (!qdir[0]) |
| 329 |
Error ("ExpandGamePath called without qdir set"); |
| 330 |
if (path[0] == '/' || path[0] == '\\' || path[1] == ':') { |
| 331 |
strcpy( full, path ); |
| 332 |
return full; |
| 333 |
} |
| 334 |
snprintf (full, sizeof(full), "%s%s", gamedir, path); |
| 335 |
return full; |
| 336 |
} |
| 337 |
|
| 338 |
char *ExpandPathAndArchive (const char *path) |
| 339 |
{ |
| 340 |
char *expanded; |
| 341 |
char archivename[2048]; |
| 342 |
|
| 343 |
expanded = ExpandPath (path); |
| 344 |
|
| 345 |
if (archive) |
| 346 |
{ |
| 347 |
snprintf (archivename, sizeof(archivename), "%s/%s", archivedir, path); |
| 348 |
QCopyFile (expanded, archivename); |
| 349 |
} |
| 350 |
return expanded; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
char *copystring(const char *s) |
| 355 |
{ |
| 356 |
char *b; |
| 357 |
b = malloc(strlen(s)+1); |
| 358 |
strcpy (b, s); |
| 359 |
return b; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
double I_FloatTime (void) |
| 370 |
{ |
| 371 |
time_t t; |
| 372 |
|
| 373 |
time (&t); |
| 374 |
|
| 375 |
return t; |
| 376 |
#if 0 |
| 377 |
|
| 378 |
struct timeval tp; |
| 379 |
struct timezone tzp; |
| 380 |
static int secbase; |
| 381 |
|
| 382 |
gettimeofday(&tp, &tzp); |
| 383 |
|
| 384 |
if (!secbase) |
| 385 |
{ |
| 386 |
secbase = tp.tv_sec; |
| 387 |
return tp.tv_usec/1000000.0; |
| 388 |
} |
| 389 |
|
| 390 |
return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0; |
| 391 |
#endif |
| 392 |
} |
| 393 |
|
| 394 |
void Q_getwd (char *out) |
| 395 |
{ |
| 396 |
int i = 0; |
| 397 |
|
| 398 |
#ifdef WIN32 |
| 399 |
if (_getcwd (out, 256) == NULL) |
| 400 |
strcpy(out, "."); |
| 401 |
strcat (out, "\\"); |
| 402 |
#else |
| 403 |
if (getcwd (out, 256) == NULL) |
| 404 |
strcpy(out, "."); |
| 405 |
strcat (out, "/"); |
| 406 |
#endif |
| 407 |
|
| 408 |
while ( out[i] != 0 ) |
| 409 |
{ |
| 410 |
if ( out[i] == '\\' ) |
| 411 |
out[i] = '/'; |
| 412 |
i++; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
void Q_mkdir (const char *path) |
| 418 |
{ |
| 419 |
#ifdef WIN32 |
| 420 |
if (_mkdir (path) != -1) |
| 421 |
return; |
| 422 |
#else |
| 423 |
if (mkdir (path, 0777) != -1) |
| 424 |
return; |
| 425 |
#endif |
| 426 |
if (errno != EEXIST) |
| 427 |
Error ("mkdir %s: %s",path, strerror(errno)); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
int FileTime (const char *path) |
| 438 |
{ |
| 439 |
struct stat buf; |
| 440 |
|
| 441 |
if (stat (path,&buf) == -1) |
| 442 |
return -1; |
| 443 |
|
| 444 |
return buf.st_mtime; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
char *COM_Parse (char *data) |
| 457 |
{ |
| 458 |
int c; |
| 459 |
int len; |
| 460 |
|
| 461 |
len = 0; |
| 462 |
com_token[0] = 0; |
| 463 |
|
| 464 |
if (!data) |
| 465 |
return NULL; |
| 466 |
|
| 467 |
|
| 468 |
skipwhite: |
| 469 |
while ( (c = *data) <= ' ') |
| 470 |
{ |
| 471 |
if (c == 0) |
| 472 |
{ |
| 473 |
com_eof = qtrue; |
| 474 |
return NULL; |
| 475 |
} |
| 476 |
data++; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
if (c=='/' && data[1] == '/') |
| 481 |
{ |
| 482 |
while (*data && *data != '\n') |
| 483 |
data++; |
| 484 |
goto skipwhite; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
if (c == '\"') |
| 490 |
{ |
| 491 |
data++; |
| 492 |
do |
| 493 |
{ |
| 494 |
c = *data++; |
| 495 |
if (c=='\"') |
| 496 |
{ |
| 497 |
com_token[len] = 0; |
| 498 |
return data; |
| 499 |
} |
| 500 |
com_token[len] = c; |
| 501 |
len++; |
| 502 |
} while (1); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') |
| 507 |
{ |
| 508 |
com_token[len] = c; |
| 509 |
len++; |
| 510 |
com_token[len] = 0; |
| 511 |
return data+1; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
do |
| 516 |
{ |
| 517 |
com_token[len] = c; |
| 518 |
data++; |
| 519 |
len++; |
| 520 |
c = *data; |
| 521 |
if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') |
| 522 |
break; |
| 523 |
} while (c>32); |
| 524 |
|
| 525 |
com_token[len] = 0; |
| 526 |
return data; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
int Q_strncasecmp (const char *s1, const char *s2, int n) |
| 531 |
{ |
| 532 |
int c1, c2; |
| 533 |
|
| 534 |
do |
| 535 |
{ |
| 536 |
c1 = *s1++; |
| 537 |
c2 = *s2++; |
| 538 |
|
| 539 |
if (!n--) |
| 540 |
return 0; |
| 541 |
|
| 542 |
if (c1 != c2) |
| 543 |
{ |
| 544 |
if (c1 >= 'a' && c1 <= 'z') |
| 545 |
c1 -= ('a' - 'A'); |
| 546 |
if (c2 >= 'a' && c2 <= 'z') |
| 547 |
c2 -= ('a' - 'A'); |
| 548 |
if (c1 != c2) |
| 549 |
return -1; |
| 550 |
} |
| 551 |
} while (c1); |
| 552 |
|
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
int Q_stricmp (const char *s1, const char *s2) |
| 557 |
{ |
| 558 |
return Q_strncasecmp (s1, s2, 99999); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
char *Q_strupr (char *start) |
| 563 |
{ |
| 564 |
char *in; |
| 565 |
in = start; |
| 566 |
while (*in) |
| 567 |
{ |
| 568 |
*in = toupper(*in); |
| 569 |
in++; |
| 570 |
} |
| 571 |
return start; |
| 572 |
} |
| 573 |
|
| 574 |
char *Q_strlower (char *start) |
| 575 |
{ |
| 576 |
char *in; |
| 577 |
in = start; |
| 578 |
while (*in) |
| 579 |
{ |
| 580 |
*in = tolower(*in); |
| 581 |
in++; |
| 582 |
} |
| 583 |
return start; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
int CheckParm (const char *check) |
| 605 |
{ |
| 606 |
int i; |
| 607 |
|
| 608 |
for (i = 1;i<myargc;i++) |
| 609 |
{ |
| 610 |
if ( !Q_stricmp(check, myargv[i]) ) |
| 611 |
return i; |
| 612 |
} |
| 613 |
|
| 614 |
return 0; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
int Q_filelength (FILE *f) |
| 625 |
{ |
| 626 |
int pos; |
| 627 |
int end; |
| 628 |
|
| 629 |
pos = ftell (f); |
| 630 |
fseek (f, 0, SEEK_END); |
| 631 |
end = ftell (f); |
| 632 |
fseek (f, pos, SEEK_SET); |
| 633 |
|
| 634 |
return end; |
| 635 |
} |
| 636 |
|
| 637 |
#ifdef MAX_PATH |
| 638 |
#undef MAX_PATH |
| 639 |
#endif |
| 640 |
#define MAX_PATH 4096 |
| 641 |
static FILE* myfopen(const char* filename, const char* mode) |
| 642 |
{ |
| 643 |
char* p; |
| 644 |
char fn[MAX_PATH]; |
| 645 |
|
| 646 |
fn[0] = '\0'; |
| 647 |
strncat(fn, filename, sizeof(fn)-1); |
| 648 |
|
| 649 |
for(p=fn;*p;++p) if(*p == '\\') *p = '/'; |
| 650 |
|
| 651 |
return fopen(fn, mode); |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
FILE *SafeOpenWrite (const char *filename) |
| 656 |
{ |
| 657 |
FILE *f; |
| 658 |
|
| 659 |
f = myfopen(filename, "wb"); |
| 660 |
|
| 661 |
if (!f) |
| 662 |
Error ("Error opening %s: %s",filename,strerror(errno)); |
| 663 |
|
| 664 |
return f; |
| 665 |
} |
| 666 |
|
| 667 |
FILE *SafeOpenRead (const char *filename) |
| 668 |
{ |
| 669 |
FILE *f; |
| 670 |
|
| 671 |
f = myfopen(filename, "rb"); |
| 672 |
|
| 673 |
if (!f) |
| 674 |
Error ("Error opening %s: %s",filename,strerror(errno)); |
| 675 |
|
| 676 |
return f; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
void SafeRead (FILE *f, void *buffer, int count) |
| 681 |
{ |
| 682 |
if ( fread (buffer, 1, count, f) != (size_t)count) |
| 683 |
Error ("File read failure"); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
void SafeWrite (FILE *f, const void *buffer, int count) |
| 688 |
{ |
| 689 |
if (fwrite (buffer, 1, count, f) != (size_t)count) |
| 690 |
Error ("File write failure"); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
qboolean FileExists (const char *filename) |
| 700 |
{ |
| 701 |
FILE *f; |
| 702 |
|
| 703 |
f = myfopen (filename, "r"); |
| 704 |
if (!f) |
| 705 |
return qfalse; |
| 706 |
fclose (f); |
| 707 |
return qtrue; |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
int LoadFile( const char *filename, void **bufferptr ) |
| 716 |
{ |
| 717 |
FILE *f; |
| 718 |
int length; |
| 719 |
void *buffer; |
| 720 |
|
| 721 |
f = SafeOpenRead (filename); |
| 722 |
length = Q_filelength (f); |
| 723 |
buffer = malloc (length+1); |
| 724 |
((char *)buffer)[length] = 0; |
| 725 |
SafeRead (f, buffer, length); |
| 726 |
fclose (f); |
| 727 |
|
| 728 |
*bufferptr = buffer; |
| 729 |
return length; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
int LoadFileBlock( const char *filename, void **bufferptr ) |
| 742 |
{ |
| 743 |
FILE *f; |
| 744 |
int length, nBlock, nAllocSize; |
| 745 |
void *buffer; |
| 746 |
|
| 747 |
f = SafeOpenRead (filename); |
| 748 |
length = Q_filelength (f); |
| 749 |
nAllocSize = length; |
| 750 |
nBlock = nAllocSize % MEM_BLOCKSIZE; |
| 751 |
if ( nBlock > 0) { |
| 752 |
nAllocSize += MEM_BLOCKSIZE - nBlock; |
| 753 |
} |
| 754 |
buffer = malloc (nAllocSize+1); |
| 755 |
memset(buffer, 0, nAllocSize+1); |
| 756 |
SafeRead (f, buffer, length); |
| 757 |
fclose (f); |
| 758 |
|
| 759 |
*bufferptr = buffer; |
| 760 |
return length; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
int TryLoadFile (const char *filename, void **bufferptr) |
| 772 |
{ |
| 773 |
FILE *f; |
| 774 |
int length; |
| 775 |
void *buffer; |
| 776 |
|
| 777 |
*bufferptr = NULL; |
| 778 |
|
| 779 |
f = myfopen (filename, "rb"); |
| 780 |
if (!f) |
| 781 |
return -1; |
| 782 |
length = Q_filelength (f); |
| 783 |
buffer = malloc (length+1); |
| 784 |
((char *)buffer)[length] = 0; |
| 785 |
SafeRead (f, buffer, length); |
| 786 |
fclose (f); |
| 787 |
|
| 788 |
*bufferptr = buffer; |
| 789 |
return length; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
void SaveFile (const char *filename, const void *buffer, int count) |
| 799 |
{ |
| 800 |
FILE *f; |
| 801 |
|
| 802 |
f = SafeOpenWrite (filename); |
| 803 |
SafeWrite (f, buffer, count); |
| 804 |
fclose (f); |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
void DefaultExtension (char *path, const char *extension) |
| 810 |
{ |
| 811 |
char *src; |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
src = path + strlen(path) - 1; |
| 817 |
|
| 818 |
while (*src != '/' && *src != '\\' && src != path) |
| 819 |
{ |
| 820 |
if (*src == '.') |
| 821 |
return; |
| 822 |
src--; |
| 823 |
} |
| 824 |
|
| 825 |
strcat (path, extension); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
void DefaultPath (char *path, const char *basepath) |
| 830 |
{ |
| 831 |
char temp[128]; |
| 832 |
|
| 833 |
if (path[0] == PATHSEPERATOR) |
| 834 |
return; |
| 835 |
strcpy (temp,path); |
| 836 |
strcpy (path,basepath); |
| 837 |
strcat (path,temp); |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
void StripFilename (char *path) |
| 842 |
{ |
| 843 |
int length; |
| 844 |
|
| 845 |
length = strlen(path)-1; |
| 846 |
while (length > 0 && path[length] != PATHSEPERATOR) |
| 847 |
length--; |
| 848 |
path[length] = 0; |
| 849 |
} |
| 850 |
|
| 851 |
void StripExtension (char *path) |
| 852 |
{ |
| 853 |
int length; |
| 854 |
|
| 855 |
length = strlen(path)-1; |
| 856 |
while (length > 0 && path[length] != '.') |
| 857 |
{ |
| 858 |
length--; |
| 859 |
if (path[length] == '/') |
| 860 |
return; |
| 861 |
} |
| 862 |
if (length) |
| 863 |
path[length] = 0; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
void ExtractFilePath (const char *path, char *dest) |
| 875 |
{ |
| 876 |
const char *src; |
| 877 |
|
| 878 |
src = path + strlen(path) - 1; |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
while (src != path && *(src-1) != '\\' && *(src-1) != '/') |
| 884 |
src--; |
| 885 |
|
| 886 |
memcpy (dest, path, src-path); |
| 887 |
dest[src-path] = 0; |
| 888 |
} |
| 889 |
|
| 890 |
void ExtractFileBase (const char *path, char *dest) |
| 891 |
{ |
| 892 |
const char *src; |
| 893 |
|
| 894 |
src = path + strlen(path) - 1; |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
while (src != path && *(src-1) != PATHSEPERATOR) |
| 900 |
src--; |
| 901 |
|
| 902 |
while (*src && *src != '.') |
| 903 |
{ |
| 904 |
*dest++ = *src++; |
| 905 |
} |
| 906 |
*dest = 0; |
| 907 |
} |
| 908 |
|
| 909 |
void ExtractFileExtension (const char *path, char *dest) |
| 910 |
{ |
| 911 |
const char *src; |
| 912 |
|
| 913 |
src = path + strlen(path) - 1; |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
while (src != path && *(src-1) != '.') |
| 919 |
src--; |
| 920 |
if (src == path) |
| 921 |
{ |
| 922 |
*dest = 0; |
| 923 |
return; |
| 924 |
} |
| 925 |
|
| 926 |
strcpy (dest,src); |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
int ParseHex (const char *hex) |
| 936 |
{ |
| 937 |
const char *str; |
| 938 |
int num; |
| 939 |
|
| 940 |
num = 0; |
| 941 |
str = hex; |
| 942 |
|
| 943 |
while (*str) |
| 944 |
{ |
| 945 |
num <<= 4; |
| 946 |
if (*str >= '0' && *str <= '9') |
| 947 |
num += *str-'0'; |
| 948 |
else if (*str >= 'a' && *str <= 'f') |
| 949 |
num += 10 + *str-'a'; |
| 950 |
else if (*str >= 'A' && *str <= 'F') |
| 951 |
num += 10 + *str-'A'; |
| 952 |
else |
| 953 |
Error ("Bad hex number: %s",hex); |
| 954 |
str++; |
| 955 |
} |
| 956 |
|
| 957 |
return num; |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
int ParseNum (const char *str) |
| 962 |
{ |
| 963 |
if (str[0] == '$') |
| 964 |
return ParseHex (str+1); |
| 965 |
if (str[0] == '0' && str[1] == 'x') |
| 966 |
return ParseHex (str+2); |
| 967 |
return atol (str); |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
short ShortSwap (short l) |
| 981 |
{ |
| 982 |
byte b1,b2; |
| 983 |
|
| 984 |
b1 = l&255; |
| 985 |
b2 = (l>>8)&255; |
| 986 |
|
| 987 |
return (b1<<8) + b2; |
| 988 |
} |
| 989 |
|
| 990 |
int LongSwap (int l) |
| 991 |
{ |
| 992 |
byte b1,b2,b3,b4; |
| 993 |
|
| 994 |
b1 = l&255; |
| 995 |
b2 = (l>>8)&255; |
| 996 |
b3 = (l>>16)&255; |
| 997 |
b4 = (l>>24)&255; |
| 998 |
|
| 999 |
return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4; |
| 1000 |
} |
| 1001 |
|
| 1002 |
typedef union { |
| 1003 |
float f; |
| 1004 |
unsigned int i; |
| 1005 |
} _FloatByteUnion; |
| 1006 |
|
| 1007 |
float FloatSwap (const float *f) { |
| 1008 |
_FloatByteUnion out; |
| 1009 |
|
| 1010 |
out.f = *f; |
| 1011 |
out.i = LongSwap(out.i); |
| 1012 |
|
| 1013 |
return out.f; |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
#define CRC_INIT_VALUE 0xffff |
| 1026 |
#define CRC_XOR_VALUE 0x0000 |
| 1027 |
|
| 1028 |
static unsigned short crctable[256] = |
| 1029 |
{ |
| 1030 |
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, |
| 1031 |
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, |
| 1032 |
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, |
| 1033 |
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, |
| 1034 |
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, |
| 1035 |
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, |
| 1036 |
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, |
| 1037 |
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, |
| 1038 |
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, |
| 1039 |
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, |
| 1040 |
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, |
| 1041 |
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, |
| 1042 |
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, |
| 1043 |
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, |
| 1044 |
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, |
| 1045 |
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, |
| 1046 |
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, |
| 1047 |
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, |
| 1048 |
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, |
| 1049 |
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, |
| 1050 |
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, |
| 1051 |
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
| 1052 |
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, |
| 1053 |
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, |
| 1054 |
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, |
| 1055 |
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, |
| 1056 |
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, |
| 1057 |
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, |
| 1058 |
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, |
| 1059 |
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, |
| 1060 |
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, |
| 1061 |
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 |
| 1062 |
}; |
| 1063 |
|
| 1064 |
void CRC_Init(unsigned short *crcvalue) |
| 1065 |
{ |
| 1066 |
*crcvalue = CRC_INIT_VALUE; |
| 1067 |
} |
| 1068 |
|
| 1069 |
void CRC_ProcessByte(unsigned short *crcvalue, byte data) |
| 1070 |
{ |
| 1071 |
*crcvalue = (*crcvalue << 8) ^ crctable[(*crcvalue >> 8) ^ data]; |
| 1072 |
} |
| 1073 |
|
| 1074 |
unsigned short CRC_Value(unsigned short crcvalue) |
| 1075 |
{ |
| 1076 |
return crcvalue ^ CRC_XOR_VALUE; |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
void CreatePath (const char *path) |
| 1086 |
{ |
| 1087 |
const char *ofs; |
| 1088 |
char c; |
| 1089 |
char dir[1024]; |
| 1090 |
|
| 1091 |
#ifdef _WIN32 |
| 1092 |
int olddrive = -1; |
| 1093 |
|
| 1094 |
if ( path[1] == ':' ) |
| 1095 |
{ |
| 1096 |
olddrive = _getdrive(); |
| 1097 |
_chdrive( toupper( path[0] ) - 'A' + 1 ); |
| 1098 |
} |
| 1099 |
#endif |
| 1100 |
|
| 1101 |
if (path[1] == ':') |
| 1102 |
path += 2; |
| 1103 |
|
| 1104 |
for (ofs = path+1 ; *ofs ; ofs++) |
| 1105 |
{ |
| 1106 |
c = *ofs; |
| 1107 |
if (c == '/' || c == '\\') |
| 1108 |
{ |
| 1109 |
memcpy( dir, path, ofs - path ); |
| 1110 |
dir[ ofs - path ] = 0; |
| 1111 |
Q_mkdir( dir ); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
#ifdef _WIN32 |
| 1116 |
if ( olddrive != -1 ) |
| 1117 |
{ |
| 1118 |
_chdrive( olddrive ); |
| 1119 |
} |
| 1120 |
#endif |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
void QCopyFile (const char *from, const char *to) |
| 1132 |
{ |
| 1133 |
void *buffer; |
| 1134 |
int length; |
| 1135 |
|
| 1136 |
length = LoadFile (from, &buffer); |
| 1137 |
CreatePath (to); |
| 1138 |
SaveFile (to, buffer, length); |
| 1139 |
free (buffer); |
| 1140 |
} |