X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=e5cdfe26da8efec03bb294f2c2218c7081074b01;hb=b8ccb648b89e24b27f8a60e2d8e9a4680322bd86;hp=b5dab8f7ebd2da86faddea65b30055e46a20ce1a;hpb=ff2510f5098b545a03965c6b95296eec6885a4fb;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index b5dab8f7..e5cdfe26 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -429,6 +429,7 @@ char *getPath2(char *path1, char *path2) strlen(path2) + 1); sprintf(complete_path, "%s/%s", path1, path2); + return complete_path; } @@ -439,6 +440,7 @@ char *getPath3(char *path1, char *path2, char *path3) strlen(path3) + 1); sprintf(complete_path, "%s/%s/%s", path1, path2, path3); + return complete_path; } @@ -447,6 +449,7 @@ char *getStringCat2(char *s1, char *s2) char *complete_string = checked_malloc(strlen(s1) + strlen(s2) + 1); sprintf(complete_string, "%s%s", s1, s2); + return complete_string; } @@ -458,8 +461,8 @@ char *getStringCopy(char *s) return NULL; s_copy = checked_malloc(strlen(s) + 1); - strcpy(s_copy, s); + return s_copy; } @@ -475,6 +478,14 @@ char *getStringToLower(char *s) return s_copy; } +void setString(char **old_value, char *new_value) +{ + if (*old_value != NULL) + free(*old_value); + + *old_value = getStringCopy(new_value); +} + /* ------------------------------------------------------------------------- */ /* command line option handling functions */ @@ -529,6 +540,11 @@ void GetOptions(char *argv[]) options.verbose = FALSE; options.debug = FALSE; +#if !defined(PLATFORM_UNIX) + if (*options_left == NULL) /* no options given -- enable verbose mode */ + options.verbose = TRUE; +#endif + while (*options_left) { char option_str[MAX_OPTION_LEN]; @@ -1499,25 +1515,92 @@ boolean FileIsArtworkType(char *basename, int type) /* functions for loading artwork configuration information */ /* ------------------------------------------------------------------------- */ -static void FreeCustomArtworkList(struct ArtworkListInfo *, - struct ListNodeInfo ***, int *); +/* This function checks if a string of the format "string1, string2, ..." + exactly contains a string . */ + +static boolean string_has_parameter(char *s, char *s_contained) +{ + char *substring; + + if (s == NULL || s_contained == NULL) + return FALSE; -static int get_parameter_value(int type, char *value) + if (strlen(s_contained) > strlen(s)) + return FALSE; + + if (strncmp(s, s_contained, strlen(s_contained)) == 0) + { + char next_char = s[strlen(s_contained)]; + + /* check if next character is delimiter or whitespace */ + return (next_char == ',' || next_char == '\0' || + next_char == ' ' || next_char == '\t' ? TRUE : FALSE); + } + + /* check if string contains another parameter string after a comma */ + substring = strchr(s, ','); + if (substring == NULL) /* string does not contain a comma */ + return FALSE; + + /* advance string pointer to next character after the comma */ + substring++; + + /* skip potential whitespaces after the comma */ + while (*substring == ' ' || *substring == '\t') + substring++; + + return string_has_parameter(substring, s_contained); +} + +int get_parameter_value(char *token, char *value_raw, int type) { - return (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE : - type == TYPE_INTEGER ? get_integer_from_string(value) : - type == TYPE_BOOLEAN ? get_boolean_from_string(value) : - -1); + char *value = getStringToLower(value_raw); + int result = 0; /* probably a save default value */ + + if (strcmp(token, ".direction") == 0) + { + result = (strcmp(value, "left") == 0 ? MV_LEFT : + strcmp(value, "right") == 0 ? MV_RIGHT : + strcmp(value, "up") == 0 ? MV_UP : + strcmp(value, "down") == 0 ? MV_DOWN : MV_NO_MOVING); + } + else if (strcmp(token, ".anim_mode") == 0) + { + result = (string_has_parameter(value, "loop") ? ANIM_LOOP : + string_has_parameter(value, "linear") ? ANIM_LINEAR : + string_has_parameter(value, "pingpong") ? ANIM_PINGPONG : + string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 : + string_has_parameter(value, "random") ? ANIM_RANDOM : + ANIM_LOOP); + + if (string_has_parameter(value, "reverse")) + result |= ANIM_REVERSE; + } + else /* generic parameter of type integer or boolean */ + { + result = (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE : + type == TYPE_INTEGER ? get_integer_from_string(value) : + type == TYPE_BOOLEAN ? get_boolean_from_string(value) : + ARG_UNDEFINED_VALUE); + } + + free(value); + + return result; } +static void FreeCustomArtworkList(struct ArtworkListInfo *, + struct ListNodeInfo ***, int *); + struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, struct ConfigInfo *suffix_list, + char **ignore_tokens, int num_file_list_entries) { struct FileInfo *file_list; int num_file_list_entries_found = 0; int num_suffix_list_entries = 0; - int list_pos = 0; + int list_pos; int i, j; file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo)); @@ -1529,46 +1612,31 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, for (i=0; i 0) { - int parameter_array_size = num_suffix_list_entries * sizeof(int); + int parameter_array_size = num_suffix_list_entries * sizeof(char *); file_list[i].default_parameter = checked_calloc(parameter_array_size); file_list[i].parameter = checked_calloc(parameter_array_size); for (j=0; j '%s'\n", - i, config_list[i].token, config_list[i].value); - - len_config_token = strlen(config_list[i].token); - len_config_value = strlen(config_list[i].value); - is_file_entry = TRUE; - -#else int len_config_token = strlen(config_list[i].token); int len_config_value = strlen(config_list[i].value); boolean is_file_entry = TRUE; -#endif for (j=0; suffix_list[j].token != NULL; j++) { @@ -1578,14 +1646,19 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, strcmp(&config_list[i].token[len_config_token - len_suffix], suffix_list[j].token) == 0) { - file_list[list_pos].default_parameter[j] = - get_parameter_value(suffix_list[j].type, config_list[i].value); + setString(&file_list[list_pos].default_parameter[j], + config_list[i].value); is_file_entry = FALSE; break; } } + /* the following tokens are no file definitions, but other config tokens */ + for (j=0; ignore_tokens[j] != NULL; j++) + if (strcmp(config_list[i].token, ignore_tokens[j]) == 0) + is_file_entry = FALSE; + if (is_file_entry) { if (i > 0) @@ -1663,24 +1736,21 @@ static void read_token_parameters(struct SetupFileList *setup_file_list, char *known_token_value = KNOWN_TOKEN_VALUE; int i; - if (file_list_entry->filename != NULL) - free(file_list_entry->filename); - if (filename != NULL) { + setString(&file_list_entry->filename, filename); + /* when file definition found, set all parameters to default values */ for (i=0; suffix_list[i].token != NULL; i++) - file_list_entry->parameter[i] = - get_parameter_value(suffix_list[i].type, suffix_list[i].value); + setString(&file_list_entry->parameter[i], suffix_list[i].value); - file_list_entry->filename = getStringCopy(filename); + file_list_entry->redefined = TRUE; /* mark config file token as well known from default config */ setTokenValue(setup_file_list, file_list_entry->token, known_token_value); } else - file_list_entry->filename = - getStringCopy(file_list_entry->default_filename); + setString(&file_list_entry->filename, file_list_entry->default_filename); /* check for config tokens that can be build by base token and suffixes */ for (i=0; suffix_list[i].token != NULL; i++) @@ -1690,8 +1760,7 @@ static void read_token_parameters(struct SetupFileList *setup_file_list, if (value != NULL) { - file_list_entry->parameter[i] = - get_parameter_value(suffix_list[i].type, value); + setString(&file_list_entry->parameter[i], value); /* mark config file token as well known from default config */ setTokenValue(setup_file_list, token, known_token_value); @@ -1709,7 +1778,7 @@ static void add_dynamic_file_list_entry(struct FileInfo **list, char *token) { struct FileInfo *new_list_entry; - int parameter_array_size = num_suffix_list_entries * sizeof(int); + int parameter_array_size = num_suffix_list_entries * sizeof(char *); #if 0 if (IS_PARENT_PROCESS()) @@ -1756,12 +1825,14 @@ void LoadArtworkConfig(struct ArtworkListInfo *artwork_info) char **ext1_suffixes = artwork_info->ext1_suffixes; char **ext2_suffixes = artwork_info->ext2_suffixes; char **ext3_suffixes = artwork_info->ext3_suffixes; + char **ignore_tokens = artwork_info->ignore_tokens; int num_file_list_entries = artwork_info->num_file_list_entries; int num_suffix_list_entries = artwork_info->num_suffix_list_entries; int num_base_prefixes = artwork_info->num_base_prefixes; int num_ext1_suffixes = artwork_info->num_ext1_suffixes; 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; char *filename = getCustomArtworkConfigFilename(artwork_info->type); struct SetupFileList *setup_file_list; struct SetupFileList *extra_file_list = NULL; @@ -1776,12 +1847,12 @@ void LoadArtworkConfig(struct ArtworkListInfo *artwork_info) /* always start with reliable default values */ for (i=0; inext) @@ -2387,3 +2458,20 @@ void debug_print_timestamp(int counter_nr, char *message) counter[counter_nr][1] = Counter(); } + +void debug_print_parent_only(char *format, ...) +{ + if (!IS_PARENT_PROCESS()) + return; + + if (format) + { + va_list ap; + + va_start(ap, format); + vprintf(format, ap); + va_end(ap); + + printf("\n"); + } +}