~ubuntu-branches/ubuntu/precise/glbsp/precise

« back to all changes in this revision

Viewing changes to util.c

  • 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
 
// UTILITY : general purpose functions
3
 
//------------------------------------------------------------------------
4
 
//
5
 
//  GL-Friendly Node Builder (C) 2000-2005 Andrew Apted
6
 
//
7
 
//  Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
8
 
//
9
 
//  This program is free software; you can redistribute it and/or
10
 
//  modify it under the terms of the GNU General Public License
11
 
//  as published by the Free Software Foundation; either version 2
12
 
//  of the License, or (at your option) any later version.
13
 
//
14
 
//  This program is distributed in the hope that it will be useful,
15
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
//  GNU General Public License for more details.
18
 
//
19
 
//------------------------------------------------------------------------
20
 
 
21
 
#include "system.h"
22
 
 
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
 
#include <string.h>
26
 
#include <stdarg.h>
27
 
#include <ctype.h>
28
 
#include <math.h>
29
 
#include <limits.h>
30
 
#include <assert.h>
31
 
 
32
 
#include "util.h"
33
 
 
34
 
#ifdef WIN32
35
 
#include <Windows.h>
36
 
#else
37
 
#include <time.h>
38
 
#endif
39
 
 
40
 
 
41
 
//
42
 
// UtilCalloc
43
 
//
44
 
// Allocate memory with error checking.  Zeros the memory.
45
 
//
46
 
void *UtilCalloc(int size)
47
 
{
48
 
  void *ret = calloc(1, size);
49
 
  
50
 
  if (!ret)
51
 
    FatalError("Out of memory (cannot allocate %d bytes)", size);
52
 
 
53
 
  return ret;
54
 
}
55
 
 
56
 
//
57
 
// UtilRealloc
58
 
//
59
 
// Reallocate memory with error checking.
60
 
//
61
 
void *UtilRealloc(void *old, int size)
62
 
{
63
 
  void *ret = realloc(old, size);
64
 
 
65
 
  if (!ret)
66
 
    FatalError("Out of memory (cannot reallocate %d bytes)", size);
67
 
 
68
 
  return ret;
69
 
}
70
 
 
71
 
//
72
 
// UtilFree
73
 
//
74
 
// Free the memory with error checking.
75
 
//
76
 
void UtilFree(void *data)
77
 
{
78
 
  if (data == NULL)
79
 
    InternalError("Trying to free a NULL pointer");
80
 
  
81
 
  free(data);
82
 
}
83
 
 
84
 
//
85
 
// UtilStrDup
86
 
//
87
 
// Duplicate a string with error checking.
88
 
//
89
 
char *UtilStrDup(const char *str)
90
 
{
91
 
  char *result;
92
 
  int len = (int)strlen(str);
93
 
 
94
 
  result = UtilCalloc(len+1);
95
 
 
96
 
  if (len > 0)
97
 
    memcpy(result, str, len);
98
 
  
99
 
  result[len] = 0;
100
 
 
101
 
  return result;
102
 
}
103
 
 
104
 
//
105
 
// UtilStrNDup
106
 
//
107
 
// Duplicate a limited length string.
108
 
//
109
 
char *UtilStrNDup(const char *str, int size)
110
 
{
111
 
  char *result;
112
 
  int len;
113
 
 
114
 
  for (len=0; len < size && str[len]; len++)
115
 
  { }
116
 
 
117
 
  result = UtilCalloc(len+1);
118
 
 
119
 
  if (len > 0)
120
 
    memcpy(result, str, len);
121
 
  
122
 
  result[len] = 0;
123
 
 
124
 
  return result;
125
 
}
126
 
 
127
 
int UtilStrCaseCmp(const char *A, const char *B)
128
 
{
129
 
  for (; *A || *B; A++, B++)
130
 
  {
131
 
    // this test also catches end-of-string conditions
132
 
    if (toupper(*A) != toupper(*B))
133
 
      return (toupper(*A) - toupper(*B));
134
 
  }
135
 
 
136
 
  // strings are equal
137
 
  return 0;
138
 
}
139
 
 
140
 
 
141
 
//
142
 
// UtilRoundPOW2
143
 
//
144
 
// Rounds the value _up_ to the nearest power of two.
145
 
