-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutil_lib.c
More file actions
155 lines (121 loc) · 3.04 KB
/
util_lib.c
File metadata and controls
155 lines (121 loc) · 3.04 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
/*
* 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
void RegisterShutdownFunction( void (* sh) (void) )
{
ShutDown = sh;
}
void Error(const char *error, ...)
{
if (ShutDown) ShutDown();
if (error) {
va_list va;
char *buf = NULL;
va_start(va,error);
(void)Bvasprintf(&buf,error,va);
va_end(va);
wm_msgbox("Fatal Error", "%s", buf);
if (buf) free(buf);
}
exit((error != NULL));
}
char CheckParm(char *check)
{
int c;
for (c=1;c<_buildargc;c++) {
if (_buildargv[c][0] == '/' || _buildargv[c][0] == '-')
if (!Bstrcasecmp(&_buildargv[c][1], check)) return c;
}
return 0;
}
void *SafeMalloc (int32 size)
{
void *p;
p = malloc(size);
if (!p) Error("SafeMalloc failure for %d bytes",size);
return p;
}
void SafeFree (void * ptr)
{
if (!ptr) Error("Tried to deallocate NULL pointer.");
free(ptr);
}
void SafeRealloc (void ** ptr, int32 newsize)
{
void *p;
p = realloc(*ptr, newsize);
if (!p) Error("SafeRealloc failure for %d bytes",newsize);
*ptr = p;
}
int32 ParseHex (char *hex)
{
return (int32)strtol(hex, NULL, 16);
}
int32 ParseNum (char *str)
{
return (int32)strtol(str, NULL, 10);
}
int16 MotoShort (int16 l)
{
#if B_LITTLE_ENDIAN != 0
return l;
#else
return ((l & 0x00ff) << 8) | ((l & 0xff00) >> 8);
#endif
}
int16 IntelShort (int16 l)
{
#if B_BIG_ENDIAN != 0
return ((l & 0x00ff) << 8) | ((l & 0xff00) >> 8);
#else
return l;
#endif
}
int32 MotoLong (int32 l)
{
#if B_LITTLE_ENDIAN != 0
return l;
#else
int32 t = ((l & 0x00ff00ffl) << 8) | ((l & 0xff00ff00l) >> 8);
return ((t & 0x0000ffffl) << 16) | ((t & 0xffff0000l) >> 16);
#endif
}
int32 IntelLong (int32 l)
{
#if B_BIG_ENDIAN != 0
int32 t = ((l & 0x00ff00ffl) << 8) | ((l & 0xff00ff00l) >> 8);
return ((t & 0x0000ffffl) << 16) | ((t & 0xffff0000l) >> 16);
#else
return l;
#endif
}