OSDN Git Service

[Refactor] #3214 ANGBAND_DIR 系をconcptr からpath に差し替えた
authorHourier <66951241+Hourier@users.noreply.github.com>
Mon, 1 May 2023 02:58:11 +0000 (11:58 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Mon, 1 May 2023 05:05:57 +0000 (14:05 +0900)
src/io/files-util.cpp
src/io/files-util.h
src/main-win.cpp
src/main.cpp
src/main/angband-initializer.cpp

index c42a2cc..0630988 100644 (file)
 #include "view/display-messages.h"
 #include <algorithm>
 
-concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
-concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
-concptr ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
-concptr ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
-concptr ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
-concptr ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
-concptr ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
-concptr ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
-concptr ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
-concptr ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
-concptr ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
-concptr ANGBAND_DIR_DEBUG_SAVE; //*< Savefiles for debug data
-concptr ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
-concptr ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
+std::filesystem::path ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
+std::filesystem::path ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
+std::filesystem::path ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
+std::filesystem::path ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
+std::filesystem::path ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
+std::filesystem::path ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
+std::filesystem::path ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
+std::filesystem::path ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
+std::filesystem::path ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
+std::filesystem::path ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
+std::filesystem::path ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
+std::filesystem::path ANGBAND_DIR_DEBUG_SAVE; //*< Savefiles for debug data
+std::filesystem::path ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
+std::filesystem::path ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
 
 /*
  * Buffer to hold the current savefile name
index 785b0af..8aadb42 100644 (file)
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "system/angband.h"
+#include <filesystem>
 #include <optional>
 #include <string>
 
@@ -8,20 +9,20 @@ extern char savefile[1024];
 extern char savefile_base[40];
 extern char debug_savefile[1024];
 
-extern concptr ANGBAND_DIR;
-extern concptr ANGBAND_DIR_APEX;
-extern concptr ANGBAND_DIR_BONE;
-extern concptr ANGBAND_DIR_DATA;
-extern concptr ANGBAND_DIR_EDIT;
-extern concptr ANGBAND_DIR_SCRIPT;
-extern concptr ANGBAND_DIR_FILE;
-extern concptr ANGBAND_DIR_HELP;
-extern concptr ANGBAND_DIR_INFO;
-extern concptr ANGBAND_DIR_PREF;
-extern concptr ANGBAND_DIR_SAVE;
-extern concptr ANGBAND_DIR_DEBUG_SAVE;
-extern concptr ANGBAND_DIR_USER;
-extern concptr ANGBAND_DIR_XTRA;
+extern std::filesystem::path ANGBAND_DIR;
+extern std::filesystem::path ANGBAND_DIR_APEX;
+extern std::filesystem::path ANGBAND_DIR_BONE;
+extern std::filesystem::path ANGBAND_DIR_DATA;
+extern std::filesystem::path ANGBAND_DIR_EDIT;
+extern std::filesystem::path ANGBAND_DIR_SCRIPT;
+extern std::filesystem::path ANGBAND_DIR_FILE;
+extern std::filesystem::path ANGBAND_DIR_HELP;
+extern std::filesystem::path ANGBAND_DIR_INFO;
+extern std::filesystem::path ANGBAND_DIR_PREF;
+extern std::filesystem::path ANGBAND_DIR_SAVE;
+extern std::filesystem::path ANGBAND_DIR_DEBUG_SAVE;
+extern std::filesystem::path ANGBAND_DIR_USER;
+extern std::filesystem::path ANGBAND_DIR_XTRA;
 
 class PlayerType;
 typedef void (*update_playtime_pf)(void);
index 2d8b77f..bf2d739 100644 (file)
 #include <direct.h>
 #include <locale>
 #include <string>
+#include <string_view>
 #include <vector>
 
 /*
@@ -420,15 +421,13 @@ static void save_prefs(void)
     WritePrivateProfileStringA("Angband", "BackGround", buf, ini_file);
     WritePrivateProfileStringA("Angband", "BackGroundBitmap", wallpaper_file[0] != '\0' ? wallpaper_file : DEFAULT_BG_FILENAME, ini_file);
 
-    int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
-    char tmp[1024] = "";
-    strncat(tmp, ANGBAND_DIR, path_length);
-
-    int n = strncmp(tmp, savefile, path_length);
-    if (n == 0) {
-        char relative_path[1024] = "";
-        snprintf(relative_path, sizeof(relative_path), ".\\%s", (savefile + path_length));
-        WritePrivateProfileStringA("Angband", "SaveFile", relative_path, ini_file);
+    std::string_view angband_dir_str(ANGBAND_DIR.string());
+    const auto path_length = angband_dir_str.length() - 4; // "\lib" を除く.
+    angband_dir_str = angband_dir_str.substr(0, path_length);
+    const std::string_view savefile_str(savefile);
+    if (angband_dir_str == savefile_str) {
+        const auto relative_path = format(".\\%s", (savefile + path_length));
+        WritePrivateProfileStringA("Angband", "SaveFile", relative_path.data(), ini_file);
     } else {
         WritePrivateProfileStringA("Angband", "SaveFile", savefile, ini_file);
     }
@@ -522,9 +521,11 @@ static void load_prefs(void)
 
     int n = strncmp(".\\", savefile, 2);
     if (n == 0) {
-        int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
+        std::string_view angband_dir_str(ANGBAND_DIR.string());
+        const auto path_length = angband_dir_str.length() - 4; // "\lib" を除く.
+        angband_dir_str = angband_dir_str.substr(0, path_length);
         char tmp[1024] = "";
-        strncat(tmp, ANGBAND_DIR, path_length);
+        strncat(tmp, angband_dir_str.data(), path_length);
         strncat(tmp, savefile + 2, strlen(savefile) - 2 + path_length);
         strncpy(savefile, tmp, strlen(tmp));
     }
@@ -1595,8 +1596,7 @@ static void process_menus(PlayerType *player_ptr, WORD wCmd)
             ofn.nFilterIndex = 1;
             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
 
-            const std::filesystem::path dir_save(ANGBAND_DIR_SAVE);
-            if (get_open_filename(&ofn, &dir_save, savefile, MAIN_WIN_MAX_PATH)) {
+            if (get_open_filename(&ofn, &ANGBAND_DIR_SAVE, savefile, MAIN_WIN_MAX_PATH)) {
                 validate_file(savefile);
                 game_in_progress = true;
             }
@@ -1667,8 +1667,7 @@ static void process_menus(PlayerType *player_ptr, WORD wCmd)
             ofn.nFilterIndex = 1;
             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
 
-            const std::filesystem::path dir_user(ANGBAND_DIR_USER);
-            if (get_open_filename(&ofn, &dir_user, savefile, MAIN_WIN_MAX_PATH)) {
+            if (get_open_filename(&ofn, &ANGBAND_DIR_USER, savefile, MAIN_WIN_MAX_PATH)) {
                 prepare_browse_movie_without_path_build(savefile);
                 movie_in_progress = true;
             }
index 0d7a73f..104c5e7 100644 (file)
@@ -114,88 +114,50 @@ static void init_stuff()
  */
 static void change_path(concptr info)
 {
-    concptr s;
-
-    /* Find equal sign */
-    s = angband_strchr(info, '=');
+    const auto s = angband_strchr(info, '=');
 
     /* Verify equal sign */
     if (!s) {
         quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
     }
 
-    /* Analyze */
     switch (tolower(info[0])) {
-    case 'a': {
-        string_free(ANGBAND_DIR_APEX);
-        ANGBAND_DIR_APEX = string_make(s + 1);
+    case 'a':
+        ANGBAND_DIR_APEX = s + 1;
         break;
-    }
-
-    case 'f': {
-        string_free(ANGBAND_DIR_FILE);
-        ANGBAND_DIR_FILE = string_make(s + 1);
+    case 'f':
+        ANGBAND_DIR_FILE = s + 1;
         break;
-    }
-
-    case 'h': {
-        string_free(ANGBAND_DIR_HELP);
-        ANGBAND_DIR_HELP = string_make(s + 1);
+    case 'h':
+        ANGBAND_DIR_HELP = s + 1;
         break;
-    }
-
-    case 'i': {
-        string_free(ANGBAND_DIR_INFO);
-        ANGBAND_DIR_INFO = string_make(s + 1);
+    case 'i':
+        ANGBAND_DIR_INFO = s + 1;
         break;
-    }
-
-    case 'u': {
-        string_free(ANGBAND_DIR_USER);
-        ANGBAND_DIR_USER = string_make(s + 1);
+    case 'u':
+        ANGBAND_DIR_USER = s + 1;
         break;
-    }
-
-    case 'x': {
-        string_free(ANGBAND_DIR_XTRA);
-        ANGBAND_DIR_XTRA = string_make(s + 1);
+    case 'x':
+        ANGBAND_DIR_XTRA = s + 1;
         break;
-    }
-
-    case 'b': {
-        string_free(ANGBAND_DIR_BONE);
-        ANGBAND_DIR_BONE = string_make(s + 1);
+    case 'b':
+        ANGBAND_DIR_BONE = s + 1;
         break;
-    }
-
-    case 'd': {
-        string_free(ANGBAND_DIR_DATA);
-        ANGBAND_DIR_DATA = string_make(s + 1);
+    case 'd':
+        ANGBAND_DIR_DATA = s + 1;
         break;
-    }
-
-    case 'e': {
-        string_free(ANGBAND_DIR_EDIT);
-        ANGBAND_DIR_EDIT = string_make(s + 1);
+    case 'e':
+        ANGBAND_DIR_EDIT = s + 1;
         break;
-    }
-
-    case 's': {
-        string_free(ANGBAND_DIR_SAVE);
-        ANGBAND_DIR_SAVE = string_make(s + 1);
+    case 's':
+        ANGBAND_DIR_SAVE = s + 1;
         break;
-    }
-
-    case 'z': {
-        string_free(ANGBAND_DIR_SCRIPT);
-        ANGBAND_DIR_SCRIPT = string_make(s + 1);
+    case 'z':
+        ANGBAND_DIR_SCRIPT = s + 1;
         break;
-    }
-
-    default: {
+    default:
         quit_fmt("Bad semantics in '-d%s'", info);
     }
-    }
 }
 
 static void display_usage(const char *program)
index 53d8323..0f9bbc5 100644 (file)
  */
 void init_file_paths(char *libpath)
 {
-    char *libtail;
-    char buf[1024];
-
-    string_free(ANGBAND_DIR);
-    string_free(ANGBAND_DIR_APEX);
-    string_free(ANGBAND_DIR_BONE);
-    string_free(ANGBAND_DIR_DATA);
-    string_free(ANGBAND_DIR_EDIT);
-    string_free(ANGBAND_DIR_SCRIPT);
-    string_free(ANGBAND_DIR_FILE);
-    string_free(ANGBAND_DIR_HELP);
-    string_free(ANGBAND_DIR_INFO);
-    string_free(ANGBAND_DIR_SAVE);
-    string_free(ANGBAND_DIR_DEBUG_SAVE);
-    string_free(ANGBAND_DIR_USER);
-    string_free(ANGBAND_DIR_XTRA);
-
-    ANGBAND_DIR = string_make(libpath);
-    libtail = libpath + strlen(libpath);
-    strcpy(libtail, "apex");
-    ANGBAND_DIR_APEX = string_make(libpath);
-    strcpy(libtail, "bone");
-    ANGBAND_DIR_BONE = string_make(libpath);
-    strcpy(libtail, "data");
-    ANGBAND_DIR_DATA = string_make(libpath);
-    strcpy(libtail, "edit");
-    ANGBAND_DIR_EDIT = string_make(libpath);
-    strcpy(libtail, "script");
-    ANGBAND_DIR_SCRIPT = string_make(libpath);
-    strcpy(libtail, "file");
-    ANGBAND_DIR_FILE = string_make(libpath);
-    strcpy(libtail, "help");
-    ANGBAND_DIR_HELP = string_make(libpath);
-    strcpy(libtail, "info");
-    ANGBAND_DIR_INFO = string_make(libpath);
-    strcpy(libtail, "pref");
-    ANGBAND_DIR_PREF = string_make(libpath);
-    strcpy(libtail, "save");
-    ANGBAND_DIR_SAVE = string_make(libpath);
-    path_build(buf, sizeof(buf), ANGBAND_DIR_SAVE, "log");
-    ANGBAND_DIR_DEBUG_SAVE = string_make(buf);
+    std::filesystem::path path(libpath);
+    ANGBAND_DIR = path;
+    ANGBAND_DIR_APEX = std::filesystem::path(path).append("apex");
+    ANGBAND_DIR_BONE = std::filesystem::path(path).append("bone");
+    ANGBAND_DIR_DATA = std::filesystem::path(path).append("data");
+    ANGBAND_DIR_EDIT = std::filesystem::path(path).append("edit");
+    ANGBAND_DIR_SCRIPT = std::filesystem::path(path).append("script");
+    ANGBAND_DIR_FILE = std::filesystem::path(path).append("file");
+    ANGBAND_DIR_HELP = std::filesystem::path(path).append("help");
+    ANGBAND_DIR_INFO = std::filesystem::path(path).append("info");
+    ANGBAND_DIR_PREF = std::filesystem::path(path).append("pref");
+    ANGBAND_DIR_SAVE = std::filesystem::path(path).append("save");
+    ANGBAND_DIR_DEBUG_SAVE = std::filesystem::path(ANGBAND_DIR_SAVE).append("log");
 #ifdef PRIVATE_USER_PATH
-    path_build(buf, sizeof(buf), PRIVATE_USER_PATH, VARIANT_NAME);
-    ANGBAND_DIR_USER = string_make(buf);
+    ANGBAND_DIR_USER = std::filesystem::path(PRIVATE_USER_PATH).append(VARIANT_NAME);
 #else
-    strcpy(libtail, "user");
-    ANGBAND_DIR_USER = string_make(libpath);
+    ANGBAND_DIR_USER = std::filesystem::path(path).append("user");
 #endif
-    strcpy(libtail, "xtra");
-    ANGBAND_DIR_XTRA = string_make(libpath);
+    ANGBAND_DIR_XTRA = std::filesystem::path(path).append("xtra");
 
     time_t now = time(nullptr);
     struct tm *t = localtime(&now);
@@ -124,7 +93,8 @@ void init_file_paths(char *libpath)
     }
 #else
     {
-        DIR *saves_dir = opendir(ANGBAND_DIR_DEBUG_SAVE);
+        const auto &debug_save_str = ANGBAND_DIR_DEBUG_SAVE.string();
+        DIR *saves_dir = opendir(debug_save_str.data());
 
         if (saves_dir) {
             struct dirent *next_entry;