X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=a20948d58d696a8fc0c7df8351753fd5f68bcdbc;hb=30f635b58c076871cee2630ce15ffd7019764b2e;hp=e300b9bb50821b6c4115fb0e2a8181af2ea57de5;hpb=e77c26313b1a1e3ddd884cdda005cb6758c1a100;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index e300b9bb..a20948d5 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -607,6 +607,23 @@ boolean strEqualN(char *s1, char *s2, int n) strncmp(s1, s2, n) == 0); } +boolean strPrefix(char *s, char *prefix) +{ + return (s == NULL && prefix == NULL ? TRUE : + s == NULL && prefix != NULL ? FALSE : + s != NULL && prefix == NULL ? FALSE : + strncmp(s, prefix, strlen(prefix)) == 0); +} + +boolean strSuffix(char *s, char *suffix) +{ + return (s == NULL && suffix == NULL ? TRUE : + s == NULL && suffix != NULL ? FALSE : + s != NULL && suffix == NULL ? FALSE : + strlen(s) < strlen(suffix) ? FALSE : + strncmp(&s[strlen(s) - strlen(suffix)], suffix, strlen(suffix)) == 0); +} + /* ------------------------------------------------------------------------- */ /* command line option handling functions */ @@ -933,6 +950,19 @@ void checked_free(void *ptr) free(ptr); } +void clear_mem(void *ptr, unsigned long size) +{ +#if defined(PLATFORM_WIN32) + /* for unknown reason, memset() sometimes crashes when compiled with MinGW */ + char *cptr = (char *)ptr; + + while (size--) + *cptr++ = 0; +#else + memset(ptr, 0, size); +#endif +} + /* ------------------------------------------------------------------------- */ /* various helper functions */ @@ -1532,9 +1562,13 @@ int get_integer_from_string(char *s) if (result == -1) { - if (strEqual(s_lower, "false")) + if (strEqual(s_lower, "false") || + strEqual(s_lower, "no") || + strEqual(s_lower, "off")) result = 0; - else if (strEqual(s_lower, "true")) + else if (strEqual(s_lower, "true") || + strEqual(s_lower, "yes") || + strEqual(s_lower, "on")) result = 1; else result = atoi(s); @@ -1853,7 +1887,11 @@ int get_parameter_value(char *value_raw, char *suffix, int type) string_has_parameter(value, "melt") ? FADE_MODE_MELT : FADE_MODE_DEFAULT); } +#if 1 + else if (strPrefix(suffix, ".font")) /* (may also be ".font_xyz") */ +#else else if (strEqualN(suffix, ".font", 5)) /* (may also be ".font_xyz") */ +#endif { result = gfx.get_font_from_token_function(value); } @@ -1870,35 +1908,6 @@ int get_parameter_value(char *value_raw, char *suffix, int type) return result; } -int get_token_parameter_value(char *token, char *value_raw) -{ - char *suffix; - - if (token == NULL || value_raw == NULL) - return ARG_UNDEFINED_VALUE; - - suffix = strrchr(token, '.'); - if (suffix == NULL) - suffix = token; - -#if 0 - if (strncmp(suffix, ".font", 5) == 0) - { - int i; - - /* !!! OPTIMIZE THIS BY USING HASH !!! */ - for (i = 0; i < NUM_FONTS; i++) - if (strEqual(value_raw, font_info[i].token_name)) - return i; - - /* if font not found, use reliable default value */ - return FONT_INITIAL_1; - } -#endif - - return get_parameter_value(value_raw, suffix, TYPE_INTEGER); -} - struct ScreenModeInfo *get_screen_mode_from_string(char *screen_mode_string) { static struct ScreenModeInfo screen_mode; @@ -2906,7 +2915,7 @@ void NotifyUserAboutErrorFile() #if DEBUG -#define DEBUG_NUM_TIMESTAMPS 3 +#define DEBUG_NUM_TIMESTAMPS 5 #define DEBUG_TIME_IN_MICROSECONDS 0 #if DEBUG_TIME_IN_MICROSECONDS @@ -2930,35 +2939,53 @@ static double Counter_Microseconds() } #endif +char *debug_print_timestamp_get_padding(int padding_size) +{ + static char *padding = NULL; + int max_padding_size = 100; + + if (padding == NULL) + { + padding = checked_calloc(max_padding_size + 1); + memset(padding, ' ', max_padding_size); + } + + return &padding[MAX(0, max_padding_size - padding_size)]; +} + void debug_print_timestamp(int counter_nr, char *message) { -#if DEBUG_TIME_IN_MICROSECONDS - static double counter[DEBUG_NUM_TIMESTAMPS][2]; + int indent_size = 8; + int padding_size = 40; + float timestamp_interval; - if (counter_nr >= DEBUG_NUM_TIMESTAMPS) + if (counter_nr < 0) + Error(ERR_EXIT, "debugging: invalid negative counter"); + else if (counter_nr >= DEBUG_NUM_TIMESTAMPS) Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c"); - counter[counter_nr][0] = Counter_Microseconds(); - - if (message) - printf("%s %.3f ms\n", message, - (counter[counter_nr][0] - counter[counter_nr][1]) / 1000); +#if DEBUG_TIME_IN_MICROSECONDS + static double counter[DEBUG_NUM_TIMESTAMPS][2]; + char *unit = "ms"; - counter[counter_nr][1] = counter[counter_nr][0]; + counter[counter_nr][0] = Counter_Microseconds(); #else static long counter[DEBUG_NUM_TIMESTAMPS][2]; - - if (counter_nr >= DEBUG_NUM_TIMESTAMPS) - Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c"); + char *unit = "s"; counter[counter_nr][0] = Counter(); +#endif - if (message) - printf("%s %.3f s\n", message, - (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000); - + timestamp_interval = counter[counter_nr][0] - counter[counter_nr][1]; counter[counter_nr][1] = counter[counter_nr][0]; -#endif + + if (message) + printf("%s%s%s %.3f %s\n", + debug_print_timestamp_get_padding(counter_nr * indent_size), + message, + debug_print_timestamp_get_padding(padding_size - strlen(message)), + timestamp_interval / 1000, + unit); } void debug_print_parent_only(char *format, ...)