1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
14 #include <sys/types.h>
24 #if !defined(PLATFORM_WIN32)
26 #include <sys/param.h>
36 /* ========================================================================= */
37 /* some generic helper functions */
38 /* ========================================================================= */
40 /* ------------------------------------------------------------------------- */
41 /* platform independent wrappers for printf() et al. (newline aware) */
42 /* ------------------------------------------------------------------------- */
44 #if defined(PLATFORM_ANDROID)
45 static int android_log_prio = ANDROID_LOG_INFO;
49 static void vfPrintLog(FILE *stream, char *format, va_list ap)
53 static void vfPrintLog(FILE *stream, char *format, va_list ap)
57 static void fPrintLog(FILE *stream, char *format, va_list ap)
61 static void fPrintLog(FILE *stream, char *format, va_list ap)
66 static void vfprintf_nonewline(FILE *stream, char *format, va_list ap)
68 #if defined(PLATFORM_ANDROID)
69 // (prefix text of logging output is currently skipped on Android)
70 //__android_log_vprint(android_log_prio, program.program_title, format, ap);
75 vfprintf(stream, format, ap);
76 vfprintf(stderr, format, ap2);
82 static void vfprintf_newline(FILE *stream, char *format, va_list ap)
84 #if defined(PLATFORM_ANDROID)
85 __android_log_vprint(android_log_prio, program.program_title, format, ap);
87 char *newline = STRING_NEWLINE;
92 vfprintf(stream, format, ap);
93 fprintf(stream, "%s", newline);
95 vfprintf(stderr, format, ap2);
96 fprintf(stderr, "%s", newline);
102 static void fprintf_nonewline(FILE *stream, char *format, ...)
106 va_start(ap, format);
107 vfprintf_nonewline(stream, format, ap);
111 static void fprintf_newline(FILE *stream, char *format, ...)
115 va_start(ap, format);
116 vfprintf_newline(stream, format, ap);
120 void fprintf_line(FILE *stream, char *line_chars, int line_length)
124 for (i = 0; i < line_length; i++)
125 fprintf_nonewline(stream, "%s", line_chars);
127 fprintf_newline(stream, "");
130 void printf_line(char *line_chars, int line_length)
132 fprintf_line(stdout, line_chars, line_length);
135 void printf_line_with_prefix(char *prefix, char *line_chars, int line_length)
137 fprintf(stdout, "%s", prefix);
138 fprintf_line(stdout, line_chars, line_length);
142 /* ------------------------------------------------------------------------- */
143 /* string functions */
144 /* ------------------------------------------------------------------------- */
146 /* int2str() returns a number converted to a string;
147 the used memory is static, but will be overwritten by later calls,
148 so if you want to save the result, copy it to a private string buffer;
149 there can be 10 local calls of int2str() without buffering the result --
150 the 11th call will then destroy the result from the first call and so on.
153 char *int2str(int number, int size)
155 static char shift_array[10][40];
156 static int shift_counter = 0;
157 char *s = shift_array[shift_counter];
159 shift_counter = (shift_counter + 1) % 10;
166 sprintf(s, " %09d", number);
167 return &s[strlen(s) - size];
171 sprintf(s, "%d", number);
177 /* something similar to "int2str()" above, but allocates its own memory
178 and has a different interface; we cannot use "itoa()", because this
179 seems to be already defined when cross-compiling to the win32 target */
181 char *i_to_a(unsigned int i)
183 static char *a = NULL;
187 if (i > 2147483647) /* yes, this is a kludge */
190 a = checked_malloc(10 + 1);
198 /* calculate base-2 logarithm of argument (rounded down to integer;
199 this function returns the number of the highest bit set in argument) */
201 int log_2(unsigned int x)
207 x -= (1 << e); /* for rounding down (rounding up: remove this line) */
214 boolean getTokenValueFromString(char *string, char **token, char **value)
216 return getTokenValueFromSetupLine(string, token, value);
220 /* ------------------------------------------------------------------------- */
221 /* counter functions */
222 /* ------------------------------------------------------------------------- */
224 /* maximal allowed length of a command line option */
225 #define MAX_OPTION_LEN 256
227 static unsigned int getCurrentMS()
229 return SDL_GetTicks();
232 static unsigned int mainCounter(int mode)
234 static unsigned int base_ms = 0;
235 unsigned int current_ms;
237 /* get current system milliseconds */
238 current_ms = getCurrentMS();
240 /* reset base timestamp in case of counter reset or wrap-around */
241 if (mode == INIT_COUNTER || current_ms < base_ms)
242 base_ms = current_ms;
244 /* return milliseconds since last counter reset */
245 return current_ms - base_ms;
248 void InitCounter() /* set counter back to zero */
250 mainCounter(INIT_COUNTER);
253 unsigned int Counter() /* get milliseconds since last call of InitCounter() */
255 return mainCounter(READ_COUNTER);
258 static void sleep_milliseconds(unsigned int milliseconds_delay)
260 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
264 /* we want to wait only a few ms -- if we assume that we have a
265 kernel timer resolution of 10 ms, we would wait far to long;
266 therefore it's better to do a short interval of busy waiting
267 to get our sleeping time more accurate */
269 unsigned int base_counter = Counter(), actual_counter = Counter();
271 while (actual_counter < base_counter + milliseconds_delay &&
272 actual_counter >= base_counter)
273 actual_counter = Counter();
277 SDL_Delay(milliseconds_delay);
281 void Delay(unsigned int delay) /* Sleep specified number of milliseconds */
283 sleep_milliseconds(delay);
286 boolean FrameReached(unsigned int *frame_counter_var,
287 unsigned int frame_delay)
289 unsigned int actual_frame_counter = FrameCounter;
291 if (actual_frame_counter >= *frame_counter_var &&
292 actual_frame_counter < *frame_counter_var + frame_delay)
295 *frame_counter_var = actual_frame_counter;
300 boolean DelayReached(unsigned int *counter_var,
303 unsigned int actual_counter = Counter();
305 if (actual_counter >= *counter_var &&
306 actual_counter < *counter_var + delay)
309 *counter_var = actual_counter;
314 void WaitUntilDelayReached(unsigned int *counter_var, unsigned int delay)
316 unsigned int actual_counter;
320 actual_counter = Counter();
322 if (actual_counter >= *counter_var &&
323 actual_counter < *counter_var + delay)
324 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
329 *counter_var = actual_counter;
333 /* ------------------------------------------------------------------------- */
334 /* random generator functions */
335 /* ------------------------------------------------------------------------- */
337 unsigned int init_random_number(int nr, int seed)
339 if (seed == NEW_RANDOMIZE)
341 /* default random seed */
342 seed = (int)time(NULL); // seconds since the epoch
344 #if !defined(PLATFORM_WIN32)
345 /* add some more randomness */
346 struct timeval current_time;
348 gettimeofday(¤t_time, NULL);
350 seed += (int)current_time.tv_usec; // microseconds since the epoch
353 /* add some more randomness */
354 seed += (int)SDL_GetTicks(); // milliseconds since SDL init
356 /* add some more randomness */
357 seed += GetSimpleRandom(1000000);
360 srandom_linux_libc(nr, (unsigned int) seed);
362 return (unsigned int) seed;
365 unsigned int get_random_number(int nr, int max)
367 return (max > 0 ? random_linux_libc(nr) % max : 0);
371 /* ------------------------------------------------------------------------- */
372 /* system info functions */
373 /* ------------------------------------------------------------------------- */
375 #if !defined(PLATFORM_ANDROID)
376 static char *get_corrected_real_name(char *real_name)
378 char *real_name_new = checked_malloc(MAX_USERNAME_LEN + 1);
379 char *from_ptr = real_name;
380 char *to_ptr = real_name_new;
382 /* copy the name string, but not more than MAX_USERNAME_LEN characters */
383 while (*from_ptr && (int)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1)
385 /* the name field read from "passwd" file may also contain additional
386 user information, separated by commas, which will be removed here */
387 if (*from_ptr == ',')
390 /* the user's real name may contain 'german sharp s' characters,
391 which have no equivalent in upper case letters (used by our fonts) */
392 if (*from_ptr == CHAR_BYTE_SHARP_S)
399 *to_ptr++ = *from_ptr++;
404 return real_name_new;
410 static char *login_name = NULL;
412 #if defined(PLATFORM_WIN32)
413 if (login_name == NULL)
415 unsigned long buffer_size = MAX_USERNAME_LEN + 1;
416 login_name = checked_malloc(buffer_size);
418 if (GetUserName(login_name, &buffer_size) == 0)
419 strcpy(login_name, ANONYMOUS_NAME);
422 if (login_name == NULL)
426 if ((pwd = getpwuid(getuid())) == NULL)
427 login_name = ANONYMOUS_NAME;
429 login_name = getStringCopy(pwd->pw_name);
438 static char *real_name = NULL;
440 #if defined(PLATFORM_WIN32)
441 if (real_name == NULL)
443 static char buffer[MAX_USERNAME_LEN + 1];
444 unsigned long buffer_size = MAX_USERNAME_LEN + 1;
446 if (GetUserName(buffer, &buffer_size) != 0)
447 real_name = get_corrected_real_name(buffer);
449 real_name = ANONYMOUS_NAME;
451 #elif defined(PLATFORM_UNIX) && !defined(PLATFORM_ANDROID)
452 if (real_name == NULL)
456 if ((pwd = getpwuid(getuid())) != NULL && strlen(pwd->pw_gecos) != 0)
457 real_name = get_corrected_real_name(pwd->pw_gecos);
459 real_name = ANONYMOUS_NAME;
462 real_name = ANONYMOUS_NAME;
468 time_t getFileTimestampEpochSeconds(char *filename)
470 struct stat file_status;
472 if (stat(filename, &file_status) != 0) /* cannot stat file */
475 return file_status.st_mtime;
479 /* ------------------------------------------------------------------------- */
480 /* path manipulation functions */
481 /* ------------------------------------------------------------------------- */
483 static char *getLastPathSeparatorPtr(char *filename)
485 char *last_separator = strrchr(filename, CHAR_PATH_SEPARATOR_UNIX);
487 if (last_separator == NULL) /* also try DOS/Windows variant */
488 last_separator = strrchr(filename, CHAR_PATH_SEPARATOR_DOS);
490 return last_separator;
493 char *getBaseNamePtr(char *filename)
495 char *last_separator = getLastPathSeparatorPtr(filename);
497 if (last_separator != NULL)
498 return last_separator + 1; /* separator found: strip base path */
500 return filename; /* no separator found: filename has no path */
503 char *getBaseName(char *filename)
505 return getStringCopy(getBaseNamePtr(filename));
508 char *getBasePath(char *filename)
510 char *basepath = getStringCopy(filename);
511 char *last_separator = getLastPathSeparatorPtr(basepath);
513 if (last_separator != NULL)
514 *last_separator = '\0'; /* separator found: strip basename */
516 basepath = "."; /* no separator found: use current path */
521 static char *getProgramMainDataPath()
523 char *main_data_path = getStringCopy(program.command_basepath);
525 #if defined(PLATFORM_MACOSX)
526 static char *main_data_binary_subdir = NULL;
528 if (main_data_binary_subdir == NULL)
530 main_data_binary_subdir = checked_malloc(strlen(program.program_title) + 1 +
532 strlen(MAC_APP_BINARY_SUBDIR) + 1);
534 sprintf(main_data_binary_subdir, "%s.app/%s",
535 program.program_title, MAC_APP_BINARY_SUBDIR);
538 // cut relative path to Mac OS X application binary directory from path
539 if (strSuffix(main_data_path, main_data_binary_subdir))
540 main_data_path[strlen(main_data_path) -
541 strlen(main_data_binary_subdir)] = '\0';
543 // cut trailing path separator from path (but not if path is root directory)
544 if (strSuffix(main_data_path, "/") && !strEqual(main_data_path, "/"))
545 main_data_path[strlen(main_data_path) - 1] = '\0';
548 return main_data_path;
552 /* ------------------------------------------------------------------------- */
553 /* various string functions */
554 /* ------------------------------------------------------------------------- */
556 char *getStringCat2WithSeparator(char *s1, char *s2, char *sep)
558 char *complete_string = checked_malloc(strlen(s1) + strlen(sep) +
561 sprintf(complete_string, "%s%s%s", s1, sep, s2);
563 return complete_string;
566 char *getStringCat3WithSeparator(char *s1, char *s2, char *s3, char *sep)
568 char *complete_string = checked_malloc(strlen(s1) + strlen(sep) +
569 strlen(s2) + strlen(sep) +
572 sprintf(complete_string, "%s%s%s%s%s", s1, sep, s2, sep, s3);
574 return complete_string;
577 char *getStringCat2(char *s1, char *s2)
579 return getStringCat2WithSeparator(s1, s2, "");
582 char *getStringCat3(char *s1, char *s2, char *s3)
584 return getStringCat3WithSeparator(s1, s2, s3, "");
587 char *getPath2(char *path1, char *path2)
589 #if defined(PLATFORM_ANDROID)
590 // workaround for reading from APK assets directory -- skip leading "./"
591 if (strEqual(path1, "."))
592 return getStringCopy(path2);
595 return getStringCat2WithSeparator(path1, path2, STRING_PATH_SEPARATOR);
598 char *getPath3(char *path1, char *path2, char *path3)
600 #if defined(PLATFORM_ANDROID)
601 // workaround for reading from APK assets directory -- skip leading "./"
602 if (strEqual(path1, "."))
603 return getStringCat2WithSeparator(path2, path3, STRING_PATH_SEPARATOR);
606 return getStringCat3WithSeparator(path1, path2, path3, STRING_PATH_SEPARATOR);
609 char *getStringCopy(const char *s)
616 s_copy = checked_malloc(strlen(s) + 1);
622 char *getStringCopyN(const char *s, int n)
625 int s_len = MAX(0, n);
630 s_copy = checked_malloc(s_len + 1);
631 strncpy(s_copy, s, s_len);
632 s_copy[s_len] = '\0';
637 char *getStringCopyNStatic(const char *s, int n)
639 static char *s_copy = NULL;
641 checked_free(s_copy);
643 s_copy = getStringCopyN(s, n);
648 char *getStringToLower(const char *s)
650 char *s_copy = checked_malloc(strlen(s) + 1);
651 char *s_ptr = s_copy;
654 *s_ptr++ = tolower(*s++);
660 void setString(char **old_value, char *new_value)
662 checked_free(*old_value);
664 *old_value = getStringCopy(new_value);
667 boolean strEqual(char *s1, char *s2)
669 return (s1 == NULL && s2 == NULL ? TRUE :
670 s1 == NULL && s2 != NULL ? FALSE :
671 s1 != NULL && s2 == NULL ? FALSE :
672 strcmp(s1, s2) == 0);
675 boolean strEqualN(char *s1, char *s2, int n)
677 return (s1 == NULL && s2 == NULL ? TRUE :
678 s1 == NULL && s2 != NULL ? FALSE :
679 s1 != NULL && s2 == NULL ? FALSE :
680 strncmp(s1, s2, n) == 0);
683 boolean strPrefix(char *s, char *prefix)
685 return (s == NULL && prefix == NULL ? TRUE :
686 s == NULL && prefix != NULL ? FALSE :
687 s != NULL && prefix == NULL ? FALSE :
688 strncmp(s, prefix, strlen(prefix)) == 0);
691 boolean strSuffix(char *s, char *suffix)
693 return (s == NULL && suffix == NULL ? TRUE :
694 s == NULL && suffix != NULL ? FALSE :
695 s != NULL && suffix == NULL ? FALSE :
696 strlen(s) < strlen(suffix) ? FALSE :
697 strncmp(&s[strlen(s) - strlen(suffix)], suffix, strlen(suffix)) == 0);
700 boolean strPrefixLower(char *s, char *prefix)
702 char *s_lower = getStringToLower(s);
703 boolean match = strPrefix(s_lower, prefix);
710 boolean strSuffixLower(char *s, char *suffix)
712 char *s_lower = getStringToLower(s);
713 boolean match = strSuffix(s_lower, suffix);
721 /* ------------------------------------------------------------------------- */
722 /* command line option handling functions */
723 /* ------------------------------------------------------------------------- */
725 void GetOptions(char *argv[],
726 void (*print_usage_function)(void),
727 void (*print_version_function)(void))
729 char *ro_base_path = RO_BASE_PATH;
730 char *rw_base_path = RW_BASE_PATH;
731 char **options_left = &argv[1];
733 /* if the program is configured to start from current directory (default),
734 determine program package directory from program binary (some versions
735 of KDE/Konqueror and Mac OS X (especially "Mavericks") apparently do not
736 set the current working directory to the program package directory) */
738 if (strEqual(ro_base_path, "."))
739 ro_base_path = getProgramMainDataPath();
740 if (strEqual(rw_base_path, "."))
741 rw_base_path = getProgramMainDataPath();
743 /* initialize global program options */
744 options.display_name = NULL;
745 options.server_host = NULL;
746 options.server_port = 0;
748 options.ro_base_directory = ro_base_path;
749 options.rw_base_directory = rw_base_path;
750 options.level_directory = getPath2(ro_base_path, LEVELS_DIRECTORY);
751 options.graphics_directory = getPath2(ro_base_path, GRAPHICS_DIRECTORY);
752 options.sounds_directory = getPath2(ro_base_path, SOUNDS_DIRECTORY);
753 options.music_directory = getPath2(ro_base_path, MUSIC_DIRECTORY);
754 options.docs_directory = getPath2(ro_base_path, DOCS_DIRECTORY);
756 options.execute_command = NULL;
757 options.special_flags = NULL;
759 options.serveronly = FALSE;
760 options.network = FALSE;
761 options.verbose = FALSE;
762 options.debug = FALSE;
765 options.verbose = TRUE;
767 #if !defined(PLATFORM_UNIX)
768 if (*options_left == NULL) /* no options given -- enable verbose mode */
769 options.verbose = TRUE;
773 while (*options_left)
775 char option_str[MAX_OPTION_LEN];
776 char *option = options_left[0];
777 char *next_option = options_left[1];
778 char *option_arg = NULL;
779 int option_len = strlen(option);
781 if (option_len >= MAX_OPTION_LEN)
782 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
784 strcpy(option_str, option); /* copy argument into buffer */
787 if (strEqual(option, "--")) /* stop scanning arguments */
790 if (strPrefix(option, "--")) /* treat '--' like '-' */
793 option_arg = strchr(option, '=');
794 if (option_arg == NULL) /* no '=' in option */
795 option_arg = next_option;
798 *option_arg++ = '\0'; /* cut argument from option */
799 if (*option_arg == '\0') /* no argument after '=' */
800 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
803 option_len = strlen(option);
805 if (strEqual(option, "-"))
807 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
809 else if (strncmp(option, "-help", option_len) == 0)
811 print_usage_function();
815 else if (strncmp(option, "-display", option_len) == 0)
817 if (option_arg == NULL)
818 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
820 options.display_name = option_arg;
821 if (option_arg == next_option)
824 else if (strncmp(option, "-basepath", option_len) == 0)
826 if (option_arg == NULL)
827 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
829 /* this should be extended to separate options for ro and rw data */
830 options.ro_base_directory = ro_base_path = option_arg;
831 options.rw_base_directory = rw_base_path = option_arg;
832 if (option_arg == next_option)
835 /* adjust paths for sub-directories in base directory accordingly */
836 options.level_directory = getPath2(ro_base_path, LEVELS_DIRECTORY);
837 options.graphics_directory = getPath2(ro_base_path, GRAPHICS_DIRECTORY);
838 options.sounds_directory = getPath2(ro_base_path, SOUNDS_DIRECTORY);
839 options.music_directory = getPath2(ro_base_path, MUSIC_DIRECTORY);
840 options.docs_directory = getPath2(ro_base_path, DOCS_DIRECTORY);
842 else if (strncmp(option, "-levels", option_len) == 0)
844 if (option_arg == NULL)
845 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
847 options.level_directory = option_arg;
848 if (option_arg == next_option)
851 else if (strncmp(option, "-graphics", option_len) == 0)
853 if (option_arg == NULL)
854 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
856 options.graphics_directory = option_arg;
857 if (option_arg == next_option)
860 else if (strncmp(option, "-sounds", option_len) == 0)
862 if (option_arg == NULL)
863 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
865 options.sounds_directory = option_arg;
866 if (option_arg == next_option)
869 else if (strncmp(option, "-music", option_len) == 0)
871 if (option_arg == NULL)
872 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
874 options.music_directory = option_arg;
875 if (option_arg == next_option)
878 else if (strncmp(option, "-network", option_len) == 0)
880 options.network = TRUE;
882 else if (strncmp(option, "-serveronly", option_len) == 0)
884 options.serveronly = TRUE;
886 else if (strncmp(option, "-debug", option_len) == 0)
888 options.debug = TRUE;
890 else if (strncmp(option, "-verbose", option_len) == 0)
892 options.verbose = TRUE;
894 else if (strncmp(option, "-version", option_len) == 0 ||
895 strncmp(option, "-V", option_len) == 0)
897 print_version_function();
901 else if (strPrefix(option, "-D"))
903 options.special_flags = getStringCopy(&option[2]);
905 else if (strncmp(option, "-execute", option_len) == 0)
907 if (option_arg == NULL)
908 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
910 options.execute_command = option_arg;
911 if (option_arg == next_option)
914 /* when doing batch processing, always enable verbose mode (warnings) */
915 options.verbose = TRUE;
917 else if (*option == '-')
919 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
921 else if (options.server_host == NULL)
923 options.server_host = *options_left;
925 else if (options.server_port == 0)
927 options.server_port = atoi(*options_left);
928 if (options.server_port < 1024)
929 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
932 Error(ERR_EXIT_HELP, "too many arguments");
939 /* ------------------------------------------------------------------------- */
940 /* error handling functions */
941 /* ------------------------------------------------------------------------- */
943 #define MAX_INTERNAL_ERROR_SIZE 1024
945 /* used by SetError() and GetError() to store internal error messages */
946 static char internal_error[MAX_INTERNAL_ERROR_SIZE];
948 void SetError(char *format, ...)
952 va_start(ap, format);
953 vsnprintf(internal_error, MAX_INTERNAL_ERROR_SIZE, format, ap);
959 return internal_error;
962 void Error(int mode, char *format, ...)
964 static boolean last_line_was_separator = FALSE;
965 char *process_name = "";
967 #if defined(PLATFORM_ANDROID)
968 android_log_prio = (mode & ERR_DEBUG ? ANDROID_LOG_DEBUG :
969 mode & ERR_INFO ? ANDROID_LOG_INFO :
970 mode & ERR_WARN ? ANDROID_LOG_WARN :
971 mode & ERR_EXIT ? ANDROID_LOG_FATAL :
972 ANDROID_LOG_UNKNOWN);
975 /* display warnings only when running in verbose mode */
976 if (mode & ERR_WARN && !options.verbose)
979 if (mode == ERR_INFO_LINE)
981 if (!last_line_was_separator)
982 fprintf_line(program.error_file, format, 79);
984 last_line_was_separator = TRUE;
989 last_line_was_separator = FALSE;
991 if (mode & ERR_SOUND_SERVER)
992 process_name = " sound server";
993 else if (mode & ERR_NETWORK_SERVER)
994 process_name = " network server";
995 else if (mode & ERR_NETWORK_CLIENT)
996 process_name = " network client **";
1002 fprintf_nonewline(program.error_file, "%s%s: ", program.command_basename,
1005 if (mode & ERR_WARN)
1006 fprintf_nonewline(program.error_file, "warning: ");
1008 va_start(ap, format);
1009 vfprintf_newline(program.error_file, format, ap);
1012 if ((mode & ERR_EXIT) && !(mode & ERR_FROM_SERVER))
1014 va_start(ap, format);
1015 program.exit_message_function(format, ap);
1020 if (mode & ERR_HELP)
1021 fprintf_newline(program.error_file,
1022 "%s: Try option '--help' for more information.",
1023 program.command_basename);
1025 if (mode & ERR_EXIT)
1026 fprintf_newline(program.error_file, "%s%s: aborting",
1027 program.command_basename, process_name);
1029 if (mode & ERR_EXIT)
1031 if (mode & ERR_FROM_SERVER)
1032 exit(1); /* child process: normal exit */
1034 program.exit_function(1); /* main process: clean up stuff */
1039 /* ------------------------------------------------------------------------- */
1040 /* checked memory allocation and freeing functions */
1041 /* ------------------------------------------------------------------------- */
1043 void *checked_malloc(unsigned int size)
1050 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
1055 void *checked_calloc(unsigned int size)
1059 ptr = calloc(1, size);
1062 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
1067 void *checked_realloc(void *ptr, unsigned int size)
1069 ptr = realloc(ptr, size);
1072 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
1077 void checked_free(void *ptr)
1079 if (ptr != NULL) /* this check should be done by free() anyway */
1083 void clear_mem(void *ptr, unsigned int size)
1085 #if defined(PLATFORM_WIN32)
1086 /* for unknown reason, memset() sometimes crashes when compiled with MinGW */
1087 char *cptr = (char *)ptr;
1092 memset(ptr, 0, size);
1097 /* ------------------------------------------------------------------------- */
1098 /* various helper functions */
1099 /* ------------------------------------------------------------------------- */
1101 inline void swap_numbers(int *i1, int *i2)
1109 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
1121 /* the "put" variants of the following file access functions check for the file
1122 pointer being != NULL and return the number of bytes they have or would have
1123 written; this allows for chunk writing functions to first determine the size
1124 of the (not yet written) chunk, write the correct chunk size and finally
1125 write the chunk itself */
1127 int getFile8BitInteger(File *file)
1129 return getByteFromFile(file);
1132 int putFile8BitInteger(FILE *file, int value)
1140 int getFile16BitInteger(File *file, int byte_order)
1142 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1143 return ((getByteFromFile(file) << 8) |
1144 (getByteFromFile(file) << 0));
1145 else /* BYTE_ORDER_LITTLE_ENDIAN */
1146 return ((getByteFromFile(file) << 0) |
1147 (getByteFromFile(file) << 8));
1150 int putFile16BitInteger(FILE *file, int value, int byte_order)
1154 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1156 fputc((value >> 8) & 0xff, file);
1157 fputc((value >> 0) & 0xff, file);
1159 else /* BYTE_ORDER_LITTLE_ENDIAN */
1161 fputc((value >> 0) & 0xff, file);
1162 fputc((value >> 8) & 0xff, file);
1169 int getFile32BitInteger(File *file, int byte_order)
1171 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1172 return ((getByteFromFile(file) << 24) |
1173 (getByteFromFile(file) << 16) |
1174 (getByteFromFile(file) << 8) |
1175 (getByteFromFile(file) << 0));
1176 else /* BYTE_ORDER_LITTLE_ENDIAN */
1177 return ((getByteFromFile(file) << 0) |
1178 (getByteFromFile(file) << 8) |
1179 (getByteFromFile(file) << 16) |
1180 (getByteFromFile(file) << 24));
1183 int putFile32BitInteger(FILE *file, int value, int byte_order)
1187 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1189 fputc((value >> 24) & 0xff, file);
1190 fputc((value >> 16) & 0xff, file);
1191 fputc((value >> 8) & 0xff, file);
1192 fputc((value >> 0) & 0xff, file);
1194 else /* BYTE_ORDER_LITTLE_ENDIAN */
1196 fputc((value >> 0) & 0xff, file);
1197 fputc((value >> 8) & 0xff, file);
1198 fputc((value >> 16) & 0xff, file);
1199 fputc((value >> 24) & 0xff, file);
1206 boolean getFileChunk(File *file, char *chunk_name, int *chunk_size,
1209 const int chunk_name_length = 4;
1211 /* read chunk name */
1212 if (getStringFromFile(file, chunk_name, chunk_name_length + 1) == NULL)
1215 if (chunk_size != NULL)
1217 /* read chunk size */
1218 *chunk_size = getFile32BitInteger(file, byte_order);
1221 return (checkEndOfFile(file) ? FALSE : TRUE);
1224 int putFileChunk(FILE *file, char *chunk_name, int chunk_size,
1229 /* write chunk name */
1231 fputs(chunk_name, file);
1233 num_bytes += strlen(chunk_name);
1235 if (chunk_size >= 0)
1237 /* write chunk size */
1239 putFile32BitInteger(file, chunk_size, byte_order);
1247 int getFileVersion(File *file)
1249 int version_major = getByteFromFile(file);
1250 int version_minor = getByteFromFile(file);
1251 int version_patch = getByteFromFile(file);
1252 int version_build = getByteFromFile(file);
1254 return VERSION_IDENT(version_major, version_minor, version_patch,
1258 int putFileVersion(FILE *file, int version)
1262 int version_major = VERSION_MAJOR(version);
1263 int version_minor = VERSION_MINOR(version);
1264 int version_patch = VERSION_PATCH(version);
1265 int version_build = VERSION_BUILD(version);
1267 fputc(version_major, file);
1268 fputc(version_minor, file);
1269 fputc(version_patch, file);
1270 fputc(version_build, file);
1276 void ReadBytesFromFile(File *file, byte *buffer, unsigned int bytes)
1280 for (i = 0; i < bytes && !checkEndOfFile(file); i++)
1281 buffer[i] = getByteFromFile(file);
1284 void WriteBytesToFile(FILE *file, byte *buffer, unsigned int bytes)
1288 for(i = 0; i < bytes; i++)
1289 fputc(buffer[i], file);
1292 void ReadUnusedBytesFromFile(File *file, unsigned int bytes)
1294 while (bytes-- && !checkEndOfFile(file))
1295 getByteFromFile(file);
1298 void WriteUnusedBytesToFile(FILE *file, unsigned int bytes)
1305 /* ------------------------------------------------------------------------- */
1306 /* functions to translate key identifiers between different format */
1307 /* ------------------------------------------------------------------------- */
1309 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
1310 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
1311 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
1312 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
1314 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
1323 /* normal cursor keys */
1324 { KSYM_Left, "XK_Left", "cursor left" },
1325 { KSYM_Right, "XK_Right", "cursor right" },
1326 { KSYM_Up, "XK_Up", "cursor up" },
1327 { KSYM_Down, "XK_Down", "cursor down" },
1329 /* keypad cursor keys */
1331 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
1332 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
1333 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
1334 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
1337 /* other keypad keys */
1338 #ifdef KSYM_KP_Enter
1339 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
1340 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
1341 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
1342 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
1343 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
1344 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
1348 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
1349 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
1350 { KSYM_Control_L, "XK_Control_L", "left control" },
1351 { KSYM_Control_R, "XK_Control_R", "right control" },
1352 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
1353 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
1354 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
1355 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
1356 #if !defined(TARGET_SDL2)
1357 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
1358 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
1360 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
1361 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
1363 /* some special keys */
1364 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
1365 { KSYM_Delete, "XK_Delete", "delete" },
1366 { KSYM_Insert, "XK_Insert", "insert" },
1367 { KSYM_Tab, "XK_Tab", "tab" },
1368 { KSYM_Home, "XK_Home", "home" },
1369 { KSYM_End, "XK_End", "end" },
1370 { KSYM_Page_Up, "XK_Page_Up", "page up" },
1371 { KSYM_Page_Down, "XK_Page_Down", "page down" },
1373 #if defined(TARGET_SDL2)
1374 { KSYM_Menu, "XK_Menu", "menu" }, /* menu key */
1375 { KSYM_Back, "XK_Back", "back" }, /* back key */
1378 /* ASCII 0x20 to 0x40 keys (except numbers) */
1379 { KSYM_space, "XK_space", "space" },
1380 { KSYM_exclam, "XK_exclam", "!" },
1381 { KSYM_quotedbl, "XK_quotedbl", "\"" },
1382 { KSYM_numbersign, "XK_numbersign", "#" },
1383 { KSYM_dollar, "XK_dollar", "$" },
1384 { KSYM_percent, "XK_percent", "%" },
1385 { KSYM_ampersand, "XK_ampersand", "&" },
1386 { KSYM_apostrophe, "XK_apostrophe", "'" },
1387 { KSYM_parenleft, "XK_parenleft", "(" },
1388 { KSYM_parenright, "XK_parenright", ")" },
1389 { KSYM_asterisk, "XK_asterisk", "*" },
1390 { KSYM_plus, "XK_plus", "+" },
1391 { KSYM_comma, "XK_comma", "," },
1392 { KSYM_minus, "XK_minus", "-" },
1393 { KSYM_period, "XK_period", "." },
1394 { KSYM_slash, "XK_slash", "/" },
1395 { KSYM_colon, "XK_colon", ":" },
1396 { KSYM_semicolon, "XK_semicolon", ";" },
1397 { KSYM_less, "XK_less", "<" },
1398 { KSYM_equal, "XK_equal", "=" },
1399 { KSYM_greater, "XK_greater", ">" },
1400 { KSYM_question, "XK_question", "?" },
1401 { KSYM_at, "XK_at", "@" },
1403 /* more ASCII keys */
1404 { KSYM_bracketleft, "XK_bracketleft", "[" },
1405 { KSYM_backslash, "XK_backslash", "\\" },
1406 { KSYM_bracketright,"XK_bracketright", "]" },
1407 { KSYM_asciicircum, "XK_asciicircum", "^" },
1408 { KSYM_underscore, "XK_underscore", "_" },
1409 { KSYM_grave, "XK_grave", "grave" },
1410 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
1411 { KSYM_braceleft, "XK_braceleft", "brace left" },
1412 { KSYM_bar, "XK_bar", "bar" },
1413 { KSYM_braceright, "XK_braceright", "brace right" },
1414 { KSYM_asciitilde, "XK_asciitilde", "~" },
1416 /* special (non-ASCII) keys */
1417 { KSYM_degree, "XK_degree", "degree" },
1418 { KSYM_Adiaeresis, "XK_Adiaeresis", "A umlaut" },
1419 { KSYM_Odiaeresis, "XK_Odiaeresis", "O umlaut" },
1420 { KSYM_Udiaeresis, "XK_Udiaeresis", "U umlaut" },
1421 { KSYM_adiaeresis, "XK_adiaeresis", "a umlaut" },
1422 { KSYM_odiaeresis, "XK_odiaeresis", "o umlaut" },
1423 { KSYM_udiaeresis, "XK_udiaeresis", "u umlaut" },
1424 { KSYM_ssharp, "XK_ssharp", "sharp s" },
1426 #if defined(TARGET_SDL2)
1427 /* special (non-ASCII) keys (UTF-8, for reverse mapping only) */
1428 { KSYM_degree, "XK_degree", "\xc2\xb0" },
1429 { KSYM_Adiaeresis, "XK_Adiaeresis", "\xc3\x84" },
1430 { KSYM_Odiaeresis, "XK_Odiaeresis", "\xc3\x96" },
1431 { KSYM_Udiaeresis, "XK_Udiaeresis", "\xc3\x9c" },
1432 { KSYM_adiaeresis, "XK_adiaeresis", "\xc3\xa4" },
1433 { KSYM_odiaeresis, "XK_odiaeresis", "\xc3\xb6" },
1434 { KSYM_udiaeresis, "XK_udiaeresis", "\xc3\xbc" },
1435 { KSYM_ssharp, "XK_ssharp", "\xc3\x9f" },
1437 /* other keys (for reverse mapping only) */
1438 { KSYM_space, "XK_space", " " },
1441 #if defined(TARGET_SDL2)
1442 /* keypad keys are not in numerical order in SDL2 */
1443 { KSYM_KP_0, "XK_KP_0", "keypad 0" },
1444 { KSYM_KP_1, "XK_KP_1", "keypad 1" },
1445 { KSYM_KP_2, "XK_KP_2", "keypad 2" },
1446 { KSYM_KP_3, "XK_KP_3", "keypad 3" },
1447 { KSYM_KP_4, "XK_KP_4", "keypad 4" },
1448 { KSYM_KP_5, "XK_KP_5", "keypad 5" },
1449 { KSYM_KP_6, "XK_KP_6", "keypad 6" },
1450 { KSYM_KP_7, "XK_KP_7", "keypad 7" },
1451 { KSYM_KP_8, "XK_KP_8", "keypad 8" },
1452 { KSYM_KP_9, "XK_KP_9", "keypad 9" },
1455 /* end-of-array identifier */
1461 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
1463 static char name_buffer[30];
1466 if (key >= KSYM_A && key <= KSYM_Z)
1467 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
1468 else if (key >= KSYM_a && key <= KSYM_z)
1469 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
1470 else if (key >= KSYM_0 && key <= KSYM_9)
1471 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
1472 #if !defined(TARGET_SDL2)
1473 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1474 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
1476 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1477 sprintf(name_buffer, "F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1478 else if (key == KSYM_UNDEFINED)
1479 strcpy(name_buffer, "(undefined)");
1486 if (key == translate_key[i].key)
1488 strcpy(name_buffer, translate_key[i].name);
1492 while (translate_key[++i].name);
1494 if (!translate_key[i].name)
1495 strcpy(name_buffer, "(unknown)");
1498 *name = name_buffer;
1500 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
1502 static char name_buffer[30];
1505 if (key >= KSYM_A && key <= KSYM_Z)
1506 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
1507 else if (key >= KSYM_a && key <= KSYM_z)
1508 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
1509 else if (key >= KSYM_0 && key <= KSYM_9)
1510 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
1511 #if !defined(TARGET_SDL2)
1512 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1513 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
1515 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1516 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1517 else if (key == KSYM_UNDEFINED)
1518 strcpy(name_buffer, "[undefined]");
1525 if (key == translate_key[i].key)
1527 strcpy(name_buffer, translate_key[i].x11name);
1531 while (translate_key[++i].x11name);
1533 if (!translate_key[i].x11name)
1534 sprintf(name_buffer, "0x%04x", (unsigned int)key);
1537 *x11name = name_buffer;
1539 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1541 Key key = KSYM_UNDEFINED;
1542 char *name_ptr = *name;
1544 if (strlen(*name) == 1)
1546 char c = name_ptr[0];
1548 if (c >= 'A' && c <= 'Z')
1549 key = KSYM_A + (Key)(c - 'A');
1550 else if (c >= 'a' && c <= 'z')
1551 key = KSYM_a + (Key)(c - 'a');
1552 else if (c >= '0' && c <= '9')
1553 key = KSYM_0 + (Key)(c - '0');
1556 if (key == KSYM_UNDEFINED)
1562 if (strEqual(translate_key[i].name, *name))
1564 key = translate_key[i].key;
1568 while (translate_key[++i].x11name);
1571 if (key == KSYM_UNDEFINED)
1572 Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
1576 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1578 Key key = KSYM_UNDEFINED;
1579 char *name_ptr = *x11name;
1581 if (strPrefix(name_ptr, "XK_") && strlen(name_ptr) == 4)
1583 char c = name_ptr[3];
1585 if (c >= 'A' && c <= 'Z')
1586 key = KSYM_A + (Key)(c - 'A');
1587 else if (c >= 'a' && c <= 'z')
1588 key = KSYM_a + (Key)(c - 'a');
1589 else if (c >= '0' && c <= '9')
1590 key = KSYM_0 + (Key)(c - '0');
1592 #if !defined(TARGET_SDL2)
1593 else if (strPrefix(name_ptr, "XK_KP_") && strlen(name_ptr) == 7)
1595 char c = name_ptr[6];
1597 if (c >= '0' && c <= '9')
1598 key = KSYM_KP_0 + (Key)(c - '0');
1601 else if (strPrefix(name_ptr, "XK_F") && strlen(name_ptr) <= 6)
1603 char c1 = name_ptr[4];
1604 char c2 = name_ptr[5];
1607 if ((c1 >= '0' && c1 <= '9') &&
1608 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1609 d = atoi(&name_ptr[4]);
1611 if (d >= 1 && d <= KSYM_NUM_FKEYS)
1612 key = KSYM_F1 + (Key)(d - 1);
1614 else if (strPrefix(name_ptr, "XK_"))
1620 if (strEqual(name_ptr, translate_key[i].x11name))
1622 key = translate_key[i].key;
1626 while (translate_key[++i].x11name);
1628 else if (strPrefix(name_ptr, "0x"))
1630 unsigned int value = 0;
1636 char c = *name_ptr++;
1639 if (c >= '0' && c <= '9')
1641 else if (c >= 'a' && c <= 'f')
1642 d = (int)(c - 'a' + 10);
1643 else if (c >= 'A' && c <= 'F')
1644 d = (int)(c - 'A' + 10);
1652 value = value * 16 + d;
1663 char *getKeyNameFromKey(Key key)
1667 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1671 char *getX11KeyNameFromKey(Key key)
1675 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1679 Key getKeyFromKeyName(char *name)
1683 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1687 Key getKeyFromX11KeyName(char *x11name)
1691 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1695 char getCharFromKey(Key key)
1701 } translate_key_char[] =
1703 /* special (non-ASCII) keys (ISO-8859-1) */
1704 { KSYM_degree, CHAR_BYTE_DEGREE },
1705 { KSYM_Adiaeresis, CHAR_BYTE_UMLAUT_A },
1706 { KSYM_Odiaeresis, CHAR_BYTE_UMLAUT_O },
1707 { KSYM_Udiaeresis, CHAR_BYTE_UMLAUT_U },
1708 { KSYM_adiaeresis, CHAR_BYTE_UMLAUT_a },
1709 { KSYM_odiaeresis, CHAR_BYTE_UMLAUT_o },
1710 { KSYM_udiaeresis, CHAR_BYTE_UMLAUT_u },
1711 { KSYM_ssharp, CHAR_BYTE_SHARP_S },
1713 /* end-of-array identifier */
1717 char *keyname = getKeyNameFromKey(key);
1720 if (strlen(keyname) == 1)
1722 else if (strEqual(keyname, "space"))
1730 if (key == translate_key_char[i].key)
1732 c = translate_key_char[i].key_char;
1737 while (translate_key_char[++i].key_char);
1743 char getValidConfigValueChar(char c)
1745 if (c == '#' || /* used to mark comments */
1746 c == '\\') /* used to mark continued lines */
1753 /* ------------------------------------------------------------------------- */
1754 /* functions to translate string identifiers to integer or boolean value */
1755 /* ------------------------------------------------------------------------- */
1757 int get_integer_from_string(char *s)
1759 static char *number_text[][3] =
1761 { "0", "zero", "null", },
1762 { "1", "one", "first" },
1763 { "2", "two", "second" },
1764 { "3", "three", "third" },
1765 { "4", "four", "fourth" },
1766 { "5", "five", "fifth" },
1767 { "6", "six", "sixth" },
1768 { "7", "seven", "seventh" },
1769 { "8", "eight", "eighth" },
1770 { "9", "nine", "ninth" },
1771 { "10", "ten", "tenth" },
1772 { "11", "eleven", "eleventh" },
1773 { "12", "twelve", "twelfth" },
1775 { NULL, NULL, NULL },
1779 char *s_lower = getStringToLower(s);
1782 for (i = 0; number_text[i][0] != NULL; i++)
1783 for (j = 0; j < 3; j++)
1784 if (strEqual(s_lower, number_text[i][j]))
1789 if (strEqual(s_lower, "false") ||
1790 strEqual(s_lower, "no") ||
1791 strEqual(s_lower, "off"))
1793 else if (strEqual(s_lower, "true") ||
1794 strEqual(s_lower, "yes") ||
1795 strEqual(s_lower, "on"))
1806 boolean get_boolean_from_string(char *s)
1808 char *s_lower = getStringToLower(s);
1809 boolean result = FALSE;
1811 if (strEqual(s_lower, "true") ||
1812 strEqual(s_lower, "yes") ||
1813 strEqual(s_lower, "on") ||
1814 get_integer_from_string(s) == 1)
1822 int get_switch3_from_string(char *s)
1824 char *s_lower = getStringToLower(s);
1827 if (strEqual(s_lower, "true") ||
1828 strEqual(s_lower, "yes") ||
1829 strEqual(s_lower, "on") ||
1830 get_integer_from_string(s) == 1)
1832 else if (strEqual(s_lower, "auto"))
1841 /* ------------------------------------------------------------------------- */
1842 /* functions for generic lists */
1843 /* ------------------------------------------------------------------------- */
1845 ListNode *newListNode()
1847 return checked_calloc(sizeof(ListNode));
1850 void addNodeToList(ListNode **node_first, char *key, void *content)
1852 ListNode *node_new = newListNode();
1854 node_new->key = getStringCopy(key);
1855 node_new->content = content;
1856 node_new->next = *node_first;
1857 *node_first = node_new;
1860 void deleteNodeFromList(ListNode **node_first, char *key,
1861 void (*destructor_function)(void *))
1863 if (node_first == NULL || *node_first == NULL)
1866 if (strEqual((*node_first)->key, key))
1868 checked_free((*node_first)->key);
1869 if (destructor_function)
1870 destructor_function((*node_first)->content);
1871 *node_first = (*node_first)->next;
1874 deleteNodeFromList(&(*node_first)->next, key, destructor_function);
1877 ListNode *getNodeFromKey(ListNode *node_first, char *key)
1879 if (node_first == NULL)
1882 if (strEqual(node_first->key, key))
1885 return getNodeFromKey(node_first->next, key);
1888 int getNumNodes(ListNode *node_first)
1890 return (node_first ? 1 + getNumNodes(node_first->next) : 0);
1893 void dumpList(ListNode *node_first)
1895 ListNode *node = node_first;
1899 printf("['%s' (%d)]\n", node->key,
1900 ((struct ListNodeInfo *)node->content)->num_references);
1904 printf("[%d nodes]\n", getNumNodes(node_first));
1908 /* ------------------------------------------------------------------------- */
1909 /* functions for file handling */
1910 /* ------------------------------------------------------------------------- */
1912 File *openFile(char *filename, char *mode)
1914 File *file = checked_calloc(sizeof(File));
1916 file->file = fopen(filename, mode);
1918 if (file->file != NULL)
1920 file->filename = getStringCopy(filename);
1925 #if defined(PLATFORM_ANDROID)
1926 file->asset_file = SDL_RWFromFile(filename, mode);
1928 if (file->asset_file != NULL)
1930 file->file_is_asset = TRUE;
1931 file->filename = getStringCopy(filename);
1942 int closeFile(File *file)
1949 #if defined(PLATFORM_ANDROID)
1950 if (file->asset_file)
1951 result = SDL_RWclose(file->asset_file);
1955 result = fclose(file->file);
1957 checked_free(file->filename);
1963 int checkEndOfFile(File *file)
1965 #if defined(PLATFORM_ANDROID)
1966 if (file->file_is_asset)
1967 return file->end_of_file;
1970 return feof(file->file);
1973 size_t readFile(File *file, void *buffer, size_t item_size, size_t num_items)
1975 #if defined(PLATFORM_ANDROID)
1976 if (file->file_is_asset)
1978 if (file->end_of_file)
1981 size_t num_items_read =
1982 SDL_RWread(file->asset_file, buffer, item_size, num_items);
1984 if (num_items_read < num_items)
1985 file->end_of_file = TRUE;
1987 return num_items_read;
1991 return fread(buffer, item_size, num_items, file->file);
1994 int seekFile(File *file, long offset, int whence)
1996 #if defined(PLATFORM_ANDROID)
1997 if (file->file_is_asset)
1999 int sdl_whence = (whence == SEEK_SET ? RW_SEEK_SET :
2000 whence == SEEK_CUR ? RW_SEEK_CUR :
2001 whence == SEEK_END ? RW_SEEK_END : 0);
2003 return (SDL_RWseek(file->asset_file, offset, sdl_whence) == -1 ? -1 : 0);
2007 return fseek(file->file, offset, whence);
2010 int getByteFromFile(File *file)
2012 #if defined(PLATFORM_ANDROID)
2013 if (file->file_is_asset)
2015 if (file->end_of_file)
2019 size_t num_bytes_read = SDL_RWread(file->asset_file, &c, 1, 1);
2021 if (num_bytes_read < 1)
2022 file->end_of_file = TRUE;
2024 return (file->end_of_file ? EOF : (int)c);
2028 return fgetc(file->file);
2031 char *getStringFromFile(File *file, char *line, int size)
2033 #if defined(PLATFORM_ANDROID)
2034 if (file->file_is_asset)
2036 if (file->end_of_file)
2039 char *line_ptr = line;
2040 int num_bytes_read = 0;
2042 while (num_bytes_read < size - 1 &&
2043 SDL_RWread(file->asset_file, line_ptr, 1, 1) == 1 &&
2044 *line_ptr++ != '\n')
2049 if (strlen(line) == 0)
2051 file->end_of_file = TRUE;
2060 return fgets(line, size, file->file);
2064 /* ------------------------------------------------------------------------- */
2065 /* functions for directory handling */
2066 /* ------------------------------------------------------------------------- */
2068 Directory *openDirectory(char *dir_name)
2070 Directory *dir = checked_calloc(sizeof(Directory));
2072 dir->dir = opendir(dir_name);
2074 if (dir->dir != NULL)
2076 dir->filename = getStringCopy(dir_name);
2081 #if defined(PLATFORM_ANDROID)
2082 char *asset_toc_filename = getPath2(dir_name, ASSET_TOC_BASENAME);
2084 dir->asset_toc_file = SDL_RWFromFile(asset_toc_filename, MODE_READ);
2086 checked_free(asset_toc_filename);
2088 if (dir->asset_toc_file != NULL)
2090 dir->directory_is_asset = TRUE;
2091 dir->filename = getStringCopy(dir_name);
2102 int closeDirectory(Directory *dir)
2109 #if defined(PLATFORM_ANDROID)
2110 if (dir->asset_toc_file)
2111 result = SDL_RWclose(dir->asset_toc_file);
2115 result = closedir(dir->dir);
2118 freeDirectoryEntry(dir->dir_entry);
2120 checked_free(dir->filename);
2126 DirectoryEntry *readDirectory(Directory *dir)
2129 freeDirectoryEntry(dir->dir_entry);
2131 dir->dir_entry = NULL;
2133 #if defined(PLATFORM_ANDROID)
2134 if (dir->directory_is_asset)
2136 char line[MAX_LINE_LEN];
2137 char *line_ptr = line;
2138 int num_bytes_read = 0;
2140 while (num_bytes_read < MAX_LINE_LEN - 1 &&
2141 SDL_RWread(dir->asset_toc_file, line_ptr, 1, 1) == 1 &&
2150 if (strlen(line) == 0)
2153 dir->dir_entry = checked_calloc(sizeof(DirectoryEntry));
2155 dir->dir_entry->is_directory = FALSE;
2156 if (line[strlen(line) - 1] == '/')
2158 dir->dir_entry->is_directory = TRUE;
2160 line[strlen(line) - 1] = '\0';
2163 dir->dir_entry->basename = getStringCopy(line);
2164 dir->dir_entry->filename = getPath2(dir->filename, line);
2166 return dir->dir_entry;
2170 struct dirent *dir_entry = readdir(dir->dir);
2172 if (dir_entry == NULL)
2175 dir->dir_entry = checked_calloc(sizeof(DirectoryEntry));
2177 dir->dir_entry->basename = getStringCopy(dir_entry->d_name);
2178 dir->dir_entry->filename = getPath2(dir->filename, dir_entry->d_name);
2180 struct stat file_status;
2182 dir->dir_entry->is_directory =
2183 (stat(dir->dir_entry->filename, &file_status) == 0 &&
2184 (file_status.st_mode & S_IFMT) == S_IFDIR);
2186 return dir->dir_entry;
2189 void freeDirectoryEntry(DirectoryEntry *dir_entry)
2191 if (dir_entry == NULL)
2194 checked_free(dir_entry->basename);
2195 checked_free(dir_entry->filename);
2196 checked_free(dir_entry);
2200 /* ------------------------------------------------------------------------- */
2201 /* functions for checking files and filenames */
2202 /* ------------------------------------------------------------------------- */
2204 boolean directoryExists(char *dir_name)
2206 if (dir_name == NULL)
2209 boolean success = (access(dir_name, F_OK) == 0);
2211 #if defined(PLATFORM_ANDROID)
2214 // this might be an asset directory; check by trying to open toc file
2215 char *asset_toc_filename = getPath2(dir_name, ASSET_TOC_BASENAME);
2216 SDL_RWops *file = SDL_RWFromFile(asset_toc_filename, MODE_READ);
2218 checked_free(asset_toc_filename);
2220 success = (file != NULL);
2230 boolean fileExists(char *filename)
2232 if (filename == NULL)
2235 boolean success = (access(filename, F_OK) == 0);
2237 #if defined(PLATFORM_ANDROID)
2240 // this might be an asset file; check by trying to open it
2241 SDL_RWops *file = SDL_RWFromFile(filename, MODE_READ);
2243 success = (file != NULL);
2253 boolean fileHasPrefix(char *basename, char *prefix)
2255 static char *basename_lower = NULL;
2256 int basename_length, prefix_length;
2258 checked_free(basename_lower);
2260 if (basename == NULL || prefix == NULL)
2263 basename_lower = getStringToLower(basename);
2264 basename_length = strlen(basename_lower);
2265 prefix_length = strlen(prefix);
2267 if (basename_length > prefix_length + 1 &&
2268 basename_lower[prefix_length] == '.' &&
2269 strncmp(basename_lower, prefix, prefix_length) == 0)
2275 boolean fileHasSuffix(char *basename, char *suffix)
2277 static char *basename_lower = NULL;
2278 int basename_length, suffix_length;
2280 checked_free(basename_lower);
2282 if (basename == NULL || suffix == NULL)
2285 basename_lower = getStringToLower(basename);
2286 basename_length = strlen(basename_lower);
2287 suffix_length = strlen(suffix);
2289 if (basename_length > suffix_length + 1 &&
2290 basename_lower[basename_length - suffix_length - 1] == '.' &&
2291 strEqual(&basename_lower[basename_length - suffix_length], suffix))
2297 static boolean FileCouldBeArtwork(char *basename)
2299 return (!strEqual(basename, ".") &&
2300 !strEqual(basename, "..") &&
2301 !fileHasSuffix(basename, "txt") &&
2302 !fileHasSuffix(basename, "conf"));
2305 boolean FileIsGraphic(char *filename)
2307 char *basename = getBaseNamePtr(filename);
2309 return FileCouldBeArtwork(basename);
2312 boolean FileIsSound(char *filename)
2314 char *basename = getBaseNamePtr(filename);
2316 return FileCouldBeArtwork(basename);
2319 boolean FileIsMusic(char *filename)
2321 char *basename = getBaseNamePtr(filename);
2323 return FileCouldBeArtwork(basename);
2326 boolean FileIsArtworkType(char *basename, int type)
2328 if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
2329 (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
2330 (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
2336 /* ------------------------------------------------------------------------- */
2337 /* functions for loading artwork configuration information */
2338 /* ------------------------------------------------------------------------- */
2340 char *get_mapped_token(char *token)
2342 /* !!! make this dynamically configurable (init.c:InitArtworkConfig) !!! */
2343 static char *map_token_prefix[][2] =
2345 { "char_procent", "char_percent" },
2350 for (i = 0; map_token_prefix[i][0] != NULL; i++)
2352 int len_token_prefix = strlen(map_token_prefix[i][0]);
2354 if (strncmp(token, map_token_prefix[i][0], len_token_prefix) == 0)
2355 return getStringCat2(map_token_prefix[i][1], &token[len_token_prefix]);
2361 /* This function checks if a string <s> of the format "string1, string2, ..."
2362 exactly contains a string <s_contained>. */
2364 static boolean string_has_parameter(char *s, char *s_contained)
2368 if (s == NULL || s_contained == NULL)
2371 if (strlen(s_contained) > strlen(s))
2374 if (strncmp(s, s_contained, strlen(s_contained)) == 0)
2376 char next_char = s[strlen(s_contained)];
2378 /* check if next character is delimiter or whitespace */
2379 return (next_char == ',' || next_char == '\0' ||
2380 next_char == ' ' || next_char == '\t' ? TRUE : FALSE);
2383 /* check if string contains another parameter string after a comma */
2384 substring = strchr(s, ',');
2385 if (substring == NULL) /* string does not contain a comma */
2388 /* advance string pointer to next character after the comma */
2391 /* skip potential whitespaces after the comma */
2392 while (*substring == ' ' || *substring == '\t')
2395 return string_has_parameter(substring, s_contained);
2398 int get_parameter_value(char *value_raw, char *suffix, int type)
2400 char *value = getStringToLower(value_raw);
2401 int result = 0; /* probably a save default value */
2403 if (strEqual(suffix, ".direction"))
2405 result = (strEqual(value, "left") ? MV_LEFT :
2406 strEqual(value, "right") ? MV_RIGHT :
2407 strEqual(value, "up") ? MV_UP :
2408 strEqual(value, "down") ? MV_DOWN : MV_NONE);
2410 else if (strEqual(suffix, ".align"))
2412 result = (strEqual(value, "left") ? ALIGN_LEFT :
2413 strEqual(value, "right") ? ALIGN_RIGHT :
2414 strEqual(value, "center") ? ALIGN_CENTER :
2415 strEqual(value, "middle") ? ALIGN_CENTER : ALIGN_DEFAULT);
2417 else if (strEqual(suffix, ".valign"))
2419 result = (strEqual(value, "top") ? VALIGN_TOP :
2420 strEqual(value, "bottom") ? VALIGN_BOTTOM :
2421 strEqual(value, "middle") ? VALIGN_MIDDLE :
2422 strEqual(value, "center") ? VALIGN_MIDDLE : VALIGN_DEFAULT);
2424 else if (strEqual(suffix, ".anim_mode"))
2426 result = (string_has_parameter(value, "none") ? ANIM_NONE :
2427 string_has_parameter(value, "loop") ? ANIM_LOOP :
2428 string_has_parameter(value, "linear") ? ANIM_LINEAR :
2429 string_has_parameter(value, "pingpong") ? ANIM_PINGPONG :
2430 string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 :
2431 string_has_parameter(value, "random") ? ANIM_RANDOM :
2432 string_has_parameter(value, "ce_value") ? ANIM_CE_VALUE :
2433 string_has_parameter(value, "ce_score") ? ANIM_CE_SCORE :
2434 string_has_parameter(value, "ce_delay") ? ANIM_CE_DELAY :
2435 string_has_parameter(value, "horizontal") ? ANIM_HORIZONTAL :
2436 string_has_parameter(value, "vertical") ? ANIM_VERTICAL :
2437 string_has_parameter(value, "centered") ? ANIM_CENTERED :
2440 if (string_has_parameter(value, "reverse"))
2441 result |= ANIM_REVERSE;
2443 if (string_has_parameter(value, "opaque_player"))
2444 result |= ANIM_OPAQUE_PLAYER;
2446 if (string_has_parameter(value, "static_panel"))
2447 result |= ANIM_STATIC_PANEL;
2449 else if (strEqual(suffix, ".class"))
2451 result = get_hash_from_key(value);
2453 else if (strEqual(suffix, ".style"))
2455 result = STYLE_DEFAULT;
2457 if (string_has_parameter(value, "accurate_borders"))
2458 result |= STYLE_ACCURATE_BORDERS;
2460 if (string_has_parameter(value, "inner_corners"))
2461 result |= STYLE_INNER_CORNERS;
2463 else if (strEqual(suffix, ".fade_mode"))
2465 result = (string_has_parameter(value, "none") ? FADE_MODE_NONE :
2466 string_has_parameter(value, "fade") ? FADE_MODE_FADE :
2467 string_has_parameter(value, "crossfade") ? FADE_MODE_CROSSFADE :
2468 string_has_parameter(value, "melt") ? FADE_MODE_MELT :
2471 else if (strPrefix(suffix, ".font")) /* (may also be ".font_xyz") */
2473 result = gfx.get_font_from_token_function(value);
2475 else /* generic parameter of type integer or boolean */
2477 result = (strEqual(value, ARG_UNDEFINED) ? ARG_UNDEFINED_VALUE :
2478 type == TYPE_INTEGER ? get_integer_from_string(value) :
2479 type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
2480 ARG_UNDEFINED_VALUE);
2488 struct ScreenModeInfo *get_screen_mode_from_string(char *screen_mode_string)
2490 static struct ScreenModeInfo screen_mode;
2491 char *screen_mode_string_x = strchr(screen_mode_string, 'x');
2492 char *screen_mode_string_copy;
2493 char *screen_mode_string_pos_w;
2494 char *screen_mode_string_pos_h;
2496 if (screen_mode_string_x == NULL) /* invalid screen mode format */
2499 screen_mode_string_copy = getStringCopy(screen_mode_string);
2501 screen_mode_string_pos_w = screen_mode_string_copy;
2502 screen_mode_string_pos_h = strchr(screen_mode_string_copy, 'x');
2503 *screen_mode_string_pos_h++ = '\0';
2505 screen_mode.width = atoi(screen_mode_string_pos_w);
2506 screen_mode.height = atoi(screen_mode_string_pos_h);
2508 return &screen_mode;
2511 void get_aspect_ratio_from_screen_mode(struct ScreenModeInfo *screen_mode,
2514 float aspect_ratio = (float)screen_mode->width / (float)screen_mode->height;
2515 float aspect_ratio_new;
2520 *x = i * aspect_ratio + 0.000001;
2523 aspect_ratio_new = (float)*x / (float)*y;
2527 while (aspect_ratio_new != aspect_ratio && *y < screen_mode->height);
2530 static void FreeCustomArtworkList(struct ArtworkListInfo *,
2531 struct ListNodeInfo ***, int *);
2533 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
2534 struct ConfigTypeInfo *suffix_list,
2535 char **ignore_tokens,
2536 int num_file_list_entries)
2538 struct FileInfo *file_list;
2539 int num_file_list_entries_found = 0;
2540 int num_suffix_list_entries = 0;
2544 file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo));
2546 for (i = 0; suffix_list[i].token != NULL; i++)
2547 num_suffix_list_entries++;
2549 /* always start with reliable default values */
2550 for (i = 0; i < num_file_list_entries; i++)
2552 file_list[i].token = NULL;
2554 file_list[i].default_filename = NULL;
2555 file_list[i].filename = NULL;
2557 if (num_suffix_list_entries > 0)
2559 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
2561 file_list[i].default_parameter = checked_calloc(parameter_array_size);
2562 file_list[i].parameter = checked_calloc(parameter_array_size);
2564 for (j = 0; j < num_suffix_list_entries; j++)
2566 setString(&file_list[i].default_parameter[j], suffix_list[j].value);
2567 setString(&file_list[i].parameter[j], suffix_list[j].value);
2570 file_list[i].redefined = FALSE;
2571 file_list[i].fallback_to_default = FALSE;
2572 file_list[i].default_is_cloned = FALSE;
2578 for (i = 0; config_list[i].token != NULL; i++)
2580 int len_config_token = strlen(config_list[i].token);
2581 boolean is_file_entry = TRUE;
2583 for (j = 0; suffix_list[j].token != NULL; j++)
2585 int len_suffix = strlen(suffix_list[j].token);
2587 if (len_suffix < len_config_token &&
2588 strEqual(&config_list[i].token[len_config_token - len_suffix],
2589 suffix_list[j].token))
2591 setString(&file_list[list_pos].default_parameter[j],
2592 config_list[i].value);
2594 is_file_entry = FALSE;
2600 /* the following tokens are no file definitions, but other config tokens */
2601 for (j = 0; ignore_tokens[j] != NULL; j++)
2602 if (strEqual(config_list[i].token, ignore_tokens[j]))
2603 is_file_entry = FALSE;
2610 if (list_pos >= num_file_list_entries)
2613 file_list[list_pos].token = config_list[i].token;
2614 file_list[list_pos].default_filename = config_list[i].value;
2617 if (strSuffix(config_list[i].token, ".clone_from"))
2618 file_list[list_pos].default_is_cloned = TRUE;
2621 num_file_list_entries_found = list_pos + 1;
2622 if (num_file_list_entries_found != num_file_list_entries)
2624 Error(ERR_INFO_LINE, "-");
2625 Error(ERR_INFO, "inconsistant config list information:");
2626 Error(ERR_INFO, "- should be: %d (according to 'src/conf_xxx.h')",
2627 num_file_list_entries);
2628 Error(ERR_INFO, "- found to be: %d (according to 'src/conf_xxx.c')",
2629 num_file_list_entries_found);
2630 Error(ERR_EXIT, "please fix");
2636 static boolean token_suffix_match(char *token, char *suffix, int start_pos)
2638 int len_token = strlen(token);
2639 int len_suffix = strlen(suffix);
2641 if (start_pos < 0) /* compare suffix from end of string */
2642 start_pos += len_token;
2644 if (start_pos < 0 || start_pos + len_suffix > len_token)
2647 if (strncmp(&token[start_pos], suffix, len_suffix) != 0)
2650 if (token[start_pos + len_suffix] == '\0')
2653 if (token[start_pos + len_suffix] == '.')
2659 #define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN_VALUE]"
2661 static void read_token_parameters(SetupFileHash *setup_file_hash,
2662 struct ConfigTypeInfo *suffix_list,
2663 struct FileInfo *file_list_entry)
2665 /* check for config token that is the base token without any suffixes */
2666 char *filename = getHashEntry(setup_file_hash, file_list_entry->token);
2667 char *known_token_value = KNOWN_TOKEN_VALUE;
2670 if (filename != NULL)
2672 setString(&file_list_entry->filename, filename);
2674 /* when file definition found, set all parameters to default values */
2675 for (i = 0; suffix_list[i].token != NULL; i++)
2676 setString(&file_list_entry->parameter[i], suffix_list[i].value);
2678 file_list_entry->redefined = TRUE;
2680 /* mark config file token as well known from default config */
2681 setHashEntry(setup_file_hash, file_list_entry->token, known_token_value);
2684 /* check for config tokens that can be build by base token and suffixes */
2685 for (i = 0; suffix_list[i].token != NULL; i++)
2687 char *token = getStringCat2(file_list_entry->token, suffix_list[i].token);
2688 char *value = getHashEntry(setup_file_hash, token);
2692 setString(&file_list_entry->parameter[i], value);
2694 /* mark config file token as well known from default config */
2695 setHashEntry(setup_file_hash, token, known_token_value);
2702 static void add_dynamic_file_list_entry(struct FileInfo **list,
2703 int *num_list_entries,
2704 SetupFileHash *extra_file_hash,
2705 struct ConfigTypeInfo *suffix_list,
2706 int num_suffix_list_entries,
2709 struct FileInfo *new_list_entry;
2710 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
2712 (*num_list_entries)++;
2713 *list = checked_realloc(*list, *num_list_entries * sizeof(struct FileInfo));
2714 new_list_entry = &(*list)[*num_list_entries - 1];
2716 new_list_entry->token = getStringCopy(token);
2717 new_list_entry->default_filename = NULL;
2718 new_list_entry->filename = NULL;
2719 new_list_entry->parameter = checked_calloc(parameter_array_size);
2721 new_list_entry->redefined = FALSE;
2722 new_list_entry->fallback_to_default = FALSE;
2723 new_list_entry->default_is_cloned = FALSE;
2725 read_token_parameters(extra_file_hash, suffix_list, new_list_entry);
2728 static void add_property_mapping(struct PropertyMapping **list,
2729 int *num_list_entries,
2730 int base_index, int ext1_index,
2731 int ext2_index, int ext3_index,
2734 struct PropertyMapping *new_list_entry;
2736 (*num_list_entries)++;
2737 *list = checked_realloc(*list,
2738 *num_list_entries * sizeof(struct PropertyMapping));
2739 new_list_entry = &(*list)[*num_list_entries - 1];
2741 new_list_entry->base_index = base_index;
2742 new_list_entry->ext1_index = ext1_index;
2743 new_list_entry->ext2_index = ext2_index;
2744 new_list_entry->ext3_index = ext3_index;
2746 new_list_entry->artwork_index = artwork_index;
2749 static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info,
2752 struct FileInfo *file_list = artwork_info->file_list;
2753 struct ConfigTypeInfo *suffix_list = artwork_info->suffix_list;
2754 char **base_prefixes = artwork_info->base_prefixes;
2755 char **ext1_suffixes = artwork_info->ext1_suffixes;
2756 char **ext2_suffixes = artwork_info->ext2_suffixes;
2757 char **ext3_suffixes = artwork_info->ext3_suffixes;
2758 char **ignore_tokens = artwork_info->ignore_tokens;
2759 int num_file_list_entries = artwork_info->num_file_list_entries;
2760 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
2761 int num_base_prefixes = artwork_info->num_base_prefixes;
2762 int num_ext1_suffixes = artwork_info->num_ext1_suffixes;
2763 int num_ext2_suffixes = artwork_info->num_ext2_suffixes;
2764 int num_ext3_suffixes = artwork_info->num_ext3_suffixes;
2765 int num_ignore_tokens = artwork_info->num_ignore_tokens;
2766 SetupFileHash *setup_file_hash, *valid_file_hash;
2767 SetupFileHash *extra_file_hash, *empty_file_hash;
2768 char *known_token_value = KNOWN_TOKEN_VALUE;
2771 if (filename == NULL)
2774 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
2777 /* separate valid (defined) from empty (undefined) config token values */
2778 valid_file_hash = newSetupFileHash();
2779 empty_file_hash = newSetupFileHash();
2780 BEGIN_HASH_ITERATION(setup_file_hash, itr)
2782 char *value = HASH_ITERATION_VALUE(itr);
2784 setHashEntry(*value ? valid_file_hash : empty_file_hash,
2785 HASH_ITERATION_TOKEN(itr), value);
2787 END_HASH_ITERATION(setup_file_hash, itr)
2789 /* at this point, we do not need the setup file hash anymore -- free it */
2790 freeSetupFileHash(setup_file_hash);
2792 /* map deprecated to current tokens (using prefix match and replace) */
2793 BEGIN_HASH_ITERATION(valid_file_hash, itr)
2795 char *token = HASH_ITERATION_TOKEN(itr);
2796 char *mapped_token = get_mapped_token(token);
2798 if (mapped_token != NULL)
2800 char *value = HASH_ITERATION_VALUE(itr);
2802 /* add mapped token */
2803 setHashEntry(valid_file_hash, mapped_token, value);
2805 /* ignore old token (by setting it to "known" keyword) */
2806 setHashEntry(valid_file_hash, token, known_token_value);
2811 END_HASH_ITERATION(valid_file_hash, itr)
2813 /* read parameters for all known config file tokens */
2814 for (i = 0; i < num_file_list_entries; i++)
2815 read_token_parameters(valid_file_hash, suffix_list, &file_list[i]);
2817 /* set all tokens that can be ignored here to "known" keyword */
2818 for (i = 0; i < num_ignore_tokens; i++)
2819 setHashEntry(valid_file_hash, ignore_tokens[i], known_token_value);
2821 /* copy all unknown config file tokens to extra config hash */
2822 extra_file_hash = newSetupFileHash();
2823 BEGIN_HASH_ITERATION(valid_file_hash, itr)
2825 char *value = HASH_ITERATION_VALUE(itr);
2827 if (!strEqual(value, known_token_value))
2828 setHashEntry(extra_file_hash, HASH_ITERATION_TOKEN(itr), value);
2830 END_HASH_ITERATION(valid_file_hash, itr)
2832 /* at this point, we do not need the valid file hash anymore -- free it */
2833 freeSetupFileHash(valid_file_hash);
2835 /* now try to determine valid, dynamically defined config tokens */
2837 BEGIN_HASH_ITERATION(extra_file_hash, itr)
2839 struct FileInfo **dynamic_file_list =
2840 &artwork_info->dynamic_file_list;
2841 int *num_dynamic_file_list_entries =
2842 &artwork_info->num_dynamic_file_list_entries;
2843 struct PropertyMapping **property_mapping =
2844 &artwork_info->property_mapping;
2845 int *num_property_mapping_entries =
2846 &artwork_info->num_property_mapping_entries;
2847 int current_summarized_file_list_entry =
2848 artwork_info->num_file_list_entries +
2849 artwork_info->num_dynamic_file_list_entries;
2850 char *token = HASH_ITERATION_TOKEN(itr);
2851 int len_token = strlen(token);
2853 boolean base_prefix_found = FALSE;
2854 boolean parameter_suffix_found = FALSE;
2856 /* skip all parameter definitions (handled by read_token_parameters()) */
2857 for (i = 0; i < num_suffix_list_entries && !parameter_suffix_found; i++)
2859 int len_suffix = strlen(suffix_list[i].token);
2861 if (token_suffix_match(token, suffix_list[i].token, -len_suffix))
2862 parameter_suffix_found = TRUE;
2865 if (parameter_suffix_found)
2868 /* ---------- step 0: search for matching base prefix ---------- */
2871 for (i = 0; i < num_base_prefixes && !base_prefix_found; i++)
2873 char *base_prefix = base_prefixes[i];
2874 int len_base_prefix = strlen(base_prefix);
2875 boolean ext1_suffix_found = FALSE;
2876 boolean ext2_suffix_found = FALSE;
2877 boolean ext3_suffix_found = FALSE;
2878 boolean exact_match = FALSE;
2879 int base_index = -1;
2880 int ext1_index = -1;
2881 int ext2_index = -1;
2882 int ext3_index = -1;
2884 base_prefix_found = token_suffix_match(token, base_prefix, start_pos);
2886 if (!base_prefix_found)
2891 if (start_pos + len_base_prefix == len_token) /* exact match */
2895 add_dynamic_file_list_entry(dynamic_file_list,
2896 num_dynamic_file_list_entries,
2899 num_suffix_list_entries,
2901 add_property_mapping(property_mapping,
2902 num_property_mapping_entries,
2903 base_index, -1, -1, -1,
2904 current_summarized_file_list_entry);
2908 /* ---------- step 1: search for matching first suffix ---------- */
2910 start_pos += len_base_prefix;
2911 for (j = 0; j < num_ext1_suffixes && !ext1_suffix_found; j++)
2913 char *ext1_suffix = ext1_suffixes[j];
2914 int len_ext1_suffix = strlen(ext1_suffix);
2916 ext1_suffix_found = token_suffix_match(token, ext1_suffix, start_pos);
2918 if (!ext1_suffix_found)
2923 if (start_pos + len_ext1_suffix == len_token) /* exact match */
2927 add_dynamic_file_list_entry(dynamic_file_list,
2928 num_dynamic_file_list_entries,
2931 num_suffix_list_entries,
2933 add_property_mapping(property_mapping,
2934 num_property_mapping_entries,
2935 base_index, ext1_index, -1, -1,
2936 current_summarized_file_list_entry);
2940 start_pos += len_ext1_suffix;
2946 /* ---------- step 2: search for matching second suffix ---------- */
2948 for (k = 0; k < num_ext2_suffixes && !ext2_suffix_found; k++)
2950 char *ext2_suffix = ext2_suffixes[k];
2951 int len_ext2_suffix = strlen(ext2_suffix);
2953 ext2_suffix_found = token_suffix_match(token, ext2_suffix, start_pos);
2955 if (!ext2_suffix_found)
2960 if (start_pos + len_ext2_suffix == len_token) /* exact match */
2964 add_dynamic_file_list_entry(dynamic_file_list,
2965 num_dynamic_file_list_entries,
2968 num_suffix_list_entries,
2970 add_property_mapping(property_mapping,
2971 num_property_mapping_entries,
2972 base_index, ext1_index, ext2_index, -1,
2973 current_summarized_file_list_entry);
2977 start_pos += len_ext2_suffix;
2983 /* ---------- step 3: search for matching third suffix ---------- */
2985 for (l = 0; l < num_ext3_suffixes && !ext3_suffix_found; l++)
2987 char *ext3_suffix = ext3_suffixes[l];
2988 int len_ext3_suffix = strlen(ext3_suffix);
2990 ext3_suffix_found = token_suffix_match(token, ext3_suffix, start_pos);
2992 if (!ext3_suffix_found)
2997 if (start_pos + len_ext3_suffix == len_token) /* exact match */
3001 add_dynamic_file_list_entry(dynamic_file_list,
3002 num_dynamic_file_list_entries,
3005 num_suffix_list_entries,
3007 add_property_mapping(property_mapping,
3008 num_property_mapping_entries,
3009 base_index, ext1_index, ext2_index, ext3_index,
3010 current_summarized_file_list_entry);
3016 END_HASH_ITERATION(extra_file_hash, itr)
3018 if (artwork_info->num_dynamic_file_list_entries > 0)
3020 artwork_info->dynamic_artwork_list =
3021 checked_calloc(artwork_info->num_dynamic_file_list_entries *
3022 artwork_info->sizeof_artwork_list_entry);
3025 if (options.verbose && IS_PARENT_PROCESS())
3027 SetupFileList *setup_file_list, *list;
3028 boolean dynamic_tokens_found = FALSE;
3029 boolean unknown_tokens_found = FALSE;
3030 boolean undefined_values_found = (hashtable_count(empty_file_hash) != 0);
3032 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
3033 Error(ERR_EXIT, "loadSetupFileHash works, but loadSetupFileList fails");
3035 BEGIN_HASH_ITERATION(extra_file_hash, itr)
3037 if (strEqual(HASH_ITERATION_VALUE(itr), known_token_value))
3038 dynamic_tokens_found = TRUE;
3040 unknown_tokens_found = TRUE;
3042 END_HASH_ITERATION(extra_file_hash, itr)
3044 if (options.debug && dynamic_tokens_found)
3046 Error(ERR_INFO_LINE, "-");
3047 Error(ERR_INFO, "dynamic token(s) found in config file:");
3048 Error(ERR_INFO, "- config file: '%s'", filename);
3050 for (list = setup_file_list; list != NULL; list = list->next)
3052 char *value = getHashEntry(extra_file_hash, list->token);
3054 if (value != NULL && strEqual(value, known_token_value))
3055 Error(ERR_INFO, "- dynamic token: '%s'", list->token);
3058 Error(ERR_INFO_LINE, "-");
3061 if (unknown_tokens_found)
3063 Error(ERR_INFO_LINE, "-");
3064 Error(ERR_INFO, "warning: unknown token(s) found in config file:");
3065 Error(ERR_INFO, "- config file: '%s'", filename);
3067 for (list = setup_file_list; list != NULL; list = list->next)
3069 char *value = getHashEntry(extra_file_hash, list->token);
3071 if (value != NULL && !strEqual(value, known_token_value))
3072 Error(ERR_INFO, "- dynamic token: '%s'", list->token);
3075 Error(ERR_INFO_LINE, "-");
3078 if (undefined_values_found)
3080 Error(ERR_INFO_LINE, "-");
3081 Error(ERR_INFO, "warning: undefined values found in config file:");
3082 Error(ERR_INFO, "- config file: '%s'", filename);
3084 for (list = setup_file_list; list != NULL; list = list->next)
3086 char *value = getHashEntry(empty_file_hash, list->token);
3089 Error(ERR_INFO, "- undefined value for token: '%s'", list->token);
3092 Error(ERR_INFO_LINE, "-");
3095 freeSetupFileList(setup_file_list);
3098 freeSetupFileHash(extra_file_hash);
3099 freeSetupFileHash(empty_file_hash);
3102 void LoadArtworkConfig(struct ArtworkListInfo *artwork_info)
3104 struct FileInfo *file_list = artwork_info->file_list;
3105 int num_file_list_entries = artwork_info->num_file_list_entries;
3106 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
3107 char *filename_base = UNDEFINED_FILENAME, *filename_local;
3110 DrawInitText("Loading artwork config", 120, FC_GREEN);
3111 DrawInitText(ARTWORKINFO_FILENAME(artwork_info->type), 150, FC_YELLOW);
3113 /* always start with reliable default values */
3114 for (i = 0; i < num_file_list_entries; i++)
3116 setString(&file_list[i].filename, file_list[i].default_filename);
3118 for (j = 0; j < num_suffix_list_entries; j++)
3119 setString(&file_list[i].parameter[j], file_list[i].default_parameter[j]);
3121 file_list[i].redefined = FALSE;
3122 file_list[i].fallback_to_default = FALSE;
3125 /* free previous dynamic artwork file array */
3126 if (artwork_info->dynamic_file_list != NULL)
3128 for (i = 0; i < artwork_info->num_dynamic_file_list_entries; i++)
3130 free(artwork_info->dynamic_file_list[i].token);
3131 free(artwork_info->dynamic_file_list[i].filename);
3132 free(artwork_info->dynamic_file_list[i].parameter);
3135 free(artwork_info->dynamic_file_list);
3136 artwork_info->dynamic_file_list = NULL;
3138 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
3139 &artwork_info->num_dynamic_file_list_entries);
3142 /* free previous property mapping */
3143 if (artwork_info->property_mapping != NULL)
3145 free(artwork_info->property_mapping);
3147 artwork_info->property_mapping = NULL;
3148 artwork_info->num_property_mapping_entries = 0;
3151 if (!GFX_OVERRIDE_ARTWORK(artwork_info->type))
3153 /* first look for special artwork configured in level series config */
3154 filename_base = getCustomArtworkLevelConfigFilename(artwork_info->type);
3156 if (fileExists(filename_base))
3157 LoadArtworkConfigFromFilename(artwork_info, filename_base);
3160 filename_local = getCustomArtworkConfigFilename(artwork_info->type);
3162 if (filename_local != NULL && !strEqual(filename_base, filename_local))
3163 LoadArtworkConfigFromFilename(artwork_info, filename_local);
3166 static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info,
3167 struct ListNodeInfo **listnode)
3171 char *filename = (*listnode)->source_filename;
3173 if (--(*listnode)->num_references <= 0)
3174 deleteNodeFromList(&artwork_info->content_list, filename,
3175 artwork_info->free_artwork);
3181 static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
3182 struct ListNodeInfo **listnode,
3183 struct FileInfo *file_list_entry)
3193 char *basename = file_list_entry->filename;
3194 char *filename = getCustomArtworkFilename(basename, artwork_info->type);
3196 if (filename == NULL)
3198 Error(ERR_WARN, "cannot find artwork file '%s'", basename);
3200 basename = file_list_entry->default_filename;
3202 /* fail for cloned default artwork that has no default filename defined */
3203 if (file_list_entry->default_is_cloned &&
3204 strEqual(basename, UNDEFINED_FILENAME))
3206 int error_mode = ERR_WARN;
3208 /* we can get away without sounds and music, but not without graphics */
3209 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
3210 error_mode = ERR_EXIT;
3212 Error(error_mode, "token '%s' was cloned and has no default filename",
3213 file_list_entry->token);
3218 /* dynamic artwork has no default filename / skip empty default artwork */
3219 if (basename == NULL || strEqual(basename, UNDEFINED_FILENAME))
3222 file_list_entry->fallback_to_default = TRUE;
3224 Error(ERR_WARN, "trying default artwork file '%s'", basename);
3226 filename = getCustomArtworkFilename(basename, artwork_info->type);
3228 if (filename == NULL)
3230 int error_mode = ERR_WARN;
3232 /* we can get away without sounds and music, but not without graphics */
3233 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
3234 error_mode = ERR_EXIT;
3236 Error(error_mode, "cannot find default artwork file '%s'", basename);
3242 /* check if the old and the new artwork file are the same */
3243 if (*listnode && strEqual((*listnode)->source_filename, filename))
3245 /* The old and new artwork are the same (have the same filename and path).
3246 This usually means that this artwork does not exist in this artwork set
3247 and a fallback to the existing artwork is done. */
3252 /* delete existing artwork file entry */
3253 deleteArtworkListEntry(artwork_info, listnode);
3255 /* check if the new artwork file already exists in the list of artwork */
3256 if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL)
3258 *listnode = (struct ListNodeInfo *)node->content;
3259 (*listnode)->num_references++;
3264 DrawInitText(init_text[artwork_info->type], 120, FC_GREEN);
3265 DrawInitText(basename, 150, FC_YELLOW);
3267 if ((*listnode = artwork_info->load_artwork(filename)) != NULL)
3269 /* add new artwork file entry to the list of artwork files */
3270 (*listnode)->num_references = 1;
3271 addNodeToList(&artwork_info->content_list, (*listnode)->source_filename,
3276 int error_mode = ERR_WARN;
3278 /* we can get away without sounds and music, but not without graphics */
3279 if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
3280 error_mode = ERR_EXIT;
3282 Error(error_mode, "cannot load artwork file '%s'", basename);
3288 static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
3289 struct ListNodeInfo **listnode,
3290 struct FileInfo *file_list_entry)
3292 if (strEqual(file_list_entry->filename, UNDEFINED_FILENAME))
3294 deleteArtworkListEntry(artwork_info, listnode);
3299 replaceArtworkListEntry(artwork_info, listnode, file_list_entry);
3302 void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
3304 struct FileInfo *file_list = artwork_info->file_list;
3305 struct FileInfo *dynamic_file_list = artwork_info->dynamic_file_list;
3306 int num_file_list_entries = artwork_info->num_file_list_entries;
3307 int num_dynamic_file_list_entries =
3308 artwork_info->num_dynamic_file_list_entries;
3311 print_timestamp_init("ReloadCustomArtworkList");
3313 for (i = 0; i < num_file_list_entries; i++)
3314 LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[i],
3317 for (i = 0; i < num_dynamic_file_list_entries; i++)
3318 LoadCustomArtwork(artwork_info, &artwork_info->dynamic_artwork_list[i],
3319 &dynamic_file_list[i]);
3321 print_timestamp_done("ReloadCustomArtworkList");
3324 dumpList(artwork_info->content_list);
3328 static void FreeCustomArtworkList(struct ArtworkListInfo *artwork_info,
3329 struct ListNodeInfo ***list,
3330 int *num_list_entries)
3337 for (i = 0; i < *num_list_entries; i++)
3338 deleteArtworkListEntry(artwork_info, &(*list)[i]);
3342 *num_list_entries = 0;
3345 void FreeCustomArtworkLists(struct ArtworkListInfo *artwork_info)
3347 if (artwork_info == NULL)
3350 FreeCustomArtworkList(artwork_info, &artwork_info->artwork_list,
3351 &artwork_info->num_file_list_entries);
3353 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
3354 &artwork_info->num_dynamic_file_list_entries);
3358 /* ------------------------------------------------------------------------- */
3359 /* functions only needed for non-Unix (non-command-line) systems */
3360 /* (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt") */
3361 /* (now also added for Windows, to create files in user data directory) */
3362 /* ------------------------------------------------------------------------- */
3364 char *getErrorFilename(char *basename)
3366 return getPath2(getUserGameDataDir(), basename);
3369 void openErrorFile()
3371 InitUserDataDirectory();
3373 if ((program.error_file = fopen(program.error_filename, MODE_WRITE)) == NULL)
3375 program.error_file = stderr;
3377 Error(ERR_WARN, "cannot open file '%s' for writing: %s",
3378 program.error_filename, strerror(errno));
3381 /* error output should be unbuffered so it is not truncated in a crash */
3382 setbuf(program.error_file, NULL);
3385 void closeErrorFile()
3387 if (program.error_file != stderr) /* do not close stream 'stderr' */
3388 fclose(program.error_file);
3391 void dumpErrorFile()
3393 FILE *error_file = fopen(program.error_filename, MODE_READ);
3395 if (error_file != NULL)
3397 while (!feof(error_file))
3398 fputc(fgetc(error_file), stderr);
3404 void NotifyUserAboutErrorFile()
3406 #if defined(PLATFORM_WIN32)
3407 char *title_text = getStringCat2(program.program_title, " Error Message");
3408 char *error_text = getStringCat2("The program was aborted due to an error; "
3409 "for details, see the following error file:"
3410 STRING_NEWLINE, program.error_filename);
3412 MessageBox(NULL, error_text, title_text, MB_OK);
3417 /* ------------------------------------------------------------------------- */
3418 /* the following is only for debugging purpose and normally not used */
3419 /* ------------------------------------------------------------------------- */
3423 #define DEBUG_PRINT_INIT_TIMESTAMPS FALSE
3424 #define DEBUG_PRINT_INIT_TIMESTAMPS_DEPTH 10
3426 #define DEBUG_NUM_TIMESTAMPS 10
3427 #define DEBUG_TIME_IN_MICROSECONDS 0
3429 #if DEBUG_TIME_IN_MICROSECONDS
3430 static double Counter_Microseconds()
3432 static struct timeval base_time = { 0, 0 };
3433 struct timeval current_time;
3436 gettimeofday(¤t_time, NULL);
3438 /* reset base time in case of wrap-around */
3439 if (current_time.tv_sec < base_time.tv_sec)
3440 base_time = current_time;
3443 ((double)(current_time.tv_sec - base_time.tv_sec)) * 1000000 +
3444 ((double)(current_time.tv_usec - base_time.tv_usec));
3446 return counter; /* return microseconds since last init */
3450 char *debug_print_timestamp_get_padding(int padding_size)
3452 static char *padding = NULL;
3453 int max_padding_size = 100;
3455 if (padding == NULL)
3457 padding = checked_calloc(max_padding_size + 1);
3458 memset(padding, ' ', max_padding_size);
3461 return &padding[MAX(0, max_padding_size - padding_size)];
3464 void debug_print_timestamp(int counter_nr, char *message)
3466 int indent_size = 8;
3467 int padding_size = 40;
3468 float timestamp_interval;
3471 Error(ERR_EXIT, "debugging: invalid negative counter");
3472 else if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
3473 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
3475 #if DEBUG_TIME_IN_MICROSECONDS
3476 static double counter[DEBUG_NUM_TIMESTAMPS][2];
3479 counter[counter_nr][0] = Counter_Microseconds();
3481 static int counter[DEBUG_NUM_TIMESTAMPS][2];
3484 counter[counter_nr][0] = Counter();
3487 timestamp_interval = counter[counter_nr][0] - counter[counter_nr][1];
3488 counter[counter_nr][1] = counter[counter_nr][0];
3491 Error(ERR_DEBUG, "%s%s%s %.3f %s",
3492 debug_print_timestamp_get_padding(counter_nr * indent_size),
3494 debug_print_timestamp_get_padding(padding_size - strlen(message)),
3495 timestamp_interval / 1000,
3499 void debug_print_parent_only(char *format, ...)
3501 if (!IS_PARENT_PROCESS())
3508 va_start(ap, format);
3509 vprintf(format, ap);
3518 void print_timestamp_ext(char *message, char *mode)
3520 #if DEBUG_PRINT_INIT_TIMESTAMPS
3521 static char *debug_message = NULL;
3522 static char *last_message = NULL;
3523 static int counter_nr = 0;
3524 int max_depth = DEBUG_PRINT_INIT_TIMESTAMPS_DEPTH;
3526 checked_free(debug_message);
3527 debug_message = getStringCat3(mode, " ", message);
3529 if (strEqual(mode, "INIT"))
3531 debug_print_timestamp(counter_nr, NULL);
3533 if (counter_nr + 1 < max_depth)
3534 debug_print_timestamp(counter_nr, debug_message);
3538 debug_print_timestamp(counter_nr, NULL);
3540 else if (strEqual(mode, "DONE"))
3544 if (counter_nr + 1 < max_depth ||
3545 (counter_nr == 0 && max_depth == 1))
3547 last_message = message;
3549 if (counter_nr == 0 && max_depth == 1)
3551 checked_free(debug_message);
3552 debug_message = getStringCat3("TIME", " ", message);
3555 debug_print_timestamp(counter_nr, debug_message);
3558 else if (!strEqual(mode, "TIME") ||
3559 !strEqual(message, last_message))
3561 if (counter_nr < max_depth)
3562 debug_print_timestamp(counter_nr, debug_message);
3567 void print_timestamp_init(char *message)
3569 print_timestamp_ext(message, "INIT");
3572 void print_timestamp_time(char *message)
3574 print_timestamp_ext(message, "TIME");
3577 void print_timestamp_done(char *message)
3579 print_timestamp_ext(message, "DONE");