1
//------------------------------------------------------------------------
2
// SYSTEM : System specific code
3
//------------------------------------------------------------------------
5
// GL-Node Viewer (C) 2004-2007 Andrew Apted
7
// This program is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU General Public License
9
// as published by the Free Software Foundation; either version 2
10
// of the License, or (at your option) any later version.
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
17
//------------------------------------------------------------------------
19
// this includes everything we need
22
static char message_buf[1024];
27
void FatalError(const char *str, ...)
29
/// strcpy(message_buf, "\nFATAL ERROR: ");
30
/// char *msg_end = message_buf + strlen(message_buf);
35
vsprintf(message_buf, str, args);
38
PrintDebug(">> FATAL ERROR: %s", message_buf);
40
throw (const char *) message_buf;
46
void InternalError(const char *str, ...)
48
strcpy(message_buf, "\nINTERNAL ERROR: ");
49
char *msg_end = message_buf + strlen(message_buf);
54
vsprintf(msg_end, str, args);
57
PrintDebug(">> %s", message_buf);
59
throw (const char *) message_buf;
65
void PrintMsg(const char *str, ...)
70
vsprintf(message_buf, str, args);
73
printf("%s", message_buf);
75
PrintDebug(">> %s", message_buf);
81
void PrintWarn(const char *str, ...)
86
vsprintf(message_buf, str, args);
89
printf("Warning: %s", message_buf);
91
PrintDebug("Warning: %s", message_buf);
94
//------------------------------------------------------------------------
96
//------------------------------------------------------------------------
98
const char **arg_list = NULL;
104
// Initialise argument list. Do NOT include the program name
105
// (usually in argv[0]). The strings (and array) are copied.
107
// NOTE: doesn't merge multiple uses of an option, hence
108
// using ArgvFind() will only return the first usage.
110
void ArgvInit(int argc, const char **argv)
113
SYS_ASSERT(arg_count >= 0);
121
arg_list = new const char *[arg_count];
125
for (int i = 0; i < arg_count; i++)
127
const char *cur = argv[i];
131
// ignore MacOS X rubbish
132
if (strncmp(cur, "-psn", 4) == 0)
136
// support GNU-style long options
137
if (cur[0] == '-' && cur[1] == '-' && isalnum(cur[2]))
140
arg_list[dest] = strdup(cur);
142
// support DOS-style short options
143
if (cur[0] == '/' && (isalnum(cur[1]) || cur[1] == '?') && cur[2] == 0)
144
*(char *)(arg_list[dest]) = '-';
157
while (arg_count-- > 0)
158
free((void *) arg_list[arg_count]);
167
// Returns index number, or -1 if not found.
169
int ArgvFind(char short_name, const char *long_name, int *num_params)
171
SYS_ASSERT(short_name || long_name);
178
for (; p < arg_count; p++)
180
if (! ArgvIsOption(p))
183
const char *str = arg_list[p];
185
if (short_name && (short_name == tolower(str[1])) && str[2] == 0)
188
if (long_name && (UtilStrCaseCmp(long_name, str + 1) == 0))
192
if (p >= arg_count) // NOT FOUND
199
while ((q < arg_count) && ! ArgvIsOption(q))
202
*num_params = q - p - 1;
208
bool ArgvIsOption(int index)
210
SYS_ASSERT(index >= 0);
211
SYS_ASSERT(index < arg_count);
213
const char *str = arg_list[index];
216
return (str[0] == '-');
219
//------------------------------------------------------------------------
221
//------------------------------------------------------------------------
223
#define DEBUGGING_FILE "nv_debug.txt"
225
static FILE *debug_fp = NULL;
230
void InitDebug(bool enable)
238
debug_fp = fopen(DEBUGGING_FILE, "w");
241
PrintWarn("Unable to open DEBUG FILE: %s\n", DEBUGGING_FILE);
243
PrintDebug("====== START OF DEBUG FILE ======\n\n");
253
PrintDebug("\n====== END OF DEBUG FILE ======\n");
263
void PrintDebug(const char *str, ...)
270
vfprintf(debug_fp, str, args);
277
//------------------------------------------------------------------------
279
//------------------------------------------------------------------------
281
static bool cpu_big_endian = false;
286
// Parts inspired by the Yadex endian.cc code.
288
void InitEndian(void)
297
/* sanity-check type sizes */
299
if (sizeof(uint8_g) != 1)
300
FatalError("Sanity check failed: sizeof(uint8_g) = %d",
303
if (sizeof(uint16_g) != 2)
304
FatalError("Sanity check failed: sizeof(uint16_g) = %d",
307
if (sizeof(uint32_g) != 4)
308
FatalError("Sanity check failed: sizeof(uint32_g) = %d",
311
/* check endianness */
313
memset((uint32_g *) u.mem, 0, sizeof(u.mem));
315
u.mem[0] = 0x70; u.mem[1] = 0x71;
316
u.mem[2] = 0x72; u.mem[3] = 0x73;
318
PrintDebug("Endianness magic value: 0x%08x\n", u.val);
320
if (u.val == 0x70717273)
321
cpu_big_endian = true;
322
else if (u.val == 0x73727170)
323
cpu_big_endian = false;
325
FatalError("Sanity check failed: weird endianness (0x%08x)", u.val);
327
PrintDebug("Endianness = %s\n", cpu_big_endian ? "BIG" : "LITTLE");
329
PrintDebug("Endianness check: 0x1234 --> 0x%04x\n",
330
(int) Endian_U16(0x1234));
332
PrintDebug("Endianness check: 0x11223344 --> 0x%08x\n\n",
333
Endian_U32(0x11223344));
339
uint16_g Endian_U16(uint16_g x)
342
return (x >> 8) | (x << 8);
350
uint32_g Endian_U32(uint32_g x)
353
return (x >> 24) | ((x >> 8) & 0xff00) |
354
((x << 8) & 0xff0000) | (x << 24);