~ubuntu-branches/ubuntu/jaunty/glbsp/jaunty

« back to all changes in this revision

Viewing changes to nodeview/system.cc

  • Committer: Bazaar Package Importer
  • Author(s): Darren Salt
  • Date: 2008-01-30 13:33:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080130133349-kgojg33vyiu8xbvp
Tags: 2.24-1
* New upstream release.
* Bumped the lib soname and the library package name due to one silly
  little binary incompatibility caused by changes in an exported struct.
  (Safe; nothing else currently in the archive has ever used libglbsp2.)
* Removed my patches since they're all applied upstream.
* Updated the list of documentation files.
* Build-time changes:
  - Switched from dh_movefiles to dh_install.
  - Updated my makefile to cope with upstream changes.
  - Corrected for debian-rules-ignores-make-clean-error.
  - Corrected for substvar-source-version-is-deprecated.
  - Link libglbsp, rather than glbsp, with libm and libz.
* Fixed shlibdeps. (Closes: #460387)
* Bumped standards version to 3.7.3 (no other changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//------------------------------------------------------------------------
 
2
//  SYSTEM : System specific code
 
3
//------------------------------------------------------------------------
 
4
//
 
5
//  GL-Node Viewer (C) 2004-2007 Andrew Apted
 
6
//
 
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.
 
11
//
 
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.
 
16
//
 
17
//------------------------------------------------------------------------
 
18
 
 
19
// this includes everything we need
 
20
#include "defs.h"
 
21
 
 
22
static char message_buf[1024];
 
23
 
 
24
//
 
25
// FatalError
 
26
//
 
27
void FatalError(const char *str, ...)
 
28
{
 
29
/// strcpy(message_buf, "\nFATAL ERROR: ");
 
30
/// char *msg_end = message_buf + strlen(message_buf);
 
31
 
 
32
  va_list args;
 
33
 
 
34
  va_start(args, str);
 
35
  vsprintf(message_buf, str, args);
 
36
  va_end(args);
 
37
 
 
38
  PrintDebug(">> FATAL ERROR: %s", message_buf);
 
39
 
 
40
  throw (const char *) message_buf;
 
41
}
 
42
 
 
43
//
 
44
// InternalError
 
45
//
 
46
void InternalError(const char *str, ...)
 
47
{
 
48
  strcpy(message_buf, "\nINTERNAL ERROR: ");
 
49
  char *msg_end = message_buf + strlen(message_buf);
 
50
 
 
51
  va_list args;
 
52
 
 
53
  va_start(args, str);
 
54
  vsprintf(msg_end, str, args);
 
55
  va_end(args);
 
56
 
 
57
  PrintDebug(">> %s", message_buf);
 
58
 
 
59
  throw (const char *) message_buf;
 
60
}
 
61
 
 
62
//
 
63
// PrintMsg
 
64
//
 
65
void PrintMsg(const char *str, ...)
 
66
{
 
67
  va_list args;
 
68
 
 
69
  va_start(args, str);
 
70
  vsprintf(message_buf, str, args);
 
71
  va_end(args);
 
72
 
 
73
  printf("%s", message_buf);
 
74
 
 
75
  PrintDebug(">> %s", message_buf);
 
76
}
 
77
 
 
78
//
 
79
// PrintWarn
 
80
//
 
81
void PrintWarn(const char *str, ...)
 
82
{
 
83
  va_list args;
 
84
 
 
85
  va_start(args, str);
 
86
  vsprintf(message_buf, str, args);
 
87
  va_end(args);
 
88
 
 
89
  printf("Warning: %s", message_buf);
 
90
 
 
91
  PrintDebug("Warning: %s", message_buf);
 
92
}
 
93
 
 
94
//------------------------------------------------------------------------
 
95
//  ARGUMENT HANDLING
 
96
//------------------------------------------------------------------------
 
97
 
 
98
const char **arg_list = NULL;
 
99
int arg_count = 0;
 
100
 
 
101
//
 
102
// ArgvInit
 
103
//
 
104
// Initialise argument list.  Do NOT include the program name
 
105
// (usually in argv[0]).  The strings (and array) are copied.
 
106
//
 
107
// NOTE: doesn't merge multiple uses of an option, hence
 
108
//       using ArgvFind() will only return the first usage.
 
109
//
 
110
void ArgvInit(int argc, const char **argv)
 
111
{
 
112
  arg_count = argc;
 
113
  SYS_ASSERT(arg_count >= 0);
 
114
 
 
115
  if (arg_count == 0)
 
116
  {
 
117
    arg_list = NULL;
 
118
    return;
 
119
  }
 
120
 
 
121
  arg_list = new const char *[arg_count];
 
122
 
 
123
  int dest = 0;
 
124
 
 
125
  for (int i = 0; i < arg_count; i++)
 
126
  {
 
127
    const char *cur = argv[i];
 
128
    SYS_NULL_CHECK(cur);
 
129
 
 
130
#ifdef MACOSX
 
131
    // ignore MacOS X rubbish
 
132
    if (strncmp(cur, "-psn", 4) == 0)
 
133
      continue;
 
134
#endif
 
135
 
 
136
    // support GNU-style long options
 
137
    if (cur[0] == '-' && cur[1] == '-' && isalnum(cur[2]))
 
138
      cur++;
 
139
 
 
140
    arg_list[dest] = strdup(cur);
 
141
 
 
142
    // support DOS-style short options
 
143
    if (cur[0] == '/' && (isalnum(cur[1]) || cur[1] == '?') && cur[2] == 0)
 
144
      *(char *)(arg_list[dest]) = '-';
 
145
 
 
146
    dest++;
 
147
  }
 
148
 
 
149
  arg_count = dest;
 
150
}
 
151
 
 
152
//
 
153
// ArgvTerm(void)
 
154
//
 
155
void ArgvTerm(void)
 
156
{
 
157
  while (arg_count-- > 0)
 
158
    free((void *) arg_list[arg_count]);
 
159
  
 
160
  if (arg_list)
 
161
    delete[] arg_list;
 
162
}
 
163
 
 
164
//
 
165
// ArgvFind
 
166
//
 
167
// Returns index number, or -1 if not found.
 
168
// 
 
169
int ArgvFind(char short_name, const char *long_name, int *num_params)
 
170
{
 
171
  SYS_ASSERT(short_name || long_name);
 
172
 
 
173
  if (num_params)
 
174
    *num_params = 0;
 
175
  
 
176
  int p = 0;
 
177
 
 
178
  for (; p < arg_count; p++)
 
179
  {
 
180
    if (! ArgvIsOption(p))
 
181
      continue;
 
182
 
 
183
    const char *str = arg_list[p];
 
184
 
 
185
    if (short_name && (short_name == tolower(str[1])) && str[2] == 0)
 
186
      break;
 
187
 
 
188
    if (long_name && (UtilStrCaseCmp(long_name, str + 1) == 0))
 
189
      break;
 
190
  }
 
191
 
 
192
  if (p >= arg_count)  // NOT FOUND
 
193
    return -1;
 
194
 
 
195
  if (num_params)
 
196
  {
 
197
    int q = p + 1;
 
198
 
 
199
    while ((q < arg_count) && ! ArgvIsOption(q))
 
200
      q++;
 
201
 
 
202
    *num_params = q - p - 1;
 
203
  }
 
204
 
 
205
  return p;
 
206
}
 
207
 
 
208
bool ArgvIsOption(int index)
 
209
{
 
210
  SYS_ASSERT(index >= 0);
 
211
  SYS_ASSERT(index < arg_count);
 
212
 
 
213
  const char *str = arg_list[index];
 
214
  SYS_NULL_CHECK(str);
 
215
 
 
216
  return (str[0] == '-');
 
217
}
 
218
 
 
219
//------------------------------------------------------------------------
 
220
//  DEBUGGING CODE
 
221
//------------------------------------------------------------------------
 
222
 
 
223
#define DEBUGGING_FILE  "nv_debug.txt"
 
224
 
 
225
static FILE *debug_fp = NULL;
 
226
 
 
227
//
 
228
// InitDebug
 
229
//
 
230
void InitDebug(bool enable)
 
231
{
 
232
  if (! enable)
 
233
  {
 
234
    debug_fp = NULL;
 
235
    return;
 
236
  }
 
237
 
 
238
  debug_fp = fopen(DEBUGGING_FILE, "w");
 
239
 
 
240
  if (! debug_fp)
 
241
    PrintWarn("Unable to open DEBUG FILE: %s\n", DEBUGGING_FILE);
 
242
 
 
243
  PrintDebug("====== START OF DEBUG FILE ======\n\n");
 
244
}
 
245
 
 
246
//
 
247
// TermDebug
 
248
//
 
249
void TermDebug(void)
 
250
{
 
251
  if (debug_fp)
 
252
  {
 
253
    PrintDebug("\n====== END OF DEBUG FILE ======\n");
 
254
 
 
255
    fclose(debug_fp);
 
256
    debug_fp = NULL;
 
257
  }
 
258
}
 
259
 
 
260
//
 
261
// PrintDebug
 
262
//
 
263
void PrintDebug(const char *str, ...)
 
264
{
 
265
  if (debug_fp)
 
266
  {
 
267
    va_list args;
 
268
 
 
269
    va_start(args, str);
 
270
    vfprintf(debug_fp, str, args);
 
271
    va_end(args);
 
272
 
 
273
    fflush(debug_fp);
 
274
  }
 
275
}
 
276
 
 
277
//------------------------------------------------------------------------
 
278
//  ENDIAN CODE
 
279
//------------------------------------------------------------------------
 
280
 
 
281
static bool cpu_big_endian = false;
 
282
 
 
283
//
 
284
// InitEndian
 
285
//
 
286
// Parts inspired by the Yadex endian.cc code.
 
287
//
 
288
void InitEndian(void)
 
289
{
 
290
  volatile union
 
291
  {
 
292
    uint8_g mem[32];
 
293
    uint32_g val;
 
294
  }
 
295
  u;
 
296
 
 
297
  /* sanity-check type sizes */
 
298
 
 
299
  if (sizeof(uint8_g) != 1)
 
300
    FatalError("Sanity check failed: sizeof(uint8_g) = %d", 
 
301
        sizeof(uint8_g));
 
302
 
 
303
  if (sizeof(uint16_g) != 2)
 
304
    FatalError("Sanity check failed: sizeof(uint16_g) = %d", 
 
305
        sizeof(uint16_g));
 
306
 
 
307
  if (sizeof(uint32_g) != 4)
 
308
    FatalError("Sanity check failed: sizeof(uint32_g) = %d", 
 
309
        sizeof(uint32_g));
 
310
 
 
311
  /* check endianness */
 
312
 
 
313
  memset((uint32_g *) u.mem, 0, sizeof(u.mem));
 
314
 
 
315
  u.mem[0] = 0x70;  u.mem[1] = 0x71;
 
316
  u.mem[2] = 0x72;  u.mem[3] = 0x73;
 
317
 
 
318
  PrintDebug("Endianness magic value: 0x%08x\n", u.val);
 
319
 
 
320
  if (u.val == 0x70717273)
 
321
    cpu_big_endian = true;
 
322
  else if (u.val == 0x73727170)
 
323
    cpu_big_endian = false;
 
324
  else
 
325
    FatalError("Sanity check failed: weird endianness (0x%08x)", u.val);
 
326
 
 
327
  PrintDebug("Endianness = %s\n", cpu_big_endian ? "BIG" : "LITTLE");
 
328
 
 
329
  PrintDebug("Endianness check: 0x1234 --> 0x%04x\n", 
 
330
      (int) Endian_U16(0x1234));
 
331
 
 
332
  PrintDebug("Endianness check: 0x11223344 --> 0x%08x\n\n", 
 
333
      Endian_U32(0x11223344));
 
334
}
 
335
 
 
336
//
 
337
// Endian_U16
 
338
//
 
339
uint16_g Endian_U16(uint16_g x)
 
340
{
 
341
  if (cpu_big_endian)
 
342
    return (x >> 8) | (x << 8);
 
343
  else
 
344
    return x;
 
345
}
 
346
 
 
347
//
 
348
// Endian_U32
 
349
//
 
350
uint32_g Endian_U32(uint32_g x)
 
351
{
 
352
  if (cpu_big_endian)
 
353
    return (x >> 24) | ((x >> 8) & 0xff00) |
 
354
             ((x << 8) & 0xff0000) | (x << 24);
 
355
  else
 
356
    return x;
 
357
}
 
358