From: Holger Schemel Date: Thu, 4 Feb 2021 16:39:22 +0000 (+0100) Subject: added sorting artwork info cache data before writing to cache file X-Git-Tag: 4.2.3.0~24 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=e1bc80b7482a6baa481391980991f42480a7c52f;p=rocksndiamonds.git added sorting artwork info cache data before writing to cache file --- diff --git a/src/libgame/setup.c b/src/libgame/setup.c index e1840b1d..e81498a0 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -2326,25 +2326,51 @@ static boolean loadSetupFileData(void *setup_file_data, char *filename, return TRUE; } +static int compareSetupFileData(const void *object1, const void *object2) +{ + const struct ConfigInfo *entry1 = (struct ConfigInfo *)object1; + const struct ConfigInfo *entry2 = (struct ConfigInfo *)object2; + + return strcmp(entry1->token, entry2->token); +} + static void saveSetupFileHash(SetupFileHash *hash, char *filename) { + int item_count = hashtable_count(hash); + int item_size = sizeof(struct ConfigInfo); + struct ConfigInfo *sort_array = checked_malloc(item_count * item_size); FILE *file; + int i = 0; - if (!(file = fopen(filename, MODE_WRITE))) + // copy string pointers from hash to array + BEGIN_HASH_ITERATION(hash, itr) { - Warn("cannot write configuration file '%s'", filename); + sort_array[i].token = HASH_ITERATION_TOKEN(itr); + sort_array[i].value = HASH_ITERATION_VALUE(itr); - return; + i++; + + if (i > item_count) // should never happen + break; } + END_HASH_ITERATION(hash, itr) - BEGIN_HASH_ITERATION(hash, itr) + // sort string pointers from hash in array + qsort(sort_array, item_count, item_size, compareSetupFileData); + + if (!(file = fopen(filename, MODE_WRITE))) { - fprintf(file, "%s\n", getFormattedSetupEntry(HASH_ITERATION_TOKEN(itr), - HASH_ITERATION_VALUE(itr))); + Warn("cannot write configuration file '%s'", filename); + + return; } - END_HASH_ITERATION(hash, itr) + for (i = 0; i < item_count; i++) + fprintf(file, "%s\n", getFormattedSetupEntry(sort_array[i].token, + sort_array[i].value)); fclose(file); + + checked_free(sort_array); } SetupFileList *loadSetupFileList(char *filename)