-}
-
-struct LevelDirInfo *getLevelDirInfoFromLevelDirName(char *level_dir_name)
-{
- struct LevelDirInfo *leveldir_node = leveldir_first;
-
- if (level_dir_name == NULL)
- return NULL;
-
- while (leveldir_node)
- {
- if (strcmp(level_dir_name, leveldir_node->name) == 0)
- return leveldir_node; /* return success value */
-
- leveldir_node = leveldir_node->next;
- }
-
- return NULL;
-}
-
-static int compareLevelDirInfoEntries(const void *object1, const void *object2)
-{
- const struct LevelDirInfo *entry1 = *((struct LevelDirInfo **)object1);
- const struct LevelDirInfo *entry2 = *((struct LevelDirInfo **)object2);
- int compare_result;
-
- 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 (LEVELSORTING(entry1) == LEVELSORTING(entry2))
- compare_result = entry1->sort_priority - entry2->sort_priority;
- else
- compare_result = LEVELSORTING(entry1) - LEVELSORTING(entry2);
-
- return compare_result;
-}
-
-static void LoadLevelInfoFromLevelDir(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 = NULL;
- char *filename = NULL;
- struct SetupFileList *setup_file_list = NULL;
-
- /* skip entries for current and parent directory */
- if (strcmp(dir_entry->d_name, ".") == 0 ||
- strcmp(dir_entry->d_name, "..") == 0)
- continue;
-
- /* find out if directory entry is itself a directory */
- directory = getPath2(level_directory, dir_entry->d_name);
- if (stat(directory, &file_status) != 0 || /* cannot stat file */
- (file_status.st_mode & S_IFMT) != S_IFDIR) /* not a directory */
- {
- free(directory);
- continue;
- }
-
- filename = getPath2(directory, LEVELINFO_FILENAME);
- setup_file_list = loadSetupFileList(filename);
-
- if (setup_file_list)
- {
- struct LevelDirInfo *leveldir_new = newLevelDirInfo();
- int i;
-
- checkSetupFileListIdentifier(setup_file_list, LEVELINFO_COOKIE);
- setLevelDirInfoToDefaults(leveldir_new);
-
- ldi = *leveldir_new;
- for (i=FIRST_LEVELINFO_TOKEN; i<=LAST_LEVELINFO_TOKEN; i++)
- setSetupInfo(i, getTokenValue(setup_file_list, token_info[i].text));
- *leveldir_new = ldi;
-
- DrawInitText(leveldir_new->name, 150, FC_YELLOW);
-
- if (leveldir_new->name_short == NULL)
- leveldir_new->name_short = getStringCopy(leveldir_new->name);
-
- if (leveldir_new->name_sorting == NULL)
- leveldir_new->name_sorting = getStringCopy(leveldir_new->name);
-
- leveldir_new->filename = getStringCopy(dir_entry->d_name);
-
- if (leveldir_new->levels < 1)
- leveldir_new->levels = 1;
-
- leveldir_new->last_level =
- leveldir_new->first_level + leveldir_new->levels - 1;
-
- leveldir_new->user_defined =
- (level_directory == options.level_directory ? FALSE : TRUE);
- leveldir_new->color = LEVELCOLOR(leveldir_new);
- leveldir_new->class_desc = getLevelClassDescription(leveldir_new);
-
- leveldir_new->handicap_level = /* set handicap to default value */
- (leveldir_new->user_defined ?
- leveldir_new->last_level :
- leveldir_new->first_level);
-
- pushLevelDirInfo(leveldir_new); /* add new LevelDirInfo to list */
-
- freeSetupFileList(setup_file_list);
- valid_entry_found = TRUE;
- }
- else
- Error(ERR_WARN, "ignoring level directory '%s'", directory);
-
- free(directory);
- free(filename);
- }
-
- closedir(dir);
-
- if (!valid_entry_found)
- Error(ERR_WARN, "cannot find any valid level series in directory '%s'",
- level_directory);
-}
-
-void LoadLevelInfo()
-{
- InitUserLevelDirectory(getLoginName());
-
- DrawInitText("Loading level series:", 120, FC_GREEN);
-
- LoadLevelInfoFromLevelDir(options.level_directory);
- LoadLevelInfoFromLevelDir(getUserLevelDir(""));
-
- num_leveldirs = numLevelDirInfo(leveldir_first);
- leveldir_current = leveldir_first;
-
- if (num_leveldirs == 0)
- Error(ERR_EXIT, "cannot find any valid level series in any directory");
-
-#if 0
- if (num_leveldirs > 1)
- qsort(leveldir, num_leveldirs, sizeof(struct LevelDirInfo),
- compareLevelDirInfoEntries);
-#else
- if (num_leveldirs > 1)
- sortLevelDirInfo(&leveldir_first, compareLevelDirInfoEntries);
-#endif
-}
-
-static void SaveUserLevelInfo()
-{
- char *filename;
- FILE *file;
- int i;
-
- filename = getPath2(getUserLevelDir(getLoginName()), LEVELINFO_FILENAME);
-
- if (!(file = fopen(filename, "w")))
- {
- Error(ERR_WARN, "cannot write level info file '%s'", filename);
- free(filename);
- return;
- }
-
- /* always start with reliable default values */
- setLevelDirInfoToDefaults(&ldi);
-
- ldi.name = getLoginName();
- ldi.author = getRealName();
- ldi.levels = 100;
- ldi.first_level = 1;
- ldi.sort_priority = LEVELCLASS_USER_START;
- ldi.readonly = FALSE;
-
- fprintf(file, "%s\n\n",
- getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER, LEVELINFO_COOKIE));
-
- for (i=FIRST_LEVELINFO_TOKEN; i<=LAST_LEVELINFO_TOKEN; i++)
- if (i != LEVELINFO_TOKEN_NAME_SHORT &&
- i != LEVELINFO_TOKEN_NAME_SORTING &&
- i != LEVELINFO_TOKEN_IMPORTED_FROM)
- fprintf(file, "%s\n", getSetupLine("", i));
-
- fclose(file);
- free(filename);