| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 89 |
static void *sc_zero_allocation = &sc_zero_allocation; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 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 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static sc_int |
| 209 |
sc_platform_rand (sc_uint new_seed) |
| 210 |
{ |
| 211 |
static sc_bool is_seeded = FALSE; |
| 212 |
|
| 213 |
|
| 214 |
if (new_seed > 0) |
| 215 |
{ |
| 216 |
srand (new_seed); |
| 217 |
is_seeded = TRUE; |
| 218 |
return 0; |
| 219 |
} |
| 220 |
else |
| 221 |
{ |
| 222 |
|
| 223 |
if (!is_seeded) |
| 224 |
{ |
| 225 |
srand ((sc_uint) time (NULL)); |
| 226 |
is_seeded = TRUE; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 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 |
|
| 241 |
if (new_seed > 0) |
| 242 |
{ |
| 243 |
rand_state = new_seed; |
| 244 |
is_seeded = TRUE; |
| 245 |
return 0; |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
|
| 250 |
if (!is_seeded) |
| 251 |
{ |
| 252 |
rand_state = (sc_uint) time (NULL); |
| 253 |
is_seeded = TRUE; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
rand_state = (rand_state * 16807 + 2147483647) & 0xffffffff; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
return rand_state >> 1; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static sc_int (*sc_rand_function) (sc_uint) = sc_platform_rand; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
return (high < low) ? low : low + sc_rand () % (high - low + 1); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
static const sc_char NUL = '\0'; |
| 336 |
static const sc_char SPACE = ' '; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
sc_bool |
| 344 |
sc_strempty (const sc_char *string) |
| 345 |
{ |
| 346 |
sc_int index_; |
| 347 |
assert (string); |
| 348 |
|
| 349 |
|
| 350 |
for (index_ = 0; string[index_] != NUL; index_++) |
| 351 |
{ |
| 352 |
if (!sc_isspace (string[index_])) |
| 353 |
return FALSE; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
return TRUE; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 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 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
sc_char * |
| 393 |
sc_normalize_string (sc_char *string) |
| 394 |
{ |
| 395 |
sc_int index_; |
| 396 |
assert (string); |
| 397 |
|
| 398 |
|
| 399 |
string = sc_trim_string (string); |
| 400 |
|
| 401 |
|
| 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 |
|
| 422 |
|
| 423 |
|
| 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 |
|
| 431 |
return sc_strncasecmp (string, word, length) == 0 |
| 432 |
&& (string[length] == NUL || sc_isspace (string[length])); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 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 |
} |