rnd-20070318-1-src
[rocksndiamonds.git] / src / libgame / setup.c
index 45a9895a9ef444b3b3f7638f543e1f743839df73..373ffe33b22920782ac4b4e9ce4a2ceac1813c9c 100644 (file)
@@ -1609,11 +1609,15 @@ static void printSetupFileHash(SetupFileHash *hash)
 
 #define ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE           1
 #define CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING           0
+#define CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH            0
 
 static boolean token_value_separator_found = FALSE;
 #if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
 static boolean token_value_separator_warning = FALSE;
 #endif
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+static boolean token_already_exists_warning = FALSE;
+#endif
 
 static boolean getTokenValueFromSetupLineExt(char *line,
                                             char **token_ptr, char **value_ptr,
@@ -1760,8 +1764,8 @@ boolean getTokenValueFromSetupLine(char *line, char **token, char **value)
 }
 
 #if 1
-static void loadSetupFileData(void *setup_file_data, char *filename,
-                             boolean top_recursion_level, boolean is_hash)
+static boolean loadSetupFileData(void *setup_file_data, char *filename,
+                                boolean top_recursion_level, boolean is_hash)
 {
   static SetupFileHash *include_filename_hash = NULL;
   char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
@@ -1769,18 +1773,21 @@ static void loadSetupFileData(void *setup_file_data, char *filename,
   void *insert_ptr = NULL;
   boolean read_continued_line = FALSE;
   FILE *file;
-  int line_nr = 0;
-  int token_count = 0;
+  int line_nr = 0, token_count = 0, include_count = 0;
 
 #if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
   token_value_separator_warning = FALSE;
 #endif
 
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+  token_already_exists_warning = FALSE;
+#endif
+
   if (!(file = fopen(filename, MODE_READ)))
   {
     Error(ERR_WARN, "cannot open configuration file '%s'", filename);
 
-    return;
+    return FALSE;
   }
 
   /* use "insert pointer" to store list end for constant insertion complexity */
@@ -1865,6 +1872,8 @@ static void loadSetupFileData(void *setup_file_data, char *filename,
          free(basepath);
          free(basename);
          free(filename_include);
+
+         include_count++;
        }
        else
        {
@@ -1874,9 +1883,34 @@ static void loadSetupFileData(void *setup_file_data, char *filename,
       else
       {
        if (is_hash)
+       {
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+         char *old_value =
+           getHashEntry((SetupFileHash *)setup_file_data, token);
+
+         if (old_value != NULL)
+         {
+           if (!token_already_exists_warning)
+           {
+             Error(ERR_INFO_LINE, "-");
+             Error(ERR_WARN, "duplicate token(s) found in config file:");
+             Error(ERR_INFO, "- config file: '%s'", filename);
+
+             token_already_exists_warning = TRUE;
+           }
+
+           Error(ERR_INFO, "- token: '%s' (in line %d)", token, line_nr);
+           Error(ERR_INFO, "  old value: '%s'", old_value);
+           Error(ERR_INFO, "  new value: '%s'", value);
+         }
+#endif
+
          setHashEntry((SetupFileHash *)setup_file_data, token, value);
+       }
        else
+       {
          insert_ptr = addListEntry((SetupFileList *)insert_ptr, token, value);
+       }
 
        token_count++;
       }
@@ -1890,17 +1924,24 @@ static void loadSetupFileData(void *setup_file_data, char *filename,
     Error(ERR_INFO_LINE, "-");
 #endif
 
-  if (token_count == 0)
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+  if (token_already_exists_warning)
+    Error(ERR_INFO_LINE, "-");
+#endif
+
+  if (token_count == 0 && include_count == 0)
     Error(ERR_WARN, "configuration file '%s' is empty", filename);
 
   if (top_recursion_level)
     freeSetupFileHash(include_filename_hash);
+
+  return TRUE;
 }
 
 #else
 
-static void loadSetupFileData(void *setup_file_data, char *filename,
-                             boolean top_recursion_level, boolean is_hash)
+static boolean loadSetupFileData(void *setup_file_data, char *filename,
+                                boolean top_recursion_level, boolean is_hash)
 {
   static SetupFileHash *include_filename_hash = NULL;
   char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
@@ -1919,7 +1960,7 @@ static void loadSetupFileData(void *setup_file_data, char *filename,
   {
     Error(ERR_WARN, "cannot open configuration file '%s'", filename);
 
-    return;
+    return FALSE;
   }
 
   /* use "insert pointer" to store list end for constant insertion complexity */
@@ -2125,6 +2166,8 @@ static void loadSetupFileData(void *setup_file_data, char *filename,
 
   if (top_recursion_level)
     freeSetupFileHash(include_filename_hash);
+
+  return TRUE;
 }
 #endif
 
@@ -2154,7 +2197,12 @@ SetupFileList *loadSetupFileList(char *filename)
   SetupFileList *setup_file_list = newSetupFileList("", "");
   SetupFileList *first_valid_list_entry;
 
-  loadSetupFileData(setup_file_list, filename, TRUE, FALSE);
+  if (!loadSetupFileData(setup_file_list, filename, TRUE, FALSE))
+  {
+    freeSetupFileList(setup_file_list);
+
+    return NULL;
+  }
 
   first_valid_list_entry = setup_file_list->next;
 
@@ -2169,7 +2217,12 @@ SetupFileHash *loadSetupFileHash(char *filename)
 {
   SetupFileHash *setup_file_hash = newSetupFileHash();
 
-  loadSetupFileData(setup_file_hash, filename, TRUE, TRUE);
+  if (!loadSetupFileData(setup_file_hash, filename, TRUE, TRUE))
+  {
+    freeSetupFileHash(setup_file_hash);
+
+    return NULL;
+  }
 
   return setup_file_hash;
 }