~ubuntu-branches/ubuntu/natty/glbsp/natty

« back to all changes in this revision

Viewing changes to gui/helper.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
// HELPER : Unix/FLTK Little Helpers...
 
3
//------------------------------------------------------------------------
 
4
//
 
5
//  GL-Friendly Node Builder (C) 2000-2007 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 "local.h"
 
22
 
 
23
#ifdef WIN32
 
24
#include <FL/x.H>
 
25
#else
 
26
#include <sys/time.h>
 
27
#endif
 
28
 
 
29
 
 
30
//
 
31
// HelperCaseCmp
 
32
//
 
33
int HelperCaseCmp(const char *A, const char *B)
 
34
{
 
35
  for (; *A && *B; A++, B++)
 
36
  {
 
37
    if (toupper(*A) != toupper(*B))
 
38
      return (toupper(*A) - toupper(*B));
 
39
  }
 
40
 
 
41
  return (*A) ? 1 : (*B) ? -1 : 0;
 
42
}
 
43
 
 
44
 
 
45
//
 
46
// HelperCaseCmpLen
 
47
//
 
48
// Like the above routine, but compares no more than 'len' characters
 
49
// at the start of each string.
 
50
//
 
51
int HelperCaseCmpLen(const char *A, const char *B, int len)
 
52
{
 
53
  for (; *A && *B && (len > 0); A++, B++, len--)
 
54
  {
 
55
    if (toupper(*A) != toupper(*B))
 
56
      return (toupper(*A) - toupper(*B));
 
57
  }
 
58
 
 
59
  if (len == 0)
 
60
    return 0;
 
61
 
 
62
  return (*A) ? 1 : (*B) ? -1 : 0;
 
63
}
 
64
 
 
65
 
 
66
//
 
67
// HelperGetMillis
 
68
//
 
69
// Be sure to handle the result overflowing (it WILL happen !).
 
70
//
 
71
unsigned int HelperGetMillis()
 
72
{
 
73
#ifdef WIN32
 
74
  unsigned long ticks = GetTickCount();
 
75
 
 
76
  return (unsigned int) ticks;
 
77
#else
 
78
  struct timeval tm;
 
79
 
 
80
  gettimeofday(&tm, NULL);
 
81
 
 
82
  return (unsigned int) ((tm.tv_sec * 1000) + (tm.tv_usec / 1000));
 
83
#endif
 
84
}
 
85
 
 
86
 
 
87
//
 
88
// HelperFilenameValid
 
89
//
 
90
boolean_g HelperFilenameValid(const char *filename)
 
91
{
 
92
  if (! filename)
 
93
    return FALSE;
 
94
 
 
95
  int len = strlen(filename);
 
96
 
 
97
  if (len == 0)
 
98
    return FALSE;
 
99
 
 
100
#ifdef WIN32
 
101
  // check drive letter
 
102
  if (len >= 2 && filename[1] == ':')
 
103
  {
 
104
    if (! isalpha(filename[0]))
 
105
      return FALSE;
 
106
 
 
107
    if (len == 2)
 
108
      return FALSE;
 
109
  }
 
110
#endif
 
111
 
 
112
  switch (filename[len - 1])
 
113
  {
 
114
    case '.':
 
115
    case '/':
 
116
    case '\\':
 
117
      return FALSE;
 
118
      
 
119
    default:
 
120
      break;
 
121
  }
 
122
 
 
123
  return TRUE;
 
124
}
 
125
 
 
126
//
 
127
// HelperHasExt
 
128
//
 
129
boolean_g HelperHasExt(const char *filename)
 
130
{
 
131
  int A = strlen(filename) - 1;
 
132
 
 
133
  if (A > 0 && filename[A] == '.')
 
134
    return FALSE;
 
135
 
 
136
  for (; A >= 0; A--)
 
137
  {
 
138
    if (filename[A] == '.')
 
139
      return TRUE;
 
140
 
 
141
    if (filename[A] == '/')
 
142
      break;
 
143
 
 
144
#ifdef WIN32
 
145
    if (filename[A] == '\\' || filename[A] == ':')
 
146
      break;
 
147
#endif
 
148
  }
 
149
 
 
150
  return FALSE;
 
151
}
 
