rnd-19981026-1
[rocksndiamonds.git] / src / files.c
index d34fe40da717af4a1ab00e4a1b927d58f4a255c7..17bc45e5986691d8b773e6ad12dad24c7a29df79 100644 (file)
 *  files.h                                                 *
 ***********************************************************/
 
+#include <ctype.h>
+
 #include "files.h"
 #include "tools.h"
 #include "misc.h"
 #include "tape.h"
 #include "joystick.h"
 
-BOOL CreateNewScoreFile()
+boolean CreateNewScoreFile()
 {
   int i,j,k;
   char filename[MAX_FILENAME_LEN];
@@ -51,7 +53,11 @@ BOOL CreateNewScoreFile()
   return(TRUE);
 }
 
-BOOL CreateNewNamesFile(int mode)
+
+
+#if 0
+
+boolean CreateNewNamesFile(int mode)
 {
   char filename[MAX_FILENAME_LEN];
   FILE *file;
@@ -72,7 +78,10 @@ BOOL CreateNewNamesFile(int mode)
   return(TRUE);
 }
 
-BOOL LoadLevelInfo()
+#endif
+
+
+boolean LoadLevelInfo()
 {
   int i;
   char filename[MAX_FILENAME_LEN];
@@ -83,14 +92,14 @@ BOOL LoadLevelInfo()
 
   if (!(file=fopen(filename,"r")))
   {
-    Error(ERR_RETURN, "cannot load level info '%s'", filename);
+    Error(ERR_WARN, "cannot read level info '%s'", filename);
     return(FALSE);
   }
 
   fscanf(file,"%s\n",cookie);
   if (strcmp(cookie,LEVELDIR_COOKIE))  /* ungültiges Format? */
   {
-    Error(ERR_RETURN, "wrong format of level info file");
+    Error(ERR_WARN, "wrong format of level info file");
     fclose(file);
     return(FALSE);
   }
@@ -111,7 +120,7 @@ BOOL LoadLevelInfo()
 
   if (!num_leveldirs)
   {
-    Error(ERR_RETURN, "empty level info '%s'", filename);
+    Error(ERR_WARN, "empty level info '%s'", filename);
     return(FALSE);
   }
 
@@ -129,7 +138,7 @@ void LoadLevel(int level_nr)
          level_directory,leveldir[leveldir_nr].filename,level_nr);
 
   if (!(file = fopen(filename,"r")))
