Debian Codegraph @

terps/scare/scutils.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o Implement smarter selective module tracing.
24 */
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "scare.h"
34 #include "scprotos.h"
35
36
37 /*
38 * sc_trace()
39 *
40 * Debugging trace function; printf wrapper that writes to stderr.
41 */
42 void
43 sc_trace (const sc_char *format, ...)
44 {
45 va_list ap;
46 assert (format);
47
48 va_start (ap, format);
49 vfprintf (stderr, format, ap);
50 va_end (ap);
51 }
52
53
54 /*
55 * sc_error()
56 * sc_fatal()
57 *
58 * Error reporting functions. sc_error() prints a message and continues.
59 * sc_fatal() prints a message, then calls abort().
60 */
61 void
62 sc_error (const sc_char *format, ...)
63 {
64 va_list ap;
65 assert (format);
66
67 fprintf (stderr, "scare: ");
68 va_start (ap, format);
69 vfprintf (stderr, format, ap);
70 va_end (ap);
71 }
72
73 void
74 sc_fatal (const sc_char *format, ...)
75 {
76 va_list ap;
77 assert (format);
78
79 fprintf (stderr, "scare: internal error: ");
80 va_start (ap, format);
81 vfprintf (stderr, format, ap);
82 va_end (ap);
83 fprintf (stderr, "scare: aborting...\n");
84 abort ();
85 }
86
87
88 /* Unique non-heap address for zero size malloc() and realloc() requests. */
89 static void *sc_zero_allocation = &sc_zero_allocation;
90
91 /*
92 * sc_malloc()
93 * sc_realloc()
94 * sc_free()
95 *
96 * Non-failing wrappers around malloc functions. Newly allocated memory is
97 * cleared to zero. In ANSI/ISO C, zero byte allocations are implementation-
98 * defined, so we have to take special care to get predictable behavior.
99 */
100 void *
101 sc_malloc (size_t size)
102 {
103 void *allocated;
104
105 if (size == 0)
106 return sc_zero_allocation;
107
108 allocated = malloc (size);
109 if (!allocated)
110 sc_fatal ("sc_malloc: requested %lu bytes\n", (sc_uint) size);
111 else if (allocated == sc_zero_allocation)
112 sc_fatal ("sc_malloc: zero-byte allocation address returned\n");
113
114 memset (allocated, 0, size);
115 return allocated;
116 }
117
118 void *
119 sc_realloc (void *pointer, size_t size)
120 {
121 void *allocated;
122
123 if (size == 0)
124 {
125 sc_free (pointer);
126 return sc_zero_allocation;
127 }
128
129 if (pointer == sc_zero_allocation)
130 pointer = NULL;
131
132 allocated = realloc (pointer, size);
133 if (!allocated)
134 sc_fatal ("sc_realloc: requested %lu bytes\n", (sc_uint) size);
135 else if (allocated == sc_zero_allocation)
136 sc_fatal ("sc_realloc: zero-byte allocation address returned\n");
137
138 if (!pointer)
139 memset (allocated, 0, size);
140 return allocated;
141 }
142
143 void
144 sc_free (void *pointer)
145 {
146 if (sc_zero_allocation != &sc_zero_allocation)
147 sc_fatal ("sc_free: write to zero-byte allocation address detected\n");
148
149 if (pointer && pointer != sc_zero_allocation)
150 free (pointer);
151 }
152
153
154 /*
155 * sc_strncasecmp()
156 * sc_strcasecmp()
157 *
158 * Strncasecmp and strcasecmp are not ANSI functions, so here are local
159 * definitions to do the same jobs.
160 */
161 sc_int
162 sc_strncasecmp (const sc_char *s1, const sc_char *s2, sc_int n)
163 {
164 sc_int index_;
165 assert (s1 && s2);
166
167 for (index_ = 0; index_ < n; index_++)
168 {
169 sc_int diff;
170
171 diff = sc_tolower (s1[index_]) - sc_tolower (s2[index_]);
172 if (diff < 0 || diff > 0)
173 return diff < 0 ? -1 : 1;
174 }
175
176 return 0;
177 }
178
179 sc_int
180 sc_strcasecmp (const sc_char *s1, const sc_char *s2)
181 {
182 sc_int s1len, s2len, result;
183 assert (s1 && s2);
184
185 s1len = strlen (s1);
186 s2len = strlen (s2);
187
188 result = sc_strncasecmp (s1, s2, s1len < s2len ? s1len : s2len);
189 if (result < 0 || result > 0)
190 return result;
191 else
192 return s1len < s2len ? -1 : s1len > s2len ? 1 : 0;
193 }
194
195
196 /*
197 * sc_platform_rand()
198 * sc_congruential_rand()
199 * sc_set_random_handler()
200 *
201 * Internal random number generation functions. We offer two: one is a self-
202 * seeding wrapper around the platform's rand(), which should generate good
203 * random numbers but with a sequence that is platform-dependent; the other
204 * is a linear congruential generator with a long period that is guaranteed
205 * to return the same sequence for all platforms. The default is the first,
206 * with the latter intended for predictability of game actions.
207 */
208 static sc_int
209 sc_platform_rand (sc_uint new_seed)
210 {
211 static sc_bool is_seeded = FALSE;
212
213 /* If reseeding, seed with the value supplied, note seeded, and return 0. */
214 if (new_seed > 0)
215 {
216 srand (new_seed);
217 is_seeded = TRUE;
218 return 0;
219 }
220 else
221 {
222 /* If not explicitly seeded yet, generate a seed from time(). */
223 if (!is_seeded)
224 {
225 srand ((sc_uint) time (NULL));
226 is_seeded = TRUE;
227 }
228
229 /* Return the next rand() number in the sequence. */
230 return rand ();
231 }
232 }
233
234 static sc_int
235 sc_congruential_rand (sc_uint new_seed)
236 {
237 static sc_bool is_seeded = FALSE;
238 static sc_uint rand_state = 1;
239
240 /* If reseeding, seed with the value supplied, and note seeded. */
241 if (new_seed > 0)
242 {
243 rand_state = new_seed;
244 is_seeded = TRUE;
245 return 0;
246 }
247 else
248 {
249 /* If not explicitly seeded yet, generate a seed from time(). */
250 if (!is_seeded)
251 {
252 rand_state = (sc_uint) time (NULL);
253 is_seeded = TRUE;
254 }
255
256 /*
257 * Advance random state, using constants from Park & Miller (1988).
258 * To keep the values the same for both 32 and 64 bit longs, mask out
259 * any bits above the bottom 32.
260 */
261 rand_state = (rand_state * 16807 + 2147483647) & 0xffffffff;
262
263 /*
264 * Discard the lowest bit as a way to map 32-bits unsigned to a 32-bit
265 * positive signed.
266 */
267 return rand_state >> 1;
268 }
269 }
270
271
272 /* Function pointer for the actual random number generator in use. */
273 static sc_int (*sc_rand_function) (sc_uint) = sc_platform_rand;
274
275 /*
276 * sc_set_congruential_random()
277 * sc_set_platform_random()
278 * sc_is_congruential_random()
279 * sc_seed_random()
280 * sc_rand()
281 * sc_randomint()
282 *
283 * Public interface to random functions; control and reseed the random
284 * handler in use, generate a random number, and a convenience function to
285 * generate a random value within a given range.
286 */
287 void
288 sc_set_congruential_random (void)
289 {
290 sc_rand_function = sc_congruential_rand;
291 }
292
293 void
294 sc_set_platform_random (void)
295 {
296 sc_rand_function = sc_platform_rand;
297 }
298
299 sc_bool
300 sc_is_congruential_random (void)
301 {
302 return sc_rand_function == sc_congruential_rand;
303 }
304
305 void
306 sc_seed_random (sc_uint new_seed)
307 {
308 /* Ignore zero values of new_seed by simply using 1 instead. */
309 sc_rand_function (new_seed > 0 ? new_seed : 1);
310 }
311
312 sc_int
313 sc_rand (void)
314 {
315 sc_int retval;
316
317 /* Passing zero indicates this is not a seed operation. */
318 retval = sc_rand_function (0);
319 return retval;
320 }
321
322 sc_int
323 sc_randomint (sc_int low, sc_int high)
324 {
325 /*
326 * If the range is invalid, just return the low value given. This mimics
327 * Adrift under the same conditions, and also guards against division by
328 * zero in the mod operation.
329 */
330 return (high < low) ? low : low + sc_rand () % (high - low + 1);
331 }
332
333
334 /* Miscellaneous general ascii constants. */
335 static const sc_char NUL = '\0';
336 static const sc_char SPACE = ' ';
337
338 /*
339 * sc_strempty()
340 *
341 * Return TRUE if a string is either zero-length or contains only whitespace.
342 */
343 sc_bool
344 sc_strempty (const sc_char *string)
345 {
346 sc_int index_;
347 assert (string);
348
349 /* Scan for any non-space character. */
350 for (index_ = 0; string[index_] != NUL; index_++)
351 {
352 if (!sc_isspace (string[index_]))
353 return FALSE;
354 }
355
356 /* None found, so string is empty. */
357 return TRUE;
358 }
359
360
361 /*
362 * sc_trim_string()
363 *
364 * Trim leading and trailing whitespace from a string. Modifies the string
365 * in place, and returns the string address for convenience.
366 */
367 sc_char *
368 sc_trim_string (sc_char *string)
369 {
370 sc_int index_;
371 assert (string);
372
373 for (index_ = strlen (string) - 1;
374 index_ >= 0 && sc_isspace (string[index_]); index_--)
375 string[index_] = NUL;
376
377 for (index_ = 0; sc_isspace (string[index_]);)
378 index_++;
379 memmove (string, string + index_, strlen (string) - index_ + 1);
380
381 return string;
382 }
383
384
385 /*
386 * sc_normalize_string()
387 *
388 * Trim a string, and set all runs of whitespace to a single space character.
389 * Modifies the string in place, and returns the string address for
390 * convenience.
391 */
392 sc_char *
393 sc_normalize_string (sc_char *string)
394 {
395 sc_int index_;
396 assert (string);
397
398 /* Trim all leading and trailing spaces. */
399 string = sc_trim_string (string);
400
401 /* Compress multiple whitespace runs into a single space character. */
402 for (index_ = 0; string[index_] != NUL; index_++)
403 {
404 if (sc_isspace (string[index_]))
405 {
406 sc_int cursor;
407
408 string[index_] = SPACE;
409 for (cursor = index_ + 1; sc_isspace (string[cursor]);)
410 cursor++;
411 memmove (string + index_ + 1,
412 string + cursor, strlen (string + cursor) + 1);
413 }
414 }
415
416 return string;
417 }
418
419
420 /*
421 * sc_compare_word()
422 *
423 * Return TRUE if the first word in the string is word, case insensitive.
424 */
425 sc_bool
426 sc_compare_word (const sc_char *string, const sc_char *word, sc_int length)
427 {
428 assert (string && word);
429
430 /* Return TRUE if string starts with word, then space or string end. */
431 return sc_strncasecmp (string, word, length) == 0
432 && (string[length] == NUL || sc_isspace (string[length]));
433 }
434
435
436 /*
437 * sc_hash()
438 *
439 * Hash a string, hashpjw algorithm, from 'Compilers, principles, techniques,
440 * and tools', page 436, unmodulo'ed and somewhat restyled.
441 */
442 sc_uint
443 sc_hash (const sc_char *string)
444 {
445 sc_int index_;
446 sc_uint hash;
447 assert (string);
448
449 hash = 0;
450 for (index_ = 0; string[index_] != NUL; index_++)
451 {
452 sc_uint temp;
453
454 hash = (hash << 4) + string[index_];
455 temp = hash & 0xf0000000;
456 if (temp != 0)
457 {
458 hash = hash ^ (temp >> 24);
459 hash = hash ^ temp;
460 }
461 }
462
463 return hash;
464 }