diff options
Diffstat (limited to 'engines/glk/adrift/scutils.cpp')
-rw-r--r-- | engines/glk/adrift/scutils.cpp | 440 |
1 files changed, 204 insertions, 236 deletions
diff --git a/engines/glk/adrift/scutils.cpp b/engines/glk/adrift/scutils.cpp index 98e10651e7..97db91dbb8 100644 --- a/engines/glk/adrift/scutils.cpp +++ b/engines/glk/adrift/scutils.cpp @@ -42,14 +42,14 @@ namespace Adrift { * * Debugging trace function; printf wrapper that writes to stderr. */ -void sc_trace (const sc_char *format, ...) { - va_list ap; - assert(format); - - va_start (ap, format); - Common::String s = Common::String::format(format, ap); - va_end (ap); - debug("%s", s.c_str()); +void sc_trace(const sc_char *format, ...) { + va_list ap; + assert(format); + + va_start(ap, format); + Common::String s = Common::String::format(format, ap); + va_end(ap); + debug("%s", s.c_str()); } @@ -60,7 +60,7 @@ void sc_trace (const sc_char *format, ...) { * Error reporting functions. sc_error() prints a message and continues. * sc_fatal() prints a message, then calls abort(). */ -void sc_error (const sc_char *format, ...) { +void sc_error(const sc_char *format, ...) { va_list ap; assert(format); @@ -70,7 +70,7 @@ void sc_error (const sc_char *format, ...) { warning("%s", s.c_str()); } -void sc_fatal (const sc_char *format, ...) { +void sc_fatal(const sc_char *format, ...) { va_list ap; assert(format); @@ -83,7 +83,7 @@ void sc_fatal (const sc_char *format, ...) { /* Unique non-heap address for zero size malloc() and realloc() requests. */ static void *sc_zero_allocation = &sc_zero_allocation; - + /* * sc_malloc() * sc_realloc() @@ -94,56 +94,52 @@ static void *sc_zero_allocation = &sc_zero_allocation; * defined, so we have to take special care to get predictable behavior. */ void * -sc_malloc (size_t size) -{ - void *allocated; +sc_malloc(size_t size) { + void *allocated; - if (size == 0) - return sc_zero_allocation; + if (size == 0) + return sc_zero_allocation; - allocated = malloc (size); - if (!allocated) - sc_fatal ("sc_malloc: requested %lu bytes\n", (sc_uint) size); - else if (allocated == sc_zero_allocation) - sc_fatal ("sc_malloc: zero-byte allocation address returned\n"); + allocated = malloc(size); + if (!allocated) + sc_fatal("sc_malloc: requested %lu bytes\n", (sc_uint) size); + else if (allocated == sc_zero_allocation) + sc_fatal("sc_malloc: zero-byte allocation address returned\n"); - memset (allocated, 0, size); - return allocated; + memset(allocated, 0, size); + return allocated; } void * -sc_realloc (void *pointer, size_t size) -{ - void *allocated; - - if (size == 0) - { - sc_free (pointer); - return sc_zero_allocation; - } - - if (pointer == sc_zero_allocation) - pointer = NULL; - - allocated = realloc (pointer, size); - if (!allocated) - sc_fatal ("sc_realloc: requested %lu bytes\n", (sc_uint) size); - else if (allocated == sc_zero_allocation) - sc_fatal ("sc_realloc: zero-byte allocation address returned\n"); - - if (!pointer) - memset (allocated, 0, size); - return allocated; +sc_realloc(void *pointer, size_t size) { + void *allocated; + + if (size == 0) { + sc_free(pointer); + return sc_zero_allocation; + } + + if (pointer == sc_zero_allocation) + pointer = NULL; + + allocated = realloc(pointer, size); + if (!allocated) + sc_fatal("sc_realloc: requested %lu bytes\n", (sc_uint) size); + else if (allocated == sc_zero_allocation) + sc_fatal("sc_realloc: zero-byte allocation address returned\n"); + + if (!pointer) + memset(allocated, 0, size); + return allocated; } void -sc_free (void *pointer) -{ - if (sc_zero_allocation != &sc_zero_allocation) - sc_fatal ("sc_free: write to zero-byte allocation address detected\n"); +sc_free(void *pointer) { + if (sc_zero_allocation != &sc_zero_allocation) + sc_fatal("sc_free: write to zero-byte allocation address detected\n"); - if (pointer && pointer != sc_zero_allocation) - free (pointer); + if (pointer && pointer != sc_zero_allocation) + free(pointer); } @@ -155,37 +151,34 @@ sc_free (void *pointer) * definitions to do the same jobs. */ sc_int -sc_strncasecmp (const sc_char *s1, const sc_char *s2, sc_int n) -{ - sc_int index_; - assert (s1 && s2); +sc_strncasecmp(const sc_char *s1, const sc_char *s2, sc_int n) { + sc_int index_; + assert(s1 && s2); - for (index_ = 0; index_ < n; index_++) - { - sc_int diff; + for (index_ = 0; index_ < n; index_++) { + sc_int diff; - diff = sc_tolower (s1[index_]) - sc_tolower (s2[index_]); - if (diff < 0 || diff > 0) - return diff < 0 ? -1 : 1; - } + diff = sc_tolower(s1[index_]) - sc_tolower(s2[index_]); + if (diff < 0 || diff > 0) + return diff < 0 ? -1 : 1; + } - return 0; + return 0; } sc_int -sc_strcasecmp (const sc_char *s1, const sc_char *s2) -{ - sc_int s1len, s2len, result; - assert (s1 && s2); - - s1len = strlen (s1); - s2len = strlen (s2); - - result = sc_strncasecmp (s1, s2, s1len < s2len ? s1len : s2len); - if (result < 0 || result > 0) - return result; - else - return s1len < s2len ? -1 : s1len > s2len ? 1 : 0; +sc_strcasecmp(const sc_char *s1, const sc_char *s2) { + sc_int s1len, s2len, result; + assert(s1 && s2); + + s1len = strlen(s1); + s2len = strlen(s2); + + result = sc_strncasecmp(s1, s2, s1len < s2len ? s1len : s2len); + if (result < 0 || result > 0) + return result; + else + return s1len < s2len ? -1 : s1len > s2len ? 1 : 0; } @@ -202,70 +195,61 @@ sc_strcasecmp (const sc_char *s1, const sc_char *s2) * with the latter intended for predictability of game actions. */ static sc_int -sc_platform_rand (sc_uint new_seed) -{ - static sc_bool is_seeded = FALSE; - - /* If reseeding, seed with the value supplied, note seeded, and return 0. */ - if (new_seed > 0) { - g_vm->setRandomNumberSeed(new_seed); - is_seeded = TRUE; - return 0; - } - else - { - /* If not explicitly seeded yet, generate a seed from time(). */ - if (!is_seeded) - { - //srand ((sc_uint) time (NULL)); - is_seeded = TRUE; - } - - /* Return the next rand() number in the sequence. */ - return g_vm->getRandomNumber(0xffffff); - } +sc_platform_rand(sc_uint new_seed) { + static sc_bool is_seeded = FALSE; + + /* If reseeding, seed with the value supplied, note seeded, and return 0. */ + if (new_seed > 0) { + g_vm->setRandomNumberSeed(new_seed); + is_seeded = TRUE; + return 0; + } else { + /* If not explicitly seeded yet, generate a seed from time(). */ + if (!is_seeded) { + //srand ((sc_uint) time (NULL)); + is_seeded = TRUE; + } + + /* Return the next rand() number in the sequence. */ + return g_vm->getRandomNumber(0xffffff); + } } static sc_int -sc_congruential_rand (sc_uint new_seed) -{ - static sc_bool is_seeded = FALSE; - static sc_uint rand_state = 1; - - /* If reseeding, seed with the value supplied, and note seeded. */ - if (new_seed > 0) - { - rand_state = new_seed; - is_seeded = TRUE; - return 0; - } - else - { - /* If not explicitly seeded yet, generate a seed from time(). */ - if (!is_seeded) - { - rand_state = (sc_uint)g_vm->_events->getTotalPlayTicks(); - is_seeded = TRUE; - } - - /* - * Advance random state, using constants from Park & Miller (1988). - * To keep the values the same for both 32 and 64 bit longs, mask out - * any bits above the bottom 32. - */ - rand_state = (rand_state * 16807 + 2147483647) & 0xffffffff; - - /* - * Discard the lowest bit as a way to map 32-bits unsigned to a 32-bit - * positive signed. - */ - return rand_state >> 1; - } +sc_congruential_rand(sc_uint new_seed) { + static sc_bool is_seeded = FALSE; + static sc_uint rand_state = 1; + + /* If reseeding, seed with the value supplied, and note seeded. */ + if (new_seed > 0) { + rand_state = new_seed; + is_seeded = TRUE; + return 0; + } else { + /* If not explicitly seeded yet, generate a seed from time(). */ + if (!is_seeded) { + rand_state = (sc_uint)g_vm->_events->getTotalPlayTicks(); + is_seeded = TRUE; + } + + /* + * Advance random state, using constants from Park & Miller (1988). + * To keep the values the same for both 32 and 64 bit longs, mask out + * any bits above the bottom 32. + */ + rand_state = (rand_state * 16807 + 2147483647) & 0xffffffff; + + /* + * Discard the lowest bit as a way to map 32-bits unsigned to a 32-bit + * positive signed. + */ + return rand_state >> 1; + } } /* Function pointer for the actual random number generator in use. */ -static sc_int (*sc_rand_function) (sc_uint) = sc_platform_rand; +static sc_int(*sc_rand_function)(sc_uint) = sc_platform_rand; /* * sc_set_congruential_random() @@ -280,49 +264,43 @@ static sc_int (*sc_rand_function) (sc_uint) = sc_platform_rand; * generate a random value within a given range. */ void -sc_set_congruential_random (void) -{ - sc_rand_function = sc_congruential_rand; +sc_set_congruential_random(void) { + sc_rand_function = sc_congruential_rand; } void -sc_set_platform_random (void) -{ - sc_rand_function = sc_platform_rand; +sc_set_platform_random(void) { + sc_rand_function = sc_platform_rand; } sc_bool -sc_is_congruential_random (void) -{ - return sc_rand_function == sc_congruential_rand; +sc_is_congruential_random(void) { + return sc_rand_function == sc_congruential_rand; } void -sc_seed_random (sc_uint new_seed) -{ - /* Ignore zero values of new_seed by simply using 1 instead. */ - sc_rand_function (new_seed > 0 ? new_seed : 1); +sc_seed_random(sc_uint new_seed) { + /* Ignore zero values of new_seed by simply using 1 instead. */ + sc_rand_function(new_seed > 0 ? new_seed : 1); } sc_int -sc_rand (void) -{ - sc_int retval; +sc_rand(void) { + sc_int retval; - /* Passing zero indicates this is not a seed operation. */ - retval = sc_rand_function (0); - return retval; + /* Passing zero indicates this is not a seed operation. */ + retval = sc_rand_function(0); + return retval; } sc_int -sc_randomint (sc_int low, sc_int high) -{ - /* - * If the range is invalid, just return the low value given. This mimics - * Adrift under the same conditions, and also guards against division by - * zero in the mod operation. - */ - return (high < low) ? low : low + sc_rand () % (high - low + 1); +sc_randomint(sc_int low, sc_int high) { + /* + * If the range is invalid, just return the low value given. This mimics + * Adrift under the same conditions, and also guards against division by + * zero in the mod operation. + */ + return (high < low) ? low : low + sc_rand() % (high - low + 1); } @@ -336,20 +314,18 @@ static const sc_char SPACE = ' '; * Return TRUE if a string is either zero-length or contains only whitespace. */ sc_bool -sc_strempty (const sc_char *string) -{ - sc_int index_; - assert (string); - - /* Scan for any non-space character. */ - for (index_ = 0; string[index_] != NUL; index_++) - { - if (!sc_isspace (string[index_])) - return FALSE; - } - - /* None found, so string is empty. */ - return TRUE; +sc_strempty(const sc_char *string) { + sc_int index_; + assert(string); + + /* Scan for any non-space character. */ + for (index_ = 0; string[index_] != NUL; index_++) { + if (!sc_isspace(string[index_])) + return FALSE; + } + + /* None found, so string is empty. */ + return TRUE; } @@ -360,20 +336,19 @@ sc_strempty (const sc_char *string) * in place, and returns the string address for convenience. */ sc_char * -sc_trim_string (sc_char *string) -{ - sc_int index_; - assert (string); +sc_trim_string(sc_char *string) { + sc_int index_; + assert(string); - for (index_ = strlen (string) - 1; - index_ >= 0 && sc_isspace (string[index_]); index_--) - string[index_] = NUL; + for (index_ = strlen(string) - 1; + index_ >= 0 && sc_isspace(string[index_]); index_--) + string[index_] = NUL; - for (index_ = 0; sc_isspace (string[index_]);) - index_++; - memmove (string, string + index_, strlen (string) - index_ + 1); + for (index_ = 0; sc_isspace(string[index_]);) + index_++; + memmove(string, string + index_, strlen(string) - index_ + 1); - return string; + return string; } @@ -385,30 +360,27 @@ sc_trim_string (sc_char *string) * convenience. */ sc_char * -sc_normalize_string (sc_char *string) -{ - sc_int index_; - assert (string); - - /* Trim all leading and trailing spaces. */ - string = sc_trim_string (string); - - /* Compress multiple whitespace runs into a single space character. */ - for (index_ = 0; string[index_] != NUL; index_++) - { - if (sc_isspace (string[index_])) - { - sc_int cursor; - - string[index_] = SPACE; - for (cursor = index_ + 1; sc_isspace (string[cursor]);) - cursor++; - memmove (string + index_ + 1, - string + cursor, strlen (string + cursor) + 1); - } - } - - return string; +sc_normalize_string(sc_char *string) { + sc_int index_; + assert(string); + + /* Trim all leading and trailing spaces. */ + string = sc_trim_string(string); + + /* Compress multiple whitespace runs into a single space character. */ + for (index_ = 0; string[index_] != NUL; index_++) { + if (sc_isspace(string[index_])) { + sc_int cursor; + + string[index_] = SPACE; + for (cursor = index_ + 1; sc_isspace(string[cursor]);) + cursor++; + memmove(string + index_ + 1, + string + cursor, strlen(string + cursor) + 1); + } + } + + return string; } @@ -418,13 +390,12 @@ sc_normalize_string (sc_char *string) * Return TRUE if the first word in the string is word, case insensitive. */ sc_bool -sc_compare_word (const sc_char *string, const sc_char *word, sc_int length) -{ - assert (string && word); +sc_compare_word(const sc_char *string, const sc_char *word, sc_int length) { + assert(string && word); - /* Return TRUE if string starts with word, then space or string end. */ - return sc_strncasecmp (string, word, length) == 0 - && (string[length] == NUL || sc_isspace (string[length])); + /* Return TRUE if string starts with word, then space or string end. */ + return sc_strncasecmp(string, word, length) == 0 + && (string[length] == NUL || sc_isspace(string[length])); } @@ -435,27 +406,24 @@ sc_compare_word (const sc_char *string, const sc_char *word, sc_int length) * and tools', page 436, unmodulo'ed and somewhat restyled. */ sc_uint -sc_hash (const sc_char *string) -{ - sc_int index_; - sc_uint hash; - assert (string); - - hash = 0; - for (index_ = 0; string[index_] != NUL; index_++) - { - sc_uint temp; - - hash = (hash << 4) + string[index_]; - temp = hash & 0xf0000000; - if (temp != 0) - { - hash = hash ^ (temp >> 24); - hash = hash ^ temp; - } - } - - return hash; +sc_hash(const sc_char *string) { + sc_int index_; + sc_uint hash; + assert(string); + + hash = 0; + for (index_ = 0; string[index_] != NUL; index_++) { + sc_uint temp; + + hash = (hash << 4) + string[index_]; + temp = hash & 0xf0000000; + if (temp != 0) { + hash = hash ^ (temp >> 24); + hash = hash ^ temp; + } + } + + return hash; } } // End of namespace Adrift |