-    Error(ERR_RETURN, "cannot load level '%s' - creating new level", filename);
+    Error(ERR_WARN, "cannot read level '%s' - creating new level", filename);
   else
   {
     fgets(cookie,LEVEL_COOKIE_LEN,file);
@@ -137,7 +146,7 @@ void LoadLevel(int level_nr)
 
     if (strcmp(cookie,LEVEL_COOKIE))   /* ungültiges Format? */
     {
-      Error(ERR_RETURN, "wrong format of level file '%s'", filename);
+      Error(ERR_WARN, "wrong format of level file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -214,7 +223,7 @@ void LoadLevelTape(int level_nr)
   char filename[MAX_FILENAME_LEN];
   char cookie[MAX_FILENAME_LEN];
   FILE *file;
-  BOOL levelrec_10 = FALSE;
+  boolean levelrec_10 = FALSE;
 
 #ifndef MSDOS
   sprintf(filename,"%s/%s/%d.tape",
@@ -232,7 +241,7 @@ void LoadLevelTape(int level_nr)
       levelrec_10 = TRUE;
     else if (strcmp(cookie,LEVELREC_COOKIE))   /* unknown tape format */
     {
-      Error(ERR_RETURN, "wrong format of level recording file '%s'", filename);
+      Error(ERR_WARN, "wrong format of level recording file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -282,7 +291,7 @@ void LoadLevelTape(int level_nr)
   fclose(file);
 
   if (i != tape.length)
-    Error(ERR_RETURN, "level recording file '%s' corrupted", filename);
+    Error(ERR_WARN, "level recording file '%s' corrupted", filename);
 
   tape.length_seconds = GetTapeLength();
 }
@@ -300,9 +309,9 @@ void LoadScore(int level_nr)
   if (!(file = fopen(filename,"r")))
   {
     if (!CreateNewScoreFile())
-      Error(ERR_RETURN, "cannot create score file '%s'", filename);
+      Error(ERR_WARN, "cannot create score file '%s'", filename);
     else if (!(file = fopen(filename,"r"))) 
-      Error(ERR_RETURN, "cannot load score for level %d", level_nr);
+      Error(ERR_WARN, "cannot read score for level %d", level_nr);
   }
 
   if (file)
@@ -310,7 +319,7 @@ void LoadScore(int level_nr)
     fgets(cookie,SCORE_COOKIE_LEN,file);
     if (strcmp(cookie,SCORE_COOKIE))   /* ungültiges Format? */
     {
-      Error(ERR_RETURN, "wrong format of score file '%s'", filename);
+      Error(ERR_WARN, "wrong format of score file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -339,6 +348,10 @@ void LoadScore(int level_nr)
   }
 }
 
+
+
+#if 0
+
 void LoadPlayerInfo(int mode)
 {
   int i;
@@ -349,6 +362,15 @@ void LoadPlayerInfo(int mode)
   struct PlayerInfo default_player, new_player;
   int version_10_file = FALSE;
 
+
+
+  if (mode == PLAYER_SETUP)
+    LoadSetup();
+  else if (mode == PLAYER_LEVEL)
+    LoadLevelSetup();
+
+
+
   if (mode==PLAYER_LEVEL)
     sprintf(filename,"%s/%s/%s",
            level_directory,leveldir[leveldir_nr].filename,NAMES_FILENAME);
@@ -369,9 +391,9 @@ void LoadPlayerInfo(int mode)
   if (!(file = fopen(filename,"r")))
   {
     if (!CreateNewNamesFile(mode))
-      Error(ERR_RETURN, "cannot create names file '%s'", filename);
+      Error(ERR_WARN, "cannot create names file '%s'", filename);
     else if (!(file = fopen(filename,"r"))) 
-      Error(ERR_RETURN, "cannot load player information file '%s'", filename);
+      Error(ERR_WARN, "cannot read player information file '%s'", filename);
   }
 
   if (file)
@@ -381,7 +403,7 @@ void LoadPlayerInfo(int mode)
       version_10_file = TRUE;
     else if (strcmp(cookie,NAMES_COOKIE))      /* ungültiges Format? */
     {
-      Error(ERR_RETURN, "wrong format of names file '%s'", filename);
+      Error(ERR_WARN, "wrong format of names file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -418,7 +440,7 @@ void LoadPlayerInfo(int mode)
 
       fclose(file);
       if (!(file = fopen(filename,"a")))
-       Error(ERR_RETURN, "cannot append new player to names file '%s'",
+       Error(ERR_WARN, "cannot append new player to names file '%s'",
              filename);
       else
       {
@@ -459,9 +481,15 @@ void LoadPlayerInfo(int mode)
   }
 
   level_nr = local_player->level_nr;
-  fclose(file);
+
+  if (file)
+    fclose(file);
 }
 
+#endif
+
+
+
 void SaveLevel(int level_nr)
 {
   int i,x,y;
@@ -473,7 +501,7 @@ void SaveLevel(int level_nr)
 
   if (!(file=fopen(filename,"w")))
   {
-    Error(ERR_RETURN, "cannot save level file '%s'", filename);
+    Error(ERR_WARN, "cannot save level file '%s'", filename);
     return;
   }
 
@@ -517,7 +545,7 @@ void SaveLevelTape(int level_nr)
   int i;
   char filename[MAX_FILENAME_LEN];
   FILE *file;
-  BOOL new_tape = TRUE;
+  boolean new_tape = TRUE;
 
 #ifndef MSDOS
   sprintf(filename,"%s/%s/%d.tape",
@@ -539,7 +567,7 @@ void SaveLevelTape(int level_nr)
 
   if (!(file=fopen(filename,"w")))
   {
-    Error(ERR_RETURN, "cannot save level recording file '%s'", filename);
+    Error(ERR_WARN, "cannot save level recording file '%s'", filename);
     return;
   }
 
@@ -592,7 +620,7 @@ void SaveScore(int level_nr)
 
   if (!(file=fopen(filename,"r+")))
   {
-    Error(ERR_RETURN, "cannot save score for level %d", level_nr);
+    Error(ERR_WARN, "cannot save score for level %d", level_nr);
     return;
   }
 
@@ -609,6 +637,10 @@ void SaveScore(int level_nr)
   fclose(file);
 }
 
+
+
+#if 0
+
 void SavePlayerInfo(int mode)
 {
   int i;
@@ -618,7 +650,16 @@ void SavePlayerInfo(int mode)
   struct PlayerInfo default_player;
   int version_10_file = FALSE;
 
-  if (mode==PLAYER_LEVEL)
+
+
+  if (mode == PLAYER_SETUP)
+    SaveSetup();
+  else if (mode == PLAYER_LEVEL)
+    SaveLevelSetup();
+
+
+
+  if (mode == PLAYER_LEVEL)
     sprintf(filename,"%s/%s/%s",
            level_directory,leveldir[leveldir_nr].filename,NAMES_FILENAME);
   else
@@ -626,7 +667,7 @@ void SavePlayerInfo(int mode)
 
   if (!(file = fopen(filename,"r+")))
   {
-    Error(ERR_RETURN, "cannot save player information to file '%s'", filename);
+    Error(ERR_WARN, "cannot save player information to file '%s'", filename);
     return;
   }
 
@@ -635,7 +676,7 @@ void SavePlayerInfo(int mode)
     version_10_file = TRUE;
   else if (strcmp(cookie,NAMES_COOKIE))        /* ungültiges Format? */
   {
-    Error(ERR_RETURN, "wrong format of names file '%s'", filename);
+    Error(ERR_WARN, "wrong format of names file '%s'", filename);
     fclose(file);
     return;
   }
@@ -689,6 +730,10 @@ void SavePlayerInfo(int mode)
   fclose(file);
 }
 
+#endif
+
+
+
 void LoadJoystickData()
 {
   int i;
@@ -705,7 +750,7 @@ void LoadJoystickData()
   fscanf(file,"%s",cookie);
   if (strcmp(cookie,JOYSTICK_COOKIE))  /* ungültiges Format? */
   {
-    Error(ERR_RETURN, "wrong format of joystick file '%s'", JOYDAT_FILE);
+    Error(ERR_WARN, "wrong format of joystick file '%s'", JOYDAT_FILE);
     fclose(file);
     return;
   }
@@ -739,7 +784,7 @@ void SaveJoystickData()
 
   if (!(file=fopen(JOYDAT_FILE,"w")))
   {
-    Error(ERR_RETURN, "cannot save joystick calibration data to file '%s'",
+    Error(ERR_WARN, "cannot save joystick calibration data to file '%s'",
          JOYDAT_FILE);
     return;
   }
@@ -761,3 +806,823 @@ void SaveJoystickData()
 #endif
 
 }
+
+/* ------------------------------------------------------------------------- */
+/* new setup functions                                                       */
+/* ------------------------------------------------------------------------- */
+
+#define TOKEN_STR_FILE_IDENTIFIER      "file_identifier"
+#define TOKEN_STR_LAST_LEVEL_SERIES    "last_level_series"
+#define TOKEN_STR_ALIAS_NAME           "alias_name"
+
+#define TOKEN_STR_PLAYER_PREFIX                "player_"
+#define TOKEN_VALUE_POSITION           30
+#define TOKEN_INVALID                  -1
+#define TOKEN_IGNORE                   -99
+
+#define SETUP_TOKEN_ALIAS_NAME         100
+
+#define SETUP_TOKEN_SOUND              0
+#define SETUP_TOKEN_SOUND_LOOPS                1
+#define SETUP_TOKEN_SOUND_MUSIC                2
+#define SETUP_TOKEN_SOUND_SIMPLE       3
+#define SETUP_TOKEN_TOONS              4
+#define SETUP_TOKEN_DIRECT_DRAW                5
+#define SETUP_TOKEN_SCROLL_DELAY       6
+#define SETUP_TOKEN_SOFT_SCROLLING     7
+#define SETUP_TOKEN_FADING             8
+#define SETUP_TOKEN_AUTORECORD         9
+#define SETUP_TOKEN_QUICK_DOORS                10
+#define SETUP_TOKEN_USE_JOYSTICK       11
+#define SETUP_TOKEN_JOYSTICK_NR                12
+#define SETUP_TOKEN_JOY_SNAP           13
+#define SETUP_TOKEN_JOY_BOMB           14
+#define SETUP_TOKEN_KEY_LEFT           15
+#define SETUP_TOKEN_KEY_RIGHT          16
+#define SETUP_TOKEN_KEY_UP             17
+#define SETUP_TOKEN_KEY_DOWN           18
+#define SETUP_TOKEN_KEY_SNAP           19
+#define SETUP_TOKEN_KEY_BOMB           20
+
+#define NUM_SETUP_TOKENS               21
+
+static struct
+{
+  char *token, *value_true, *value_false;
+} setup_info[] =
+{
+  { "sound",                   "on", "off" },
+  { "repeating_sound_loops",   "on", "off" },
+  { "background_music",                "on", "off" },
+  { "simple_sound_effects",    "on", "off" },
+  { "toons",                   "on", "off" },
+  { "double_buffering",        "off", "on" },
+  { "scroll_delay",            "on", "off" },
+  { "soft_scrolling",          "on", "off" },
+  { "screen_fading",           "on", "off" },
+  { "automatic_tape_recording",        "on", "off" },
+  { "quick_doors",             "on", "off" },
+
+  /* for each player: */
+  { ".use_joystick",           "true", "false" },
+  { ".joystick_device",                "second", "first" },
+  { ".joy.snap_field",         "", "" },
+  { ".joy.place_bomb",         "", "" },
+  { ".key.move_left",          "", "" },
+  { ".key.move_right",         "", "" },
+  { ".key.move_up",            "", "" },
+  { ".key.move_down",          "", "" },
+  { ".key.snap_field",         "", "" },
+  { ".key.place_bomb",         "", "" }
+};
+
+static char *string_tolower(char *s)
+{
+  static char s_lower[100];
+  int i;
+
+  if (strlen(s) >= 100)
+    return s;
+
+  strcpy(s_lower, s);
+
+  for (i=0; i<strlen(s_lower); i++)
+    s_lower[i] = tolower(s_lower[i]);
+
+  return s_lower;
+}
+
+static int get_string_integer_value(char *s)
+{
+  static char *number_text[][3] =
+  {
+    { "0", "zero", "null", },
+    { "1", "one", "first" },
+    { "2", "two", "second" },
+    { "3", "three", "third" },
+    { "4", "four", "fourth" },
+    { "5", "five", "fifth" },
+    { "6", "six", "sixth" },
+    { "7", "seven", "seventh" },
+    { "8", "eight", "eighth" },
+    { "9", "nine", "ninth" },
+    { "10", "ten", "tenth" },
+    { "11", "eleven", "eleventh" },
+    { "12", "twelve", "twelfth" },
+  };
+
+  int i, j;
+
+  for (i=0; i<13; i++)
+    for (j=0; j<3; j++)
+      if (strcmp(string_tolower(s), number_text[i][j]) == 0)
+       return i;
+
+  return -1;
+}
+
+static boolean get_string_boolean_value(char *s)
+{
+  if (strcmp(string_tolower(s), "true") == 0 ||
+      strcmp(string_tolower(s), "yes") == 0 ||
+      strcmp(string_tolower(s), "on") == 0 ||
+      get_string_integer_value(s) == 1)
+    return TRUE;
+  else
+    return FALSE;
+}
+
+static char *getSetupToken(int token_nr)
+{
+  return setup_info[token_nr].token;
+}
+
+static char *getSetupValue(int token_nr, boolean token_value)
+{
+  if (token_value == TRUE)
+    return setup_info[token_nr].value_true;
+  else
+    return setup_info[token_nr].value_false;
+}
+
+static char *getFormattedSetupEntry(char *token, char *value)
+{
+  int i;
+  static char entry[MAX_LINE_LEN];
+
+  sprintf(entry, "%s:", token);
+  for (i=strlen(entry); i<TOKEN_VALUE_POSITION; i++)
+    entry[i] = ' ';
+  entry[i] = '\0';
+
+  strcat(entry, value);
+
+  return entry;
+}
+
+static char *getSetupEntry(char *prefix, int token_nr, int token_value)
+{
+  int i;
+  static char entry[MAX_LINE_LEN];
+
+  sprintf(entry, "%s%s:", prefix, getSetupToken(token_nr));
+  for (i=strlen(entry); i<TOKEN_VALUE_POSITION; i++)
+    entry[i] = ' ';
+  entry[i] = '\0';
+
+  strcat(entry, getSetupValue(token_nr, token_value));
+
+  return entry;
+}
+
+static char *getSetupEntryWithComment(char *prefix,int token_nr, KeySym keysym)
+{
+  int i;
+  static char entry[MAX_LINE_LEN];
+  char *keyname = getKeyNameFromKeySym(keysym);
+
+  sprintf(entry, "%s%s:", prefix, getSetupToken(token_nr));
+  for (i=strlen(entry); i<TOKEN_VALUE_POSITION; i++)
+    entry[i] = ' ';
+  entry[i] = '\0';
+
+  strcat(entry, getX11KeyNameFromKeySym(keysym));
+  for (i=strlen(entry); i<50; i++)
+    entry[i] = ' ';
+  entry[i] = '\0';
+
+  /* add comment, if useful */
+  if (strcmp(keyname, "(undefined)") != 0 &&
+      strcmp(keyname, "(unknown)") != 0)
+  {
+    strcat(entry, "# ");
+    strcat(entry, keyname);
+  }
+
+  return entry;
+}
+
+static void freeSetupFileList(struct SetupFileList *setup_file_list)
+{
+  if (!setup_file_list)
+    return;
+
+  if (setup_file_list->token)
+    free(setup_file_list->token);
+  if (setup_file_list->value)
+    free(setup_file_list->value);
+  if (setup_file_list->next)
+    freeSetupFileList(setup_file_list->next);
+  free(setup_file_list);
+}
+
+static struct SetupFileList *newSetupFileList(char *token, char *value)
+{
+  struct SetupFileList *new = checked_malloc(sizeof(struct SetupFileList));
+
+  new->token = checked_malloc(strlen(token) + 1);
+  strcpy(new->token, token);
+
+  new->value = checked_malloc(strlen(value) + 1);
+  strcpy(new->value, value);
+
+  new->next = NULL;
+
+  return new;
+}
+
+static char *getSetupFileListEntry(struct SetupFileList *setup_file_list,
+                                  char *token)
+{
+  if (!setup_file_list)
+    return NULL;
+
+  if (strcmp(setup_file_list->token, token) == 0)
+    return setup_file_list->value;
+  else
+    return getSetupFileListEntry(setup_file_list->next, token);
+}
+
+boolean setSetupFileListEntry(struct SetupFileList *setup_file_list,
+                             char *token, char *value)
+{
+  if (!setup_file_list)
+    return FALSE;
+
+  if (strcmp(setup_file_list->token, token) == 0)
+  {
+    free(setup_file_list->value);
+    setup_file_list->value = checked_malloc(strlen(value) + 1);
+    strcpy(setup_file_list->value, value);
+
+    return TRUE;
+  }
+  else
+    return setSetupFileListEntry(setup_file_list->next, token, value);
+}
+
+void updateSetupFileListEntry(struct SetupFileList *setup_file_list,
+                             char *token, char *value)
+{
+  if (!setup_file_list)
+    return;
+
+  if (getSetupFileListEntry(setup_file_list, token) != NULL)
+    setSetupFileListEntry(setup_file_list, token, value);
+  else
+  {
+    struct SetupFileList *list_entry = setup_file_list;
+
+    while (list_entry->next)
+      list_entry = list_entry->next;
+
+    list_entry->next = newSetupFileList(token, value);
+  }
+}
+
+#ifdef DEBUG
+static void printSetupFileList(struct SetupFileList *setup_file_list)
+{
+  if (!setup_file_list)
+    return;
+
+  printf("token: '%s'\n", setup_file_list->token);
+  printf("value: '%s'\n", setup_file_list->value);
+
+  printSetupFileList(setup_file_list->next);
+}
+#endif
+
+static struct SetupFileList *loadSetupFileList(char *filename)
+{
+  int line_len;
+  char line[MAX_LINE_LEN];
+  char *token, *value, *line_ptr;
+  struct SetupFileList *setup_file_list = newSetupFileList("", "");
+  struct SetupFileList *first_valid_list_entry;
+
+
+
+  /*
+  struct SetupFileList **next_entry = &setup_file_list;
+  */
+
+
+  FILE *file;
+
+  if (!(file = fopen(filename, "r")))
+  {
+    Error(ERR_WARN, "cannot open setup file '%s'", filename);
+    return NULL;
+  }
+
+  while(!feof(file))
+  {
+    /* read next line of input file */
+    if (!fgets(line, MAX_LINE_LEN, file))
+      break;
+
+    /* cut trailing comment or whitespace from input line */
+    for (line_ptr = line; *line_ptr; line_ptr++)
+    {
+      if (*line_ptr == '#' || *line_ptr == '\n')
+      {
+       *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;
+
+    line_len = strlen(line);
+
+    /* cut leading whitespaces from token */
+    for (token = line; *token; token++)
+      if (*token != ' ' && *token != '\t')
+       break;
+
+    /* find end of token */
+    for (line_ptr = token; *line_ptr; line_ptr++)
+    {
+      if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':')
+      {
+       *line_ptr = '\0';
+       break;
+      }
+    }
+
+    if (line_ptr < line + line_len)
+      value = line_ptr + 1;
+    else
+      value = "\0";
+
+    /* cut leading whitespaces from value */
+    for (; *value; value++)
+      if (*value != ' ' && *value != '\t')
+       break;
+
+    if (*token && *value)
+      updateSetupFileListEntry(setup_file_list, token, value);
+
+
+#if 0
+    {
+      /* allocate new token/value pair */
+
+      *next_entry = newSetupFileList(token, value);
+      next_entry = &((*next_entry)->next);
+    }
+#endif
+
+
+
+  }
+
+  fclose(file);
+
+  first_valid_list_entry = setup_file_list->next;
+
+  /* free empty list header */
+  setup_file_list->next = NULL;
+  freeSetupFileList(setup_file_list);
+
+  if (!first_valid_list_entry)
+    Error(ERR_WARN, "setup file is empty");
+
+  return first_valid_list_entry;
+}
+
+static void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list,
+                                        char *identifier)
+{
+  if (!setup_file_list)
+    return;
+
+  if (strcmp(setup_file_list->token, TOKEN_STR_FILE_IDENTIFIER) == 0)
+  {
+    if (strcmp(setup_file_list->value, identifier) != 0)
+    {
+      Error(ERR_WARN, "setup file has wrong version");
+      return;
+    }
+    else
+      return;
+  }
+
+  if (setup_file_list->next)
+    checkSetupFileListIdentifier(setup_file_list->next, identifier);
+  else
+  {
+    Error(ERR_WARN, "setup file has no version information");
+    return;
+  }
+}
+
+static void decodeSetupFileList(struct SetupFileList *setup_file_list)
+{
+  int i;
+  int token_nr = TOKEN_INVALID;
+  int player_nr = 0;
+  char *token;
+  char *token_value;
+  int token_integer_value;
+  boolean token_boolean_value;
+  int token_player_prefix_len;
+
+  if (!setup_file_list)
+    return;
+
+  token = setup_file_list->token;
+  token_value = setup_file_list->value;
+  token_integer_value = get_string_integer_value(token_value);
+  token_boolean_value = get_string_boolean_value(token_value);
+
+  token_player_prefix_len = strlen(TOKEN_STR_PLAYER_PREFIX);
+
+  if (strncmp(token, TOKEN_STR_PLAYER_PREFIX,
+             token_player_prefix_len) == 0)
+  {
+    token += token_player_prefix_len;
+
+    if (*token >= '0' && *token <= '9')
+    {
+      player_nr = ((int)(*token - '0') - 1 + MAX_PLAYERS) % MAX_PLAYERS;
+      token++;
+    }
+  }
+
+  for (i=0; i<NUM_SETUP_TOKENS; i++)
+  {
+    if (strcmp(token, setup_info[i].token) == 0)
+    {
+      token_nr = i;
+      break;
+    }
+  }
+
+  if (strcmp(token, TOKEN_STR_FILE_IDENTIFIER) == 0)
+    token_nr = TOKEN_IGNORE;
+  else if (strcmp(token, TOKEN_STR_ALIAS_NAME) == 0)
+    token_nr = SETUP_TOKEN_ALIAS_NAME;
+
+  switch (token_nr)
+  {
+    case SETUP_TOKEN_SOUND:
+      setup.sound_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_SOUND_LOOPS:
+      setup.sound_loops_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_SOUND_MUSIC:
+      setup.sound_music_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_SOUND_SIMPLE:
+      setup.sound_simple_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_TOONS:
+      setup.toons_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_DIRECT_DRAW:
+      setup.direct_draw_on = !token_boolean_value;
+      break;
+    case SETUP_TOKEN_SCROLL_DELAY:
+      setup.scroll_delay_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_SOFT_SCROLLING:
+      setup.soft_scrolling_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_FADING:
+      setup.fading_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_AUTORECORD:
+      setup.autorecord_on = token_boolean_value;
+      break;
+    case SETUP_TOKEN_QUICK_DOORS:
+      setup.quick_doors = token_boolean_value;
+      break;
+
+    case SETUP_TOKEN_USE_JOYSTICK:
+      setup.input[player_nr].use_joystick = token_boolean_value;
+      break;
+    case SETUP_TOKEN_JOYSTICK_NR:
+      if (token_integer_value < 0 || token_integer_value > 1)
+       token_integer_value = 1;
+      setup.input[player_nr].joystick_nr = token_integer_value - 1;
+      break;
+    case SETUP_TOKEN_JOY_SNAP:
+      setup.input[player_nr].joy.snap = getJoySymbolFromJoyName(token_value);
+      break;
+    case SETUP_TOKEN_JOY_BOMB    :
+      setup.input[player_nr].joy.bomb = getJoySymbolFromJoyName(token_value);
+      break;
+    case SETUP_TOKEN_KEY_LEFT:
+      setup.input[player_nr].key.left = getKeySymFromX11KeyName(token_value);
+      break;
+    case SETUP_TOKEN_KEY_RIGHT:
+      setup.input[player_nr].key.right = getKeySymFromX11KeyName(token_value);
+      break;
+    case SETUP_TOKEN_KEY_UP:
+      setup.input[player_nr].key.up = getKeySymFromX11KeyName(token_value);
+      break;
+    case SETUP_TOKEN_KEY_DOWN:
+      setup.input[player_nr].key.down = getKeySymFromX11KeyName(token_value);
+      break;
+    case SETUP_TOKEN_KEY_SNAP:
+      setup.input[player_nr].key.snap = getKeySymFromX11KeyName(token_value);
+      break;
+    case SETUP_TOKEN_KEY_BOMB:
+      setup.input[player_nr].key.bomb = getKeySymFromX11KeyName(token_value);
+      break;
+
+    case SETUP_TOKEN_ALIAS_NAME:
+      strncpy(local_player->alias_name, token_value, MAX_NAMELEN-1);
+      local_player->alias_name[MAX_NAMELEN-1] = '\0';
+      break;
+
+    case TOKEN_INVALID:
+      Error(ERR_WARN, "unknown token '%s' not recognized", token);
+      break;
+
+    case TOKEN_IGNORE:
+    default:
+      break;
+  }
+
+  decodeSetupFileList(setup_file_list->next);
+}
+
+void LoadSetup()
+{
+  int i;
+  char filename[MAX_FILENAME_LEN];
+  struct SetupFileList *setup_file_list = NULL;
+
+  /* always start with reliable default setup values */
+
+  strncpy(local_player->login_name, GetLoginName(), MAX_NAMELEN-1);
+  local_player->login_name[MAX_NAMELEN-1] = '\0';
+
+  strncpy(local_player->alias_name, GetLoginName(), MAX_NAMELEN-1);
+  local_player->alias_name[MAX_NAMELEN-1] = '\0';
+
+  setup.sound_on = TRUE;
+  setup.sound_loops_on = FALSE;
+  setup.sound_music_on = FALSE;
+  setup.sound_simple_on = FALSE;
+  setup.toons_on = TRUE;
+  setup.direct_draw_on = FALSE;
+  setup.scroll_delay_on = FALSE;
+  setup.soft_scrolling_on = TRUE;
+  setup.fading_on = FALSE;
+  setup.autorecord_on = FALSE;
+  setup.quick_doors = FALSE;
+
+  for (i=0; i<MAX_PLAYERS; i++)
+  {
+    setup.input[i].use_joystick = FALSE;
+    setup.input[i].joystick_nr = 0;
+    setup.input[i].joy.snap  = (i == 0 ? JOY_BUTTON_1 : 0);
+    setup.input[i].joy.bomb  = (i == 0 ? JOY_BUTTON_2 : 0);
+    setup.input[i].key.left  = (i == 0 ? DEFAULT_KEY_LEFT  : KEY_UNDEFINDED);
+    setup.input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KEY_UNDEFINDED);
+    setup.input[i].key.up    = (i == 0 ? DEFAULT_KEY_UP    : KEY_UNDEFINDED);
+    setup.input[i].key.down  = (i == 0 ? DEFAULT_KEY_DOWN  : KEY_UNDEFINDED);
+    setup.input[i].key.snap  = (i == 0 ? DEFAULT_KEY_SNAP  : KEY_UNDEFINDED);
+    setup.input[i].key.bomb  = (i == 0 ? DEFAULT_KEY_BOMB  : KEY_UNDEFINDED);
+  }
+
+  sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME);
+
+  setup_file_list = loadSetupFileList(filename);
+
+  if (setup_file_list)
+  {
+
+#if 0
+    printSetupFileList(setup_file_list);
+#endif
+
+    checkSetupFileListIdentifier(setup_file_list, SETUP_COOKIE);
+    decodeSetupFileList(setup_file_list);
+
+    freeSetupFileList(setup_file_list);
+  }
+  else
+    Error(ERR_RETURN, "using default setup values");
+}
+
+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)
+  {
+    int i;
+
+    char *last_level_series =
+      getSetupFileListEntry(level_setup_list, TOKEN_STR_LAST_LEVEL_SERIES);
+
+    if (last_level_series)
+    {
+      for (i=0; i<num_leveldirs; i++)
+      {
+       if (strcmp(last_level_series, leveldir[i].filename) == 0)
+       {
+         char *token_value =
+           getSetupFileListEntry(level_setup_list, last_level_series);
+
+         leveldir_nr = i;
+
+         if (token_value)
+         {
+           level_nr = atoi(token_value);
+
+           if (level_nr < 0)
+             level_nr = 0;
+           if (level_nr > leveldir[leveldir_nr].levels - 1)
+             level_nr = leveldir[leveldir_nr].levels - 1;
+         }
+
+         break;
+       }
+      }
+    }
+
+
+#if 0
+    printSetupFileList(level_setup_list);
+#endif
+
+    checkSetupFileListIdentifier(level_setup_list, LEVELSETUP_COOKIE);
+  }
+  else
+    Error(ERR_RETURN, "using default setup values");
+}
+
+void SaveSetup()
+{
+  int i;
+  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");
+
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_SOUND,
+                       setup.sound_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_SOUND_LOOPS,
+                       setup.sound_loops_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_SOUND_MUSIC,
+                       setup.sound_music_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_SOUND_SIMPLE,
+                       setup.sound_simple_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_TOONS,
+                       setup.toons_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_DIRECT_DRAW,
+                       setup.direct_draw_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_SCROLL_DELAY,
+                       setup.scroll_delay_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_SOFT_SCROLLING,
+                       setup.soft_scrolling_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_FADING,
+                       setup.fading_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_AUTORECORD,
+                       setup.autorecord_on));
+  fprintf(file, "%s\n",
+         getSetupEntry("", SETUP_TOKEN_QUICK_DOORS,
+                       setup.quick_doors));
+
+  fprintf(file, "\n");
+
+  fprintf(file, "%s\n",
+         getFormattedSetupEntry(TOKEN_STR_ALIAS_NAME,
+                                local_player->alias_name));
+
+  for (i=0; i<MAX_PLAYERS; i++)
+  {
+    char prefix[30];
+
+    sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, i + 1);
+
+    fprintf(file, "\n");
+
+    fprintf(file, "%s\n",
+           getSetupEntry(prefix, SETUP_TOKEN_USE_JOYSTICK,
+                         setup.input[i].use_joystick));
+    fprintf(file, "%s\n",
+           getSetupEntry(prefix, SETUP_TOKEN_JOYSTICK_NR,
+                         setup.input[i].joystick_nr));
+
+    fprintf(file, "%s%s:      %s\n", prefix,
+           getSetupToken(SETUP_TOKEN_JOY_SNAP),
+           getJoyNameFromJoySymbol(setup.input[i].joy.snap));
+    fprintf(file, "%s%s:      %s\n", prefix,
+           getSetupToken(SETUP_TOKEN_JOY_BOMB),
+           getJoyNameFromJoySymbol(setup.input[i].joy.bomb));
+
+    fprintf(file, "%s\n",
+           getSetupEntryWithComment(prefix, SETUP_TOKEN_KEY_LEFT,
+                                    setup.input[i].key.left));
+    fprintf(file, "%s\n",
+           getSetupEntryWithComment(prefix, SETUP_TOKEN_KEY_RIGHT,
+                                    setup.input[i].key.right));
+    fprintf(file, "%s\n",
+           getSetupEntryWithComment(prefix, SETUP_TOKEN_KEY_UP,
+                                    setup.input[i].key.up));
+    fprintf(file, "%s\n",
+           getSetupEntryWithComment(prefix, SETUP_TOKEN_KEY_DOWN,
+                                    setup.input[i].key.down));
+    fprintf(file, "%s\n",
+           getSetupEntryWithComment(prefix, SETUP_TOKEN_KEY_SNAP,
+                                    setup.input[i].key.snap));
+    fprintf(file, "%s\n",
+           getSetupEntryWithComment(prefix, SETUP_TOKEN_KEY_BOMB,
+                                    setup.input[i].key.bomb));
+  }
+
+  fclose(file);
+
+  chmod(filename, SETUP_PERMS);
+}
+
+void SaveLevelSetup()
+{
+  char filename[MAX_FILENAME_LEN];
+  struct SetupFileList *list_entry = level_setup_list;
+  FILE *file;
+
+  updateSetupFileListEntry(level_setup_list,
+                          TOKEN_STR_LAST_LEVEL_SERIES,
+                          leveldir[leveldir_nr].filename);
+
+  updateSetupFileListEntry(level_setup_list,
+                          leveldir[leveldir_nr].filename,
+                          int2str(level_nr, 0));
+
+  sprintf(filename, "%s/%s", SETUP_PATH, LEVELSETUP_FILENAME);
+
+  if (!(file = fopen(filename, "w")))
+  {
+    Error(ERR_WARN, "cannot write setup file '%s'", filename);
+    return;
+  }
+
+  fprintf(file, "%s:              %s\n\n",
+         TOKEN_STR_FILE_IDENTIFIER, LEVELSETUP_COOKIE);
+
+  while (list_entry)
+  {
+    if (strcmp(list_entry->token, TOKEN_STR_FILE_IDENTIFIER) != 0)
+      fprintf(file, "%s\n",
+             getFormattedSetupEntry(list_entry->token, list_entry->value));
+
+    /* just to make things nicer :) */
+    if (strcmp(list_entry->token, TOKEN_STR_LAST_LEVEL_SERIES) == 0)
+      fprintf(file, "\n");
+
+    list_entry = list_entry->next;
+  }
+
+  fclose(file);
+
+  chmod(filename, SETUP_PERMS);
+}