rnd-20020315-2-src
[rocksndiamonds.git] / src / libgame / misc.c
index 2947adc4b000dc36a85a867908559b56e424492d..d8140b7e8e09a189909ef7939bdba771ba45114b 100644 (file)
@@ -1,14 +1,14 @@
 /***********************************************************
-*  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
+* Artsoft Retro-Game Library                               *
 *----------------------------------------------------------*
-*  (c) 1995-98 Artsoft Entertainment                       *
-*              Holger Schemel                              *
-*              Oststrasse 11a                              *
-*              33604 Bielefeld                             *
-*              phone: ++49 +521 290471                     *
-*              email: aeglos@valinor.owl.de                *
+* (c) 1994-2001 Artsoft Entertainment                      *
+*               Holger Schemel                             *
+*               Detmolder Strasse 189                      *
+*               33604 Bielefeld                            *
+*               Germany                                    *
+*               e-mail: info@artsoft.org                   *
 *----------------------------------------------------------*
-*  misc.c                                                  *
+* misc.c                                                   *
 ***********************************************************/
 
 #include <time.h>
@@ -473,7 +473,8 @@ void GetOptions(char *argv[])
             "  -l, --level directory         alternative level directory\n"
             "  -s, --serveronly              only start network server\n"
             "  -n, --network                 network multiplayer game\n"
-            "  -v, --verbose                 verbose mode\n",
+            "  -v, --verbose                 verbose mode\n"
+            "      --debug                   display debugging information\n",
             program.command_basename);
       exit(0);
     }
@@ -551,15 +552,18 @@ void Error(int mode, char *format, ...)
 {
   char *process_name = "";
   FILE *error = stderr;
+  char *newline = "\n";
 
   /* display warnings only when running in verbose mode */
   if (mode & ERR_WARN && !options.verbose)
     return;
 
 #if !defined(PLATFORM_UNIX)
+  newline = "\r\n";
+
   if ((error = openErrorFile()) == NULL)
   {
-    printf("Cannot write to error output file!\n");
+    printf("Cannot write to error output file!%s", newline);
     program.exit_function(1);
   }
 #endif
@@ -584,15 +588,16 @@ void Error(int mode, char *format, ...)
     vfprintf(error, format, ap);
     va_end(ap);
   
-    fprintf(error, "\n");
+    fprintf(error, "%s", newline);
   }
   
   if (mode & ERR_HELP)
-    fprintf(error, "%s: Try option '--help' for more information.\n",
-           program.command_basename);
+    fprintf(error, "%s: Try option '--help' for more information.%s",
+           program.command_basename, newline);
 
   if (mode & ERR_EXIT)
-    fprintf(error, "%s%s: aborting\n", program.command_basename, process_name);
+    fprintf(error, "%s%s: aborting%s",
+           program.command_basename, process_name, newline);
 
   if (error != stderr)
     fclose(error);
@@ -630,6 +635,16 @@ void *checked_calloc(unsigned long size)
   return ptr;
 }
 
+void *checked_realloc(void *ptr, unsigned long size)
+{
+  ptr = realloc(ptr, size);
+
+  if (ptr == NULL)
+    Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
+
+  return ptr;
+}
+
 short getFile16BitInteger(FILE *file, int byte_order)
 {
   if (byte_order == BYTE_ORDER_BIG_ENDIAN)
@@ -686,26 +701,28 @@ void putFile32BitInteger(FILE *file, int value, int byte_order)
   }
 }
 
-void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
-                 int byte_order)
+boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
+                    int byte_order)
 {
-  const int chunk_identifier_length = 4;
+  const int chunk_name_length = 4;
+
+  /* read chunk name */
+  fgets(chunk_name, chunk_name_length + 1, file);
 
-  /* read chunk identifier */
-  fgets(chunk_buffer, chunk_identifier_length + 1, file);
+  /* read chunk size */
+  *chunk_size = getFile32BitInteger(file, byte_order);
 
-  /* read chunk length */
-  *chunk_length = getFile32BitInteger(file, byte_order);
+  return (feof(file) || ferror(file) ? FALSE : TRUE);
 }
 
-void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
+void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
                  int byte_order)
 {
-  /* write chunk identifier */
+  /* write chunk name */
   fputs(chunk_name, file);
 
-  /* write chunk length */
-  putFile32BitInteger(file, chunk_length, byte_order);
+  /* write chunk size */
+  putFile32BitInteger(file, chunk_size, byte_order);
 }
 
 #define TRANSLATE_KEYSYM_TO_KEYNAME    0
@@ -1314,7 +1331,7 @@ FILE *openErrorFile()
   FILE *error_file;
 
   filename = getPath2(getUserDataDir(), ERROR_FILENAME);
-  error_file = fopen(filename, "a");
+  error_file = fopen(filename, MODE_APPEND);
   free(filename);
 
   return error_file;
@@ -1326,7 +1343,7 @@ void dumpErrorFile()
   FILE *error_file;
 
   filename = getPath2(getUserDataDir(), ERROR_FILENAME);
-  error_file = fopen(filename, "r");
+  error_file = fopen(filename, MODE_READ);
   free(filename);
 
   if (error_file != NULL)