From: Holger Schemel Date: Thu, 15 Oct 2015 18:44:37 +0000 (+0200) Subject: improved handling of R'n'D program filenames with suffix X-Git-Tag: 4.0.0.0-rc1~140 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=bc101e8e3ab8515fcc5fbed3d8bf8c3efc258846;p=rocksndiamonds.git improved handling of R'n'D program filenames with suffix --- diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 966abb02..c1e8eee4 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -654,6 +654,23 @@ char *getBaseName(char *filename) return getStringCopy(getBaseNamePtr(filename)); } +char *getBaseNameNoSuffix(char *filename) +{ + char *basename = getStringCopy(getBaseNamePtr(filename)); + + // remove trailing suffix (separated by dot or hyphen) + if (basename[0] != '.' && basename[0] != '-') + { + if (strchr(basename, '.') != NULL) + *strchr(basename, '.') = '\0'; + + if (strchr(basename, '-') != NULL) + *strchr(basename, '-') = '\0'; + } + + return basename; +} + char *getBasePath(char *filename) { char *basepath = getStringCopy(filename); diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 0e2be9c9..593521ea 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -147,6 +147,7 @@ time_t getFileTimestampEpochSeconds(char *); char *getBasePath(char *); char *getBaseName(char *); char *getBaseNamePtr(char *); +char *getBaseNameNoSuffix(char *); char *getStringCat2WithSeparator(char *, char *, char *); char *getStringCat3WithSeparator(char *, char *, char *, char *); diff --git a/src/libgame/setup.c b/src/libgame/setup.c index 7439108a..85832274 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -395,13 +395,25 @@ inline static char *getLevelArtworkDir(int type) char *getProgramConfigFilename(char *command_filename_ptr) { - char *command_filename = getStringCopy(command_filename_ptr); + char *command_filename_1 = getStringCopy(command_filename_ptr); // strip trailing executable suffix from command filename - if (strSuffix(command_filename, ".exe")) - command_filename[strlen(command_filename) - 4] = '\0'; + if (strSuffix(command_filename_1, ".exe")) + command_filename_1[strlen(command_filename_1) - 4] = '\0'; - return getStringCat2(command_filename, ".conf"); + char *command_basepath = getBasePath(command_filename_ptr); + char *command_basename = getBaseNameNoSuffix(command_filename_ptr); + char *command_filename_2 = getPath2(command_basepath, command_basename); + + char *config_filename_1 = getStringCat2(command_filename_1, ".conf"); + char *config_filename_2 = getStringCat2(command_filename_2, ".conf"); + + // 1st try: look for config file that exactly matches the binary filename + if (fileExists(config_filename_1)) + return config_filename_1; + + // 2nd try: return config filename that matches binary filename without suffix + return config_filename_2; } char *getTapeFilename(int nr) diff --git a/src/main.c b/src/main.c index af4bcd34..84004f46 100644 --- a/src/main.c +++ b/src/main.c @@ -5630,7 +5630,7 @@ static void print_version() static void InitProgramConfig(char *command_filename) { - char *command_basename = getBaseName(command_filename); + char *userdata_basename = getBaseNameNoSuffix(command_filename); char *config_filename = getProgramConfigFilename(command_filename); char *program_title = PROGRAM_TITLE_STRING; char *program_icon_file = PROGRAM_ICON_FILENAME; @@ -5639,7 +5639,18 @@ static void InitProgramConfig(char *command_filename) // read default program config, if existing if (fileExists(config_filename)) + { + // if program config file exists, derive Unix user data directory from it + userdata_basename = getBaseName(config_filename); + + if (strSuffix(userdata_basename, ".conf")) + userdata_basename[strlen(userdata_basename) - 4] = '\0'; + LoadSetupFromFilename(config_filename); + } + + // set user data directory for Linux/Unix (but not Mac OS X) + userdata_subdir_unix = getStringCat2(".", userdata_basename); // set program title from potentially redefined program title if (setup.internal.program_title != NULL && @@ -5651,12 +5662,6 @@ static void InitProgramConfig(char *command_filename) strlen(setup.internal.program_icon_file) > 0) program_icon_file = getStringCopy(setup.internal.program_icon_file); - // strip trailing executable suffix from command basename - if (strSuffix(command_basename, ".exe")) - command_basename[strlen(command_basename) - 4] = '\0'; - - userdata_subdir_unix = getStringCat2(".", command_basename); - #if defined(PLATFORM_WIN32) || defined(PLATFORM_MACOSX) userdata_subdir = program_title; #elif defined(PLATFORM_UNIX)