2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// cmdlib.c
|
|
|
|
// TTimo 09/30/2000
|
|
|
|
// from an intial copy of common/cmdlib.c
|
|
|
|
// stripped out the Sys_Printf Sys_Printf stuff
|
|
|
|
|
|
|
|
// SPoG 05/27/2001
|
|
|
|
// merging alpha branch into trunk
|
2012-03-17 20:01:54 +00:00
|
|
|
// replaced qprintf with Sys_Printf
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "mathlib.h"
|
|
|
|
#include "inout.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <direct.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#if defined ( __linux__ ) || defined ( __APPLE__ )
|
2007-11-04 03:34:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NeXT
|
|
|
|
#include <libc.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define BASEDIRNAME "quake" // assumed to have a 2 or 3 following
|
2007-11-04 03:34:51 +00:00
|
|
|
#define PATHSEPERATOR '/'
|
|
|
|
|
|
|
|
#ifdef SAFE_MALLOC
|
2012-03-17 20:01:54 +00:00
|
|
|
void *safe_malloc( size_t size ){
|
|
|
|
void *p;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
p = malloc( size );
|
|
|
|
if ( !p ) {
|
|
|
|
Error( "safe_malloc failed on allocation of %i bytes", size );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return p;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void *safe_malloc_info( size_t size, char* info ){
|
|
|
|
void *p;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
p = malloc( size );
|
|
|
|
if ( !p ) {
|
|
|
|
Error( "%s: safe_malloc failed on allocation of %i bytes", info, size );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return p;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// set these before calling CheckParm
|
|
|
|
int myargc;
|
|
|
|
char **myargv;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char com_token[1024];
|
|
|
|
qboolean com_eof;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
qboolean archive;
|
|
|
|
char archivedir[1024];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===================
|
|
|
|
ExpandWildcards
|
|
|
|
|
|
|
|
Mimic unix command line expansion
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
#define MAX_EX_ARGC 1024
|
|
|
|
int ex_argc;
|
|
|
|
char *ex_argv[MAX_EX_ARGC];
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "io.h"
|
2012-03-17 20:01:54 +00:00
|
|
|
void ExpandWildcards( int *argc, char ***argv ){
|
2007-11-04 03:34:51 +00:00
|
|
|
struct _finddata_t fileinfo;
|
2012-03-17 20:01:54 +00:00
|
|
|
int handle;
|
|
|
|
int i;
|
|
|
|
char filename[1024];
|
|
|
|
char filebase[1024];
|
|
|
|
char *path;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
ex_argc = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < *argc ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
path = ( *argv )[i];
|
2007-11-04 03:34:51 +00:00
|
|
|
if ( path[0] == '-'
|
2012-03-17 20:01:54 +00:00
|
|
|
|| ( !strstr( path, "*" ) && !strstr( path, "?" ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ex_argv[ex_argc++] = path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
handle = _findfirst( path, &fileinfo );
|
|
|
|
if ( handle == -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ExtractFilePath( path, filebase );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( filename, "%s%s", filebase, fileinfo.name );
|
|
|
|
ex_argv[ex_argc++] = copystring( filename );
|
|
|
|
} while ( _findnext( handle, &fileinfo ) != -1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
_findclose( handle );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*argc = ex_argc;
|
|
|
|
*argv = ex_argv;
|
|
|
|
}
|
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
void ExpandWildcards( int *argc, char ***argv ){
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
qdir will hold the path up to the quake directory, including the slash
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
f:\quake\
|
|
|
|
/raid/quake/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
gamedir will hold qdir + the game directory (id1, id2, etc)
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char qdir[1024];
|
|
|
|
char gamedir[1024];
|
|
|
|
char writedir[1024];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SetQdirFromPath( const char *path ){
|
|
|
|
char temp[1024];
|
|
|
|
const char *c;
|
|
|
|
const char *sep;
|
|
|
|
int len, count;
|
|
|
|
|
|
|
|
if ( !( path[0] == '/' || path[0] == '\\' || path[1] == ':' ) ) { // path is partial
|
|
|
|
Q_getwd( temp );
|
|
|
|
strcat( temp, path );
|
2007-11-04 03:34:51 +00:00
|
|
|
path = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// search for "quake2" in path
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
len = strlen( BASEDIRNAME );
|
|
|
|
for ( c = path + strlen( path ) - 1 ; c != path ; c-- )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !Q_strncasecmp( c, BASEDIRNAME, len ) ) {
|
|
|
|
//
|
2007-11-04 03:34:51 +00:00
|
|
|
//strncpy (qdir, path, c+len+2-path);
|
2012-03-17 20:01:54 +00:00
|
|
|
// the +2 assumes a 2 or 3 following quake which is not the
|
|
|
|
// case with a retail install
|
|
|
|
// so we need to add up how much to the next separator
|
|
|
|
sep = c + len;
|
|
|
|
count = 1;
|
|
|
|
while ( *sep && *sep != '/' && *sep != '\\' )
|
|
|
|
{
|
|
|
|
sep++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
strncpy( qdir, path, c + len + count - path );
|
|
|
|
Sys_Printf( "qdir: %s\n", qdir );
|
2007-11-04 03:34:51 +00:00
|
|
|
for ( i = 0; i < strlen( qdir ); i++ )
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( qdir[i] == '\\' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
qdir[i] = '/';
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
c += len + count;
|
|
|
|
while ( *c )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( *c == '/' || *c == '\\' ) {
|
|
|
|
strncpy( gamedir, path, c + 1 - path );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < strlen( gamedir ); i++ )
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( gamedir[i] == '\\' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
gamedir[i] = '/';
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "gamedir: %s\n", gamedir );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !writedir[0] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
strcpy( writedir, gamedir );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( writedir[strlen( writedir ) - 1] != '/' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
writedir[strlen( writedir )] = '/';
|
2012-03-17 20:01:54 +00:00
|
|
|
writedir[strlen( writedir ) + 1] = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "No gamedir in %s", path );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "SetQdirFromPath: no '%s' in %s", BASEDIRNAME, path );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char *ExpandArg( const char *path ){
|
2007-11-04 03:34:51 +00:00
|
|
|
static char full[1024];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( path[0] != '/' && path[0] != '\\' && path[1] != ':' ) {
|
|
|
|
Q_getwd( full );
|
|
|
|
strcat( full, path );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
strcpy( full, path );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
return full;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char *ExpandPath( const char *path ){
|
2007-11-04 03:34:51 +00:00
|
|
|
static char full[1024];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( path[0] == '/' || path[0] == '\\' || path[1] == ':' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
strcpy( full, path );
|
|
|
|
return full;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( full, "%s%s", qdir, path );
|
2007-11-04 03:34:51 +00:00
|
|
|
return full;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char *copystring( const char *s ){
|
|
|
|
char *b;
|
|
|
|
b = safe_malloc( strlen( s ) + 1 );
|
|
|
|
strcpy( b, s );
|
2007-11-04 03:34:51 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
================
|
|
|
|
I_FloatTime
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
double I_FloatTime( void ){
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
time( &t );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
return t;
|
|
|
|
#if 0
|
|
|
|
// more precise, less portable
|
|
|
|
struct timeval tp;
|
|
|
|
struct timezone tzp;
|
2012-03-17 20:01:54 +00:00
|
|
|
static int secbase;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
gettimeofday( &tp, &tzp );
|
|
|
|
|
|
|
|
if ( !secbase ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
secbase = tp.tv_sec;
|
2012-03-17 20:01:54 +00:00
|
|
|
return tp.tv_usec / 1000000.0;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
return ( tp.tv_sec - secbase ) + tp.tv_usec / 1000000.0;
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Q_getwd( char *out ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int i = 0;
|
|
|
|
|
2008-07-25 07:31:37 +00:00
|
|
|
#ifdef WIN32
|
2012-03-17 20:01:54 +00:00
|
|
|
_getcwd( out, 256 );
|
|
|
|
strcat( out, "\\" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
// Gef: Changed from getwd() to getcwd() to avoid potential buffer overflow
|
|
|
|
getcwd( out, 256 );
|
|
|
|
strcat( out, "/" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( out[i] != 0 )
|
|
|
|
{
|
|
|
|
if ( out[i] == '\\' ) {
|
|
|
|
out[i] = '/';
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Q_mkdir( const char *path ){
|
2008-07-25 07:31:37 +00:00
|
|
|
#ifdef WIN32
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( _mkdir( path ) != -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( mkdir( path, 0777 ) != -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( errno != EEXIST ) {
|
|
|
|
Error( "mkdir %s: %s",path, strerror( errno ) );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
FileTime
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
returns -1 if not present
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
int FileTime( const char *path ){
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if ( stat( path,&buf ) == -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
return buf.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
COM_Parse
|
|
|
|
|
|
|
|
Parse a token out of a string
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
char *COM_Parse( char *data ){
|
|
|
|
int c;
|
|
|
|
int len;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
com_token[0] = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
if ( !data ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// skip whitespace
|
|
|
|
skipwhite:
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( ( c = *data ) <= ' ' )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
com_eof = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
return NULL; // end of file;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
data++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// skip // comments
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '/' && data[1] == '/' ) {
|
|
|
|
while ( *data && *data != '\n' )
|
2007-11-04 03:34:51 +00:00
|
|
|
data++;
|
|
|
|
goto skipwhite;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// handle quoted strings specially
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '\"' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
data++;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c = *data++;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '\"' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
com_token[len] = 0;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
com_token[len] = c;
|
|
|
|
len++;
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse single characters
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
com_token[len] = c;
|
|
|
|
len++;
|
|
|
|
com_token[len] = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
return data + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse a regular word
|
|
|
|
do
|
|
|
|
{
|
|
|
|
com_token[len] = c;
|
|
|
|
data++;
|
|
|
|
len++;
|
|
|
|
c = *data;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
} while ( c > 32 );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
com_token[len] = 0;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int Q_strncasecmp( const char *s1, const char *s2, int n ){
|
|
|
|
int c1, c2;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
c1 = *s1++;
|
|
|
|
c2 = *s2++;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !n-- ) {
|
|
|
|
return 0; // strings are equal until end point
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c1 != c2 ) {
|
|
|
|
if ( c1 >= 'a' && c1 <= 'z' ) {
|
|
|
|
c1 -= ( 'a' - 'A' );
|
|
|
|
}
|
|
|
|
if ( c2 >= 'a' && c2 <= 'z' ) {
|
|
|
|
c2 -= ( 'a' - 'A' );
|
|
|
|
}
|
|
|
|
if ( c1 != c2 ) {
|
|
|
|
return -1; // strings not equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ( c1 );
|
|
|
|
|
|
|
|
return 0; // strings are equal
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int Q_stricmp( const char *s1, const char *s2 ){
|
|
|
|
return Q_strncasecmp( s1, s2, 99999 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE TTimo when switching to Multithread DLL (Release/Debug) in the config
|
|
|
|
// started getting warnings about that function, prolly a duplicate with the runtime function
|
|
|
|
// maybe we still need to have it in linux builds
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
char *strupr (char *start)
|
|
|
|
{
|
|
|
|
char *in;
|
|
|
|
in = start;
|
|
|
|
while (*in)
|
|
|
|
{
|
|
|
|
*in = toupper(*in);
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char *strlower( char *start ){
|
|
|
|
char *in;
|
2007-11-04 03:34:51 +00:00
|
|
|
in = start;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( *in )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
*in = tolower( *in );
|
2007-11-04 03:34:51 +00:00
|
|
|
in++;
|
|
|
|
}
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============================================================================
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
MISC FUNCTIONS
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
=============================================================================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=================
|
|
|
|
CheckParm
|
|
|
|
|
|
|
|
Checks for the given parameter in the program's command line arguments
|
|
|
|
Returns the argument number (1 to argc-1) or 0 if not present
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
int CheckParm( const char *check ){
|
|
|
|
int i;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 1; i < myargc; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !Q_stricmp( check, myargv[i] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return i;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
================
|
|
|
|
Q_filelength
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
int Q_filelength( FILE *f ){
|
|
|
|
int pos;
|
|
|
|
int end;
|
|
|
|
|
|
|
|
pos = ftell( f );
|
|
|
|
fseek( f, 0, SEEK_END );
|
|
|
|
end = ftell( f );
|
|
|
|
fseek( f, pos, SEEK_SET );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
FILE *SafeOpenWrite( const char *filename ){
|
|
|
|
FILE *f;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
f = fopen( filename, "wb" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !f ) {
|
|
|
|
Error( "Error opening %s: %s",filename,strerror( errno ) );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
FILE *SafeOpenRead( const char *filename ){
|
|
|
|
FILE *f;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
f = fopen( filename, "rb" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !f ) {
|
|
|
|
Error( "Error opening %s: %s",filename,strerror( errno ) );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SafeRead( FILE *f, void *buffer, int count ){
|
|
|
|
if ( fread( buffer, 1, count, f ) != (size_t)count ) {
|
|
|
|
Error( "File read failure" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SafeWrite( FILE *f, const void *buffer, int count ){
|
|
|
|
if ( fwrite( buffer, 1, count, f ) != (size_t)count ) {
|
|
|
|
Error( "File write failure" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
FileExists
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
qboolean FileExists( const char *filename ){
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fopen( filename, "r" );
|
|
|
|
if ( !f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
LoadFile
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
int LoadFile( const char *filename, void **bufferptr ){
|
|
|
|
FILE *f;
|
|
|
|
int length;
|
2007-11-04 03:34:51 +00:00
|
|
|
void *buffer;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
f = SafeOpenRead( filename );
|
|
|
|
length = Q_filelength( f );
|
|
|
|
buffer = safe_malloc( length + 1 );
|
|
|
|
( (char *)buffer )[length] = 0;
|
|
|
|
SafeRead( f, buffer, length );
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
*bufferptr = buffer;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
LoadFileBlock
|
|
|
|
-
|
|
|
|
rounds up memory allocation to 4K boundry
|
|
|
|
-
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
int LoadFileBlock( const char *filename, void **bufferptr ){
|
|
|
|
FILE *f;
|
|
|
|
int length, nBlock, nAllocSize;
|
2007-11-04 03:34:51 +00:00
|
|
|
void *buffer;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
f = SafeOpenRead( filename );
|
|
|
|
length = Q_filelength( f );
|
|
|
|
nAllocSize = length;
|
|
|
|
nBlock = nAllocSize % MEM_BLOCKSIZE;
|
|
|
|
if ( nBlock > 0 ) {
|
|
|
|
nAllocSize += MEM_BLOCKSIZE - nBlock;
|
|
|
|
}
|
|
|
|
buffer = safe_malloc( nAllocSize + 1 );
|
|
|
|
memset( buffer, 0, nAllocSize + 1 );
|
|
|
|
SafeRead( f, buffer, length );
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
*bufferptr = buffer;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
TryLoadFile
|
|
|
|
|
|
|
|
Allows failure
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
int TryLoadFile( const char *filename, void **bufferptr ){
|
|
|
|
FILE *f;
|
|
|
|
int length;
|
2007-11-04 03:34:51 +00:00
|
|
|
void *buffer;
|
|
|
|
|
|
|
|
*bufferptr = NULL;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
f = fopen( filename, "rb" );
|
|
|
|
if ( !f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
length = Q_filelength( f );
|
|
|
|
buffer = safe_malloc( length + 1 );
|
|
|
|
( (char *)buffer )[length] = 0;
|
|
|
|
SafeRead( f, buffer, length );
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
*bufferptr = buffer;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
SaveFile
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void SaveFile( const char *filename, const void *buffer, int count ){
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = SafeOpenWrite( filename );
|
|
|
|
SafeWrite( f, buffer, count );
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void DefaultExtension( char *path, const char *extension ){
|
2007-11-04 03:34:51 +00:00
|
|
|
char *src;
|
|
|
|
//
|
|
|
|
// if path doesnt have a .EXT, append extension
|
|
|
|
// (extension should include the .)
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
src = path + strlen( path ) - 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( *src != '/' && *src != '\\' && src != path )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( *src == '.' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return; // it has an extension
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
src--;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcat( path, extension );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void DefaultPath( char *path, const char *basepath ){
|
|
|
|
char temp[128];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( path[ 0 ] == '/' || path[ 0 ] == '\\' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return; // absolute path location
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
strcpy( temp,path );
|
|
|
|
strcpy( path,basepath );
|
|
|
|
strcat( path,temp );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void StripFilename( char *path ){
|
|
|
|
int length;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
length = strlen( path ) - 1;
|
|
|
|
while ( length > 0 && path[length] != '/' && path[ length ] != '\\' )
|
2007-11-04 03:34:51 +00:00
|
|
|
length--;
|
|
|
|
path[length] = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void StripExtension( char *path ){
|
|
|
|
int length;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
length = strlen( path ) - 1;
|
|
|
|
while ( length > 0 && path[length] != '.' )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
length--;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( path[length] == '/' || path[ length ] == '\\' ) {
|
|
|
|
return; // no extension
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( length ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
path[length] = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
====================
|
|
|
|
Extract file parts
|
|
|
|
====================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
// FIXME: should include the slash, otherwise
|
|
|
|
// backing to an empty path will be wrong when appending a slash
|
2012-03-17 20:01:54 +00:00
|
|
|
void ExtractFilePath( const char *path, char *dest ){
|
2007-11-04 03:34:51 +00:00
|
|
|
const char *src;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
src = path + strlen( path ) - 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// back up until a \ or the start
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( src != path && *( src - 1 ) != '\\' && *( src - 1 ) != '/' )
|
2007-11-04 03:34:51 +00:00
|
|
|
src--;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( dest, path, src - path );
|
|
|
|
dest[src - path] = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void ExtractFileBase( const char *path, char *dest ){
|
2007-11-04 03:34:51 +00:00
|
|
|
const char *src;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
src = path + strlen( path ) - 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// back up until a \ or the start
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( src != path && *( src - 1 ) != '/' && *( src - 1 ) != '\\' )
|
2007-11-04 03:34:51 +00:00
|
|
|
src--;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( *src && *src != '.' )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
*dest = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void ExtractFileExtension( const char *path, char *dest ){
|
2007-11-04 03:34:51 +00:00
|
|
|
const char *src;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
src = path + strlen( path ) - 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// back up until a . or the start
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( src != path && *( src - 1 ) != '.' )
|
2007-11-04 03:34:51 +00:00
|
|
|
src--;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( src == path ) {
|
|
|
|
*dest = 0; // no extension
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( dest,src );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
ParseNum / ParseHex
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
int ParseHex( const char *hex ){
|
2007-11-04 03:34:51 +00:00
|
|
|
const char *str;
|
2012-03-17 20:01:54 +00:00
|
|
|
int num;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
num = 0;
|
|
|
|
str = hex;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( *str )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
num <<= 4;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( *str >= '0' && *str <= '9' ) {
|
|
|
|
num += *str - '0';
|
|
|
|
}
|
|
|
|
else if ( *str >= 'a' && *str <= 'f' ) {
|
|
|
|
num += 10 + *str - 'a';
|
|
|
|
}
|
|
|
|
else if ( *str >= 'A' && *str <= 'F' ) {
|
|
|
|
num += 10 + *str - 'A';
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
Error( "Bad hex number: %s",hex );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int ParseNum( const char *str ){
|
|
|
|
if ( str[0] == '$' ) {
|
|
|
|
return ParseHex( str + 1 );
|
|
|
|
}
|
|
|
|
if ( str[0] == '0' && str[1] == 'x' ) {
|
|
|
|
return ParseHex( str + 2 );
|
|
|
|
}
|
|
|
|
return atol( str );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============================================================================
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
BYTE ORDER FUNCTIONS
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
============================================================================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#ifdef _SGI_SOURCE
|
2012-03-17 20:01:54 +00:00
|
|
|
#define __BIG_ENDIAN__
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __BIG_ENDIAN__
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
short LittleShort( short l ){
|
|
|
|
byte b1,b2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = ( l >> 8 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return ( b1 << 8 ) + b2;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
short BigShort( short l ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int LittleLong( int l ){
|
|
|
|
byte b1,b2,b3,b4;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = ( l >> 8 ) & 255;
|
|
|
|
b3 = ( l >> 16 ) & 255;
|
|
|
|
b4 = ( l >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return ( (int)b1 << 24 ) + ( (int)b2 << 16 ) + ( (int)b3 << 8 ) + b4;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int BigLong( int l ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
float LittleFloat( float l ){
|
|
|
|
union {byte b[4]; float f; } in, out;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
in.f = l;
|
|
|
|
out.b[0] = in.b[3];
|
|
|
|
out.b[1] = in.b[2];
|
|
|
|
out.b[2] = in.b[1];
|
|
|
|
out.b[3] = in.b[0];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
return out.f;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
float BigFloat( float l ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
short BigShort( short l ){
|
|
|
|
byte b1,b2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = ( l >> 8 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return ( b1 << 8 ) + b2;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
short LittleShort( short l ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int BigLong( int l ){
|
|
|
|
byte b1,b2,b3,b4;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
b1 = l & 255;
|
|
|
|
b2 = ( l >> 8 ) & 255;
|
|
|
|
b3 = ( l >> 16 ) & 255;
|
|
|
|
b4 = ( l >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return ( (int)b1 << 24 ) + ( (int)b2 << 16 ) + ( (int)b3 << 8 ) + b4;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int LittleLong( int l ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
float BigFloat( float l ){
|
|
|
|
union {byte b[4]; float f; } in, out;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
in.f = l;
|
|
|
|
out.b[0] = in.b[3];
|
|
|
|
out.b[1] = in.b[2];
|
|
|
|
out.b[2] = in.b[1];
|
|
|
|
out.b[3] = in.b[0];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
return out.f;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
float LittleFloat( float l ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
//=======================================================
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: byte swap?
|
|
|
|
|
|
|
|
// this is a 16 bit, non-reflected CRC using the polynomial 0x1021
|
|
|
|
// and the initial and final xor values shown below... in other words, the
|
|
|
|
// CCITT standard CRC used by XMODEM
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define CRC_INIT_VALUE 0xffff
|
|
|
|
#define CRC_XOR_VALUE 0x0000
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
static unsigned short crctable[256] =
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
|
|
|
|
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
|
|
|
|
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
|
|
|
|
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
|
|
|
|
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
|
|
|
|
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
|
|
|
|
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
|
|
|
|
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
|
|
|
|
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
|
|
|
|
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
|
|
|
|
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
|
|
|
|
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
|
|
|
|
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
|
|
|
|
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
|
|
|
|
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
|
|
|
|
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
|
|
|
|
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
|
|
|
|
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
|
|
|
|
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
|
|
|
|
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
|
|
|
|
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
|
|
|
|
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
|
|
|
|
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
|
|
|
|
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
|
|
|
|
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
|
|
|
|
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
|
|
|
|
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
|
|
|
|
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
|
|
|
|
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
|
|
|
|
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
|
|
|
|
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
|
|
|
|
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
|
2007-11-04 03:34:51 +00:00
|
|
|
};
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CRC_Init( unsigned short *crcvalue ){
|
2007-11-04 03:34:51 +00:00
|
|
|
*crcvalue = CRC_INIT_VALUE;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CRC_ProcessByte( unsigned short *crcvalue, byte data ){
|
|
|
|
*crcvalue = ( *crcvalue << 8 ) ^ crctable[( *crcvalue >> 8 ) ^ data];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned short CRC_Value( unsigned short crcvalue ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return crcvalue ^ CRC_XOR_VALUE;
|
|
|
|
}
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
CreatePath
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void CreatePath( const char *path ){
|
|
|
|
const char *ofs;
|
|
|
|
char c;
|
|
|
|
char dir[1024];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-03-17 20:01:54 +00:00
|
|
|
int olddrive = -1;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( path[1] == ':' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
olddrive = _getdrive();
|
|
|
|
_chdrive( toupper( path[0] ) - 'A' + 1 );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( path[1] == ':' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
path += 2;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ofs = path + 1 ; *ofs ; ofs++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
c = *ofs;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '/' || c == '\\' ) { // create the directory
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( dir, path, ofs - path );
|
|
|
|
dir[ ofs - path ] = 0;
|
|
|
|
Q_mkdir( dir );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( olddrive != -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
_chdrive( olddrive );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
QCopyFile
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Used to archive source files
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void QCopyFile( const char *from, const char *to ){
|
|
|
|
void *buffer;
|
|
|
|
int length;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
length = LoadFile( from, &buffer );
|
|
|
|
CreatePath( to );
|
|
|
|
SaveFile( to, buffer, length );
|
|
|
|
free( buffer );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Sys_Sleep( int n ){
|
2008-07-25 07:31:37 +00:00
|
|
|
#ifdef WIN32
|
2012-03-17 20:01:54 +00:00
|
|
|
Sleep( n );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
#if defined ( __linux__ ) || defined ( __APPLE__ )
|
|
|
|
usleep( n * 1000 );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
}
|