rnd-20040612-1-src
[rocksndiamonds.git] / src / libgame / misc.c
index 0c4547e04782a096e6b72865cda4274bb47b42c7..225d385cf3323002b1c1257e87aa966efcb7e9c6 100644 (file)
@@ -52,6 +52,7 @@ void printf_line(char *line_string, int line_length)
   fprintf_line(stdout, line_string, line_length);
 }
 
+
 /* int2str() returns a number converted to a string;
    the used memory is static, but will be overwritten by later calls,
    so if you want to save the result, copy it to a private string buffer;
@@ -82,6 +83,7 @@ char *int2str(int number, int size)
   }
 }
 
+
 /* something similar to "int2str()" above, but allocates its own memory
    and has a different interface; we cannot use "itoa()", because this
    seems to be already defined when cross-compiling to the win32 target */
@@ -90,8 +92,7 @@ char *i_to_a(unsigned int i)
 {
   static char *a = NULL;
 
-  if (a != NULL)
-    free(a);
+  checked_free(a);
 
   if (i > 2147483647)  /* yes, this is a kludge */
     i = 2147483647;
@@ -104,6 +105,23 @@ char *i_to_a(unsigned int i)
 }
 
 
+/* calculate base-2 logarithm of argument (rounded down to integer;
+   this function returns the number of the highest bit set in argument) */
+
+int log_2(unsigned int x)
+{
+  int e = 0;
+
+  while ((1 << e) < x)
+  {
+    x -= (1 << e);     /* for rounding down (rounding up: remove this line) */
+    e++;
+  }
+
+  return e;
+}
+
+
 /* ------------------------------------------------------------------------- */
 /* counter functions                                                         */
 /* ------------------------------------------------------------------------- */
@@ -501,8 +519,7 @@ char *getStringToLower(char *s)
 
 void setString(char **old_value, char *new_value)
 {
-  if (*old_value != NULL)
-    free(*old_value);
+  checked_free(*old_value);
 
   *old_value = getStringCopy(new_value);
 }
@@ -598,9 +615,17 @@ void GetOptions(char *argv[], void (*print_usage_function)(void))
       if (option_arg == next_option)
        options_left++;
 
-      /* adjust path for level directory accordingly */
+      /* adjust paths for sub-directories in base directory accordingly */
       options.level_directory =
        getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
+      options.graphics_directory =
+       getPath2(options.ro_base_directory, GRAPHICS_DIRECTORY);
+      options.sounds_directory =
+       getPath2(options.ro_base_directory, SOUNDS_DIRECTORY);
+      options.music_directory =
+       getPath2(options.ro_base_directory, MUSIC_DIRECTORY);
+      options.docs_directory =
+       getPath2(options.ro_base_directory, DOCS_DIRECTORY);
     }
     else if (strncmp(option, "-levels", option_len) == 0)
     {
@@ -662,6 +687,11 @@ void GetOptions(char *argv[], void (*print_usage_function)(void))
       options.execute_command = option_arg;
       if (option_arg == next_option)
        options_left++;
+
+#if 1
+      /* when doing batch processing, always enable verbose mode (warnings) */
+      options.verbose = TRUE;
+#endif
     }
     else if (*option == '-')
     {
@@ -784,7 +814,7 @@ void Error(int mode, char *format, ...)
 
 
 /* ------------------------------------------------------------------------- */
-/* memory allocation functions                                               */
+/* checked memory allocation and freeing functions                           */
 /* ------------------------------------------------------------------------- */
 
 void *checked_malloc(unsigned long size)
@@ -821,6 +851,12 @@ void *checked_realloc(void *ptr, unsigned long size)
   return ptr;
 }
 
+void checked_free(void *ptr)
+{
+  if (ptr != NULL)     /* this check should be done by free() anyway */
+    free(ptr);
+}
+
 
 /* ------------------------------------------------------------------------- */
 /* various helper functions                                                  */
@@ -846,27 +882,27 @@ inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
   *y2 = help_y;
 }
 
-short getFile16BitInteger(FILE *file, int byte_order)
+int getFile16BitInteger(FILE *file, int byte_order)
 {
   if (byte_order == BYTE_ORDER_BIG_ENDIAN)
-    return ((fgetc(file) <<  8) |
-           (fgetc(file) <<  0));
+    return ((fgetc(file) << 8) |
+           (fgetc(file) << 0));
   else          /* BYTE_ORDER_LITTLE_ENDIAN */
-    return ((fgetc(file) <<  0) |
-           (fgetc(file) <<  8));
+    return ((fgetc(file) << 0) |
+           (fgetc(file) << 8));
 }
 
