+char *getCookie(char *file_type)
+{
+ static char cookie[MAX_COOKIE_LEN + 1];
+
+ if (strlen(program.cookie_prefix) + 1 +
+ strlen(file_type) + strlen("_FILE_VERSION_x.x") > MAX_COOKIE_LEN)
+ return "[COOKIE ERROR]"; /* should never happen */
+
+ sprintf(cookie, "%s_%s_FILE_VERSION_%d.%d",
+ program.cookie_prefix, file_type,
+ program.version_major, program.version_minor);
+
+ return cookie;
+}
+
+int getFileVersionFromCookieString(const char *cookie)
+{
+ const char *ptr_cookie1, *ptr_cookie2;
+ const char *pattern1 = "_FILE_VERSION_";
+ const char *pattern2 = "?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_pattern1 = strlen(pattern1);
+ const int len_pattern2 = strlen(pattern2);
+ const int len_pattern = len_pattern1 + len_pattern2;
+ int version_major, version_minor;
+
+ if (len_cookie <= len_pattern)
+ return -1;
+
+ ptr_cookie1 = &cookie[len_cookie - len_pattern];
+ ptr_cookie2 = &cookie[len_cookie - len_pattern2];
+
+ if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
+ return -1;
+
+ if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
+ ptr_cookie2[1] != '.' ||
+ ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
+ return -1;
+
+ version_major = ptr_cookie2[0] - '0';
+ version_minor = ptr_cookie2[2] - '0';
+
+ return VERSION_IDENT(version_major, version_minor, 0, 0);
+}
+
+boolean checkCookieString(const char *cookie, const char *template)
+{
+ const char *pattern = "_FILE_VERSION_?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_template = strlen(template);
+ const int len_pattern = strlen(pattern);
+
+ if (len_cookie != len_template)
+ return FALSE;
+
+ if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+
+/* ------------------------------------------------------------------------- */
+/* setup file list and hash handling functions */
+/* ------------------------------------------------------------------------- */
+
+char *getFormattedSetupEntry(char *token, char *value)
+{
+ int i;
+ static char entry[MAX_LINE_LEN];
+
+ /* if value is an empty string, just return token without value */
+ if (*value == '\0')
+ return token;
+
+ /* start with the token and some spaces to format output line */
+ sprintf(entry, "%s:", token);
+ for (i = strlen(entry); i < TOKEN_VALUE_POSITION; i++)
+ strcat(entry, " ");
+
+ /* continue with the token's value */
+ strcat(entry, value);
+
+ return entry;
+}
+
+SetupFileList *newSetupFileList(char *token, char *value)
+{
+ SetupFileList *new = checked_malloc(sizeof(SetupFileList));
+
+ new->token = getStringCopy(token);
+ new->value = getStringCopy(value);
+
+ new->next = NULL;
+
+ return new;
+}
+
+void freeSetupFileList(SetupFileList *list)
+{
+ if (list == NULL)
+ return;
+
+ checked_free(list->token);
+ checked_free(list->value);
+
+ if (list->next)
+ freeSetupFileList(list->next);
+
+ free(list);
+}
+
+char *getListEntry(SetupFileList *list, char *token)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strcmp(list->token, token) == 0)
+ return list->value;
+ else
+ return getListEntry(list->next, token);
+}
+
+SetupFileList *setListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strcmp(list->token, token) == 0)
+ {
+ checked_free(list->value);
+
+ list->value = getStringCopy(value);
+
+ return list;
+ }
+ else if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return setListEntry(list->next, token, value);
+}
+
+SetupFileList *addListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return addListEntry(list->next, token, value);
+}
+
+#ifdef DEBUG
+static void printSetupFileList(SetupFileList *list)
+{
+ if (!list)
+ return;
+
+ printf("token: '%s'\n", list->token);
+ printf("value: '%s'\n", list->value);
+
+ printSetupFileList(list->next);
+}
+#endif
+
+#ifdef DEBUG
+DEFINE_HASHTABLE_INSERT(insert_hash_entry, char, char);
+DEFINE_HASHTABLE_SEARCH(search_hash_entry, char, char);
+DEFINE_HASHTABLE_CHANGE(change_hash_entry, char, char);
+DEFINE_HASHTABLE_REMOVE(remove_hash_entry, char, char);
+#else
+#define insert_hash_entry hashtable_insert
+#define search_hash_entry hashtable_search
+#define change_hash_entry hashtable_change
+#define remove_hash_entry hashtable_remove
+#endif
+
+static unsigned int get_hash_from_key(void *key)
+{
+ /*
+ djb2
+
+ This algorithm (k=33) was first reported by Dan Bernstein many years ago in
+ 'comp.lang.c'. Another version of this algorithm (now favored by Bernstein)
+ uses XOR: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why
+ it works better than many other constants, prime or not) has never been
+ adequately explained.
+
+ If you just want to have a good hash function, and cannot wait, djb2
+ is one of the best string hash functions i know. It has excellent
+ distribution and speed on many different sets of keys and table sizes.
+ You are not likely to do better with one of the "well known" functions
+ such as PJW, K&R, etc.
+
+ Ozan (oz) Yigit [http://www.cs.yorku.ca/~oz/hash.html]
+ */
+
+ char *str = (char *)key;
+ unsigned int hash = 5381;
+ int c;
+
+ while ((c = *str++))
+ hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
+
+ return hash;
+}
+
+static int keys_are_equal(void *key1, void *key2)
+{
+ return (strcmp((char *)key1, (char *)key2) == 0);
+}
+
+SetupFileHash *newSetupFileHash()
+{
+ SetupFileHash *new_hash =
+ create_hashtable(16, 0.75, get_hash_from_key, keys_are_equal);
+
+ if (new_hash == NULL)
+ Error(ERR_EXIT, "create_hashtable() failed -- out of memory");
+
+ return new_hash;
+}
+
+void freeSetupFileHash(SetupFileHash *hash)
+{
+ if (hash == NULL)
+ return;
+
+ hashtable_destroy(hash, 1); /* 1 == also free values stored in hash */
+}
+
+char *getHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return NULL;
+
+ return search_hash_entry(hash, token);
+}
+
+void setHashEntry(SetupFileHash *hash, char *token, char *value)
+{
+ char *value_copy;
+
+ if (hash == NULL)
+ return;
+
+ value_copy = getStringCopy(value);
+
+ /* change value; if it does not exist, insert it as new */
+ if (!change_hash_entry(hash, token, value_copy))
+ if (!insert_hash_entry(hash, getStringCopy(token), value_copy))
+ Error(ERR_EXIT, "cannot insert into hash -- aborting");
+}
+
+char *removeHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return NULL;
+
+ return remove_hash_entry(hash, token);
+}
+
+#if 0
+#ifdef DEBUG
+static void printSetupFileHash(SetupFileHash *hash)
+{
+ BEGIN_HASH_ITERATION(hash, itr)
+ {
+ printf("token: '%s'\n", HASH_ITERATION_TOKEN(itr));
+ printf("value: '%s'\n", HASH_ITERATION_VALUE(itr));
+ }
+ END_HASH_ITERATION(hash, itr)
+}
+#endif
+#endif
+
+static void *loadSetupFileData(char *filename, boolean use_hash)
+{
+ char line[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
+ char *token, *value, *line_ptr;
+ void *setup_file_data, *insert_ptr = NULL;
+ boolean read_continued_line = FALSE;
+ FILE *file;
+
+ if (use_hash)
+ setup_file_data = newSetupFileHash();
+ else
+ insert_ptr = setup_file_data = newSetupFileList("", "");
+
+ if (!(file = fopen(filename, MODE_READ)))
+ {
+ Error(ERR_WARN, "cannot open configuration file '%s'", filename);
+ return NULL;
+ }
+
+ while (!feof(file))
+ {
+ /* read next line of input file */
+ if (!fgets(line, MAX_LINE_LEN, file))
+ break;
+
+ /* cut trailing newline or carriage return */
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ if (read_continued_line)
+ {
+ /* cut leading whitespaces from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ if (*line_ptr != ' ' && *line_ptr != '\t')
+ break;
+
+ /* append new line to existing line, if there is enough space */
+ if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
+ strcat(previous_line, line_ptr);
+
+ strcpy(line, previous_line); /* copy storage buffer to line */
+
+ read_continued_line = FALSE;
+ }
+
+ /* if the last character is '\', continue at next line */
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
+ {
+ line[strlen(line) - 1] = '\0'; /* cut off trailing backslash */
+ strcpy(previous_line, line); /* copy line to storage buffer */
+
+ read_continued_line = TRUE;
+
+ continue;
+ }
+
+ /* cut trailing comment from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == '#')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ /* cut trailing whitespaces from input line */
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ /* ignore empty lines */
+ if (*line == '\0')
+ continue;
+
+ /* cut leading whitespaces from token */
+ for (token = line; *token; token++)
+ if (*token != ' ' && *token != '\t')
+ break;
+
+ /* start with empty value as reliable default */
+ value = "";
+
+ /* find end of token to determine start of value */
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':')
+ {
+ *line_ptr = '\0'; /* terminate token string */
+ value = line_ptr + 1; /* set beginning of value */
+
+ break;
+ }
+ }
+
+ /* cut leading whitespaces from value */
+ for (; *value; value++)
+ if (*value != ' ' && *value != '\t')
+ break;
+
+#if 0
+ if (*value == '\0')
+ value = "true"; /* treat tokens without value as "true" */
+#endif
+
+ if (*token)
+ {
+ if (use_hash)
+ setHashEntry((SetupFileHash *)setup_file_data, token, value);
+ else
+ insert_ptr = addListEntry((SetupFileList *)insert_ptr, token, value);
+ }
+ }
+
+ fclose(file);
+
+ if (use_hash)
+ {
+ if (hashtable_count((SetupFileHash *)setup_file_data) == 0)
+ Error(ERR_WARN, "configuration file '%s' is empty", filename);
+ }
+ else
+ {
+ SetupFileList *setup_file_list = (SetupFileList *)setup_file_data;
+ SetupFileList *first_valid_list_entry = setup_file_list->next;
+
+ /* free empty list header */
+ setup_file_list->next = NULL;
+ freeSetupFileList(setup_file_list);
+ setup_file_data = first_valid_list_entry;
+
+ if (first_valid_list_entry == NULL)
+ Error(ERR_WARN, "configuration file '%s' is empty", filename);
+ }
+
+ return setup_file_data;
+}
+
+SetupFileList *loadSetupFileList(char *filename)
+{
+ return (SetupFileList *)loadSetupFileData(filename, FALSE);
+}
+
+SetupFileHash *loadSetupFileHash(char *filename)
+{
+ return (SetupFileHash *)loadSetupFileData(filename, TRUE);
+}
+
+void checkSetupFileHashIdentifier(SetupFileHash *setup_file_hash,
+ char *identifier)
+{
+ char *value = getHashEntry(setup_file_hash, TOKEN_STR_FILE_IDENTIFIER);
+
+ if (value == NULL)
+ Error(ERR_WARN, "configuration file has no file identifier");
+ else if (!checkCookieString(value, identifier))
+ Error(ERR_WARN, "configuration file has wrong file identifier");
+}
+
+
+/* ========================================================================= */
+/* setup file stuff */
+/* ========================================================================= */
+
+#define TOKEN_STR_LAST_LEVEL_SERIES "last_level_series"
+#define TOKEN_STR_LAST_PLAYED_LEVEL "last_played_level"
+#define TOKEN_STR_HANDICAP_LEVEL "handicap_level"
+
+/* level directory info */
+#define LEVELINFO_TOKEN_IDENTIFIER 0
+#define LEVELINFO_TOKEN_NAME 1
+#define LEVELINFO_TOKEN_NAME_SORTING 2
+#define LEVELINFO_TOKEN_AUTHOR 3
+#define LEVELINFO_TOKEN_IMPORTED_FROM 4
+#define LEVELINFO_TOKEN_IMPORTED_BY 5
+#define LEVELINFO_TOKEN_LEVELS 6
+#define LEVELINFO_TOKEN_FIRST_LEVEL 7
+#define LEVELINFO_TOKEN_SORT_PRIORITY 8
+#define LEVELINFO_TOKEN_LATEST_ENGINE 9
+#define LEVELINFO_TOKEN_LEVEL_GROUP 10
+#define LEVELINFO_TOKEN_READONLY 11
+#define LEVELINFO_TOKEN_GRAPHICS_SET 12
+#define LEVELINFO_TOKEN_SOUNDS_SET 13
+#define LEVELINFO_TOKEN_MUSIC_SET 14
+#define LEVELINFO_TOKEN_FILENAME 15
+#define LEVELINFO_TOKEN_FILETYPE 16
+#define LEVELINFO_TOKEN_HANDICAP 17
+#define LEVELINFO_TOKEN_SKIP_LEVELS 18
+
+#define NUM_LEVELINFO_TOKENS 19
+
+static LevelDirTree ldi;
+
+static struct TokenInfo levelinfo_tokens[] =
+{
+ /* level directory info */
+ { TYPE_STRING, &ldi.identifier, "identifier" },
+ { TYPE_STRING, &ldi.name, "name" },
+ { TYPE_STRING, &ldi.name_sorting, "name_sorting" },
+ { TYPE_STRING, &ldi.author, "author" },
+ { TYPE_STRING, &ldi.imported_from, "imported_from" },
+ { TYPE_STRING, &ldi.imported_by, "imported_by" },
+ { TYPE_INTEGER, &ldi.levels, "levels" },
+ { TYPE_INTEGER, &ldi.first_level, "first_level" },
+ { TYPE_INTEGER, &ldi.sort_priority, "sort_priority" },
+ { TYPE_BOOLEAN, &ldi.latest_engine, "latest_engine" },
+ { TYPE_BOOLEAN, &ldi.level_group, "level_group" },
+ { TYPE_BOOLEAN, &ldi.readonly, "readonly" },
+ { TYPE_STRING, &ldi.graphics_set, "graphics_set" },
+ { TYPE_STRING, &ldi.sounds_set, "sounds_set" },
+ { TYPE_STRING, &ldi.music_set, "music_set" },
+ { TYPE_STRING, &ldi.level_filename, "filename" },
+ { TYPE_STRING, &ldi.level_filetype, "filetype" },
+ { TYPE_BOOLEAN, &ldi.handicap, "handicap" },
+ { TYPE_BOOLEAN, &ldi.skip_levels, "skip_levels" }
+};
+
+static void setTreeInfoToDefaults(TreeInfo *ldi, int type)
+{
+ ldi->type = type;
+
+ ldi->node_top = (ldi->type == TREE_TYPE_LEVEL_DIR ? &leveldir_first :
+ ldi->type == TREE_TYPE_GRAPHICS_DIR ? &artwork.gfx_first :
+ ldi->type == TREE_TYPE_SOUNDS_DIR ? &artwork.snd_first :
+ ldi->type == TREE_TYPE_MUSIC_DIR ? &artwork.mus_first :
+ NULL);
+
+ ldi->node_parent = NULL;
+ ldi->node_group = NULL;
+ ldi->next = NULL;
+
+ ldi->cl_first = -1;
+ ldi->cl_cursor = -1;
+
+ ldi->subdir = NULL;
+ ldi->fullpath = NULL;
+ ldi->basepath = NULL;
+ ldi->identifier = NULL;
+ ldi->name = getStringCopy(ANONYMOUS_NAME);
+ ldi->name_sorting = NULL;
+ ldi->author = getStringCopy(ANONYMOUS_NAME);
+
+ ldi->sort_priority = LEVELCLASS_UNDEFINED; /* default: least priority */
+ ldi->latest_engine = FALSE; /* default: get from level */
+ ldi->parent_link = FALSE;
+ ldi->in_user_dir = FALSE;
+ ldi->user_defined = FALSE;
+ ldi->color = 0;
+ ldi->class_desc = NULL;
+
+ if (ldi->type == TREE_TYPE_LEVEL_DIR)
+ {
+ ldi->imported_from = NULL;
+ ldi->imported_by = NULL;
+
+ ldi->graphics_set = NULL;
+ ldi->sounds_set = NULL;
+ ldi->music_set = NULL;
+ ldi->graphics_path = getStringCopy(UNDEFINED_FILENAME);
+ ldi->sounds_path = getStringCopy(UNDEFINED_FILENAME);
+ ldi->music_path = getStringCopy(UNDEFINED_FILENAME);
+
+ ldi->level_filename = NULL;
+ ldi->level_filetype = NULL;
+
+ ldi->levels = 0;
+ ldi->first_level = 0;
+ ldi->last_level = 0;
+ ldi->level_group = FALSE;
+ ldi->handicap_level = 0;
+ ldi->readonly = TRUE;
+ ldi->handicap = TRUE;
+ ldi->skip_levels = FALSE;
+ }
+}
+
+static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi, TreeInfo *parent)
+{
+ if (parent == NULL)
+ {
+ Error(ERR_WARN, "setTreeInfoToDefaultsFromParent(): parent == NULL");
+
+ setTreeInfoToDefaults(ldi, TREE_TYPE_UNDEFINED);
+
+ return;
+ }
+
+#if 1
+ /* copy all values from the parent structure */
+
+ ldi->type = parent->type;
+
+ ldi->node_top = parent->node_top;
+ ldi->node_parent = parent;
+ ldi->node_group = NULL;
+ ldi->next = NULL;
+
+ ldi->cl_first = -1;
+ ldi->cl_cursor = -1;
+
+ ldi->subdir = NULL;
+ ldi->fullpath = NULL;
+ ldi->basepath = NULL;
+ ldi->identifier = NULL;
+ ldi->name = getStringCopy(ANONYMOUS_NAME);
+ ldi->name_sorting = NULL;
+ ldi->author = getStringCopy(parent->author);
+
+ ldi->sort_priority = parent->sort_priority;
+ ldi->latest_engine = parent->latest_engine;
+ ldi->parent_link = FALSE;
+ ldi->in_user_dir = parent->in_user_dir;
+ ldi->user_defined = parent->user_defined;
+ ldi->color = parent->color;
+ ldi->class_desc = getStringCopy(parent->class_desc);
+
+ if (ldi->type == TREE_TYPE_LEVEL_DIR)
+ {
+ ldi->imported_from = getStringCopy(parent->imported_from);
+ ldi->imported_by = getStringCopy(parent->imported_by);
+
+ ldi->graphics_set = NULL;
+ ldi->sounds_set = NULL;
+ ldi->music_set = NULL;
+ ldi->graphics_path = getStringCopy(UNDEFINED_FILENAME);
+ ldi->sounds_path = getStringCopy(UNDEFINED_FILENAME);
+ ldi->music_path = getStringCopy(UNDEFINED_FILENAME);
+
+ ldi->level_filename = NULL;
+ ldi->level_filetype = NULL;
+
+ ldi->levels = 0;
+ ldi->first_level = 0;
+ ldi->last_level = 0;
+ ldi->level_group = FALSE;
+ ldi->handicap_level = 0;
+ ldi->readonly = TRUE;
+ ldi->handicap = TRUE;
+ ldi->skip_levels = FALSE;
+ }
+
+#else
+
+ /* first copy all values from the parent structure ... */
+ *ldi = *parent;
+
+ /* ... then set all fields to default that cannot be inherited from parent.
+ This is especially important for all those fields that can be set from
+ the 'levelinfo.conf' config file, because the function 'setSetupInfo()'
+ calls 'free()' for all already set token values which requires that no
+ other structure's pointer may point to them!
+ */
+
+ ldi->subdir = NULL;
+ ldi->fullpath = NULL;
+ ldi->basepath = NULL;
+ ldi->identifier = NULL;
+ ldi->name = getStringCopy(ANONYMOUS_NAME);
+ ldi->name_sorting = NULL;
+ ldi->author = getStringCopy(parent->author);
+
+ ldi->imported_from = getStringCopy(parent->imported_from);
+ ldi->imported_by = getStringCopy(parent->imported_by);
+ ldi->class_desc = getStringCopy(parent->class_desc);
+
+ ldi->graphics_set = NULL;
+ ldi->sounds_set = NULL;
+ ldi->music_set = NULL;
+ ldi->graphics_path = NULL;
+ ldi->sounds_path = NULL;
+ ldi->music_path = NULL;
+
+ ldi->level_group = FALSE;
+ ldi->parent_link = FALSE;
+
+ ldi->node_top = parent->node_top;
+ ldi->node_parent = parent;
+ ldi->node_group = NULL;
+ ldi->next = NULL;
+
+#endif
+}
+
+static void freeTreeInfo(TreeInfo *ldi)
+{
+ checked_free(ldi->subdir);
+ checked_free(ldi->fullpath);
+ checked_free(ldi->basepath);
+ checked_free(ldi->identifier);
+
+ checked_free(ldi->name);
+ checked_free(ldi->name_sorting);
+ checked_free(ldi->author);
+
+ checked_free(ldi->class_desc);
+
+ if (ldi->type == TREE_TYPE_LEVEL_DIR)
+ {
+ checked_free(ldi->imported_from);
+ checked_free(ldi->imported_by);
+
+ checked_free(ldi->graphics_set);
+ checked_free(ldi->sounds_set);
+ checked_free(ldi->music_set);
+
+ checked_free(ldi->graphics_path);
+ checked_free(ldi->sounds_path);
+ checked_free(ldi->music_path);
+
+ checked_free(ldi->level_filename);
+ checked_free(ldi->level_filetype);
+ }
+}
+
+void setSetupInfo(struct TokenInfo *token_info,
+ int token_nr, char *token_value)
+{
+ int token_type = token_info[token_nr].type;
+ void *setup_value = token_info[token_nr].value;
+
+ if (token_value == NULL)
+ return;
+
+ /* set setup field to corresponding token value */
+ switch (token_type)
+ {
+ case TYPE_BOOLEAN:
+ case TYPE_SWITCH:
+ *(boolean *)setup_value = get_boolean_from_string(token_value);
+ break;
+
+ case TYPE_KEY:
+ *(Key *)setup_value = getKeyFromKeyName(token_value);
+ break;
+
+ case TYPE_KEY_X11:
+ *(Key *)setup_value = getKeyFromX11KeyName(token_value);
+ break;
+
+ case TYPE_INTEGER:
+ *(int *)setup_value = get_integer_from_string(token_value);
+ break;
+
+ case TYPE_STRING:
+ checked_free(*(char **)setup_value);
+ *(char **)setup_value = getStringCopy(token_value);
+ break;
+
+ default:
+ break;
+ }
+}
+
+static int compareTreeInfoEntries(const void *object1, const void *object2)
+{
+ const TreeInfo *entry1 = *((TreeInfo **)object1);
+ const TreeInfo *entry2 = *((TreeInfo **)object2);
+ int class_sorting1, class_sorting2;
+ int compare_result;
+
+ if (entry1->type == TREE_TYPE_LEVEL_DIR)
+ {
+ class_sorting1 = LEVELSORTING(entry1);
+ class_sorting2 = LEVELSORTING(entry2);
+ }
+ else
+ {
+ class_sorting1 = ARTWORKSORTING(entry1);
+ class_sorting2 = ARTWORKSORTING(entry2);
+ }
+
+ if (entry1->parent_link || entry2->parent_link)
+ compare_result = (entry1->parent_link ? -1 : +1);
+ else if (entry1->sort_priority == entry2->sort_priority)
+ {
+ char *name1 = getStringToLower(entry1->name_sorting);
+ char *name2 = getStringToLower(entry2->name_sorting);
+
+ compare_result = strcmp(name1, name2);
+
+ free(name1);
+ free(name2);
+ }
+ else if (class_sorting1 == class_sorting2)
+ compare_result = entry1->sort_priority - entry2->sort_priority;
+ else
+ compare_result = class_sorting1 - class_sorting2;
+
+ return compare_result;
+}
+
+static void createParentTreeInfoNode(TreeInfo *node_parent)
+{
+ TreeInfo *ti_new;
+
+ if (node_parent == NULL)
+ return;
+
+ ti_new = newTreeInfo();
+ setTreeInfoToDefaults(ti_new, node_parent->type);
+
+ ti_new->node_parent = node_parent;
+ ti_new->parent_link = TRUE;
+
+#if 1
+ setString(&ti_new->identifier, node_parent->identifier);
+ setString(&ti_new->name, ".. (parent directory)");
+ setString(&ti_new->name_sorting, ti_new->name);
+
+ setString(&ti_new->subdir, "..");
+ setString(&ti_new->fullpath, node_parent->fullpath);
+
+ ti_new->sort_priority = node_parent->sort_priority;
+ ti_new->latest_engine = node_parent->latest_engine;
+
+ setString(&ti_new->class_desc, getLevelClassDescription(ti_new));
+#else
+ ti_new->identifier = getStringCopy(node_parent->identifier);
+ ti_new->name = ".. (parent directory)";
+ ti_new->name_sorting = getStringCopy(ti_new->name);
+
+ ti_new->subdir = "..";
+ ti_new->fullpath = getStringCopy(node_parent->fullpath);
+
+ ti_new->sort_priority = node_parent->sort_priority;
+ ti_new->latest_engine = node_parent->latest_engine;
+
+ ti_new->class_desc = getLevelClassDescription(ti_new);
+#endif
+
+ pushTreeInfo(&node_parent->node_group, ti_new);
+}
+
+/* forward declaration for recursive call by "LoadLevelInfoFromLevelDir()" */
+static void LoadLevelInfoFromLevelDir(TreeInfo **, TreeInfo *, char *);
+
+static boolean LoadLevelInfoFromLevelConf(TreeInfo **node_first,
+ TreeInfo *node_parent,
+ char *level_directory,
+ char *directory_name)
+{
+ char *directory_path = getPath2(level_directory, directory_name);
+ char *filename = getPath2(directory_path, LEVELINFO_FILENAME);
+ SetupFileHash *setup_file_hash = loadSetupFileHash(filename);
+ LevelDirTree *leveldir_new = NULL;
+ int i;
+
+ if (setup_file_hash == NULL)
+ {
+ Error(ERR_WARN, "ignoring level directory '%s'", directory_path);
+
+ free(directory_path);
+ free(filename);
+
+ return FALSE;
+ }
+
+ leveldir_new = newTreeInfo();
+
+ if (node_parent)
+ setTreeInfoToDefaultsFromParent(leveldir_new, node_parent);
+ else
+ setTreeInfoToDefaults(leveldir_new, TREE_TYPE_LEVEL_DIR);
+
+ leveldir_new->subdir = getStringCopy(directory_name);
+
+ checkSetupFileHashIdentifier(setup_file_hash, getCookie("LEVELINFO"));
+
+ /* set all structure fields according to the token/value pairs */
+ ldi = *leveldir_new;
+ for (i = 0; i < NUM_LEVELINFO_TOKENS; i++)
+ setSetupInfo(levelinfo_tokens, i,
+ getHashEntry(setup_file_hash, levelinfo_tokens[i].text));
+ *leveldir_new = ldi;
+
+#if 1
+ if (strcmp(leveldir_new->name, ANONYMOUS_NAME) == 0)
+ setString(&leveldir_new->name, leveldir_new->subdir);
+#else
+ if (strcmp(leveldir_new->name, ANONYMOUS_NAME) == 0)
+ {
+ free(leveldir_new->name);
+ leveldir_new->name = getStringCopy(leveldir_new->subdir);
+ }
+#endif
+
+ DrawInitText(leveldir_new->name, 150, FC_YELLOW);
+
+ if (leveldir_new->identifier == NULL)
+ leveldir_new->identifier = getStringCopy(leveldir_new->subdir);
+
+ if (leveldir_new->name_sorting == NULL)
+ leveldir_new->name_sorting = getStringCopy(leveldir_new->name);
+
+ if (node_parent == NULL) /* top level group */
+ {
+ leveldir_new->basepath = getStringCopy(level_directory);
+ leveldir_new->fullpath = getStringCopy(leveldir_new->subdir);
+ }
+ else /* sub level group */
+ {
+ leveldir_new->basepath = getStringCopy(node_parent->basepath);
+ leveldir_new->fullpath = getPath2(node_parent->fullpath, directory_name);
+ }
+
+ if (leveldir_new->levels < 1)
+ leveldir_new->levels = 1;
+
+ leveldir_new->last_level =
+ leveldir_new->first_level + leveldir_new->levels - 1;
+
+#if 1
+ leveldir_new->in_user_dir =
+ (strcmp(leveldir_new->basepath, options.level_directory) != 0);
+#else
+ leveldir_new->in_user_dir =
+ (leveldir_new->basepath == options.level_directory ? FALSE : TRUE);
+#endif
+
+ leveldir_new->user_defined =
+ (leveldir_new->in_user_dir && IS_LEVELCLASS_PRIVATE(leveldir_new));
+
+ leveldir_new->color = LEVELCOLOR(leveldir_new);
+#if 1
+ setString(&leveldir_new->class_desc, getLevelClassDescription(leveldir_new));
+#else
+ leveldir_new->class_desc = getLevelClassDescription(leveldir_new);
+#endif
+
+ leveldir_new->handicap_level = /* set handicap to default value */
+ (leveldir_new->user_defined || !leveldir_new->handicap ?
+ leveldir_new->last_level : leveldir_new->first_level);
+
+ pushTreeInfo(node_first, leveldir_new);
+
+ freeSetupFileHash(setup_file_hash);
+
+ if (leveldir_new->level_group)
+ {
+ /* create node to link back to current level directory */
+ createParentTreeInfoNode(leveldir_new);
+
+ /* step into sub-directory and look for more level series */
+ LoadLevelInfoFromLevelDir(&leveldir_new->node_group,
+ leveldir_new, directory_path);
+ }
+
+ free(directory_path);
+ free(filename);
+
+ return TRUE;
+}
+
+static void LoadLevelInfoFromLevelDir(TreeInfo **node_first,
+ TreeInfo *node_parent,
+ char *level_directory)
+{
+ DIR *dir;
+ struct dirent *dir_entry;
+ boolean valid_entry_found = FALSE;
+
+ if ((dir = opendir(level_directory)) == NULL)
+ {
+ Error(ERR_WARN, "cannot read level directory '%s'", level_directory);
+ return;
+ }
+
+ while ((dir_entry = readdir(dir)) != NULL) /* loop until last dir entry */
+ {
+ struct stat file_status;
+ char *directory_name = dir_entry->d_name;
+ char *directory_path = getPath2(level_directory, directory_name);
+
+ /* skip entries for current and parent directory */
+ if (strcmp(directory_name, ".") == 0 ||
+ strcmp(directory_name, "..") == 0)
+ {
+ free(directory_path);
+ continue;
+ }
+
+ /* find out if directory entry is itself a directory */
+ if (stat(directory_path, &file_status) != 0 || /* cannot stat file */
+ (file_status.st_mode & S_IFMT) != S_IFDIR) /* not a directory */
+ {
+ free(directory_path);
+ continue;
+ }
+
+ free(directory_path);
+
+ if (strcmp(directory_name, GRAPHICS_DIRECTORY) == 0 ||
+ strcmp(directory_name, SOUNDS_DIRECTORY) == 0 ||
+ strcmp(directory_name, MUSIC_DIRECTORY) == 0)
+ continue;
+
+ valid_entry_found |= LoadLevelInfoFromLevelConf(node_first, node_parent,
+ level_directory,
+ directory_name);
+ }
+
+ closedir(dir);
+
+ if (!valid_entry_found)
+ {
+ /* check if this directory directly contains a file "levelinfo.conf" */
+ valid_entry_found |= LoadLevelInfoFromLevelConf(node_first, node_parent,
+ level_directory, ".");
+ }
+
+ if (!valid_entry_found)
+ Error(ERR_WARN, "cannot find any valid level series in directory '%s'",
+ level_directory);
+}
+
+void LoadLevelInfo()