//
146
 
int UtilRoundPOW2(int x)
147
 
{
148
 
  int tmp;
149
 
 
150
 
  if (x <= 2)
151
 
    return x;
152
 
 
153
 
  x--;
154
 
  
155
 
  for (tmp=x / 2; tmp; tmp /= 2)
156
 
    x |= tmp;
157
 
  
158
 
  return (x + 1);
159
 
}
160
 
 
161
 
 
162
 
//
163
 
// UtilComputeAngle
164
 
//
165
 
// Translate (dx, dy) into an angle value (degrees)
166
 
//
167
 
angle_g UtilComputeAngle(float_g dx, float_g dy)
168
 
{
169
 
  double angle;
170
 
 
171
 
  if (dx == 0)
172
 
    return (dy > 0) ? 90.0 : 270.0;
173
 
 
174
 
  angle = atan2((double) dy, (double) dx) * 180.0 / M_PI;
175
 
 
176
 
  if (angle < 0) 
177
 
    angle += 360.0;
178
 
 
179
 
  return angle;
180
 
}
181
 
 
182
 
 
183
 
//
184
 
// UtilFileExists
185
 
//
186
 
int UtilFileExists(const char *filename)
187
 
{
188
 
  FILE *fp = fopen(filename, "rb");
189
 
 
190
 
  if (fp)
191
 
  {
192
 
    fclose(fp);
193
 
    return TRUE;
194
 
  }
195
 
 
196
 
  return FALSE;
197
 
}
198
 
 
199
 
//
200
 
// UtilTimeString
201
 
//
202
 
const char *UtilTimeString(void)
203
 
{
204
 
#ifdef WIN32
205
 
 
206
 
  SYSTEMTIME sys_time;
207
 
 
208
 
  static char str_buf[200];
209
 
 
210
 
  GetSystemTime(&sys_time);
211
 
 
212
 
  sprintf(str_buf, "%04d-%02d-%02d %02d:%02d:%02d.%04d",
213
 
      sys_time.wYear, sys_time.wMonth, sys_time.wDay,
214
 
      sys_time.wHour, sys_time.wMinute, sys_time.wSecond,
215
 
      sys_time.wMilliseconds * 10);
216
 
 
217
 
  return str_buf;
218
 
 
219
 
#else // LINUX or MACOSX
220
 
 
221
 
  time_t epoch_time;
222
 
  struct tm *calend_time;
223
 
 
224
 
  static char str_buf[200];
225
 
 
226
 
  if (time(&epoch_time) == (time_t)-1)
227
 
    return NULL;
228
 
 
229
 
  calend_time = localtime(&epoch_time);
230
 
  if (! calend_time)
231
 
    return NULL;
232
 
 
233
 
  sprintf(str_buf, "%04d-%02d-%02d %02d:%02d:%02d.%04d",
234
 
      calend_time->tm_year + 1900, calend_time->tm_mon + 1,
235
 
      calend_time->tm_mday,
236
 
      calend_time->tm_hour, calend_time->tm_min,
237
 
      calend_time->tm_sec,  0);
238
 
 
239
 
  return str_buf;
240
 
 
241
 
#endif  
242
 
}
243
 
 
244
 
//------------------------------------------------------------------------
245
 
//  Adler-32 CHECKSUM Code
246
 
//------------------------------------------------------------------------
247
 
 
248
 
void Adler32_Begin(uint32_g *crc)
249
 
{
250
 
  *crc = 1;
251
 
}
252
 
 
253
 
void Adler32_AddBlock(uint32_g *crc, const uint8_g *data, int length)
254
 
{
255
 
    uint32_g s1 = (*crc) & 0xFFFF;
256
 
    uint32_g s2 = ((*crc) >> 16) & 0xFFFF;
257
 
 
258
 
    for (; length > 0; data++, length--)
259
 
    {
260
 
        s1 = (s1 + *data) % 65521;
261
 
        s2 = (s2 + s1)    % 65521;
262
 
    }
263
 
 
264
 
    *crc = (s2 << 16) | s1;
265
 
}
266
 
 
267
 
void Adler32_Finish(uint32_g *crc)
268
 
{
269
 
  /* nothing to do */
270
 
}
271