-void putFile16BitInteger(FILE *file, short value, int byte_order)
+void putFile16BitInteger(FILE *file, int value, int byte_order)
 {
   if (byte_order == BYTE_ORDER_BIG_ENDIAN)
   {
-    fputc((value >>  8) & 0xff, file);
-    fputc((value >>  0) & 0xff, file);
+    fputc((value >> 8) & 0xff, file);
+    fputc((value >> 0) & 0xff, file);
   }
   else          /* BYTE_ORDER_LITTLE_ENDIAN */
   {
-    fputc((value >>  0) & 0xff, file);
-    fputc((value >>  8) & 0xff, file);
+    fputc((value >> 0) & 0xff, file);
+    fputc((value >> 8) & 0xff, file);
   }
 }
 
@@ -1204,7 +1240,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
       char c = name_ptr[6];
 
       if (c >= '0' && c <= '9')
-       key = KSYM_0 + (Key)(c - '0');
+       key = KSYM_KP_0 + (Key)(c - '0');
     }
     else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
     {
@@ -1480,8 +1516,7 @@ boolean fileHasPrefix(char *basename, char *prefix)
   static char *basename_lower = NULL;
   int basename_length, prefix_length;
 
-  if (basename_lower != NULL)
-    free(basename_lower);
+  checked_free(basename_lower);
 
   if (basename == NULL || prefix == NULL)
     return FALSE;
@@ -1503,8 +1538,7 @@ boolean fileHasSuffix(char *basename, char *suffix)
   static char *basename_lower = NULL;
   int basename_length, suffix_length;
 
-  if (basename_lower != NULL)
-    free(basename_lower);
+  checked_free(basename_lower);
 
   if (basename == NULL || suffix == NULL)
     return FALSE;
@@ -1673,7 +1707,7 @@ static void FreeCustomArtworkList(struct ArtworkListInfo *,
                                  struct ListNodeInfo ***, int *);
 
 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
-                                          struct ConfigInfo *suffix_list,
+                                          struct ConfigTypeInfo *suffix_list,
                                           char **ignore_tokens,
                                           int num_file_list_entries)
 {
@@ -1808,7 +1842,7 @@ static boolean token_suffix_match(char *token, char *suffix, int start_pos)
 #define KNOWN_TOKEN_VALUE      "[KNOWN_TOKEN_VALUE]"
 
 static void read_token_parameters(SetupFileHash *setup_file_hash,
-                                 struct ConfigInfo *suffix_list,
+                                 struct ConfigTypeInfo *suffix_list,
                                  struct FileInfo *file_list_entry)
 {
   /* check for config token that is the base token without any suffixes */
@@ -1861,7 +1895,7 @@ static void read_token_parameters(SetupFileHash *setup_file_hash,
 static void add_dynamic_file_list_entry(struct FileInfo **list,
                                        int *num_list_entries,
                                        SetupFileHash *extra_file_hash,
-                                       struct ConfigInfo *suffix_list,
+                                       struct ConfigTypeInfo *suffix_list,
                                        int num_suffix_list_entries,
                                        char *token)
 {
@@ -1878,6 +1912,7 @@ static void add_dynamic_file_list_entry(struct FileInfo **list,
   new_list_entry = &(*list)[*num_list_entries - 1];
 
   new_list_entry->token = getStringCopy(token);
+  new_list_entry->default_filename = NULL;
   new_list_entry->filename = NULL;
   new_list_entry->parameter = checked_calloc(parameter_array_size);
 
@@ -1909,7 +1944,7 @@ static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info,
                                          char *filename)
 {
   struct FileInfo *file_list = artwork_info->file_list;
-  struct ConfigInfo *suffix_list = artwork_info->suffix_list;
+  struct ConfigTypeInfo *suffix_list = artwork_info->suffix_list;
   char **base_prefixes = artwork_info->base_prefixes;
   char **ext1_suffixes = artwork_info->ext1_suffixes;
   char **ext2_suffixes = artwork_info->ext2_suffixes;
@@ -2547,9 +2582,17 @@ void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
 #endif
 
   for (i = 0; i < num_dynamic_file_list_entries; i++)
+  {
     LoadArtworkToList(artwork_info, &artwork_info->dynamic_artwork_list[i],
                      dynamic_file_list[i].filename, i);
 
+#if 0
+    printf("::: '%s', '0x%08x'\n",
+          dynamic_file_list[i].filename,
+          dynamic_file_list[i].default_filename);
+#endif
+  }
+
 #if 0
   dumpList(artwork_info->content_list);
 #endif