2006-04-13 20:47:06 +00:00
|
|
|
/*
|
|
|
|
* util_lib.c
|
|
|
|
* Utility functions to emulate MACT
|
|
|
|
*
|
|
|
|
* by Jonathon Fowler
|
|
|
|
*
|
|
|
|
* Since we weren't given the source for MACT386.LIB so I've had to do some
|
|
|
|
* creative interpolation here.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Duke Nukem Copyright (C) 1996, 2003 3D Realms Entertainment
|
|
|
|
|
|
|
|
This file is part of Duke Nukem 3D version 1.5 - Atomic Edition
|
|
|
|
|
|
|
|
Duke Nukem 3D 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 the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "util_lib.h"
|
|
|
|
#include "baselayer.h"
|
|
|
|
|
|
|
|
//#define MOTOROLA
|
|
|
|
|
|
|
|
|
|
|
|
static void (*ShutDown)(void) = NULL; // this is defined by whoever links us
|
|
|
|
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
void RegisterShutdownFunction(void (* sh)(void))
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2008-06-29 10:41:01 +00:00
|
|
|
ShutDown = sh;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef RENDERTYPEWIN
|
|
|
|
void Error(char *error, ...)
|
|
|
|
{
|
2008-06-29 10:41:01 +00:00
|
|
|
va_list va;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
if (ShutDown) ShutDown();
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
va_start(va, error);
|
|
|
|
vprintf(error, va);
|
|
|
|
va_end(va);
|
|
|
|
printf("\n\n");
|
|
|
|
}
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
exit((error != NULL));
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char CheckParm(char *check)
|
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t c;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
for (c=1;c<_buildargc;c++)
|
|
|
|
{
|
|
|
|
if (_buildargv[c][0] == '/' || _buildargv[c][0] == '-')
|
|
|
|
if (!Bstrcasecmp(&_buildargv[c][1], check)) return c;
|
|
|
|
}
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
return 0;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
void *SafeMalloc(int32 size)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2008-06-29 10:41:01 +00:00
|
|
|
void *p;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
p = malloc(size);
|
|
|
|
if (!p) Error("SafeMalloc failure for %d bytes",size);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
return p;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
void SafeFree(void * ptr)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2008-06-29 10:41:01 +00:00
|
|
|
if (!ptr) Error("Tried to deallocate NULL pointer.");
|
|
|
|
free(ptr);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
void SafeRealloc(void ** ptr, int32 newsize)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2008-06-29 10:41:01 +00:00
|
|
|
void *p;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
p = realloc(*ptr, newsize);
|
|
|
|
if (!p) Error("SafeRealloc failure for %d bytes",newsize);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
*ptr = p;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
int32 ParseHex(char *hex)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2008-06-29 10:41:01 +00:00
|
|
|
return strtol(hex, NULL, 16);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
int32 ParseNum(char *str)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2008-06-29 10:41:01 +00:00
|
|
|
return strtol(str, NULL, 10);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
int16 MotoShort(int16 l)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
#if B_LITTLE_ENDIAN != 0
|
2008-06-29 10:41:01 +00:00
|
|
|
return l;
|
2006-04-13 20:47:06 +00:00
|
|
|
#else
|
2008-06-29 10:41:01 +00:00
|
|
|
return ((l & 0x00ff) << 8) | ((l & 0xff00) >> 8);
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
int16 IntelShort(int16 l)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
#if B_BIG_ENDIAN != 0
|
2008-06-29 10:41:01 +00:00
|
|
|
return ((l & 0x00ff) << 8) | ((l & 0xff00) >> 8);
|
2006-04-13 20:47:06 +00:00
|
|
|
#else
|
2008-06-29 10:41:01 +00:00
|
|
|
return l;
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
int32 MotoLong(int32 l)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
#if B_LITTLE_ENDIAN != 0
|
2008-06-29 10:41:01 +00:00
|
|
|
return l;
|
2006-04-13 20:47:06 +00:00
|
|
|
#else
|
2008-06-29 10:41:01 +00:00
|
|
|
int32 t = ((l & 0x00ff00ffl) << 8) | ((l & 0xff00ff00l) >> 8);
|
|
|
|
return ((t & 0x0000ffffl) << 16) | ((t & 0xffff0000l) >> 16);
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-06-29 10:41:01 +00:00
|
|
|
int32 IntelLong(int32 l)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
#if B_BIG_ENDIAN != 0
|
2008-06-29 10:41:01 +00:00
|
|
|
int32 t = ((l & 0x00ff00ffl) << 8) | ((l & 0xff00ff00l) >> 8);
|
|
|
|
return ((t & 0x0000ffffl) << 16) | ((t & 0xffff0000l) >> 16);
|
2006-04-13 20:47:06 +00:00
|
|
|
#else
|
2008-06-29 10:41:01 +00:00
|
|
|
return l;
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|