152
 
 
153
//
 
154
// HelperCheckExt
 
155
//
 
156
boolean_g HelperCheckExt(const char *filename, const char *ext)
 
157
{
 
158
  int A = strlen(filename) - 1;
 
159
  int B = strlen(ext) - 1;
 
160
 
 
161
  for (; B >= 0; B--, A--)
 
162
  {
 
163
    if (A < 0)
 
164
      return FALSE;
 
165
    
 
166
    if (toupper(filename[A]) != toupper(ext[B]))
 
167
      return FALSE;
 
168
  }
 
169
 
 
170
  return (A >= 1) && (filename[A] == '.');
 
171
}
 
172
 
 
173
//
 
174
// HelperReplaceExt
 
175
//
 
176
// Returns NULL if the filename was NULL, otherwise returns a pointer
 
177
// to a static buffer containing the new filename.
 
178
// 
 
179
char *HelperReplaceExt(const char *filename, const char *ext)
 
180
{
 
181
  char *dot_pos;
 
182
  static char buffer[512];
 
183
 
 
184
  if (! filename || filename[0] == 0)
 
185
    return NULL;
 
186
 
 
187
  strcpy(buffer, filename);
 
188
  
 
189
  int len = strlen(buffer);
 
190
 
 
191
  if (HelperHasExt(filename))
 
192
  {
 
193
    dot_pos = strrchr(buffer, '.');
 
194
 
 
195
    if (dot_pos)
 
196
      dot_pos[1] = 0;
 
197
  }
 
198
  else
 
199
  {
 
200
    if (len > 0 && buffer[len-1] != '.')
 
201
      strcat(buffer, ".");
 
202
  }
 
203
  
 
204
  strcat(buffer, ext);
 
205
  return buffer;
 
206
}
 
207
 
 
208
//
 
209
// HelperGuessOutput
 
210
//
 
211
// Computes an output filename given an input one.  Returns NULL if
 
212
// the filename was NULL, otherwise returns a pointer to a static
 
213
// buffer containing the new filename.
 
214
// 
 
215
char *HelperGuessOutput(const char *filename)
 
216
{
 
217
  char *dot_pos;
 
218
  static char buffer[512];
 
219
 
 
220
  if (! filename || filename[0] == 0)
 
221
    return NULL;
 
222
 
 
223
  strcpy(buffer, filename);
 
224
  
 
225
  dot_pos = strrchr(buffer, '.');
 
226
 
 
227
  if (dot_pos)
 
228
    dot_pos[0] = 0;
 
229
  else
 
230
    dot_pos = buffer + strlen(buffer);
 
231
  
 
232
  // check for existing modification ("_b" etc) and update it when
 
233
  // found rather than getting level_b_b_b_b.wad :)
 
234
  
 
235
  dot_pos -= 2;
 
236
 
 
237
  if (dot_pos > buffer && dot_pos[0] == '_' &&
 
238
      ('a' <= dot_pos[1] && dot_pos[1] <= 'z'))
 
239
  {
 
240
    if (dot_pos[1] == 'z')
 
241
      dot_pos[1] = 'a';
 
242
    else
 
243
      dot_pos[1] += 1;
 
244
  }
 
245
  else
 
246
  {
 
247
    strcat(buffer, "_b");
 
248
  }
 
249
 
 
250
  strcat(buffer, ".wad");
 
251
  return buffer;
 
252
}
 
253
 
 
254
//
 
255
// HelperFileExists
 
256
//
 
257
boolean_g HelperFileExists(const char *filename)
 
258
{
 
259
  FILE *fp = fopen(filename, "rb");
 
260
 
 
261
  if (fp)
 
262
  {
 
263
    fclose(fp);
 
264
    return TRUE;
 
265
  }
 
266
 
 
267
  return FALSE;
 
268
}
 
269