+#endif
+
+
+
+static char *getSetupLine(char *prefix, int token_nr)
+{
+ int i;
+ static char entry[MAX_LINE_LEN];
+ int token_type = token_info[token_nr].type;
+ void *token_value = token_info[token_nr].value;
+ char *token_text = token_info[token_nr].text;
+
+ /* start with the prefix, token and some spaces to format output line */
+ sprintf(entry, "%s%s:", prefix, token_text);
+ for (i=strlen(entry); i<TOKEN_VALUE_POSITION; i++)
+ strcat(entry, " ");
+
+ /* continue with the token's value (which can have different types) */
+ switch (token_type)
+ {
+ case TYPE_BOOLEAN:
+ strcat(entry, (*(boolean *)token_value ? "true" : "false"));
+ break;
+
+ case TYPE_SWITCH:
+ strcat(entry, (*(boolean *)token_value ? "on" : "off"));
+ break;
+
+ case TYPE_KEYSYM:
+ {
+ KeySym keysym = *(KeySym *)token_value;
+ char *keyname = getKeyNameFromKeySym(keysym);
+
+ strcat(entry, getX11KeyNameFromKeySym(keysym));
+ for (i=strlen(entry); i<50; i++)
+ strcat(entry, " ");
+
+ /* add comment, if useful */
+ if (strcmp(keyname, "(undefined)") != 0 &&
+ strcmp(keyname, "(unknown)") != 0)
+ {
+ strcat(entry, "# ");
+ strcat(entry, keyname);
+ }
+ }
+ break;
+
+ case TYPE_INTEGER:
+ {
+ char buffer[MAX_LINE_LEN];
+
+ sprintf(buffer, "%d", *(int *)token_value);
+ strcat(entry, buffer);
+ }
+ break;
+
+ case TYPE_STRING:
+ strcat(entry, *(char **)token_value);
+ break;
+
+ default:
+ break;
+ }
+
+ return entry;
+}
+
+void SaveSetup()
+{
+ int i, pnr;
+ char filename[MAX_FILENAME_LEN];
+ FILE *file;
+
+ sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME);
+
+ if (!(file = fopen(filename, "w")))
+ {
+ Error(ERR_WARN, "cannot write setup file '%s'", filename);
+ return;
+ }
+
+ fprintf(file, "%s: %s\n",
+ TOKEN_STR_FILE_IDENTIFIER, SETUP_COOKIE);
+
+ fprintf(file, "\n");
+
+ for (i=FIRST_GLOBAL_SETUP_TOKEN; i<=LAST_GLOBAL_SETUP_TOKEN; i++)
+ fprintf(file, "%s\n", getSetupLine("", i));
+
+ for (pnr=0; pnr<MAX_PLAYERS; pnr++)
+ {
+ char prefix[30];
+
+ sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, i + 1);
+ fprintf(file, "\n");
+
+ for (i=FIRST_PLAYER_SETUP_TOKEN; i<=LAST_PLAYER_SETUP_TOKEN; i++)
+ {
+ sii = setup.input[pnr];
+ fprintf(file, "%s\n", getSetupLine(prefix, i));
+ }
+ }
+
+ fclose(file);
+
+ chmod(filename, SETUP_PERMS);
+}
+
+void LoadLevelSetup()
+{
+ char filename[MAX_FILENAME_LEN];
+
+ /* always start with reliable default setup values */
+
+ leveldir_nr = 0;
+ level_nr = 0;
+
+ sprintf(filename, "%s/%s", SETUP_PATH, LEVELSETUP_FILENAME);
+
+ if (level_setup_list)
+ freeSetupFileList(level_setup_list);
+
+ level_setup_list = loadSetupFileList(filename);
+
+ if (level_setup_list)
+ {
+ char *last_level_series =
+ getSetupFileListEntry(level_setup_list, TOKEN_STR_LAST_LEVEL_SERIES);
+
+ leveldir_nr = getLevelSeriesNrFromLevelSeriesName(last_level_series);
+ level_nr = getLastPlayedLevelOfLevelSeries(last_level_series);
+
+#if 0
+ printSetupFileList(level_setup_list);
+#endif
+
+ checkSetupFileListIdentifier(level_setup_list, LEVELSETUP_COOKIE);
+ }
+ else
+ Error(ERR_WARN, "using default setup values");
+}
+