rnd-19981120-1
[rocksndiamonds.git] / src / misc.c
index a7e52dcc4a71aaf2273ae986ec1ff2551ed46a50..2c01f36401e0e7eed881a71f23b5e1f4ab2baa3e 100644 (file)
@@ -18,6 +18,7 @@
 #include <sys/param.h>
 #include <sys/types.h>
 #include <stdarg.h>
+#include <ctype.h>
 
 #include "misc.h"
 #include "init.h"
@@ -158,9 +159,22 @@ unsigned int SimpleRND(unsigned int max)
   return (root % max);
 }
 
+#ifdef DEBUG
+static unsigned int last_RND_value = 0;
+
+unsigned int last_RND()
+{
+  return last_RND_value;
+}
+#endif
+
 unsigned int RND(unsigned int max)
 {
+#ifdef DEBUG
+  return (last_RND_value = random_linux_libc() % max);
+#else
   return (random_linux_libc() % max);
+#endif
 }
 
 unsigned int InitRND(long seed)
@@ -210,6 +224,33 @@ char *getHomeDir()
   return home_dir;
 }
 
+char *getPath2(char *path1, char *path2)
+{
+  char *complete_path = checked_malloc(strlen(path1) + strlen(path2) + 2);
+
+  sprintf(complete_path, "%s/%s", path1, path2);
+  return complete_path;
+}
+
+char *getStringCopy(char *s)
+{
+  char *s_copy = checked_malloc(strlen(s) + 1);
+
+  strcpy(s_copy, s);
+  return s_copy;
+}
+
+char *getStringToLower(char *s)
+{
+  char *s_copy = checked_malloc(strlen(s) + 1);
+  char *s_ptr = s_copy;
+
+  while (*s)
+    *s_ptr++ = tolower(*s++);
+
+  return s_copy;
+}
+
 void MarkTileDirty(int x, int y)
 {
   int xx = redraw_x1 + x;
@@ -363,7 +404,7 @@ void GetOptions(char *argv[])
   }
 }
 
-void Error(int mode, char *format_str, ...)
+void Error(int mode, char *format, ...)
 {
   FILE *output_stream = stderr;
   char *process_name = "";
@@ -371,53 +412,17 @@ void Error(int mode, char *format_str, ...)
   if (mode & ERR_SOUNDSERVER)
     process_name = " sound server";
 
-  if (format_str)
+  if (format)
   {
     va_list ap;
-    char *format_ptr;
-    char *s_value;
-    int i_value;
-    double d_value;
 
     fprintf(output_stream, "%s%s: ", program_name, process_name);
 
     if (mode & ERR_WARN)
       fprintf(output_stream, "warning: ");
 
-    va_start(ap, format_str);  /* ap points to first unnamed argument */
-  
-    for(format_ptr=format_str; *format_ptr; format_ptr++)
-    {
-      if (*format_ptr != '%')
-      {
-       fprintf(output_stream, "%c", *format_ptr);
-       continue;
-      }
-  
-      switch(*++format_ptr)
-      {
-       case 'd':
-         i_value = va_arg(ap, int);
-         fprintf(output_stream, "%d", i_value);
-         break;
-  
-       case 'f':
-         d_value = va_arg(ap, double);
-         fprintf(output_stream, "%f", d_value);
-         break;
-  
-       case 's':
-         s_value = va_arg(ap, char *);
-         fprintf(output_stream, "%s", s_value);
-         break;
-  
-       default:
-         fprintf(stderr, "\n%s: Error(): invalid format string: %s\n",
-                 program_name, format_str);
-         CloseAllAndExit(10);
-      }
-    }
-
+    va_start(ap, format);
+    vfprintf(output_stream, format, ap);
     va_end(ap);
   
     fprintf(output_stream, "\n");
@@ -786,3 +791,22 @@ int getJoySymbolFromJoyName(char *name)
   translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
   return joysymbol;
 }
+
+/* the following is only for debugging purpose and normally not used */
+#define DEBUG_NUM_TIMESTAMPS   3
+
+void debug_print_timestamp(int counter_nr, char *message)
+{
+  static long counter[DEBUG_NUM_TIMESTAMPS][2];
+
+  if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
+    Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
+
+  counter[counter_nr][0] = Counter();
+
+  if (message)
+    printf("%s %.2f seconds\n", message,
+          (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
+
+  counter[counter_nr][1] = Counter();
+}