improved handling of R'n'D program filenames with suffix
authorHolger Schemel <info@artsoft.org>
Thu, 15 Oct 2015 18:44:37 +0000 (20:44 +0200)
committerHolger Schemel <info@artsoft.org>
Thu, 15 Oct 2015 18:44:37 +0000 (20:44 +0200)
src/libgame/misc.c
src/libgame/misc.h
src/libgame/setup.c
src/main.c

index 966abb021611fc22c78d8c37adfaa0131d33f198..c1e8eee4f9c492a11ba7829282c967a89b09e4b8 100644 (file)
@@ -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);
index 0e2be9c931f4f0d626adf2842562309a1d232fe8..593521ea323e114a534ad5b2b6ccecb96b0abcb9 100644 (file)
@@ -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 *);
index 7439108a883e1ecb921b40c628b14df747fc5f02..85832274fba1df988edcb2ce417db38a67392288 100644 (file)
@@ -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)
index af4bcd34a58b0f218827a87756f8b6152296cffc..84004f468283e1c8b5e3e28b9948fa962fca2fb6 100644 (file)
@@ -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)