1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://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 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 #define DUPLICATE_LOG_OUT_TO_STDOUT TRUE
45 #define DUPLICATE_LOG_ERR_TO_STDERR TRUE
48 #if defined(PLATFORM_ANDROID)
49 static int android_log_prio = ANDROID_LOG_INFO;
50 static char *android_log_buffer = NULL;
52 static void append_to_android_log_buffer(char *format, va_list ap)
54 char text_new[MAX_OUTPUT_LINESIZE];
56 // print text to temporary string
57 vsnprintf(text_new, MAX_OUTPUT_LINESIZE, format, ap);
59 if (android_log_buffer == NULL)
61 android_log_buffer = getStringCopy(text_new);
65 char *android_log_buffer_old = android_log_buffer;
67 // append new text to existing text
68 android_log_buffer = getStringCat2(android_log_buffer, text_new);
70 checked_free(android_log_buffer_old);
74 static void vprintf_log_nonewline(char *format, va_list ap)
76 // add log output to buffer until text with newline is printed
77 append_to_android_log_buffer(format, ap);
80 static void vprintf_log(char *format, va_list ap)
82 // add log output to buffer
83 append_to_android_log_buffer(format, ap);
85 // __android_log_vprint(android_log_prio, program.program_title, format, ap);
86 __android_log_write(android_log_prio, program.program_title,
89 checked_free(android_log_buffer);
90 android_log_buffer = NULL;
95 static void vprintf_log_nonewline(char *format, va_list ap)
97 FILE *file = program.log_file[LOG_ERR_ID];
99 #if DUPLICATE_LOG_ERR_TO_STDERR
100 if (file != program.log_file_default[LOG_ERR_ID])
105 vfprintf(program.log_file_default[LOG_ERR_ID], format, ap2);
111 vfprintf(file, format, ap);
114 static void vprintf_log(char *format, va_list ap)
116 FILE *file = program.log_file[LOG_ERR_ID];
117 char *newline = STRING_NEWLINE;
119 #if DUPLICATE_LOG_ERR_TO_STDERR
120 if (file != program.log_file_default[LOG_ERR_ID])
125 vfprintf(program.log_file_default[LOG_ERR_ID], format, ap2);
126 fprintf(program.log_file_default[LOG_ERR_ID], "%s", newline);
132 vfprintf(file, format, ap);
133 fprintf(file, "%s", newline);
137 static void printf_log_nonewline(char *format, ...)
141 va_start(ap, format);
142 vprintf_log_nonewline(format, ap);
146 static void printf_log(char *format, ...)
150 va_start(ap, format);
151 vprintf_log(format, ap);
155 static void printf_log_line(char *line_chars, int line_length)
159 for (i = 0; i < line_length; i++)
160 printf_log_nonewline("%s", line_chars);
166 // ----------------------------------------------------------------------------
167 // platform independent wrappers for printf() et al.
168 // ----------------------------------------------------------------------------
170 void fprintf_line(FILE *file, char *line_chars, int line_length)
174 for (i = 0; i < line_length; i++)
175 fprintf(file, "%s", line_chars);
180 void fprintf_line_with_prefix(FILE *file, char *prefix, char *line_chars,
183 fprintf(file, "%s", prefix);
184 fprintf_line(file, line_chars, line_length);
187 void printf_line(char *line_chars, int line_length)
189 fprintf_line(stdout, line_chars, line_length);
192 void printf_line_with_prefix(char *prefix, char *line_chars, int line_length)
194 fprintf_line_with_prefix(stdout, prefix, line_chars, line_length);
197 static void vPrint(char *format, va_list ap)
199 FILE *file = program.log_file[LOG_OUT_ID];
201 #if DUPLICATE_LOG_OUT_TO_STDOUT
202 if (file != program.log_file_default[LOG_OUT_ID])
207 vfprintf(program.log_file_default[LOG_OUT_ID], format, ap2);
213 vfprintf(file, format, ap);
216 void Print(char *format, ...)
220 va_start(ap, format);
225 void PrintNoLog(char *format, ...)
227 FILE *file = program.log_file_default[LOG_OUT_ID];
230 va_start(ap, format);
231 vfprintf(file, format, ap);
237 void PrintLine(char *line_chars, int line_length)
241 for (i = 0; i < line_length; i++)
247 void PrintLineWithPrefix(char *prefix, char *line_chars, int line_length)
250 PrintLine(line_chars, line_length);
254 // ----------------------------------------------------------------------------
255 // generic logging functions
256 // ----------------------------------------------------------------------------
268 static char *log_tokens[] =
278 static void printf_log_prefix(int log_level, char *mode)
280 if (log_level < 0 || log_level > LOG_FATAL)
283 char *log_token = log_tokens[log_level];
285 if (log_level == LOG_DEBUG)
286 printf_log_nonewline("[%s] [%s] ", log_token, mode);
288 printf_log_nonewline("[%s] ", log_token);
291 static void Log(int log_level, char *mode, char *format, va_list ap)
293 if (log_level < 0 || log_level > LOG_FATAL)
296 #if defined(PLATFORM_ANDROID)
297 android_log_prio = (log_level == LOG_DEBUG ? ANDROID_LOG_DEBUG :
298 log_level == LOG_INFO ? ANDROID_LOG_INFO :
299 log_level == LOG_WARN ? ANDROID_LOG_WARN :
300 log_level == LOG_ERROR ? ANDROID_LOG_ERROR :
301 log_level == LOG_FATAL ? ANDROID_LOG_FATAL :
302 ANDROID_LOG_UNKNOWN);
305 static boolean last_line_was_separator = FALSE;
306 char *log_token = log_tokens[log_level];
308 if (strEqual(format, "===") ||
309 strEqual(format, "---"))
311 static char *mode_last = NULL;
312 char line_char[2] = { format[0], '\0' };
313 int line_length = 80 - strlen(log_token) - 3;
315 if (log_level == LOG_DEBUG)
316 line_length -= strlen(mode) + 3;
318 if (last_line_was_separator && strEqual(mode, mode_last))
321 printf_log_prefix(log_level, mode);
322 printf_log_line(line_char, line_length);
324 if (!strEqual(mode, mode_last))
325 setString(&mode_last, mode);
327 last_line_was_separator = TRUE;
332 last_line_was_separator = FALSE;
334 printf_log_prefix(log_level, mode);
336 vprintf_log(format, ap);
339 void Debug(char *mode, char *format, ...)
346 // if optional debug mode specified, limit debug output accordingly
347 if (options.debug_mode != NULL &&
348 !strEqual(options.debug_mode, mode))
351 va_start(ap, format);
352 Log(LOG_DEBUG, mode, format, ap);
356 void Info(char *format, ...)
360 va_start(ap, format);
361 Log(LOG_INFO, NULL, format, ap);
365 void Warn(char *format, ...)
369 va_start(ap, format);
370 Log(LOG_WARN, NULL, format, ap);
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 /* int2str() returns a number converted to a string;
380 the used memory is static, but will be overwritten by later calls,
381 so if you want to save the result, copy it to a private string buffer;
382 there can be 10 local calls of int2str() without buffering the result --
383 the 11th call will then destroy the result from the first call and so on. */
385 char *int2str(int number, int size)
387 static char shift_array[10][40];
388 static int shift_counter = 0;
389 char *s = shift_array[shift_counter];
391 shift_counter = (shift_counter + 1) % 10;
398 sprintf(s, " %09d", number);
399 return &s[strlen(s) - size];
403 sprintf(s, "%d", number);
409 // something similar to "int2str()" above, but allocates its own memory
410 // and has a different interface; we cannot use "itoa()", because this
411 // seems to be already defined when cross-compiling to the win32 target
413 char *i_to_a(unsigned int i)
415 static char *a = NULL;
419 if (i > 2147483647) // yes, this is a kludge
422 a = checked_malloc(10 + 1);
430 // calculate base-2 logarithm of argument (rounded down to integer;
431 // this function returns the number of the highest bit set in argument)
433 int log_2(unsigned int x)
439 x -= (1 << e); // for rounding down (rounding up: remove this line)
446 boolean getTokenValueFromString(char *string, char **token, char **value)
448 return getTokenValueFromSetupLine(string, token, value);
452 // ----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
456 // maximal allowed length of a command line option
457 #define MAX_OPTION_LEN 256
459 static unsigned int getCurrentMS(void)
461 return SDL_GetTicks();
464 static unsigned int mainCounter(int mode)
466 static unsigned int base_ms = 0;
467 unsigned int current_ms;
469 // get current system milliseconds
470 current_ms = getCurrentMS();
472 // reset base timestamp in case of counter reset or wrap-around
473 if (mode == INIT_COUNTER || current_ms < base_ms)
474 base_ms = current_ms;
476 // return milliseconds since last counter reset
477 return current_ms - base_ms;
480 void InitCounter() // set counter back to zero
482 mainCounter(INIT_COUNTER);
485 unsigned int Counter() // get milliseconds since last call of InitCounter()
487 return mainCounter(READ_COUNTER);
490 static void sleep_milliseconds(unsigned int milliseconds_delay)
492 SDL_Delay(milliseconds_delay);
495 void Delay(unsigned int delay) // Sleep specified number of milliseconds
497 sleep_milliseconds(delay);
500 boolean DelayReachedExt(unsigned int *counter_var, unsigned int delay,
501 unsigned int actual_counter)
503 if (actual_counter >= *counter_var &&
504 actual_counter < *counter_var + delay)
507 *counter_var = actual_counter;
512 boolean FrameReached(unsigned int *frame_counter_var, unsigned int frame_delay)
514 return DelayReachedExt(frame_counter_var, frame_delay, FrameCounter);
517 boolean DelayReached(unsigned int *counter_var, unsigned int delay)
519 return DelayReachedExt(counter_var, delay, Counter());
522 void ResetDelayCounterExt(unsigned int *counter_var,
523 unsigned int actual_counter)
525 DelayReachedExt(counter_var, 0, actual_counter);
528 void ResetFrameCounter(unsigned int *frame_counter_var)
530 FrameReached(frame_counter_var, 0);
533 void ResetDelayCounter(unsigned int *counter_var)
535 DelayReached(counter_var, 0);
538 int WaitUntilDelayReached(unsigned int *counter_var, unsigned int delay)
540 unsigned int actual_counter;
545 actual_counter = Counter();
547 if (actual_counter >= *counter_var &&
548 actual_counter < *counter_var + delay)
549 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
554 if (*counter_var != 0 &&
556 actual_counter >= *counter_var + delay)
558 int lag = actual_counter - (*counter_var + delay);
559 int delay2 = (delay + 1) / 2;
562 skip_frames = (lag + delay2) / delay;
565 *counter_var = actual_counter;
570 void SkipUntilDelayReached(unsigned int *counter_var, unsigned int delay,
571 int *loop_var, int last_loop_value)
573 int skip_frames = WaitUntilDelayReached(counter_var, delay);
578 Debug("internal:SkipUntilDelayReached",
579 "%d: %d ms -> SKIP %d FRAME(S) [%d ms]",
581 skip_frames, skip_frames * delay);
583 Debug("internal:SkipUntilDelayReached",
589 if (skip_frames == 0)
592 // when skipping frames, make sure to never skip the last frame, as
593 // this may be needed for animations to reach a defined end state;
594 // furthermore, we assume that this function is called at the end
595 // of a "for" loop, which continues by incrementing the loop variable
596 // by one before checking the loop condition again; therefore we have
597 // to check against the last loop value minus one here
601 if (*loop_var < last_loop_value) // never skip the last frame
603 *loop_var += skip_frames;
605 if (*loop_var > last_loop_value) // never skip the last frame
606 *loop_var = last_loop_value;
611 // ----------------------------------------------------------------------------
612 // random generator functions
613 // ----------------------------------------------------------------------------
615 unsigned int init_random_number(int nr, int seed)
617 if (seed == NEW_RANDOMIZE)
619 // default random seed
620 seed = (int)time(NULL); // seconds since the epoch
622 #if !defined(PLATFORM_WIN32)
623 // add some more randomness
624 struct timeval current_time;
626 gettimeofday(¤t_time, NULL);
628 seed += (int)current_time.tv_usec; // microseconds since the epoch
631 // add some more randomness
632 seed += (int)SDL_GetTicks(); // milliseconds since SDL init
634 // add some more randomness
635 seed += GetSimpleRandom(1000000);
638 srandom_linux_libc(nr, (unsigned int) seed);
640 return (unsigned int) seed;
643 unsigned int get_random_number(int nr, int max)
645 return (max > 0 ? random_linux_libc(nr) % max : 0);
649 // ----------------------------------------------------------------------------
650 // system info functions
651 // ----------------------------------------------------------------------------
653 #if !defined(PLATFORM_ANDROID)
654 static char *get_corrected_real_name(char *real_name)
656 char *real_name_new = checked_malloc(MAX_USERNAME_LEN + 1);
657 char *from_ptr = real_name;
658 char *to_ptr = real_name_new;
660 // copy the name string, but not more than MAX_USERNAME_LEN characters
661 while (*from_ptr && (int)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1)
663 // the name field read from "passwd" file may also contain additional
664 // user information, separated by commas, which will be removed here
665 if (*from_ptr == ',')
668 // the user's real name may contain 'german sharp s' characters,
669 // which have no equivalent in upper case letters (used by our fonts)
670 if (*from_ptr == CHAR_BYTE_SHARP_S)
677 *to_ptr++ = *from_ptr++;
682 return real_name_new;
686 char *getLoginName(void)
688 static char *login_name = NULL;
690 #if defined(PLATFORM_WIN32)
691 if (login_name == NULL)
693 unsigned long buffer_size = MAX_USERNAME_LEN + 1;
694 login_name = checked_malloc(buffer_size);
696 if (GetUserName(login_name, &buffer_size) == 0)
697 strcpy(login_name, ANONYMOUS_NAME);
699 #elif defined(PLATFORM_UNIX) && !defined(PLATFORM_ANDROID)
700 if (login_name == NULL)
704 if ((pwd = getpwuid(getuid())) == NULL)
705 login_name = ANONYMOUS_NAME;
707 login_name = getStringCopy(pwd->pw_name);
710 login_name = ANONYMOUS_NAME;
716 char *getRealName(void)
718 static char *real_name = NULL;
720 #if defined(PLATFORM_WIN32)
721 if (real_name == NULL)
723 static char buffer[MAX_USERNAME_LEN + 1];
724 unsigned long buffer_size = MAX_USERNAME_LEN + 1;
726 if (GetUserName(buffer, &buffer_size) != 0)
727 real_name = get_corrected_real_name(buffer);
729 real_name = ANONYMOUS_NAME;
731 #elif defined(PLATFORM_UNIX) && !defined(PLATFORM_ANDROID)
732 if (real_name == NULL)
736 if ((pwd = getpwuid(getuid())) != NULL && strlen(pwd->pw_gecos) != 0)
737 real_name = get_corrected_real_name(pwd->pw_gecos);
739 real_name = ANONYMOUS_NAME;
742 real_name = ANONYMOUS_NAME;
748 time_t getFileTimestampEpochSeconds(char *filename)
750 struct stat file_status;
752 if (stat(filename, &file_status) != 0) // cannot stat file
755 return file_status.st_mtime;
759 // ----------------------------------------------------------------------------
760 // path manipulation functions
761 // ----------------------------------------------------------------------------
763 static char *getLastPathSeparatorPtr(char *filename)
765 char *last_separator = strrchr(filename, CHAR_PATH_SEPARATOR_UNIX);
767 if (last_separator == NULL) // also try DOS/Windows variant
768 last_separator = strrchr(filename, CHAR_PATH_SEPARATOR_DOS);
770 return last_separator;
773 char *getBaseNamePtr(char *filename)
775 char *last_separator = getLastPathSeparatorPtr(filename);
777 if (last_separator != NULL)
778 return last_separator + 1; // separator found: strip base path
780 return filename; // no separator found: filename has no path
783 char *getBaseName(char *filename)
785 return getStringCopy(getBaseNamePtr(filename));
788 char *getBaseNameNoSuffix(char *filename)
790 char *basename = getStringCopy(getBaseNamePtr(filename));
792 // remove trailing suffix (separated by dot or hyphen)
793 if (basename[0] != '.' && basename[0] != '-')
795 if (strchr(basename, '.') != NULL)
796 *strchr(basename, '.') = '\0';
798 if (strchr(basename, '-') != NULL)
799 *strchr(basename, '-') = '\0';
805 char *getBasePath(char *filename)
807 char *basepath = getStringCopy(filename);
808 char *last_separator = getLastPathSeparatorPtr(basepath);
810 // if no separator was found, use current directory
811 if (last_separator == NULL)
815 return getStringCopy(".");
818 // separator found: strip basename
819 *last_separator = '\0';
825 // ----------------------------------------------------------------------------
826 // various string functions
827 // ----------------------------------------------------------------------------
829 char *getStringCat2WithSeparator(char *s1, char *s2, char *sep)
831 if (s1 == NULL || s2 == NULL || sep == NULL)
834 char *complete_string = checked_malloc(strlen(s1) + strlen(sep) +
837 sprintf(complete_string, "%s%s%s", s1, sep, s2);
839 return complete_string;
842 char *getStringCat3WithSeparator(char *s1, char *s2, char *s3, char *sep)
844 if (s1 == NULL || s2 == NULL || s3 == NULL || sep == NULL)
847 char *complete_string = checked_malloc(strlen(s1) + strlen(sep) +
848 strlen(s2) + strlen(sep) +
851 sprintf(complete_string, "%s%s%s%s%s", s1, sep, s2, sep, s3);
853 return complete_string;
856 char *getStringCat2(char *s1, char *s2)
858 return getStringCat2WithSeparator(s1, s2, "");
861 char *getStringCat3(char *s1, char *s2, char *s3)
863 return getStringCat3WithSeparator(s1, s2, s3, "");
866 char *getPath2(char *path1, char *path2)
868 #if defined(PLATFORM_ANDROID)
869 // workaround for reading from assets directory -- skip "." subdirs in path
870 if (strEqual(path1, "."))
871 return getStringCopy(path2);
872 else if (strEqual(path2, "."))
873 return getStringCopy(path1);
876 return getStringCat2WithSeparator(path1, path2, STRING_PATH_SEPARATOR);
879 char *getPath3(char *path1, char *path2, char *path3)
881 #if defined(PLATFORM_ANDROID)
882 // workaround for reading from assets directory -- skip "." subdirs in path
883 if (strEqual(path1, "."))
884 return getStringCat2WithSeparator(path2, path3, STRING_PATH_SEPARATOR);
885 else if (strEqual(path2, "."))
886 return getStringCat2WithSeparator(path1, path3, STRING_PATH_SEPARATOR);
887 else if (strEqual(path3, "."))
888 return getStringCat2WithSeparator(path1, path2, STRING_PATH_SEPARATOR);
891 return getStringCat3WithSeparator(path1, path2, path3, STRING_PATH_SEPARATOR);
894 static char *getPngOrPcxIfNotExists(char *filename)
896 // switch from PNG to PCX file and vice versa, if file does not exist
897 // (backwards compatibility with PCX files used in previous versions)
899 if (!fileExists(filename) && strSuffix(filename, ".png"))
900 strcpy(&filename[strlen(filename) - 3], "pcx");
901 else if (!fileExists(filename) && strSuffix(filename, ".pcx"))
902 strcpy(&filename[strlen(filename) - 3], "png");
907 char *getImg2(char *path1, char *path2)
909 return getPngOrPcxIfNotExists(getPath2(path1, path2));
912 char *getImg3(char *path1, char *path2, char *path3)
914 return getPngOrPcxIfNotExists(getPath3(path1, path2, path3));
917 char *getStringCopy(const char *s)
924 s_copy = checked_malloc(strlen(s) + 1);
930 char *getStringCopyN(const char *s, int n)
933 int s_len = MAX(0, n);
938 s_copy = checked_malloc(s_len + 1);
939 strncpy(s_copy, s, s_len);
940 s_copy[s_len] = '\0';
945 char *getStringCopyNStatic(const char *s, int n)
947 static char *s_copy = NULL;
949 checked_free(s_copy);
951 s_copy = getStringCopyN(s, n);
956 char *getStringToLower(const char *s)
958 char *s_copy = checked_malloc(strlen(s) + 1);
959 char *s_ptr = s_copy;
962 *s_ptr++ = tolower(*s++);
968 void setString(char **old_value, char *new_value)
970 checked_free(*old_value);
972 *old_value = getStringCopy(new_value);
975 boolean strEqual(char *s1, char *s2)
977 return (s1 == NULL && s2 == NULL ? TRUE :
978 s1 == NULL && s2 != NULL ? FALSE :
979 s1 != NULL && s2 == NULL ? FALSE :
980 strcmp(s1, s2) == 0);
983 boolean strEqualN(char *s1, char *s2, int n)
985 return (s1 == NULL && s2 == NULL ? TRUE :
986 s1 == NULL && s2 != NULL ? FALSE :
987 s1 != NULL && s2 == NULL ? FALSE :
988 strncmp(s1, s2, n) == 0);
991 boolean strPrefix(char *s, char *prefix)
993 return (s == NULL && prefix == NULL ? TRUE :
994 s == NULL && prefix != NULL ? FALSE :
995 s != NULL && prefix == NULL ? FALSE :
996 strncmp(s, prefix, strlen(prefix)) == 0);
999 boolean strSuffix(char *s, char *suffix)
1001 return (s == NULL && suffix == NULL ? TRUE :
1002 s == NULL && suffix != NULL ? FALSE :
1003 s != NULL && suffix == NULL ? FALSE :
1004 strlen(s) < strlen(suffix) ? FALSE :
1005 strcmp(&s[strlen(s) - strlen(suffix)], suffix) == 0);
1008 boolean strPrefixLower(char *s, char *prefix)
1010 char *s_lower = getStringToLower(s);
1011 boolean match = strPrefix(s_lower, prefix);
1018 boolean strSuffixLower(char *s, char *suffix)
1020 char *s_lower = getStringToLower(s);
1021 boolean match = strSuffix(s_lower, suffix);
1029 // ----------------------------------------------------------------------------
1030 // command line option handling functions
1031 // ----------------------------------------------------------------------------
1033 void GetOptions(int argc, char *argv[],
1034 void (*print_usage_function)(void),
1035 void (*print_version_function)(void))
1037 char *ro_base_path = getProgramMainDataPath(argv[0], RO_BASE_PATH);
1038 char *rw_base_path = getProgramMainDataPath(argv[0], RW_BASE_PATH);
1039 char **argvplus = checked_calloc((argc + 1) * sizeof(char **));
1040 char **options_left = &argvplus[1];
1042 // replace original "argv" with null-terminated array of string pointers
1044 argvplus[argc] = argv[argc];
1046 // initialize global program options
1047 options.server_host = NULL;
1048 options.server_port = 0;
1050 options.ro_base_directory = ro_base_path;
1051 options.rw_base_directory = rw_base_path;
1052 options.level_directory = getPath2(ro_base_path, LEVELS_DIRECTORY);
1053 options.graphics_directory = getPath2(ro_base_path, GRAPHICS_DIRECTORY);
1054 options.sounds_directory = getPath2(ro_base_path, SOUNDS_DIRECTORY);
1055 options.music_directory = getPath2(ro_base_path, MUSIC_DIRECTORY);
1056 options.docs_directory = getPath2(ro_base_path, DOCS_DIRECTORY);
1057 options.conf_directory = getPath2(ro_base_path, CONF_DIRECTORY);
1059 options.execute_command = NULL;
1060 options.special_flags = NULL;
1061 options.debug_mode = NULL;
1063 options.mytapes = FALSE;
1064 options.serveronly = FALSE;
1065 options.network = FALSE;
1066 options.verbose = FALSE;
1067 options.debug = FALSE;
1070 options.verbose = TRUE;
1072 #if !defined(PLATFORM_UNIX)
1073 if (*options_left == NULL) // no options given -- enable verbose mode
1074 options.verbose = TRUE;
1079 #if defined(PLATFORM_ANDROID)
1080 options.debug = TRUE;
1084 while (*options_left)
1086 char option_str[MAX_OPTION_LEN];
1087 char *option = options_left[0];
1088 char *next_option = options_left[1];
1089 char *option_arg = NULL;
1090 int option_len = strlen(option);
1092 if (option_len >= MAX_OPTION_LEN)
1093 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
1095 strcpy(option_str, option); // copy argument into buffer
1096 option = option_str;
1098 if (strEqual(option, "--")) // stop scanning arguments
1101 if (strPrefix(option, "--")) // treat '--' like '-'
1104 option_arg = strchr(option, '=');
1105 if (option_arg == NULL) // no '=' in option
1106 option_arg = next_option;
1109 *option_arg++ = '\0'; // cut argument from option
1110 if (*option_arg == '\0') // no argument after '='
1111 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
1114 option_len = strlen(option);
1116 if (strEqual(option, "-"))
1118 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
1120 else if (strncmp(option, "-help", option_len) == 0)
1122 print_usage_function();
1126 else if (strncmp(option, "-basepath", option_len) == 0)
1128 if (option_arg == NULL)
1129 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
1131 // this should be extended to separate options for ro and rw data
1132 options.ro_base_directory = ro_base_path = getStringCopy(option_arg);
1133 options.rw_base_directory = rw_base_path = getStringCopy(option_arg);
1134 if (option_arg == next_option)
1137 // adjust paths for sub-directories in base directory accordingly
1138 options.level_directory = getPath2(ro_base_path, LEVELS_DIRECTORY);
1139 options.graphics_directory = getPath2(ro_base_path, GRAPHICS_DIRECTORY);
1140 options.sounds_directory = getPath2(ro_base_path, SOUNDS_DIRECTORY);
1141 options.music_directory = getPath2(ro_base_path, MUSIC_DIRECTORY);
1142 options.docs_directory = getPath2(ro_base_path, DOCS_DIRECTORY);
1143 options.conf_directory = getPath2(ro_base_path, CONF_DIRECTORY);
1145 else if (strncmp(option, "-levels", option_len) == 0)
1147 if (option_arg == NULL)
1148 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
1150 options.level_directory = getStringCopy(option_arg);
1151 if (option_arg == next_option)
1154 else if (strncmp(option, "-graphics", option_len) == 0)
1156 if (option_arg == NULL)
1157 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
1159 options.graphics_directory = getStringCopy(option_arg);
1160 if (option_arg == next_option)
1163 else if (strncmp(option, "-sounds", option_len) == 0)
1165 if (option_arg == NULL)
1166 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
1168 options.sounds_directory = getStringCopy(option_arg);
1169 if (option_arg == next_option)
1172 else if (strncmp(option, "-music", option_len) == 0)
1174 if (option_arg == NULL)
1175 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
1177 options.music_directory = getStringCopy(option_arg);
1178 if (option_arg == next_option)
1181 else if (strncmp(option, "-mytapes", option_len) == 0)
1183 options.mytapes = TRUE;
1185 else if (strncmp(option, "-network", option_len) == 0)
1187 options.network = TRUE;
1189 else if (strncmp(option, "-serveronly", option_len) == 0)
1191 options.serveronly = TRUE;
1193 else if (strncmp(option, "-debug", option_len) == 0)
1195 options.debug = TRUE;
1197 // optionally, debug output can be limited to a specific debug mode
1198 if (option_arg != next_option)
1199 options.debug_mode = getStringCopy(option_arg);
1201 else if (strncmp(option, "-verbose", option_len) == 0)
1203 options.verbose = TRUE;
1205 else if (strncmp(option, "-version", option_len) == 0 ||
1206 strncmp(option, "-V", option_len) == 0)
1208 print_version_function();
1212 else if (strPrefix(option, "-D"))
1214 options.special_flags = getStringCopy(&option[2]);
1216 else if (strncmp(option, "-execute", option_len) == 0)
1218 if (option_arg == NULL)
1219 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
1221 options.execute_command = getStringCopy(option_arg);
1222 if (option_arg == next_option)
1225 // when doing batch processing, always enable verbose mode (warnings)
1226 options.verbose = TRUE;
1228 #if defined(PLATFORM_MACOSX)
1229 else if (strPrefix(option, "-psn"))
1231 // ignore process serial number when launched via GUI on Mac OS X
1234 else if (*option == '-')
1236 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
1238 else if (options.server_host == NULL)
1240 options.server_host = *options_left;
1242 else if (options.server_port == 0)
1244 options.server_port = atoi(*options_left);
1245 if (options.server_port < 1024)
1246 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
1249 Error(ERR_EXIT_HELP, "too many arguments");
1256 // ----------------------------------------------------------------------------
1257 // error handling functions
1258 // ----------------------------------------------------------------------------
1260 #define MAX_INTERNAL_ERROR_SIZE 1024
1262 // used by SetError() and GetError() to store internal error messages
1263 static char internal_error[MAX_INTERNAL_ERROR_SIZE];
1265 void SetError(char *format, ...)
1269 va_start(ap, format);
1270 vsnprintf(internal_error, MAX_INTERNAL_ERROR_SIZE, format, ap);
1274 char *GetError(void)
1276 return internal_error;
1279 void Error(int mode, char *format, ...)
1281 static boolean last_line_was_separator = FALSE;
1282 char *process_name = "";
1284 if (program.log_file[LOG_ERR_ID] == NULL)
1287 #if defined(PLATFORM_ANDROID)
1288 android_log_prio = (mode & ERR_DEBUG ? ANDROID_LOG_DEBUG :
1289 mode & ERR_INFO ? ANDROID_LOG_INFO :
1290 mode & ERR_WARN ? ANDROID_LOG_WARN :
1291 mode & ERR_EXIT ? ANDROID_LOG_FATAL :
1292 ANDROID_LOG_UNKNOWN);
1295 // display debug messages only when running in debug mode
1296 if (mode & ERR_DEBUG && !options.debug)
1299 // display warnings only when running in verbose mode
1300 if (mode & ERR_WARN && !options.verbose)
1303 if (mode == ERR_INFO_LINE)
1305 if (!last_line_was_separator)
1306 printf_log_line(format, 79);
1308 last_line_was_separator = TRUE;
1313 last_line_was_separator = FALSE;
1315 if (mode & ERR_SOUND_SERVER)
1316 process_name = " sound server";
1317 else if (mode & ERR_NETWORK_SERVER)
1318 process_name = " network server";
1319 else if (mode & ERR_NETWORK_CLIENT)
1320 process_name = " network client **";
1324 #if !defined(PLATFORM_ANDROID)
1325 printf_log_nonewline("%s%s: ", program.command_basename, process_name);
1328 if (mode & ERR_WARN)
1329 printf_log_nonewline("warning: ");
1331 if (mode & ERR_EXIT)
1332 printf_log_nonewline("fatal error: ");
1336 va_start(ap, format);
1337 vprintf_log(format, ap);
1340 if ((mode & ERR_EXIT) && !(mode & ERR_FROM_SERVER))
1342 va_start(ap, format);
1343 program.exit_message_function(format, ap);
1348 if (mode & ERR_HELP)
1349 printf_log("%s: Try option '--help' for more information.",
1350 program.command_basename);
1352 if (mode & ERR_EXIT)
1353 printf_log("%s%s: aborting", program.command_basename, process_name);
1355 if (mode & ERR_EXIT)
1357 if (mode & ERR_FROM_SERVER)
1358 exit(1); // child process: normal exit
1360 program.exit_function(1); // main process: clean up stuff
1365 // ----------------------------------------------------------------------------
1366 // checked memory allocation and freeing functions
1367 // ----------------------------------------------------------------------------
1369 void *checked_malloc(unsigned int size)
1376 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
1381 void *checked_calloc(unsigned int size)
1385 ptr = calloc(1, size);
1388 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
1393 void *checked_realloc(void *ptr, unsigned int size)
1395 ptr = realloc(ptr, size);
1398 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
1403 void checked_free(void *ptr)
1405 if (ptr != NULL) // this check should be done by free() anyway
1409 void clear_mem(void *ptr, unsigned int size)
1411 #if defined(PLATFORM_WIN32)
1412 // for unknown reason, memset() sometimes crashes when compiled with MinGW
1413 char *cptr = (char *)ptr;
1418 memset(ptr, 0, size);
1423 // ----------------------------------------------------------------------------
1424 // various helper functions
1425 // ----------------------------------------------------------------------------
1427 void swap_numbers(int *i1, int *i2)
1435 void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
1447 /* the "put" variants of the following file access functions check for the file
1448 pointer being != NULL and return the number of bytes they have or would have
1449 written; this allows for chunk writing functions to first determine the size
1450 of the (not yet written) chunk, write the correct chunk size and finally
1451 write the chunk itself */
1453 int getFile8BitInteger(File *file)
1455 return getByteFromFile(file);
1458 int putFile8BitInteger(FILE *file, int value)
1466 int getFile16BitInteger(File *file, int byte_order)
1468 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1469 return ((getByteFromFile(file) << 8) |
1470 (getByteFromFile(file) << 0));
1471 else // BYTE_ORDER_LITTLE_ENDIAN
1472 return ((getByteFromFile(file) << 0) |
1473 (getByteFromFile(file) << 8));
1476 int putFile16BitInteger(FILE *file, int value, int byte_order)
1480 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1482 fputc((value >> 8) & 0xff, file);
1483 fputc((value >> 0) & 0xff, file);
1485 else // BYTE_ORDER_LITTLE_ENDIAN
1487 fputc((value >> 0) & 0xff, file);
1488 fputc((value >> 8) & 0xff, file);
1495 int getFile32BitInteger(File *file, int byte_order)
1497 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1498 return ((getByteFromFile(file) << 24) |
1499 (getByteFromFile(file) << 16) |
1500 (getByteFromFile(file) << 8) |
1501 (getByteFromFile(file) << 0));
1502 else // BYTE_ORDER_LITTLE_ENDIAN
1503 return ((getByteFromFile(file) << 0) |
1504 (getByteFromFile(file) << 8) |
1505 (getByteFromFile(file) << 16) |
1506 (getByteFromFile(file) << 24));
1509 int putFile32BitInteger(FILE *file, int value, int byte_order)
1513 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
1515 fputc((value >> 24) & 0xff, file);
1516 fputc((value >> 16) & 0xff, file);
1517 fputc((value >> 8) & 0xff, file);
1518 fputc((value >> 0) & 0xff, file);
1520 else // BYTE_ORDER_LITTLE_ENDIAN
1522 fputc((value >> 0) & 0xff, file);
1523 fputc((value >> 8) & 0xff, file);
1524 fputc((value >> 16) & 0xff, file);
1525 fputc((value >> 24) & 0xff, file);
1532 boolean getFileChunk(File *file, char *chunk_name, int *chunk_size,
1535 const int chunk_name_length = 4;
1538 if (getStringFromFile(file, chunk_name, chunk_name_length + 1) == NULL)
1541 if (chunk_size != NULL)
1544 *chunk_size = getFile32BitInteger(file, byte_order);
1547 return (checkEndOfFile(file) ? FALSE : TRUE);
1550 int putFileChunk(FILE *file, char *chunk_name, int chunk_size,
1557 fputs(chunk_name, file);
1559 num_bytes += strlen(chunk_name);
1561 if (chunk_size >= 0)
1565 putFile32BitInteger(file, chunk_size, byte_order);
1573 int getFileVersion(File *file)
1575 int version_super = getByteFromFile(file);
1576 int version_major = getByteFromFile(file);
1577 int version_minor = getByteFromFile(file);
1578 int version_patch = getByteFromFile(file);
1580 return VERSION_IDENT(version_super, version_major, version_minor,
1584 int putFileVersion(FILE *file, int version)
1588 int version_super = VERSION_SUPER(version);
1589 int version_major = VERSION_MAJOR(version);
1590 int version_minor = VERSION_MINOR(version);
1591 int version_patch = VERSION_PATCH(version);
1593 fputc(version_super, file);
1594 fputc(version_major, file);
1595 fputc(version_minor, file);
1596 fputc(version_patch, file);
1602 void ReadBytesFromFile(File *file, byte *buffer, unsigned int bytes)
1606 for (i = 0; i < bytes && !checkEndOfFile(file); i++)
1607 buffer[i] = getByteFromFile(file);
1610 void WriteBytesToFile(FILE *file, byte *buffer, unsigned int bytes)
1614 for (i = 0; i < bytes; i++)
1615 fputc(buffer[i], file);
1618 void ReadUnusedBytesFromFile(File *file, unsigned int bytes)
1620 while (bytes-- && !checkEndOfFile(file))
1621 getByteFromFile(file);
1624 void WriteUnusedBytesToFile(FILE *file, unsigned int bytes)
1631 // ----------------------------------------------------------------------------
1632 // functions to translate key identifiers between different format
1633 // ----------------------------------------------------------------------------
1635 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
1636 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
1637 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
1638 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
1640 static void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
1649 // return and escape keys
1650 { KSYM_Return, "XK_Return", "return" },
1651 { KSYM_Escape, "XK_Escape", "escape" },
1653 // normal cursor keys
1654 { KSYM_Left, "XK_Left", "cursor left" },
1655 { KSYM_Right, "XK_Right", "cursor right" },
1656 { KSYM_Up, "XK_Up", "cursor up" },
1657 { KSYM_Down, "XK_Down", "cursor down" },
1659 // keypad cursor keys
1661 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
1662 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
1663 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
1664 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
1667 // other keypad keys
1668 #ifdef KSYM_KP_Enter
1669 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
1670 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
1671 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
1672 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
1673 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
1674 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
1678 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
1679 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
1680 { KSYM_Control_L, "XK_Control_L", "left control" },
1681 { KSYM_Control_R, "XK_Control_R", "right control" },
1682 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
1683 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
1684 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
1685 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
1686 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, // Alt-R
1687 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, // Ctrl-R
1689 // some special keys
1690 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
1691 { KSYM_Delete, "XK_Delete", "delete" },
1692 { KSYM_Insert, "XK_Insert", "insert" },
1693 { KSYM_Tab, "XK_Tab", "tab" },
1694 { KSYM_Home, "XK_Home", "home" },
1695 { KSYM_End, "XK_End", "end" },
1696 { KSYM_Page_Up, "XK_Page_Up", "page up" },
1697 { KSYM_Page_Down, "XK_Page_Down", "page down" },
1699 { KSYM_Select, "XK_Select", "select" },
1700 { KSYM_Menu, "XK_Menu", "menu" }, // menu key
1701 { KSYM_Back, "XK_Back", "back" }, // back key
1702 { KSYM_PlayPause, "XK_PlayPause", "play/pause" },
1703 #if defined(PLATFORM_ANDROID)
1704 { KSYM_Rewind, "XK_Rewind", "rewind" },
1705 { KSYM_FastForward, "XK_FastForward", "fast forward" },
1708 // ASCII 0x20 to 0x40 keys (except numbers)
1709 { KSYM_space, "XK_space", "space" },
1710 { KSYM_exclam, "XK_exclam", "!" },
1711 { KSYM_quotedbl, "XK_quotedbl", "\"" },
1712 { KSYM_numbersign, "XK_numbersign", "#" },
1713 { KSYM_dollar, "XK_dollar", "$" },
1714 { KSYM_percent, "XK_percent", "%" },
1715 { KSYM_ampersand, "XK_ampersand", "&" },
1716 { KSYM_apostrophe, "XK_apostrophe", "'" },
1717 { KSYM_parenleft, "XK_parenleft", "(" },
1718 { KSYM_parenright, "XK_parenright", ")" },
1719 { KSYM_asterisk, "XK_asterisk", "*" },
1720 { KSYM_plus, "XK_plus", "+" },
1721 { KSYM_comma, "XK_comma", "," },
1722 { KSYM_minus, "XK_minus", "-" },
1723 { KSYM_period, "XK_period", "." },
1724 { KSYM_slash, "XK_slash", "/" },
1725 { KSYM_colon, "XK_colon", ":" },
1726 { KSYM_semicolon, "XK_semicolon", ";" },
1727 { KSYM_less, "XK_less", "<" },
1728 { KSYM_equal, "XK_equal", "=" },
1729 { KSYM_greater, "XK_greater", ">" },
1730 { KSYM_question, "XK_question", "?" },
1731 { KSYM_at, "XK_at", "@" },
1734 { KSYM_bracketleft, "XK_bracketleft", "[" },
1735 { KSYM_backslash, "XK_backslash", "\\" },
1736 { KSYM_bracketright,"XK_bracketright", "]" },
1737 { KSYM_asciicircum, "XK_asciicircum", "^" },
1738 { KSYM_underscore, "XK_underscore", "_" },
1739 { KSYM_grave, "XK_grave", "grave" },
1740 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
1741 { KSYM_braceleft, "XK_braceleft", "brace left" },
1742 { KSYM_bar, "XK_bar", "bar" },
1743 { KSYM_braceright, "XK_braceright", "brace right" },
1744 { KSYM_asciitilde, "XK_asciitilde", "~" },
1746 // special (non-ASCII) keys
1747 { KSYM_degree, "XK_degree", "degree" },
1748 { KSYM_Adiaeresis, "XK_Adiaeresis", "A umlaut" },
1749 { KSYM_Odiaeresis, "XK_Odiaeresis", "O umlaut" },
1750 { KSYM_Udiaeresis, "XK_Udiaeresis", "U umlaut" },
1751 { KSYM_adiaeresis, "XK_adiaeresis", "a umlaut" },
1752 { KSYM_odiaeresis, "XK_odiaeresis", "o umlaut" },
1753 { KSYM_udiaeresis, "XK_udiaeresis", "u umlaut" },
1754 { KSYM_ssharp, "XK_ssharp", "sharp s" },
1756 // special (non-ASCII) keys (UTF-8, for reverse mapping only)
1757 { KSYM_degree, "XK_degree", "\xc2\xb0" },
1758 { KSYM_Adiaeresis, "XK_Adiaeresis", "\xc3\x84" },
1759 { KSYM_Odiaeresis, "XK_Odiaeresis", "\xc3\x96" },
1760 { KSYM_Udiaeresis, "XK_Udiaeresis", "\xc3\x9c" },
1761 { KSYM_adiaeresis, "XK_adiaeresis", "\xc3\xa4" },
1762 { KSYM_odiaeresis, "XK_odiaeresis", "\xc3\xb6" },
1763 { KSYM_udiaeresis, "XK_udiaeresis", "\xc3\xbc" },
1764 { KSYM_ssharp, "XK_ssharp", "\xc3\x9f" },
1766 // other keys (for reverse mapping only)
1767 { KSYM_space, "XK_space", " " },
1769 // keypad keys are not in numerical order in SDL2
1770 { KSYM_KP_0, "XK_KP_0", "keypad 0" },
1771 { KSYM_KP_1, "XK_KP_1", "keypad 1" },
1772 { KSYM_KP_2, "XK_KP_2", "keypad 2" },
1773 { KSYM_KP_3, "XK_KP_3", "keypad 3" },
1774 { KSYM_KP_4, "XK_KP_4", "keypad 4" },
1775 { KSYM_KP_5, "XK_KP_5", "keypad 5" },
1776 { KSYM_KP_6, "XK_KP_6", "keypad 6" },
1777 { KSYM_KP_7, "XK_KP_7", "keypad 7" },
1778 { KSYM_KP_8, "XK_KP_8", "keypad 8" },
1779 { KSYM_KP_9, "XK_KP_9", "keypad 9" },
1781 // end-of-array identifier
1787 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
1789 static char name_buffer[30];
1792 if (key >= KSYM_A && key <= KSYM_Z)
1793 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
1794 else if (key >= KSYM_a && key <= KSYM_z)
1795 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
1796 else if (key >= KSYM_0 && key <= KSYM_9)
1797 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
1798 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1799 sprintf(name_buffer, "F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1800 else if (key == KSYM_UNDEFINED)
1801 strcpy(name_buffer, "(undefined)");
1808 if (key == translate_key[i].key)
1810 strcpy(name_buffer, translate_key[i].name);
1814 while (translate_key[++i].name);
1816 if (!translate_key[i].name)
1817 strcpy(name_buffer, "(unknown)");
1820 *name = name_buffer;
1822 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
1824 static char name_buffer[30];
1827 if (key >= KSYM_A && key <= KSYM_Z)
1828 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
1829 else if (key >= KSYM_a && key <= KSYM_z)
1830 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
1831 else if (key >= KSYM_0 && key <= KSYM_9)
1832 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
1833 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1834 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1835 else if (key == KSYM_UNDEFINED)
1836 strcpy(name_buffer, "[undefined]");
1843 if (key == translate_key[i].key)
1845 strcpy(name_buffer, translate_key[i].x11name);
1849 while (translate_key[++i].x11name);
1851 if (!translate_key[i].x11name)
1852 sprintf(name_buffer, "0x%04x", (unsigned int)key);
1855 *x11name = name_buffer;
1857 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1859 Key key = KSYM_UNDEFINED;
1860 char *name_ptr = *name;
1862 if (strlen(*name) == 1)
1864 char c = name_ptr[0];
1866 if (c >= 'A' && c <= 'Z')
1867 key = KSYM_A + (Key)(c - 'A');
1868 else if (c >= 'a' && c <= 'z')
1869 key = KSYM_a + (Key)(c - 'a');
1870 else if (c >= '0' && c <= '9')
1871 key = KSYM_0 + (Key)(c - '0');
1874 if (key == KSYM_UNDEFINED)
1880 if (strEqual(translate_key[i].name, *name))
1882 key = translate_key[i].key;
1886 while (translate_key[++i].x11name);
1889 if (key == KSYM_UNDEFINED)
1890 Warn("getKeyFromKeyName(): not completely implemented");
1894 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1896 Key key = KSYM_UNDEFINED;
1897 char *name_ptr = *x11name;
1899 if (strPrefix(name_ptr, "XK_") && strlen(name_ptr) == 4)
1901 char c = name_ptr[3];
1903 if (c >= 'A' && c <= 'Z')
1904 key = KSYM_A + (Key)(c - 'A');
1905 else if (c >= 'a' && c <= 'z')
1906 key = KSYM_a + (Key)(c - 'a');
1907 else if (c >= '0' && c <= '9')
1908 key = KSYM_0 + (Key)(c - '0');
1910 else if (strPrefix(name_ptr, "XK_F") && strlen(name_ptr) <= 6)
1912 char c1 = name_ptr[4];
1913 char c2 = name_ptr[5];
1916 if ((c1 >= '0' && c1 <= '9') &&
1917 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1918 d = atoi(&name_ptr[4]);
1920 if (d >= 1 && d <= KSYM_NUM_FKEYS)
1921 key = KSYM_F1 + (Key)(d - 1);
1923 else if (strPrefix(name_ptr, "XK_"))
1929 if (strEqual(name_ptr, translate_key[i].x11name))
1931 key = translate_key[i].key;
1935 while (translate_key[++i].x11name);
1937 else if (strPrefix(name_ptr, "0x"))
1939 unsigned int value = 0;
1945 char c = *name_ptr++;
1948 if (c >= '0' && c <= '9')
1950 else if (c >= 'a' && c <= 'f')
1951 d = (int)(c - 'a' + 10);
1952 else if (c >= 'A' && c <= 'F')
1953 d = (int)(c - 'A' + 10);
1961 value = value * 16 + d;
1972 char *getKeyNameFromKey(Key key)
1976 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1980 char *getX11KeyNameFromKey(Key key)
1984 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1988 Key getKeyFromKeyName(char *name)
1992 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1996 Key getKeyFromX11KeyName(char *x11name)
2000 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
2004 char getCharFromKey(Key key)
2010 } translate_key_char[] =
2012 // special (non-ASCII) keys (ISO-8859-1)
2013 { KSYM_degree, CHAR_BYTE_DEGREE },
2014 { KSYM_Adiaeresis, CHAR_BYTE_UMLAUT_A },
2015 { KSYM_Odiaeresis, CHAR_BYTE_UMLAUT_O },
2016 { KSYM_Udiaeresis, CHAR_BYTE_UMLAUT_U },
2017 { KSYM_adiaeresis, CHAR_BYTE_UMLAUT_a },
2018 { KSYM_odiaeresis, CHAR_BYTE_UMLAUT_o },
2019 { KSYM_udiaeresis, CHAR_BYTE_UMLAUT_u },
2020 { KSYM_ssharp, CHAR_BYTE_SHARP_S },
2022 // end-of-array identifier
2026 char *keyname = getKeyNameFromKey(key);
2029 if (strlen(keyname) == 1)
2031 else if (strEqual(keyname, "space"))
2039 if (key == translate_key_char[i].key)
2041 c = translate_key_char[i].key_char;
2046 while (translate_key_char[++i].key_char);
2052 char getValidConfigValueChar(char c)
2054 if (c == '#' || // used to mark comments
2055 c == '\\') // used to mark continued lines
2062 // ----------------------------------------------------------------------------
2063 // functions to translate string identifiers to integer or boolean value
2064 // ----------------------------------------------------------------------------
2066 int get_integer_from_string(char *s)
2068 static char *number_text[][3] =
2070 { "0", "zero", "null", },
2071 { "1", "one", "first" },
2072 { "2", "two", "second" },
2073 { "3", "three", "third" },
2074 { "4", "four", "fourth" },
2075 { "5", "five", "fifth" },
2076 { "6", "six", "sixth" },
2077 { "7", "seven", "seventh" },
2078 { "8", "eight", "eighth" },
2079 { "9", "nine", "ninth" },
2080 { "10", "ten", "tenth" },
2081 { "11", "eleven", "eleventh" },
2082 { "12", "twelve", "twelfth" },
2084 { NULL, NULL, NULL },
2088 char *s_lower = getStringToLower(s);
2091 for (i = 0; number_text[i][0] != NULL; i++)
2092 for (j = 0; j < 3; j++)
2093 if (strEqual(s_lower, number_text[i][j]))
2098 if (strEqual(s_lower, "false") ||
2099 strEqual(s_lower, "no") ||
2100 strEqual(s_lower, "off"))
2102 else if (strEqual(s_lower, "true") ||
2103 strEqual(s_lower, "yes") ||
2104 strEqual(s_lower, "on"))
2115 boolean get_boolean_from_string(char *s)
2117 char *s_lower = getStringToLower(s);
2118 boolean result = FALSE;
2120 if (strEqual(s_lower, "true") ||
2121 strEqual(s_lower, "yes") ||
2122 strEqual(s_lower, "on") ||
2123 get_integer_from_string(s) == 1)
2131 int get_switch3_from_string(char *s)
2133 char *s_lower = getStringToLower(s);
2136 if (strEqual(s_lower, "true") ||
2137 strEqual(s_lower, "yes") ||
2138 strEqual(s_lower, "on") ||
2139 get_integer_from_string(s) == 1)
2141 else if (strEqual(s_lower, "auto"))
2149 int get_player_nr_from_string(char *s)
2151 static char *player_text[] =
2161 char *s_lower = getStringToLower(s);
2165 for (i = 0; player_text[i] != NULL; i++)
2166 if (strEqual(s_lower, player_text[i]))
2175 // ----------------------------------------------------------------------------
2176 // functions for generic lists
2177 // ----------------------------------------------------------------------------
2179 ListNode *newListNode(void)
2181 return checked_calloc(sizeof(ListNode));
2184 void addNodeToList(ListNode **node_first, char *key, void *content)
2186 ListNode *node_new = newListNode();
2188 node_new->key = getStringCopy(key);
2189 node_new->content = content;
2190 node_new->next = *node_first;
2193 (*node_first)->prev = node_new;
2195 *node_first = node_new;
2198 void deleteNodeFromList(ListNode **node_first, char *key,
2199 void (*destructor_function)(void *))
2201 if (node_first == NULL || *node_first == NULL)
2204 if (strEqual((*node_first)->key, key))
2206 // after first recursion, (*node_first)->prev->next == *node_first,
2207 // so *node_first would be overwritten with (*node_first)->next
2208 // => use a copy of *node_first (and later of (*node_first)->next)
2209 ListNode *node = *node_first;
2210 ListNode *node_next = node->next;
2212 checked_free(node->key);
2214 if (destructor_function)
2215 destructor_function(node->content);
2218 node->prev->next = node->next;
2221 node->next->prev = node->prev;
2225 // after removing node, set list pointer to next valid list node
2226 // (this is important if the first node of the list was deleted)
2227 *node_first = node_next;
2231 deleteNodeFromList(&(*node_first)->next, key, destructor_function);
2235 ListNode *getNodeFromKey(ListNode *node_first, char *key)
2237 if (node_first == NULL)
2240 if (strEqual(node_first->key, key))
2243 return getNodeFromKey(node_first->next, key);
2246 int getNumNodes(ListNode *node_first)
2248 return (node_first ? 1 + getNumNodes(node_first->next) : 0);
2252 static void dumpList(ListNode *node_first)
2254 ListNode *node = node_first;
2258 Debug("internal:dumpList", "['%s' (%d)]", node->key,
2259 ((struct ListNodeInfo *)node->content)->num_references);
2263 Debug("internal:dumpList", "[%d nodes]", getNumNodes(node_first));
2268 // ----------------------------------------------------------------------------
2269 // functions for file handling
2270 // ----------------------------------------------------------------------------
2272 #define MAX_BUFFER_SIZE 4096
2274 File *openFile(char *filename, char *mode)
2276 File *file = checked_calloc(sizeof(File));
2278 file->file = fopen(filename, mode);
2280 if (file->file != NULL)
2282 file->filename = getStringCopy(filename);
2287 #if defined(PLATFORM_ANDROID)
2288 file->asset_file = SDL_RWFromFile(filename, mode);
2290 if (file->asset_file != NULL)
2292 file->file_is_asset = TRUE;
2293 file->filename = getStringCopy(filename);
2304 int closeFile(File *file)
2311 #if defined(PLATFORM_ANDROID)
2312 if (file->asset_file)
2313 result = SDL_RWclose(file->asset_file);
2317 result = fclose(file->file);
2319 checked_free(file->filename);
2325 int checkEndOfFile(File *file)
2327 #if defined(PLATFORM_ANDROID)
2328 if (file->file_is_asset)
2329 return file->end_of_file;
2332 return feof(file->file);
2335 size_t readFile(File *file, void *buffer, size_t item_size, size_t num_items)
2337 #if defined(PLATFORM_ANDROID)
2338 if (file->file_is_asset)
2340 if (file->end_of_file)
2343 size_t num_items_read =
2344 SDL_RWread(file->asset_file, buffer, item_size, num_items);
2346 if (num_items_read < num_items)
2347 file->end_of_file = TRUE;
2349 return num_items_read;
2353 return fread(buffer, item_size, num_items, file->file);
2356 size_t writeFile(File *file, void *buffer, size_t item_size, size_t num_items)
2358 return fwrite(buffer, item_size, num_items, file->file);
2361 int seekFile(File *file, long offset, int whence)
2363 #if defined(PLATFORM_ANDROID)
2364 if (file->file_is_asset)
2366 int sdl_whence = (whence == SEEK_SET ? RW_SEEK_SET :
2367 whence == SEEK_CUR ? RW_SEEK_CUR :
2368 whence == SEEK_END ? RW_SEEK_END : 0);
2370 return (SDL_RWseek(file->asset_file, offset, sdl_whence) == -1 ? -1 : 0);
2374 return fseek(file->file, offset, whence);
2377 int getByteFromFile(File *file)
2379 #if defined(PLATFORM_ANDROID)
2380 if (file->file_is_asset)
2382 if (file->end_of_file)
2386 size_t num_bytes_read = SDL_RWread(file->asset_file, &c, 1, 1);
2388 if (num_bytes_read < 1)
2389 file->end_of_file = TRUE;
2391 return (file->end_of_file ? EOF : (int)c);
2395 return fgetc(file->file);
2398 char *getStringFromFile(File *file, char *line, int size)
2400 #if defined(PLATFORM_ANDROID)
2401 if (file->file_is_asset)
2403 if (file->end_of_file)
2406 char *line_ptr = line;
2407 int num_bytes_read = 0;
2409 while (num_bytes_read < size - 1 &&
2410 SDL_RWread(file->asset_file, line_ptr, 1, 1) == 1 &&
2411 *line_ptr++ != '\n')
2416 if (strlen(line) == 0)
2418 file->end_of_file = TRUE;
2427 return fgets(line, size, file->file);
2430 int copyFile(char *filename_from, char *filename_to)
2432 File *file_from, *file_to;
2434 if ((file_from = openFile(filename_from, MODE_READ)) == NULL)
2439 if ((file_to = openFile(filename_to, MODE_WRITE)) == NULL)
2441 closeFile(file_from);
2446 while (!checkEndOfFile(file_from))
2448 byte buffer[MAX_BUFFER_SIZE];
2449 size_t bytes_read = readFile(file_from, buffer, 1, MAX_BUFFER_SIZE);
2451 writeFile(file_to, buffer, 1, bytes_read);
2454 closeFile(file_from);
2461 // ----------------------------------------------------------------------------
2462 // functions for directory handling
2463 // ----------------------------------------------------------------------------
2465 Directory *openDirectory(char *dir_name)
2467 Directory *dir = checked_calloc(sizeof(Directory));
2469 dir->dir = opendir(dir_name);
2471 if (dir->dir != NULL)
2473 dir->filename = getStringCopy(dir_name);
2478 #if defined(PLATFORM_ANDROID)
2479 char *asset_toc_filename = getPath2(dir_name, ASSET_TOC_BASENAME);
2481 dir->asset_toc_file = SDL_RWFromFile(asset_toc_filename, MODE_READ);
2483 checked_free(asset_toc_filename);
2485 if (dir->asset_toc_file != NULL)
2487 dir->directory_is_asset = TRUE;
2488 dir->filename = getStringCopy(dir_name);
2499 int closeDirectory(Directory *dir)
2506 #if defined(PLATFORM_ANDROID)
2507 if (dir->asset_toc_file)
2508 result = SDL_RWclose(dir->asset_toc_file);
2512 result = closedir(dir->dir);
2515 freeDirectoryEntry(dir->dir_entry);
2517 checked_free(dir->filename);
2523 DirectoryEntry *readDirectory(Directory *dir)
2526 freeDirectoryEntry(dir->dir_entry);
2528 dir->dir_entry = NULL;
2530 #if defined(PLATFORM_ANDROID)
2531 if (dir->directory_is_asset)
2533 char line[MAX_LINE_LEN];
2534 char *line_ptr = line;
2535 int num_bytes_read = 0;
2537 while (num_bytes_read < MAX_LINE_LEN - 1 &&
2538 SDL_RWread(dir->asset_toc_file, line_ptr, 1, 1) == 1 &&
2547 if (strlen(line) == 0)
2550 dir->dir_entry = checked_calloc(sizeof(DirectoryEntry));
2552 dir->dir_entry->is_directory = FALSE;
2553 if (line[strlen(line) - 1] == '/')
2555 dir->dir_entry->is_directory = TRUE;
2557 line[strlen(line) - 1] = '\0';
2560 dir->dir_entry->basename = getStringCopy(line);
2561 dir->dir_entry->filename = getPath2(dir->filename, line);
2563 return dir->dir_entry;
2567 struct dirent *dir_entry = readdir(dir->dir);
2569 if (dir_entry == NULL)
2572 dir->dir_entry = checked_calloc(sizeof(DirectoryEntry));
2574 dir->dir_entry->basename = getStringCopy(dir_entry->d_name);
2575 dir->dir_entry->filename = getPath2(dir->filename, dir_entry->d_name);
2577 struct stat file_status;
2579 dir->dir_entry->is_directory =
2580 (stat(dir->dir_entry->filename, &file_status) == 0 &&
2581 S_ISDIR(file_status.st_mode));
2583 return dir->dir_entry;
2586 void freeDirectoryEntry(DirectoryEntry *dir_entry)
2588 if (dir_entry == NULL)
2591 checked_free(dir_entry->basename);
2592 checked_free(dir_entry->filename);
2593 checked_free(dir_entry);
2597 // ----------------------------------------------------------------------------
2598 // functions for checking files and filenames
2599 // ----------------------------------------------------------------------------
2601 boolean directoryExists(char *dir_name)
2603 if (dir_name == NULL)
2606 struct stat file_status;
2607 boolean success = (stat(dir_name, &file_status) == 0 &&
2608 S_ISDIR(file_status.st_mode));
2610 #if defined(PLATFORM_ANDROID)
2613 // this might be an asset directory; check by trying to open toc file
2614 char *asset_toc_filename = getPath2(dir_name, ASSET_TOC_BASENAME);
2615 SDL_RWops *file = SDL_RWFromFile(asset_toc_filename, MODE_READ);
2617 checked_free(asset_toc_filename);
2619 success = (file != NULL);
2629 boolean fileExists(char *filename)
2631 if (filename == NULL)
2634 boolean success = (access(filename, F_OK) == 0);
2636 #if defined(PLATFORM_ANDROID)
2639 // this might be an asset file; check by trying to open it
2640 SDL_RWops *file = SDL_RWFromFile(filename, MODE_READ);
2642 success = (file != NULL);
2653 static boolean fileHasPrefix(char *basename, char *prefix)
2655 static char *basename_lower = NULL;
2656 int basename_length, prefix_length;
2658 checked_free(basename_lower);
2660 if (basename == NULL || prefix == NULL)
2663 basename_lower = getStringToLower(basename);
2664 basename_length = strlen(basename_lower);
2665 prefix_length = strlen(prefix);
2667 if (basename_length > prefix_length + 1 &&
2668 basename_lower[prefix_length] == '.' &&
2669 strncmp(basename_lower, prefix, prefix_length) == 0)
2676 static boolean fileHasSuffix(char *basename, char *suffix)
2678 static char *basename_lower = NULL;
2679 int basename_length, suffix_length;
2681 checked_free(basename_lower);
2683 if (basename == NULL || suffix == NULL)
2686 basename_lower = getStringToLower(basename);
2687 basename_length = strlen(basename_lower);
2688 suffix_length = strlen(suffix);
2690 if (basename_length > suffix_length + 1 &&
2691 basename_lower[basename_length - suffix_length - 1] == '.' &&
2692 strEqual(&basename_lower[basename_length - suffix_length], suffix))
2698 static boolean FileCouldBeArtwork(char *filename)
2700 char *basename = getBaseNamePtr(filename);
2702 return (!strEqual(basename, ".") &&
2703 !strEqual(basename, "..") &&
2704 !fileHasSuffix(basename, "txt") &&
2705 !fileHasSuffix(basename, "conf") &&
2706 !directoryExists(filename));
2709 boolean FileIsGraphic(char *filename)
2711 return FileCouldBeArtwork(filename);
2714 boolean FileIsSound(char *filename)
2716 return FileCouldBeArtwork(filename);
2719 boolean FileIsMusic(char *filename)
2721 return FileCouldBeArtwork(filename);
2724 boolean FileIsArtworkType(char *filename, int type)
2726 if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(filename)) ||
2727 (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(filename)) ||
2728 (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(filename)))
2734 // ----------------------------------------------------------------------------
2735 // functions for loading artwork configuration information
2736 // ----------------------------------------------------------------------------
2738 char *get_mapped_token(char *token)
2740 // !!! make this dynamically configurable (init.c:InitArtworkConfig) !!!
2741 static char *map_token_prefix[][2] =
2743 { "char_procent", "char_percent" },
2744 { "bd_magic_wall_filling", "bd_magic_wall.filling" },
2745 { "bd_magic_wall_emptying", "bd_magic_wall.emptying" },
2746 { "bd_butterfly_left", "bd_butterfly.left" },
2747 { "bd_butterfly_right", "bd_butterfly.right" },
2748 { "bd_butterfly_up", "bd_butterfly.up" },
2749 { "bd_butterfly_down", "bd_butterfly.down" },
2750 { "bd_firefly_left", "bd_firefly.left" },
2751 { "bd_firefly_right", "bd_firefly.right" },
2752 { "bd_firefly_up", "bd_firefly.up" },
2753 { "bd_firefly_down", "bd_firefly.down" },
2759 for (i = 0; map_token_prefix[i][0] != NULL; i++)
2761 int len_token_prefix = strlen(map_token_prefix[i][0]);
2763 if (strncmp(token, map_token_prefix[i][0], len_token_prefix) == 0)
2764 return getStringCat2(map_token_prefix[i][1], &token[len_token_prefix]);
2767 // change tokens containing ".gfx" by moving the "gfx" part to the very left
2768 char *gfx_substring = ".gfx";
2769 char *gfx_prefix = "gfx.";
2770 if (strstr(token, gfx_substring) != NULL)
2772 char *token_prefix = getStringCopy(token);
2773 char *token_gfx_pos = strstr(token_prefix, gfx_substring);
2774 char *token_suffix = &token_gfx_pos[strlen(gfx_substring)];
2777 // cut off token string at ".gfx" substring position
2778 *token_gfx_pos = '\0';
2780 // put together prefix "gfx." and token prefix and suffix without ".gfx"
2781 mapped_token = getStringCat3(gfx_prefix, token_prefix, token_suffix);
2785 return mapped_token;
2791 static char *get_special_base_token(struct ArtworkListInfo *artwork_info,
2794 // !!! make this dynamically configurable (init.c:InitArtworkConfig) !!!
2795 static struct ConfigTypeInfo prefix_list[] =
2797 { "global.anim_1" },
2798 { "global.anim_2" },
2799 { "global.anim_3" },
2800 { "global.anim_4" },
2801 { "global.anim_5" },
2802 { "global.anim_6" },
2803 { "global.anim_7" },
2804 { "global.anim_8" },
2805 { "global.anim_9" },
2806 { "global.anim_10" },
2807 { "global.anim_11" },
2808 { "global.anim_12" },
2809 { "global.anim_13" },
2810 { "global.anim_14" },
2811 { "global.anim_15" },
2812 { "global.anim_16" },
2813 { "global.anim_17" },
2814 { "global.anim_18" },
2815 { "global.anim_19" },
2816 { "global.anim_20" },
2817 { "global.anim_21" },
2818 { "global.anim_22" },
2819 { "global.anim_23" },
2820 { "global.anim_24" },
2821 { "global.anim_25" },
2822 { "global.anim_26" },
2823 { "global.anim_27" },
2824 { "global.anim_28" },
2825 { "global.anim_29" },
2826 { "global.anim_30" },
2827 { "global.anim_31" },
2828 { "global.anim_32" },
2832 struct ConfigTypeInfo *suffix_list = artwork_info->suffix_list;
2833 boolean prefix_found = FALSE;
2837 // search for prefix to check if base token has to be created
2838 for (i = 0; prefix_list[i].token != NULL; i++)
2839 if (strPrefix(token, prefix_list[i].token))
2840 prefix_found = TRUE;
2845 // search for suffix (parameter) to determine base token length
2846 for (i = 0; suffix_list[i].token != NULL; i++)
2847 if (strSuffix(token, suffix_list[i].token))
2848 len_suffix = strlen(suffix_list[i].token);
2850 return getStringCopyN(token, strlen(token) - len_suffix);
2853 static void FreeCustomArtworkList(struct ArtworkListInfo *,
2854 struct ListNodeInfo ***, int *);
2856 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
2857 struct ConfigTypeInfo *suffix_list,
2858 char **ignore_tokens,
2859 int num_file_list_entries)
2861 SetupFileHash *ignore_tokens_hash;
2862 struct FileInfo *file_list;
2863 int num_file_list_entries_found = 0;
2864 int num_suffix_list_entries = 0;
2868 // create hash from list of tokens to be ignored (for quick access)
2869 ignore_tokens_hash = newSetupFileHash();
2870 for (i = 0; ignore_tokens[i] != NULL; i++)
2871 setHashEntry(ignore_tokens_hash, ignore_tokens[i], "");
2873 file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo));
2875 for (i = 0; suffix_list[i].token != NULL; i++)
2876 num_suffix_list_entries++;
2878 // always start with reliable default values
2879 for (i = 0; i < num_file_list_entries; i++)
2881 file_list[i].token = NULL;
2883 file_list[i].default_filename = NULL;
2884 file_list[i].filename = NULL;
2886 if (num_suffix_list_entries > 0)
2888 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
2890 file_list[i].default_parameter = checked_calloc(parameter_array_size);
2891 file_list[i].parameter = checked_calloc(parameter_array_size);
2893 for (j = 0; j < num_suffix_list_entries; j++)
2895 setString(&file_list[i].default_parameter[j], suffix_list[j].value);
2896 setString(&file_list[i].parameter[j], suffix_list[j].value);
2899 file_list[i].redefined = FALSE;
2900 file_list[i].fallback_to_default = FALSE;
2901 file_list[i].default_is_cloned = FALSE;
2907 for (i = 0; config_list[i].token != NULL; i++)
2909 int len_config_token = strlen(config_list[i].token);
2910 boolean is_file_entry = TRUE;
2912 for (j = 0; suffix_list[j].token != NULL; j++)
2914 int len_suffix = strlen(suffix_list[j].token);
2916 if (len_suffix < len_config_token &&
2917 strEqual(&config_list[i].token[len_config_token - len_suffix],
2918 suffix_list[j].token))
2920 setString(&file_list[list_pos].default_parameter[j],
2921 config_list[i].value);
2923 is_file_entry = FALSE;
2929 // the following tokens are no file definitions, but other config tokens
2930 if (getHashEntry(ignore_tokens_hash, config_list[i].token) != NULL)
2931 is_file_entry = FALSE;
2938 if (list_pos >= num_file_list_entries)
2941 file_list[list_pos].token = config_list[i].token;
2942 file_list[list_pos].default_filename = config_list[i].value;
2945 if (strSuffix(config_list[i].token, ".clone_from"))
2946 file_list[list_pos].default_is_cloned = TRUE;
2949 num_file_list_entries_found = list_pos + 1;
2950 if (num_file_list_entries_found != num_file_list_entries)
2952 Error(ERR_INFO_LINE, "-");
2953 Error(ERR_INFO, "inconsistant config list information:");
2954 Error(ERR_INFO, "- should be: %d (according to 'src/conf_xxx.h')",
2955 num_file_list_entries);
2956 Error(ERR_INFO, "- found to be: %d (according to 'src/conf_xxx.c')",
2957 num_file_list_entries_found);
2958 Error(ERR_EXIT, "please fix");
2961 freeSetupFileHash(ignore_tokens_hash);
2966 static boolean token_suffix_match(char *token, char *suffix, int start_pos)
2968 int len_token = strlen(token);
2969 int len_suffix = strlen(suffix);
2971 if (start_pos < 0) // compare suffix from end of string
2972 start_pos += len_token;
2974 if (start_pos < 0 || start_pos + len_suffix > len_token)
2977 if (strncmp(&token[start_pos], suffix, len_suffix) != 0)
2980 if (token[start_pos + len_suffix] == '\0')
2983 if (token[start_pos + len_suffix] == '.')
2989 #define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN_VALUE]"
2991 static void read_token_parameters(SetupFileHash *setup_file_hash,
2992 struct ConfigTypeInfo *suffix_list,
2993 struct FileInfo *file_list_entry)
2995 // check for config token that is the base token without any suffixes
2996 char *filename = getHashEntry(setup_file_hash, file_list_entry->token);
2997 char *known_token_value = KNOWN_TOKEN_VALUE;
3000 if (filename != NULL)
3002 setString(&file_list_entry->filename, filename);
3004 // when file definition found, set all parameters to default values
3005 for (i = 0; suffix_list[i].token != NULL; i++)
3006 setString(&file_list_entry->parameter[i], suffix_list[i].value);
3008 file_list_entry->redefined = TRUE;
3010 // mark config file token as well known from default config
3011 setHashEntry(setup_file_hash, file_list_entry->token, known_token_value);
3014 // check for config tokens that can be build by base token and suffixes
3015 for (i = 0; suffix_list[i].token != NULL; i++)
3017 char *token = getStringCat2(file_list_entry->token, suffix_list[i].token);
3018 char *value = getHashEntry(setup_file_hash, token);
3022 setString(&file_list_entry->parameter[i], value);
3024 // mark config file token as well known from default config
3025 setHashEntry(setup_file_hash, token, known_token_value);
3032 static void add_dynamic_file_list_entry(struct FileInfo **list,
3033 int *num_list_entries,
3034 SetupFileHash *extra_file_hash,
3035 struct ConfigTypeInfo *suffix_list,
3036 int num_suffix_list_entries,
3039 struct FileInfo *new_list_entry;
3040 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
3042 (*num_list_entries)++;
3043 *list = checked_realloc(*list, *num_list_entries * sizeof(struct FileInfo));
3044 new_list_entry = &(*list)[*num_list_entries - 1];
3046 new_list_entry->token = getStringCopy(token);
3047 new_list_entry->default_filename = NULL;
3048 new_list_entry->filename = NULL;
3049 new_list_entry->parameter = checked_calloc(parameter_array_size);
3051 new_list_entry->redefined = FALSE;
3052 new_list_entry->fallback_to_default = FALSE;
3053 new_list_entry->default_is_cloned = FALSE;
3055 read_token_parameters(extra_file_hash, suffix_list, new_list_entry);
3058 static void add_property_mapping(struct PropertyMapping **list,
3059 int *num_list_entries,
3060 int base_index, int ext1_index,
3061 int ext2_index, int ext3_index,
3064 struct PropertyMapping *new_list_entry;
3066 (*num_list_entries)++;
3067 *list = checked_realloc(*list,
3068 *num_list_entries * sizeof(struct PropertyMapping));
3069 new_list_entry = &(*list)[*num_list_entries - 1];
3071 new_list_entry->base_index = base_index;
3072 new_list_entry->ext1_index = ext1_index;
3073 new_list_entry->ext2_index = ext2_index;
3074 new_list_entry->ext3_index = ext3_index;
3076 new_list_entry->artwork_index = artwork_index;
3079 static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info,
3082 struct FileInfo *file_list = artwork_info->file_list;
3083 struct ConfigTypeInfo *suffix_list = artwork_info->suffix_list;
3084 char **base_prefixes = artwork_info->base_prefixes;
3085 char **ext1_suffixes = artwork_info->ext1_suffixes;
3086 char **ext2_suffixes = artwork_info->ext2_suffixes;
3087 char **ext3_suffixes = artwork_info->ext3_suffixes;
3088 char **ignore_tokens = artwork_info->ignore_tokens;
3089 int num_file_list_entries = artwork_info->num_file_list_entries;
3090 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
3091 int num_base_prefixes = artwork_info->num_base_prefixes;
3092 int num_ext1_suffixes = artwork_info->num_ext1_suffixes;
3093 int num_ext2_suffixes = artwork_info->num_ext2_suffixes;
3094 int num_ext3_suffixes = artwork_info->num_ext3_suffixes;
3095 int num_ignore_tokens = artwork_info->num_ignore_tokens;
3096 SetupFileHash *setup_file_hash, *valid_file_hash, *valid_file_hash_tmp;
3097 SetupFileHash *extra_file_hash, *empty_file_hash;
3098 char *known_token_value = KNOWN_TOKEN_VALUE;
3099 char *base_token_value = UNDEFINED_FILENAME;
3102 if (filename == NULL)
3105 if ((setup_file_hash = loadSetupFileHash(filename)) == NULL)
3108 // separate valid (defined) from empty (undefined) config token values
3109 valid_file_hash = newSetupFileHash();
3110 empty_file_hash = newSetupFileHash();
3111 BEGIN_HASH_ITERATION(setup_file_hash, itr)
3113 char *value = HASH_ITERATION_VALUE(itr);
3115 setHashEntry(*value ? valid_file_hash : empty_file_hash,
3116 HASH_ITERATION_TOKEN(itr), value);
3118 END_HASH_ITERATION(setup_file_hash, itr)
3120 // at this point, we do not need the setup file hash anymore -- free it
3121 freeSetupFileHash(setup_file_hash);
3123 // prevent changing hash while iterating over it by using a temporary copy
3124 valid_file_hash_tmp = newSetupFileHash();
3125 BEGIN_HASH_ITERATION(valid_file_hash, itr)
3127 setHashEntry(valid_file_hash_tmp,
3128 HASH_ITERATION_TOKEN(itr),
3129 HASH_ITERATION_VALUE(itr));
3131 END_HASH_ITERATION(valid_file_hash, itr)
3133 // (iterate over same temporary hash, as modifications are independent)
3135 // map deprecated to current tokens (using prefix match and replace)
3136 BEGIN_HASH_ITERATION(valid_file_hash_tmp, itr)
3138 char *token = HASH_ITERATION_TOKEN(itr);
3139 char *mapped_token = get_mapped_token(token);
3141 if (mapped_token != NULL)
3143 char *value = HASH_ITERATION_VALUE(itr);
3146 setHashEntry(valid_file_hash, mapped_token, value);
3148 // ignore old token (by setting it to "known" keyword)
3149 setHashEntry(valid_file_hash, token, known_token_value);
3154 END_HASH_ITERATION(valid_file_hash_tmp, itr)
3156 // add special base tokens (using prefix match and replace)
3157 BEGIN_HASH_ITERATION(valid_file_hash_tmp, itr)
3159 char *token = HASH_ITERATION_TOKEN(itr);
3160 char *base_token = get_special_base_token(artwork_info, token);
3162 if (base_token != NULL)
3164 // add base token only if it does not already exist
3165 if (getHashEntry(valid_file_hash, base_token) == NULL)
3166 setHashEntry(valid_file_hash, base_token, base_token_value);
3171 END_HASH_ITERATION(valid_file_hash_tmp, itr)
3173 // free temporary hash used for iteration
3174 freeSetupFileHash(valid_file_hash_tmp);
3176 // read parameters for all known config file tokens
3177 for (i = 0; i < num_file_list_entries; i++)
3178 read_token_parameters(valid_file_hash, suffix_list, &file_list[i]);
3180 // set all tokens that can be ignored here to "known" keyword
3181 for (i = 0; i < num_ignore_tokens; i++)
3182 setHashEntry(valid_file_hash, ignore_tokens[i], known_token_value);
3184 // copy all unknown config file tokens to extra config hash
3185 extra_file_hash = newSetupFileHash();
3186 BEGIN_HASH_ITERATION(valid_file_hash, itr)
3188 char *value = HASH_ITERATION_VALUE(itr);
3190 if (!strEqual(value, known_token_value))
3191 setHashEntry(extra_file_hash, HASH_ITERATION_TOKEN(itr), value);
3193 END_HASH_ITERATION(valid_file_hash, itr)
3195 // at this point, we do not need the valid file hash anymore -- free it
3196 freeSetupFileHash(valid_file_hash);
3198 // now try to determine valid, dynamically defined config tokens
3200 BEGIN_HASH_ITERATION(extra_file_hash, itr)
3202 struct FileInfo **dynamic_file_list =
3203 &artwork_info->dynamic_file_list;
3204 int *num_dynamic_file_list_entries =
3205 &artwork_info->num_dynamic_file_list_entries;
3206 struct PropertyMapping **property_mapping =
3207 &artwork_info->property_mapping;
3208 int *num_property_mapping_entries =
3209 &artwork_info->num_property_mapping_entries;
3210 int current_summarized_file_list_entry =
3211 artwork_info->num_file_list_entries +
3212 artwork_info->num_dynamic_file_list_entries;
3213 char *token = HASH_ITERATION_TOKEN(itr);
3214 int len_token = strlen(token);
3216 boolean base_prefix_found = FALSE;
3217 boolean parameter_suffix_found = FALSE;
3219 // skip all parameter definitions (handled by read_token_parameters())
3220 for (i = 0; i < num_suffix_list_entries && !parameter_suffix_found; i++)
3222 int len_suffix = strlen(suffix_list[i].token);
3224 if (token_suffix_match(token, suffix_list[i].token, -len_suffix))
3225 parameter_suffix_found = TRUE;
3228 if (parameter_suffix_found)
3231 // ---------- step 0: search for matching base prefix ----------
3234 for (i = 0; i < num_base_prefixes && !base_prefix_found; i++)
3236 char *base_prefix = base_prefixes[i];
3237 int len_base_prefix = strlen(base_prefix);
3238 boolean ext1_suffix_found = FALSE;
3239 boolean ext2_suffix_found = FALSE;
3240 boolean ext3_suffix_found = FALSE;
3241 boolean exact_match = FALSE;
3242 int base_index = -1;
3243 int ext1_index = -1;
3244 int ext2_index = -1;
3245 int ext3_index = -1;
3247 base_prefix_found = token_suffix_match(token, base_prefix, start_pos);
3249 if (!base_prefix_found)
3254 if (start_pos + len_base_prefix == len_token) // exact match
3258 add_dynamic_file_list_entry(dynamic_file_list,
3259 num_dynamic_file_list_entries,
3262 num_suffix_list_entries,
3264 add_property_mapping(property_mapping,
3265 num_property_mapping_entries,
3266 base_index, -1, -1, -1,
3267 current_summarized_file_list_entry);
3271 // ---------- step 1: search for matching first suffix ----------
3273 start_pos += len_base_prefix;
3274 for (j = 0; j < num_ext1_suffixes && !ext1_suffix_found; j++)
3276 char *ext1_suffix = ext1_suffixes[j];
3277 int len_ext1_suffix = strlen(ext1_suffix);
3279 ext1_suffix_found = token_suffix_match(token, ext1_suffix, start_pos);
3281 if (!ext1_suffix_found)
3286 if (start_pos + len_ext1_suffix == len_token) // exact match
3290 add_dynamic_file_list_entry(dynamic_file_list,
3291 num_dynamic_file_list_entries,
3294 num_suffix_list_entries,
3296 add_property_mapping(property_mapping,
3297 num_property_mapping_entries,
3298 base_index, ext1_index, -1, -1,
3299 current_summarized_file_list_entry);
3303 start_pos += len_ext1_suffix;
3309 // ---------- step 2: search for matching second suffix ----------
3311 for (k = 0; k < num_ext2_suffixes && !ext2_suffix_found; k++)
3313 char *ext2_suffix = ext2_suffixes[k];
3314 int len_ext2_suffix = strlen(ext2_suffix);
3316 ext2_suffix_found = token_suffix_match(token, ext2_suffix, start_pos);
3318 if (!ext2_suffix_found)
3323 if (start_pos + len_ext2_suffix == len_token) // exact match
3327 add_dynamic_file_list_entry(dynamic_file_list,
3328 num_dynamic_file_list_entries,
3331 num_suffix_list_entries,
3333 add_property_mapping(property_mapping,
3334 num_property_mapping_entries,
3335 base_index, ext1_index, ext2_index, -1,
3336 current_summarized_file_list_entry);
3340 start_pos += len_ext2_suffix;
3346 // ---------- step 3: search for matching third suffix ----------
3348 for (l = 0; l < num_ext3_suffixes && !ext3_suffix_found; l++)
3350 char *ext3_suffix = ext3_suffixes[l];
3351 int len_ext3_suffix = strlen(ext3_suffix);
3353 ext3_suffix_found = token_suffix_match(token, ext3_suffix, start_pos);
3355 if (!ext3_suffix_found)
3360 if (start_pos + len_ext3_suffix == len_token) // exact match
3364 add_dynamic_file_list_entry(dynamic_file_list,
3365 num_dynamic_file_list_entries,
3368 num_suffix_list_entries,
3370 add_property_mapping(property_mapping,
3371 num_property_mapping_entries,
3372 base_index, ext1_index, ext2_index, ext3_index,
3373 current_summarized_file_list_entry);
3379 END_HASH_ITERATION(extra_file_hash, itr)
3381 if (artwork_info->num_dynamic_file_list_entries > 0)
3383 artwork_info->dynamic_artwork_list =
3384 checked_calloc(artwork_info->num_dynamic_file_list_entries *
3385 artwork_info->sizeof_artwork_list_entry);
3388 if (options.verbose && IS_PARENT_PROCESS())
3390 SetupFileList *setup_file_list, *list;
3391 boolean dynamic_tokens_found = FALSE;
3392 boolean unknown_tokens_found = FALSE;
3393 boolean undefined_values_found = (hashtable_count(empty_file_hash) != 0);
3395 // list may be NULL for empty artwork config files
3396 setup_file_list = loadSetupFileList(filename);
3398 BEGIN_HASH_ITERATION(extra_file_hash, itr)
3400 if (strEqual(HASH_ITERATION_VALUE(itr), known_token_value))
3401 dynamic_tokens_found = TRUE;
3403 unknown_tokens_found = TRUE;
3405 END_HASH_ITERATION(extra_file_hash, itr)
3407 if (options.debug && dynamic_tokens_found)
3409 Debug("config", "---");
3410 Debug("config", "dynamic token(s) found in config file:");
3411 Debug("config", "- config file: '%s'", filename);
3413 for (list = setup_file_list; list != NULL; list = list->next)
3415 char *value = getHashEntry(extra_file_hash, list->token);
3417 if (value != NULL && strEqual(value, known_token_value))
3418 Debug("config", "- dynamic token: '%s'", list->token);
3421 Debug("config", "---");
3424 if (unknown_tokens_found)
3427 Warn("unknown token(s) found in config file:");
3428 Warn("- config file: '%s'", filename);
3430 for (list = setup_file_list; list != NULL; list = list->next)
3432 char *value = getHashEntry(extra_file_hash, list->token);
3434 if (value != NULL && !strEqual(value, known_token_value))
3435 Warn("- dynamic token: '%s'", list->token);
3441 if (undefined_values_found)
3444 Warn("undefined values found in config file:");
3445 Warn("- config file: '%s'", filename);
3447 for (list = setup_file_list; list != NULL; list = list->next)
3449 char *value = getHashEntry(empty_file_hash, list->token);
3452 Warn("- undefined value for token: '%s'", list->token);
3458 freeSetupFileList(setup_file_list);
3461 freeSetupFileHash(extra_file_hash);
3462 freeSetupFileHash(empty_file_hash);
3465 void LoadArtworkConfig(struct ArtworkListInfo *artwork_info)
3467 struct FileInfo *file_list = artwork_info->file_list;
3468 int num_file_list_entries = artwork_info->num_file_list_entries;
3469 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
3470 char *filename_base = UNDEFINED_FILENAME, *filename_local;
3473 DrawInitText("Loading artwork config", 120, FC_GREEN);
3474 DrawInitText(ARTWORKINFO_FILENAME(artwork_info->type), 150, FC_YELLOW);
3476 // always start with reliable default values
3477 for (i = 0; i < num_file_list_entries; i++)
3479 setString(&file_list[i].filename, file_list[i].default_filename);
3481 for (j = 0; j < num_suffix_list_entries; j++)
3482 setString(&file_list[i].parameter[j], file_list[i].default_parameter[j]);
3484 file_list[i].redefined = FALSE;
3485 file_list[i].fallback_to_default = FALSE;
3488 // free previous dynamic artwork file array
3489 if (artwork_info->dynamic_file_list != NULL)
3491 for (i = 0; i < artwork_info->num_dynamic_file_list_entries; i++)
3493 free(artwork_info->dynamic_file_list[i].token);
3494 free(artwork_info->dynamic_file_list[i].filename);
3495 free(artwork_info->dynamic_file_list[i].parameter);
3498 free(artwork_info->dynamic_file_list);
3499 artwork_info->dynamic_file_list = NULL;
3501 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
3502 &artwork_info->num_dynamic_file_list_entries);
3505 // free previous property mapping
3506 if (artwork_info->property_mapping != NULL)
3508 free(artwork_info->property_mapping);
3510 artwork_info->property_mapping = NULL;
3511 artwork_info->num_property_mapping_entries = 0;
3514 if (!GFX_OVERRIDE_ARTWORK(artwork_info->type))
3516 // first look for special artwork configured in level series config
3517 filename_base = getCustomArtworkLevelConfigFilename(artwork_info->type);
3519 if (fileExists(filename_base))
3520 LoadArtworkConfigFromFilename(artwork_info, filename_base);
3523 filename_local = getCustomArtworkConfigFilename(artwork_info->type);
3525 if (filename_local != NULL && !strEqual(filename_base, filename_local))
3526 LoadArtworkConfigFromFilename(artwork_info, filename_local);
3529 static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info,
3530 struct ListNodeInfo **listnode)
3534 char *filename = (*listnode)->source_filename;
3536 if (--(*listnode)->num_references <= 0)
3537 deleteNodeFromList(&artwork_info->content_list, filename,
3538 artwork_info->free_artwork);
3544 static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
3545 struct ListNodeInfo **listnode,
3546 struct FileInfo *file_list_entry)
3556 char *basename = file_list_entry->filename;
3557 char *filename = getCustomArtworkFilename(basename, artwork_info->type);
3559 if (filename == NULL)
3561 Warn("cannot find artwork file '%s'", basename);
3563 basename = file_list_entry->default_filename;
3565 // fail for cloned default artwork that has no default filename defined
3566 if (file_list_entry->default_is_cloned &&
3567 strEqual(basename, UNDEFINED_FILENAME))
3569 int error_mode = ERR_WARN;
3571 // we can get away without sounds and music, but not without graphics
3572 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
3573 error_mode = ERR_EXIT;
3575 Error(error_mode, "token '%s' was cloned and has no default filename",
3576 file_list_entry->token);
3581 // dynamic artwork has no default filename / skip empty default artwork
3582 if (basename == NULL || strEqual(basename, UNDEFINED_FILENAME))
3585 file_list_entry->fallback_to_default = TRUE;
3587 Warn("trying default artwork file '%s'", basename);
3589 filename = getCustomArtworkFilename(basename, artwork_info->type);
3591 if (filename == NULL)
3593 int error_mode = ERR_WARN;
3595 // we can get away without sounds and music, but not without graphics
3596 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
3597 error_mode = ERR_EXIT;
3599 Error(error_mode, "cannot find default artwork file '%s'", basename);
3605 // check if the old and the new artwork file are the same
3606 if (*listnode && strEqual((*listnode)->source_filename, filename))
3608 // The old and new artwork are the same (have the same filename and path).
3609 // This usually means that this artwork does not exist in this artwork set
3610 // and a fallback to the existing artwork is done.
3615 // delete existing artwork file entry
3616 deleteArtworkListEntry(artwork_info, listnode);
3618 // check if the new artwork file already exists in the list of artwork
3619 if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL)
3621 *listnode = (struct ListNodeInfo *)node->content;
3622 (*listnode)->num_references++;
3627 DrawInitText(init_text[artwork_info->type], 120, FC_GREEN);
3628 DrawInitText(basename, 150, FC_YELLOW);
3630 if ((*listnode = artwork_info->load_artwork(filename)) != NULL)
3632 // add new artwork file entry to the list of artwork files
3633 (*listnode)->num_references = 1;
3634 addNodeToList(&artwork_info->content_list, (*listnode)->source_filename,
3639 int error_mode = ERR_WARN;
3641 // we can get away without sounds and music, but not without graphics
3642 if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
3643 error_mode = ERR_EXIT;
3645 Error(error_mode, "cannot load artwork file '%s'", basename);
3651 static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
3652 struct ListNodeInfo **listnode,
3653 struct FileInfo *file_list_entry)
3655 if (strEqual(file_list_entry->filename, UNDEFINED_FILENAME))
3657 deleteArtworkListEntry(artwork_info, listnode);
3662 replaceArtworkListEntry(artwork_info, listnode, file_list_entry);
3665 void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
3667 struct FileInfo *file_list = artwork_info->file_list;
3668 struct FileInfo *dynamic_file_list = artwork_info->dynamic_file_list;
3669 int num_file_list_entries = artwork_info->num_file_list_entries;
3670 int num_dynamic_file_list_entries =
3671 artwork_info->num_dynamic_file_list_entries;
3674 print_timestamp_init("ReloadCustomArtworkList");
3676 for (i = 0; i < num_file_list_entries; i++)
3677 LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[i],
3680 for (i = 0; i < num_dynamic_file_list_entries; i++)
3681 LoadCustomArtwork(artwork_info, &artwork_info->dynamic_artwork_list[i],
3682 &dynamic_file_list[i]);
3684 print_timestamp_done("ReloadCustomArtworkList");
3687 dumpList(artwork_info->content_list);
3691 static void FreeCustomArtworkList(struct ArtworkListInfo *artwork_info,
3692 struct ListNodeInfo ***list,
3693 int *num_list_entries)
3700 for (i = 0; i < *num_list_entries; i++)
3701 deleteArtworkListEntry(artwork_info, &(*list)[i]);
3705 *num_list_entries = 0;
3708 void FreeCustomArtworkLists(struct ArtworkListInfo *artwork_info)
3710 if (artwork_info == NULL)
3713 FreeCustomArtworkList(artwork_info, &artwork_info->artwork_list,
3714 &artwork_info->num_file_list_entries);
3716 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
3717 &artwork_info->num_dynamic_file_list_entries);
3721 // ----------------------------------------------------------------------------
3722 // functions only needed for non-Unix (non-command-line) systems
3723 // (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt")
3724 // (now also added for Windows, to create files in user data directory)
3725 // ----------------------------------------------------------------------------
3727 char *getLogFilename(char *basename)
3729 return getPath2(getUserGameDataDir(), basename);
3732 void OpenLogFiles(void)
3736 InitUserDataDirectory();
3738 for (i = 0; i < NUM_LOGS; i++)
3740 if ((program.log_file[i] = fopen(program.log_filename[i], MODE_WRITE))
3743 program.log_file[i] = program.log_file_default[i]; // reset to default
3745 Warn("cannot open file '%s' for writing: %s",
3746 program.log_filename[i], strerror(errno));
3749 // output should be unbuffered so it is not truncated in a crash
3750 setbuf(program.log_file[i], NULL);
3754 void CloseLogFiles(void)
3758 for (i = 0; i < NUM_LOGS; i++)
3759 if (program.log_file[i] != program.log_file_default[i])
3760 fclose(program.log_file[i]);
3763 void DumpLogFile(int nr)
3765 FILE *log_file = fopen(program.log_filename[nr], MODE_READ);
3767 if (log_file == NULL)
3770 while (!feof(log_file))
3771 fputc(fgetc(log_file), stdout);
3776 void NotifyUserAboutErrorFile(void)
3778 #if defined(PLATFORM_WIN32)
3779 char *title_text = getStringCat2(program.program_title, " Error Message");
3780 char *error_text = getStringCat2("The program was aborted due to an error; "
3781 "for details, see the following error file:"
3783 program.log_filename[LOG_ERR_ID]);
3785 MessageBox(NULL, error_text, title_text, MB_OK);
3790 // ----------------------------------------------------------------------------
3791 // the following is only for debugging purpose and normally not used
3792 // ----------------------------------------------------------------------------
3796 #define DEBUG_PRINT_INIT_TIMESTAMPS FALSE
3797 #define DEBUG_PRINT_INIT_TIMESTAMPS_DEPTH 10
3799 #define DEBUG_NUM_TIMESTAMPS 10
3800 #define DEBUG_TIME_IN_MICROSECONDS 0
3802 #if DEBUG_TIME_IN_MICROSECONDS
3803 static double Counter_Microseconds(void)
3805 static struct timeval base_time = { 0, 0 };
3806 struct timeval current_time;
3809 gettimeofday(¤t_time, NULL);
3811 // reset base time in case of wrap-around
3812 if (current_time.tv_sec < base_time.tv_sec)
3813 base_time = current_time;
3816 ((double)(current_time.tv_sec - base_time.tv_sec)) * 1000000 +
3817 ((double)(current_time.tv_usec - base_time.tv_usec));
3819 return counter; // return microseconds since last init
3823 static char *debug_print_timestamp_get_padding(int padding_size)
3825 static char *padding = NULL;
3826 int max_padding_size = 100;
3828 if (padding == NULL)
3830 padding = checked_calloc(max_padding_size + 1);
3831 memset(padding, ' ', max_padding_size);
3834 return &padding[MAX(0, max_padding_size - padding_size)];
3837 void debug_print_timestamp(int counter_nr, char *message)
3839 int indent_size = 8;
3840 int padding_size = 40;
3841 float timestamp_interval;
3844 Error(ERR_EXIT, "debugging: invalid negative counter");
3845 else if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
3846 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
3848 #if DEBUG_TIME_IN_MICROSECONDS
3849 static double counter[DEBUG_NUM_TIMESTAMPS][2];
3852 counter[counter_nr][0] = Counter_Microseconds();
3854 static int counter[DEBUG_NUM_TIMESTAMPS][2];
3857 counter[counter_nr][0] = Counter();
3860 timestamp_interval = counter[counter_nr][0] - counter[counter_nr][1];
3861 counter[counter_nr][1] = counter[counter_nr][0];
3864 Debug("time", "%s%s%s %.3f %s",
3865 debug_print_timestamp_get_padding(counter_nr * indent_size),
3867 debug_print_timestamp_get_padding(padding_size - strlen(message)),
3868 timestamp_interval / 1000,
3873 static void debug_print_parent_only(char *format, ...)
3875 if (!IS_PARENT_PROCESS())
3882 va_start(ap, format);
3883 vprintf(format, ap);
3893 static void print_timestamp_ext(char *message, char *mode)
3895 #if DEBUG_PRINT_INIT_TIMESTAMPS
3896 static char *debug_message = NULL;
3897 static char *last_message = NULL;
3898 static int counter_nr = 0;
3899 int max_depth = DEBUG_PRINT_INIT_TIMESTAMPS_DEPTH;
3901 checked_free(debug_message);
3902 debug_message = getStringCat3(mode, " ", message);
3904 if (strEqual(mode, "INIT"))
3906 debug_print_timestamp(counter_nr, NULL);
3908 if (counter_nr + 1 < max_depth)
3909 debug_print_timestamp(counter_nr, debug_message);
3913 debug_print_timestamp(counter_nr, NULL);
3915 else if (strEqual(mode, "DONE"))
3919 if (counter_nr + 1 < max_depth ||
3920 (counter_nr == 0 && max_depth == 1))
3922 last_message = message;
3924 if (counter_nr == 0 && max_depth == 1)
3926 checked_free(debug_message);
3927 debug_message = getStringCat3("TIME", " ", message);
3930 debug_print_timestamp(counter_nr, debug_message);
3933 else if (!strEqual(mode, "TIME") ||
3934 !strEqual(message, last_message))
3936 if (counter_nr < max_depth)
3937 debug_print_timestamp(counter_nr, debug_message);
3942 void print_timestamp_init(char *message)
3944 print_timestamp_ext(message, "INIT");
3947 void print_timestamp_time(char *message)
3949 print_timestamp_ext(message, "TIME");
3952 void print_timestamp_done(char *message)
3954 print_timestamp_ext(message, "DONE");