X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=3b447821aafe06315a5286fd63386ebbd8e36234;hb=cb97a0d81529dc81696e5df8e17083b30dfe633e;hp=66d1064902c4c947e216a8c59566411e3d981ea3;hpb=4bb90da7a98b741b4576099be6b89d324c8a80de;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 66d10649..3b447821 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -52,6 +52,7 @@ void printf_line(char *line_string, int line_length) fprintf_line(stdout, line_string, line_length); } + /* int2str() returns a number converted to a string; the used memory is static, but will be overwritten by later calls, so if you want to save the result, copy it to a private string buffer; @@ -82,6 +83,7 @@ char *int2str(int number, int size) } } + /* something similar to "int2str()" above, but allocates its own memory and has a different interface; we cannot use "itoa()", because this seems to be already defined when cross-compiling to the win32 target */ @@ -90,8 +92,7 @@ char *i_to_a(unsigned int i) { static char *a = NULL; - if (a != NULL) - free(a); + checked_free(a); if (i > 2147483647) /* yes, this is a kludge */ i = 2147483647; @@ -104,6 +105,23 @@ char *i_to_a(unsigned int i) } +/* calculate base-2 logarithm of argument (rounded down to integer; + this function returns the number of the highest bit set in argument) */ + +int log_2(unsigned int x) +{ + int e = 0; + + while ((1 << e) < x) + { + x -= (1 << e); /* for rounding down (rounding up: remove this line) */ + e++; + } + + return e; +} + + /* ------------------------------------------------------------------------- */ /* counter functions */ /* ------------------------------------------------------------------------- */ @@ -283,48 +301,6 @@ void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay) /* random generator functions */ /* ------------------------------------------------------------------------- */ -#if 0 -unsigned int SimpleRND(unsigned int max) -{ - return (random_linux_libc(RND_FREE) % max); -} - -unsigned int InitSimpleRND(long seed) -{ - if (seed == NEW_RANDOMIZE) - { - struct timeval current_time; - - gettimeofday(¤t_time, NULL); - seed = (long)current_time.tv_usec; - } - - srandom_linux_libc(RND_FREE, (unsigned int) seed); - - return (unsigned int) seed; -} - -unsigned int RND(unsigned int max) -{ - return (random_linux_libc(RND_GAME) % max); -} - -unsigned int InitRND(long seed) -{ - if (seed == NEW_RANDOMIZE) - { - struct timeval current_time; - - gettimeofday(¤t_time, NULL); - seed = (long)current_time.tv_usec; - } - - srandom_linux_libc(RND_GAME, (unsigned int) seed); - - return (unsigned int) seed; -} -#endif - unsigned int init_random_number(int nr, long seed) { if (seed == NEW_RANDOMIZE) @@ -344,27 +320,10 @@ unsigned int init_random_number(int nr, long seed) return (unsigned int) seed; } -#if 1 -unsigned int get_random_number(int nr, unsigned int max) +unsigned int get_random_number(int nr, int max) { return (max > 0 ? random_linux_libc(nr) % max : 0); } -#else -unsigned int get_random_number(int nr, unsigned int max) -{ - unsigned int rnd = (max > 0 ? random_linux_libc(nr) % max : 0); - - if (nr == 0 && FrameCounter < 2) - printf("::: %d [%d]\n", rnd, FrameCounter); - -#if 0 - if (nr == 0 && FrameCounter < 2 && rnd == 8) - rnd /= 0; -#endif - - return rnd; -} -#endif /* ------------------------------------------------------------------------- */ @@ -560,8 +519,7 @@ char *getStringToLower(char *s) void setString(char **old_value, char *new_value) { - if (*old_value != NULL) - free(*old_value); + checked_free(*old_value); *old_value = getStringCopy(new_value); } @@ -843,7 +801,7 @@ void Error(int mode, char *format, ...) /* ------------------------------------------------------------------------- */ -/* memory allocation functions */ +/* checked memory allocation and freeing functions */ /* ------------------------------------------------------------------------- */ void *checked_malloc(unsigned long size) @@ -880,6 +838,12 @@ void *checked_realloc(void *ptr, unsigned long size) return ptr; } +void checked_free(void *ptr) +{ + if (ptr != NULL) /* this check should be done by free() anyway */ + free(ptr); +} + /* ------------------------------------------------------------------------- */ /* various helper functions */ @@ -905,27 +869,27 @@ inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2) *y2 = help_y; } -short getFile16BitInteger(FILE *file, int byte_order) +int getFile16BitInteger(FILE *file, int byte_order) { if (byte_order == BYTE_ORDER_BIG_ENDIAN) - return ((fgetc(file) << 8) | - (fgetc(file) << 0)); + return ((fgetc(file) << 8) | + (fgetc(file) << 0)); else /* BYTE_ORDER_LITTLE_ENDIAN */ - return ((fgetc(file) << 0) | - (fgetc(file) << 8)); + return ((fgetc(file) << 0) | + (fgetc(file) << 8)); } -void putFile16BitInteger(FILE *file, short value, int byte_order) +void putFile16BitInteger(FILE *file, int value, int byte_order) { if (byte_order == BYTE_ORDER_BIG_ENDIAN) { - fputc((value >> 8) & 0xff, file); - fputc((value >> 0) & 0xff, file); + fputc((value >> 8) & 0xff, file); + fputc((value >> 0) & 0xff, file); } else /* BYTE_ORDER_LITTLE_ENDIAN */ { - fputc((value >> 0) & 0xff, file); - fputc((value >> 8) & 0xff, file); + fputc((value >> 0) & 0xff, file); + fputc((value >> 8) & 0xff, file); } } @@ -1539,8 +1503,7 @@ boolean fileHasPrefix(char *basename, char *prefix) static char *basename_lower = NULL; int basename_length, prefix_length; - if (basename_lower != NULL) - free(basename_lower); + checked_free(basename_lower); if (basename == NULL || prefix == NULL) return FALSE; @@ -1562,8 +1525,7 @@ boolean fileHasSuffix(char *basename, char *suffix) static char *basename_lower = NULL; int basename_length, suffix_length; - if (basename_lower != NULL) - free(basename_lower); + checked_free(basename_lower); if (basename == NULL || suffix == NULL) return FALSE; @@ -1864,7 +1826,7 @@ static boolean token_suffix_match(char *token, char *suffix, int start_pos) return FALSE; } -#define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN]" +#define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN_VALUE]" static void read_token_parameters(SetupFileHash *setup_file_hash, struct ConfigInfo *suffix_list, @@ -1937,6 +1899,7 @@ static void add_dynamic_file_list_entry(struct FileInfo **list, new_list_entry = &(*list)[*num_list_entries - 1]; new_list_entry->token = getStringCopy(token); + new_list_entry->default_filename = NULL; new_list_entry->filename = NULL; new_list_entry->parameter = checked_calloc(parameter_array_size); @@ -1981,7 +1944,8 @@ static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info, int num_ext2_suffixes = artwork_info->num_ext2_suffixes; int num_ext3_suffixes = artwork_info->num_ext3_suffixes; int num_ignore_tokens = artwork_info->num_ignore_tokens; - SetupFileHash *setup_file_hash, *extra_file_hash; + SetupFileHash *setup_file_hash, *valid_file_hash; + SetupFileHash *extra_file_hash, *empty_file_hash; char *known_token_value = KNOWN_TOKEN_VALUE; int i, j, k, l; @@ -1995,26 +1959,42 @@ static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info, if ((setup_file_hash = loadSetupFileHash(filename)) == NULL) return; + /* separate valid (defined) from empty (undefined) config token values */ + valid_file_hash = newSetupFileHash(); + empty_file_hash = newSetupFileHash(); + BEGIN_HASH_ITERATION(setup_file_hash, itr) + { + char *value = HASH_ITERATION_VALUE(itr); + + setHashEntry(*value ? valid_file_hash : empty_file_hash, + HASH_ITERATION_TOKEN(itr), value); + } + END_HASH_ITERATION(setup_file_hash, itr) + + /* at this point, we do not need the setup file hash anymore -- free it */ + freeSetupFileHash(setup_file_hash); + /* read parameters for all known config file tokens */ for (i = 0; i < num_file_list_entries; i++) - read_token_parameters(setup_file_hash, suffix_list, &file_list[i]); + read_token_parameters(valid_file_hash, suffix_list, &file_list[i]); /* set all tokens that can be ignored here to "known" keyword */ for (i = 0; i < num_ignore_tokens; i++) - setHashEntry(setup_file_hash, ignore_tokens[i], known_token_value); + setHashEntry(valid_file_hash, ignore_tokens[i], known_token_value); - /* copy all unknown config file tokens to extra config list */ + /* copy all unknown config file tokens to extra config hash */ extra_file_hash = newSetupFileHash(); - BEGIN_HASH_ITERATION(setup_file_hash, itr) + BEGIN_HASH_ITERATION(valid_file_hash, itr) { - if (strcmp(HASH_ITERATION_VALUE(itr), known_token_value) != 0) - setHashEntry(extra_file_hash, - HASH_ITERATION_TOKEN(itr), HASH_ITERATION_VALUE(itr)); + char *value = HASH_ITERATION_VALUE(itr); + + if (strcmp(value, known_token_value) != 0) + setHashEntry(extra_file_hash, HASH_ITERATION_TOKEN(itr), value); } - END_HASH_ITERATION(setup_file_hash, itr) + END_HASH_ITERATION(valid_file_hash, itr) - /* at this point, we do not need the config file hash anymore -- free it */ - freeSetupFileHash(setup_file_hash); + /* at this point, we do not need the valid file hash anymore -- free it */ + freeSetupFileHash(valid_file_hash); /* now try to determine valid, dynamically defined config tokens */ @@ -2231,11 +2211,12 @@ static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info, artwork_info->sizeof_artwork_list_entry); } - if (extra_file_hash != NULL && options.verbose && IS_PARENT_PROCESS()) + if (options.verbose && IS_PARENT_PROCESS()) { SetupFileList *setup_file_list, *list; boolean dynamic_tokens_found = FALSE; boolean unknown_tokens_found = FALSE; + boolean undefined_values_found = (hashtable_count(empty_file_hash) != 0); if ((setup_file_list = loadSetupFileList(filename)) == NULL) Error(ERR_EXIT, "loadSetupFileHash works, but loadSetupFileList fails"); @@ -2283,10 +2264,28 @@ static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info, Error(ERR_RETURN_LINE, "-"); } + if (undefined_values_found) + { + Error(ERR_RETURN_LINE, "-"); + Error(ERR_RETURN, "warning: undefined values found in config file:"); + Error(ERR_RETURN, "- config file: '%s'", filename); + + for (list = setup_file_list; list != NULL; list = list->next) + { + char *value = getHashEntry(empty_file_hash, list->token); + + if (value != NULL) + Error(ERR_RETURN, "- undefined value for token: '%s'", list->token); + } + + Error(ERR_RETURN_LINE, "-"); + } + freeSetupFileList(setup_file_list); } freeSetupFileHash(extra_file_hash); + freeSetupFileHash(empty_file_hash); #if 0 for (i = 0; i < num_file_list_entries; i++) @@ -2570,9 +2569,17 @@ void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info) #endif for (i = 0; i < num_dynamic_file_list_entries; i++) + { LoadArtworkToList(artwork_info, &artwork_info->dynamic_artwork_list[i], dynamic_file_list[i].filename, i); +#if 0 + printf("::: '%s', '0x%08x'\n", + dynamic_file_list[i].filename, + dynamic_file_list[i].default_filename); +#endif + } + #if 0 dumpList(artwork_info->content_list); #endif